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 junit.framework.TestCase; 20 21 import java.io.File; 22 import java.io.FileOutputStream; 23 import java.io.IOException; 24 25 import java.util.List; 26 import java.util.Map; 27 28 /** 29 * Unit tests for {@link DynamicConfigHandler} 30 */ 31 public class DynamicConfigHandlerTest extends TestCase { 32 33 private static final String localConfig = 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>test config 2</value>\n" + 41 " </entry>\n" + 42 " <entry key=\"override-config-2\">\n" + 43 " <value>test config 3</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=\"override-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 overrideJson = 62 "{\n" + 63 " \"dynamicConfigEntries\": {\n" + 64 " \"override-config-1\": {\n" + 65 " \"configValues\": [\n" + 66 " \"override-config-val-1\"\n" + 67 " ]\n" + 68 " },\n" + 69 " \"override-config-2\": {\n" + 70 " \"configValues\": [\n" + 71 " \"override-config-val-2\"\n" + 72 " ]\n" + 73 " },\n" + 74 " \"override-config-list-1\": {\n" + 75 " \"configValues\": [\n" + 76 " \"override-config-list-val-1-1\",\n" + 77 " \"override-config-list-val-1-2\"\n" + 78 " ]\n" + 79 " },\n" + 80 " \"override-config-list-2\": {\n" + 81 " \"configValues\": [\n" + 82 " \"override-config-list-val-2-1\"\n" + 83 " ]\n" + 84 " },\n" + 85 " \"override-config-list-3\": {\n" + 86 " \"configValues\": [\n" + 87 " ]\n" + 88 " }\n" + 89 " }\n" + 90 "}"; 91 testDynamicConfigHandler()92 public void testDynamicConfigHandler() throws Exception { 93 String module = "test1"; 94 File localConfigFile = createFileFromStr(localConfig, module); 95 96 File mergedFile = DynamicConfigHandler 97 .getMergedDynamicConfigFile(localConfigFile, overrideJson, module); 98 99 Map<String, List<String>> configMap = DynamicConfig.createConfigMap(mergedFile); 100 101 assertEquals("override-config-val-1", configMap.get("override-config-1").get(0)); 102 assertTrue(configMap.get("override-config-list-1") 103 .contains("override-config-list-val-1-1")); 104 assertTrue(configMap.get("override-config-list-1") 105 .contains("override-config-list-val-1-2")); 106 assertTrue(configMap.get("override-config-list-3").size() == 0); 107 108 assertEquals("test config 1", configMap.get("test-config-1").get(0)); 109 assertTrue(configMap.get("config-list").contains("config2")); 110 111 assertEquals("override-config-val-2", configMap.get("override-config-2").get(0)); 112 assertEquals(1, configMap.get("override-config-list-2").size()); 113 assertTrue(configMap.get("override-config-list-2") 114 .contains("override-config-list-val-2-1")); 115 } 116 117 createFileFromStr(String configStr, String module)118 private File createFileFromStr(String configStr, String module) throws IOException { 119 File file = File.createTempFile(module, "dynamic"); 120 FileOutputStream stream = null; 121 try { 122 stream = new FileOutputStream(file); 123 stream.write(configStr.getBytes()); 124 stream.flush(); 125 } finally { 126 if (stream != null) { 127 stream.close(); 128 } 129 } 130 return file; 131 } 132 } 133