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=\"test-config-1\">\n" + 37 " <value>test config 1</value>\n" + 38 " </entry>\n" + 39 " <entry key=\"test-config-2\">\n" + 40 " <value>testconfig2</value>\n" + 41 " </entry>\n" + 42 " <entry key=\"config-list\">\n" + 43 " <value>config0</value>\n" + 44 " <value>config1</value>\n" + 45 " <value>config2</value>\n" + 46 " <value>config3</value>\n" + 47 " <value>config4</value>\n" + 48 " </entry>\n" + 49 " <entry key=\"config-list-2\">\n" + 50 " <value>A</value>\n" + 51 " <value>B</value>\n" + 52 " <value>C</value>\n" + 53 " <value>D</value>\n" + 54 " <value>E</value>\n" + 55 " </entry>\n" + 56 "</dynamicConfig>\n"; 57 58 private static final String CONFIG_WRONG_NODE_NAME = 59 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + 60 "<dynamicCsonfig>\n" + //The node name dynamicConfig is intentionally mistyped 61 " <entry key=\"test-config-1\">\n" + 62 " <value>test config 1</value>\n" + 63 " </entry>\n" + 64 " <entry key=\"test-config-2\">\n" + 65 " <value>testconfig2</value>\n" + 66 " </entry>\n" + 67 " <entry key=\"config-list\">\n" + 68 " <value>Nevermore</value>\n" + 69 " <value>Puck</value>\n" + 70 " <value>Zeus</value>\n" + 71 " <value>Earth Shaker</value>\n" + 72 " <value>Vengeful Spirit</value>\n" + 73 " </entry>\n" + 74 " <entry key=\"config-list-2\">\n" + 75 " <value>A</value>\n" + 76 " <value>B</value>\n" + 77 " <value>C</value>\n" + 78 " <value>D</value>\n" + 79 " <value>E</value>\n" + 80 " </entry>\n" + 81 "</dynamicConfig>\n"; 82 83 public void testCorrectConfig() throws Exception { 84 DynamicConfig config = new DynamicConfig(); 85 File file = createFileFromStr(CORRECT_CONFIG); 86 try { 87 config.initializeConfig(file); 88 assertEquals("Wrong Config", config.getValue("test-config-1"), "test config 1"); 89 assertEquals("Wrong Config", config.getValue("test-config-2"), "testconfig2"); 90 assertEquals("Wrong Config List", config.getValues("config-list").get(0), "config0"); 91 assertEquals("Wrong Config List", config.getValues("config-list").get(2), "config2"); 92 assertEquals("Wrong Config List", config.getValues("config-list-2").get(2), "C"); 93 } finally { 94 FileUtil.deleteFile(file); 95 } 96 } 97 98 public void testConfigWithWrongNodeName() throws Exception { 99 DynamicConfig config = new DynamicConfig(); 100 File file = createFileFromStr(CONFIG_WRONG_NODE_NAME); 101 try { 102 config.initializeConfig(file); 103 fail("Cannot detect error when config file has wrong node name"); 104 } catch (XmlPullParserException e) { 105 //expected 106 } finally { 107 FileUtil.deleteFile(file); 108 } 109 } 110 111 private File createFileFromStr(String configStr) throws IOException { 112 File file = File.createTempFile("test", "dynamic"); 113 FileOutputStream stream = new FileOutputStream(file); 114 stream.write(configStr.getBytes()); 115 stream.flush(); 116 stream.close(); 117 return file; 118 } 119 } 120