1 /* 2 * Copyright (C) 2010 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.cts.tradefed.testtype; 18 19 import com.android.cts.util.AbiUtils; 20 import com.android.ddmlib.testrunner.TestIdentifier; 21 import com.android.tradefed.util.xml.AbstractXmlParser.ParseException; 22 23 import junit.framework.TestCase; 24 25 import java.io.ByteArrayInputStream; 26 import java.io.ByteArrayOutputStream; 27 import java.io.IOException; 28 import java.io.InputStream; 29 import java.util.ArrayList; 30 import java.util.Collections; 31 import java.util.Iterator; 32 import java.util.List; 33 import java.util.Set; 34 35 /** 36 * Unit tests for {@link TestPlan}. 37 */ 38 public class TestPlanTest extends TestCase { 39 40 private static final String TEST_NAME1 = "foo"; 41 private static final String TEST_NAME2 = "foo2"; 42 private static final String EXCLUDE_TEST_CLASS = "com.example.FooTest"; 43 private static final String EXCLUDE_TEST_METHOD = "testFoo"; 44 private static final String EXCLUDE_TEST_METHOD2 = "testFoo2"; 45 46 static final String EMPTY_DATA = "<TestPlan version=\"1.0\" />"; 47 48 static final String TEST_DATA = 49 "<TestPlan version=\"1.0\">" + 50 String.format("<Entry name=\"%s\" />", TEST_NAME1) + 51 String.format("<Entry name=\"%s\" />", TEST_NAME2) + 52 "</TestPlan>"; 53 54 static final String TEST_EXCLUDED_DATA = 55 "<TestPlan version=\"1.0\">" + 56 String.format("<Entry name=\"%s\" exclude=\"%s#%s\" />", TEST_NAME1, EXCLUDE_TEST_CLASS, 57 EXCLUDE_TEST_METHOD) + 58 "</TestPlan>"; 59 60 static final String TEST_MULTI_EXCLUDED_DATA = 61 "<TestPlan version=\"1.0\">" + 62 String.format("<Entry name=\"%s\" exclude=\"%s#%s;%s#%s\" />", TEST_NAME1, 63 EXCLUDE_TEST_CLASS, EXCLUDE_TEST_METHOD, EXCLUDE_TEST_CLASS, 64 EXCLUDE_TEST_METHOD2) + 65 "</TestPlan>"; 66 67 static final String TEST_CLASS_EXCLUDED_DATA = 68 "<TestPlan version=\"1.0\">" + 69 String.format("<Entry name=\"%s\" exclude=\"%s\" />", TEST_NAME1, 70 EXCLUDE_TEST_CLASS) + 71 "</TestPlan>"; 72 73 private TestPlan mPlan; 74 75 @Override setUp()76 protected void setUp() throws Exception { 77 super.setUp(); 78 mPlan = new TestPlan("plan", AbiUtils.getAbisSupportedByCts()); 79 } 80 81 /** 82 * Simple test for parsing a plan containing two names 83 */ testParse()84 public void testParse() throws ParseException { 85 mPlan.parse(getStringAsStream(TEST_DATA)); 86 assertTestData(mPlan); 87 } 88 89 /** 90 * Perform checks to ensure TEST_DATA was parsed correctly 91 * @param plan 92 */ assertTestData(TestPlan plan)93 private void assertTestData(TestPlan plan) { 94 Set<String> abis = AbiUtils.getAbisSupportedByCts(); 95 assertEquals(2 * abis.size(), plan.getTestIds().size()); 96 List<String> sortedAbis = new ArrayList<String>(abis); 97 Collections.sort(sortedAbis); 98 Iterator<String> iter = plan.getTestIds().iterator(); 99 for (String abi : sortedAbis) { 100 String test1Id = AbiUtils.createId(abi, TEST_NAME1); 101 String test2Id = AbiUtils.createId(abi, TEST_NAME2); 102 // assert names in order 103 assertEquals(test1Id, iter.next()); 104 assertEquals(test2Id, iter.next()); 105 assertFalse(plan.getTestFilter(test1Id).hasExclusion()); 106 assertFalse(plan.getTestFilter(test2Id).hasExclusion()); 107 } 108 } 109 110 /** 111 * Test parsing a plan containing a single excluded test 112 */ testParse_exclude()113 public void testParse_exclude() throws ParseException { 114 mPlan.parse(getStringAsStream(TEST_EXCLUDED_DATA)); 115 Set<String> abis = AbiUtils.getAbisSupportedByCts(); 116 assertEquals(abis.size(), mPlan.getTestIds().size()); 117 118 for (String abi : abis) { 119 String test1Id = AbiUtils.createId(abi, TEST_NAME1); 120 TestFilter filter = mPlan.getTestFilter(test1Id); 121 assertTrue(filter.getExcludedTests().contains(new TestIdentifier(EXCLUDE_TEST_CLASS, 122 EXCLUDE_TEST_METHOD))); 123 } 124 } 125 126 /** 127 * Test parsing a plan containing multiple excluded tests 128 */ testParse_multiExclude()129 public void testParse_multiExclude() throws ParseException { 130 mPlan.parse(getStringAsStream(TEST_MULTI_EXCLUDED_DATA)); 131 assertMultiExcluded(mPlan); 132 } 133 134 /** 135 * Perform checks to ensure TEST_MULTI_EXCLUDED_DATA was parsed correctly 136 * @param plan 137 */ assertMultiExcluded(TestPlan plan)138 private void assertMultiExcluded(TestPlan plan) { 139 Set<String> abis = AbiUtils.getAbisSupportedByCts(); 140 assertEquals(abis.size(), plan.getTestIds().size()); 141 142 for (String abi : abis) { 143 String test1Id = AbiUtils.createId(abi, TEST_NAME1); 144 TestFilter filter = plan.getTestFilter(test1Id); 145 assertTrue(filter.getExcludedTests().contains(new TestIdentifier(EXCLUDE_TEST_CLASS, 146 EXCLUDE_TEST_METHOD))); 147 assertTrue(filter.getExcludedTests().contains(new TestIdentifier(EXCLUDE_TEST_CLASS, 148 EXCLUDE_TEST_METHOD2))); 149 } 150 } 151 152 /** 153 * Test parsing a plan containing an excluded class 154 */ testParse_classExclude()155 public void testParse_classExclude() throws ParseException { 156 mPlan.parse(getStringAsStream(TEST_CLASS_EXCLUDED_DATA)); 157 Set<String> abis = AbiUtils.getAbisSupportedByCts(); 158 assertEquals(abis.size(), mPlan.getTestIds().size()); 159 160 for (String abi : abis) { 161 String test1Id = AbiUtils.createId(abi, TEST_NAME1); 162 TestFilter filter = mPlan.getTestFilter(test1Id); 163 assertTrue(filter.getExcludedClasses().contains(EXCLUDE_TEST_CLASS)); 164 } 165 } 166 167 /** 168 * Test serializing an empty plan 169 * @throws IOException 170 */ testSerialize_empty()171 public void testSerialize_empty() throws IOException { 172 ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 173 mPlan.serialize(outStream); 174 assertTrue(outStream.toString().contains(EMPTY_DATA)); 175 } 176 177 /** 178 * Test serializing and deserializing plan with two packages 179 * @throws IOException 180 */ testSerialize_packages()181 public void testSerialize_packages() throws ParseException, IOException { 182 Set<String> abis = AbiUtils.getAbisSupportedByCts(); 183 for (String abi : abis) { 184 mPlan.addPackage(AbiUtils.createId(abi, TEST_NAME1)); 185 mPlan.addPackage(AbiUtils.createId(abi, TEST_NAME2)); 186 } 187 ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 188 mPlan.serialize(outStream); 189 TestPlan parsedPlan = new TestPlan("parsed", AbiUtils.getAbisSupportedByCts()); 190 parsedPlan.parse(getStringAsStream(outStream.toString())); 191 // parsedPlan should contain same contents as TEST_DATA 192 assertTestData(parsedPlan); 193 } 194 195 /** 196 * Test serializing and deserializing plan with multiple excluded tests 197 */ testSerialize_multiExclude()198 public void testSerialize_multiExclude() throws ParseException, IOException { 199 Set<String> abis = AbiUtils.getAbisSupportedByCts(); 200 201 for (String abi : abis) { 202 String test1Id = AbiUtils.createId(abi, TEST_NAME1); 203 mPlan.addPackage(test1Id); 204 mPlan.addExcludedTest(test1Id, new TestIdentifier(EXCLUDE_TEST_CLASS, 205 EXCLUDE_TEST_METHOD)); 206 mPlan.addExcludedTest(test1Id, new TestIdentifier(EXCLUDE_TEST_CLASS, 207 EXCLUDE_TEST_METHOD2)); 208 } 209 ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 210 mPlan.serialize(outStream); 211 TestPlan parsedPlan = new TestPlan("parsed", AbiUtils.getAbisSupportedByCts()); 212 parsedPlan.parse(getStringAsStream(outStream.toString())); 213 // parsedPlan should contain same contents as TEST_DATA 214 assertMultiExcluded(parsedPlan); 215 } 216 getStringAsStream(String input)217 private InputStream getStringAsStream(String input) { 218 return new ByteArrayInputStream(input.getBytes()); 219 } 220 } 221