1 /* 2 * Copyright (C) 2015 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 17 package com.android.compatibility.common.util; 18 19 import com.android.tradefed.util.FileUtil; 20 21 import junit.framework.TestCase; 22 23 import org.xmlpull.v1.XmlPullParserException; 24 25 import java.io.File; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 29 /** 30 * Unit tests for {@link DynamicConfig} 31 */ 32 public class DynamicConfigTest extends TestCase { 33 private static final String CORRECT_CONFIG = 34 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + 35 "<dynamicConfig>\n" + 36 " <entry key=\"remote_config_required\">\n" + 37 " <value>false</value>\n" + 38 " </entry>\n" + 39 " <entry key=\"test-config-1\">\n" + 40 " <value>test config 1</value>\n" + 41 " </entry>\n" + 42 " <entry key=\"test-config-2\">\n" + 43 " <value>testconfig2</value>\n" + 44 " </entry>\n" + 45 " <entry key=\"config-list\">\n" + 46 " <value>config0</value>\n" + 47 " <value>config1</value>\n" + 48 " <value>config2</value>\n" + 49 " <value>config3</value>\n" + 50 " <value>config4</value>\n" + 51 " </entry>\n" + 52 " <entry key=\"config-list-2\">\n" + 53 " <value>A</value>\n" + 54 " <value>B</value>\n" + 55 " <value>C</value>\n" + 56 " <value>D</value>\n" + 57 " <value>E</value>\n" + 58 " </entry>\n" + 59 "</dynamicConfig>\n"; 60 61 private static final String CONFIG_WRONG_NODE_NAME = 62 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + 63 "<dynamicCsonfig>\n" + //The node name dynamicConfig is intentionally mistyped 64 " <entry key=\"remote_config_required\">\n" + 65 " <value>false</value>\n" + 66 " </entry>\n" + 67 " <entry key=\"test-config-1\">\n" + 68 " <value>test config 1</value>\n" + 69 " </entry>\n" + 70 " <entry key=\"test-config-2\">\n" + 71 " <value>testconfig2</value>\n" + 72 " </entry>\n" + 73 " <entry key=\"config-list\">\n" + 74 " <value>Nevermore</value>\n" + 75 " <value>Puck</value>\n" + 76 " <value>Zeus</value>\n" + 77 " <value>Earth Shaker</value>\n" + 78 " <value>Vengeful Spirit</value>\n" + 79 " </entry>\n" + 80 " <entry key=\"config-list-2\">\n" + 81 " <value>A</value>\n" + 82 " <value>B</value>\n" + 83 " <value>C</value>\n" + 84 " <value>D</value>\n" + 85 " <value>E</value>\n" + 86 " </entry>\n" + 87 "</dynamicConfig>\n"; 88 testCorrectConfig()89 public void testCorrectConfig() throws Exception { 90 DynamicConfig config = new DynamicConfig(); 91 File file = createFileFromStr(CORRECT_CONFIG); 92 try { 93 config.initializeConfig(file); 94 assertEquals("Wrong Config", config.getValue("test-config-1"), "test config 1"); 95 assertEquals("Wrong Config", config.getValue("test-config-2"), "testconfig2"); 96 assertEquals("Wrong Config List", config.getValues("config-list").get(0), "config0"); 97 assertEquals("Wrong Config List", config.getValues("config-list").get(2), "config2"); 98 assertEquals("Wrong Config List", config.getValues("config-list-2").get(2), "C"); 99 } finally { 100 FileUtil.deleteFile(file); 101 } 102 } 103 testConfigWithWrongNodeName()104 public void testConfigWithWrongNodeName() throws Exception { 105 DynamicConfig config = new DynamicConfig(); 106 File file = createFileFromStr(CONFIG_WRONG_NODE_NAME); 107 try { 108 config.initializeConfig(file); 109 fail("Cannot detect error when config file has wrong node name"); 110 } catch (XmlPullParserException e) { 111 //expected 112 } finally { 113 FileUtil.deleteFile(file); 114 } 115 } 116 createFileFromStr(String configStr)117 private File createFileFromStr(String configStr) throws IOException { 118 File file = File.createTempFile("test", "dynamic"); 119 FileOutputStream stream = new FileOutputStream(file); 120 stream.write(configStr.getBytes()); 121 stream.flush(); 122 stream.close(); 123 return file; 124 } 125 } 126