1 /*
2  * Copyright (C) 2016 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.tradefed.testtype;
18 
19 import com.android.compatibility.common.util.TestFilter;
20 import com.android.tradefed.util.FileUtil;
21 
22 import junit.framework.TestCase;
23 
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.FileWriter;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.util.HashSet;
31 import java.util.Set;
32 
33 /**
34  * Tests for {@link SubPlan}
35  */
36 public class SubPlanTest extends TestCase {
37 
38     private static final String ABI = "armeabi-v7a";
39     private static final String MODULE_A = "ModuleA";
40     private static final String MODULE_B = "ModuleB";
41     private static final String TEST_1 = "android.test.Foo#test1";
42     private static final String TEST_2 = "android.test.Foo#test2";
43     private static final String TEST_3 = "android.test.Foo#test3";
44 
45     private static final String XML_BASE =
46             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
47             "<SubPlan version=\"2.0\">\n" +
48             "%s\n" +
49             "</SubPlan>";
50     private static final String XML_ENTRY = "  <Entry %s/>\n";
51     private static final String XML_ATTR = "%s=\"%s\"";
52 
testSerialization()53     public void testSerialization() throws Exception {
54         ISubPlan subPlan = new SubPlan();
55         subPlan.addIncludeFilter(new TestFilter(ABI, MODULE_A, TEST_1).toString());
56         Set<String> includeFilterSet = new HashSet<String>();
57         includeFilterSet.add(new TestFilter(ABI, MODULE_A, TEST_2).toString());
58         includeFilterSet.add(new TestFilter(ABI, MODULE_A, TEST_3).toString());
59         subPlan.addAllIncludeFilters(includeFilterSet); // add multiple include filters simultaneously
60         subPlan.addIncludeFilter(new TestFilter(null, MODULE_B, null).toString());
61         subPlan.addExcludeFilter(new TestFilter(null, MODULE_B, TEST_1).toString());
62         Set<String> excludeFilterSet = new HashSet<String>();
63         excludeFilterSet.add(new TestFilter(null, MODULE_B, TEST_2).toString());
64         excludeFilterSet.add(new TestFilter(null, MODULE_B, TEST_3).toString());
65         subPlan.addAllExcludeFilters(excludeFilterSet);
66 
67         // Serialize to file
68         File subPlanFile = FileUtil.createTempFile("test-subPlan-serialization", ".txt");
69         try {
70             OutputStream subPlanOutputStream = new FileOutputStream(subPlanFile);
71             subPlan.serialize(subPlanOutputStream);
72             subPlanOutputStream.close();
73             // Parse subPlan and assert correctness
74             checkSubPlan(subPlanFile);
75         } finally {
76             FileUtil.deleteFile(subPlanFile);
77         }
78     }
79 
testParsing()80     public void testParsing() throws Exception {
81         File planFile = FileUtil.createTempFile("test-plan-parsing", ".txt");
82         FileWriter writer = new FileWriter(planFile);
83         try {
84             Set<String> entries = new HashSet<String>();
85             entries.add(generateEntryXml(ABI, MODULE_A, TEST_1, true)); // include format 1
86             entries.add(generateEntryXml(ABI, MODULE_A, TEST_2, true));
87             entries.add(generateEntryXml(null, null,
88                     new TestFilter(ABI, MODULE_A, TEST_3).toString(), true)); // include format 2
89             entries.add(generateEntryXml(null, MODULE_B, null, true));
90             entries.add(generateEntryXml(null, null,
91                     new TestFilter(null, MODULE_B, TEST_1).toString(), false));
92             entries.add(generateEntryXml(null, null,
93                     new TestFilter(null, MODULE_B, TEST_2).toString(), false));
94             entries.add(generateEntryXml(null, null,
95                     new TestFilter(null, MODULE_B, TEST_3).toString(), false));
96             String xml = String.format(XML_BASE, String.join("\n", entries));
97             writer.write(xml);
98             writer.flush();
99             checkSubPlan(planFile);
100         } finally {
101             writer.close();
102             FileUtil.deleteFile(planFile);
103         }
104     }
105 
checkSubPlan(File subPlanFile)106     private void checkSubPlan(File subPlanFile) throws Exception {
107         InputStream subPlanInputStream = new FileInputStream(subPlanFile);
108         ISubPlan subPlan = new SubPlan();
109         subPlan.parse(subPlanInputStream);
110         Set<String> subPlanIncludes = subPlan.getIncludeFilters();
111         Set<String> subPlanExcludes = subPlan.getExcludeFilters();
112         assertEquals("Expected 4 includes", 4, subPlanIncludes.size());
113         assertTrue("Missing expected test include", subPlanIncludes.contains(
114                 new TestFilter(ABI, MODULE_A, TEST_1).toString()));
115         assertTrue("Missing expected test include", subPlanIncludes.contains(
116                 new TestFilter(ABI, MODULE_A, TEST_2).toString()));
117         assertTrue("Missing expected test include", subPlanIncludes.contains(
118                 new TestFilter(ABI, MODULE_A, TEST_3).toString()));
119         assertTrue("Missing expected module include", subPlanIncludes.contains(
120                 new TestFilter(null, MODULE_B, null).toString()));
121 
122         assertEquals("Expected 3 excludes", 3, subPlanExcludes.size());
123         assertTrue("Missing expected exclude", subPlanExcludes.contains(
124                 new TestFilter(null, MODULE_B, TEST_1).toString()));
125         assertTrue("Missing expected exclude", subPlanExcludes.contains(
126                 new TestFilter(null, MODULE_B, TEST_2).toString()));
127         assertTrue("Missing expected exclude", subPlanExcludes.contains(
128                 new TestFilter(null, MODULE_B, TEST_3).toString()));
129     }
130 
131     // Helper for generating Entry XML tags
generateEntryXml(String abi, String name, String filter, boolean include)132     private String generateEntryXml(String abi, String name, String filter, boolean include) {
133         String filterType = include ? "include" : "exclude";
134         Set<String> attributes = new HashSet<String>();
135         if (filter != null) {
136             attributes.add(String.format(XML_ATTR, filterType, filter));
137         }
138         if (name != null) {
139             attributes.add(String.format(XML_ATTR, "name", name));
140         }
141         if (abi != null) {
142             attributes.add(String.format(XML_ATTR, "abi", abi));
143         }
144         return String.format(XML_ENTRY, String.join(" ", attributes));
145     }
146 }
147