1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.compatibility.common.tradefed.util; 17 18 import static org.junit.Assert.*; 19 20 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper; 21 import com.android.tradefed.build.BuildInfo; 22 import com.android.tradefed.build.IBuildInfo; 23 import com.android.tradefed.util.FileUtil; 24 25 import org.junit.After; 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 import org.xmlpull.v1.XmlPullParserException; 31 32 import java.io.File; 33 import java.io.IOException; 34 35 /** 36 * Unit tests for {@link DynamicConfigFileReader}. 37 */ 38 @RunWith(JUnit4.class) 39 public class DynamicConfigFileReaderTest { 40 41 private static final String MODULE_NAME = "cts"; 42 private IBuildInfo mBuildInfo; 43 private File mConfigFile; 44 45 @Before setUp()46 public void setUp() throws Exception { 47 mConfigFile = FileUtil.createTempFile("temp-dynamic-config", ".xml"); 48 mBuildInfo = new BuildInfo(); 49 CompatibilityBuildHelper helper = new CompatibilityBuildHelper(mBuildInfo); 50 helper.addDynamicConfigFile(MODULE_NAME, mConfigFile); 51 } 52 53 @After tearDown()54 public void tearDown() { 55 FileUtil.deleteFile(mConfigFile); 56 mBuildInfo.cleanUp(); 57 } 58 writeDynamicConfigFile()59 private void writeDynamicConfigFile() throws IOException { 60 String content = "<dynamicConfig>\n" + 61 " <entry key=\"remote_config_required\">\n" + 62 " <value>false</value>\n" + 63 " </entry>\n" + 64 " <entry key=\"media_files_url\">\n" + 65 " <value>some value</value>\n" + 66 " </entry>\n" + 67 "</dynamicConfig>"; 68 FileUtil.writeToFile(content, mConfigFile); 69 } 70 71 /** 72 * Test when the dynamic file is completely invalid. 73 */ 74 @Test testGetValueFromFile_invalidFile()75 public void testGetValueFromFile_invalidFile() throws Exception { 76 try { 77 DynamicConfigFileReader.getValueFromConfig(mConfigFile, "doesnotexit"); 78 fail("Should have thrown an exception."); 79 } catch (XmlPullParserException expected) { 80 // expected 81 } 82 } 83 84 /** 85 * Test when requesting a key not part of the dynamic file. 86 */ 87 @Test testGetValueFromFile_keyNotFound()88 public void testGetValueFromFile_keyNotFound() throws Exception { 89 writeDynamicConfigFile(); 90 String res = DynamicConfigFileReader.getValueFromConfig(mConfigFile, "doesnotexit"); 91 assertNull(res); 92 } 93 94 /** 95 * Test when getting the value associated with the key. 96 */ 97 @Test testGetValueFromFile()98 public void testGetValueFromFile() throws Exception { 99 writeDynamicConfigFile(); 100 String res = DynamicConfigFileReader.getValueFromConfig(mConfigFile, "media_files_url"); 101 assertEquals("some value", res); 102 } 103 104 /** 105 * Test when getting the value using directly the build info and module name. 106 */ 107 @Test testGetValueFromBuild()108 public void testGetValueFromBuild() throws Exception { 109 writeDynamicConfigFile(); 110 String res = DynamicConfigFileReader.getValueFromConfig( 111 mBuildInfo, MODULE_NAME, "media_files_url"); 112 assertEquals("some value", res); 113 } 114 115 /** 116 * Test when trying to get a value from an unknown module. 117 */ 118 @Test testGetValueFromBuild_moduleNotFound()119 public void testGetValueFromBuild_moduleNotFound() throws Exception { 120 writeDynamicConfigFile(); 121 String res = DynamicConfigFileReader.getValueFromConfig( 122 mBuildInfo, "NOT_A_MODULE", "media_files_url"); 123 assertNull(res); 124 } 125 } 126