The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 17 | import org.junit.runner.RunWith; |
Brian Muramatsu | 7f64e85 | 2011-02-17 16:52:16 -0800 | [diff] [blame] | 18 | import org.w3c.dom.Document; |
| 19 | import org.w3c.dom.Element; |
| 20 | import org.w3c.dom.Node; |
| 21 | import org.w3c.dom.NodeList; |
| 22 | |
Brian Muramatsu | 7f64e85 | 2011-02-17 16:52:16 -0800 | [diff] [blame] | 23 | import vogar.ExpectationStore; |
Brian Muramatsu | 7f64e85 | 2011-02-17 16:52:16 -0800 | [diff] [blame] | 24 | |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 25 | import java.io.BufferedReader; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 26 | import java.io.File; |
| 27 | import java.io.FileInputStream; |
Urs Grob | fc77f6e | 2009-04-17 02:07:14 -0700 | [diff] [blame] | 28 | import java.io.FileReader; |
| 29 | import java.io.IOException; |
| 30 | import java.lang.annotation.Annotation; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 31 | import java.lang.reflect.Method; |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 32 | import java.lang.reflect.Modifier; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 33 | import java.util.ArrayList; |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 34 | import java.util.Enumeration; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 35 | import java.util.HashSet; |
| 36 | import java.util.Iterator; |
Urs Grob | fc77f6e | 2009-04-17 02:07:14 -0700 | [diff] [blame] | 37 | import java.util.LinkedHashMap; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 38 | import java.util.Map; |
| 39 | import java.util.Set; |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 40 | import java.util.jar.JarEntry; |
| 41 | import java.util.jar.JarFile; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 42 | |
| 43 | import javax.xml.parsers.DocumentBuilderFactory; |
| 44 | import javax.xml.parsers.ParserConfigurationException; |
| 45 | |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 46 | import junit.framework.TestCase; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 47 | |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 48 | public class CollectAllTests extends DescriptionGenerator { |
| 49 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 50 | private static final String ATTRIBUTE_RUNNER = "runner"; |
| 51 | private static final String ATTRIBUTE_PACKAGE = "appPackageName"; |
| 52 | private static final String ATTRIBUTE_NS = "appNameSpace"; |
| 53 | private static final String ATTRIBUTE_TARGET = "targetNameSpace"; |
| 54 | private static final String ATTRIBUTE_TARGET_BINARY = "targetBinaryName"; |
| 55 | private static final String ATTRIBUTE_HOST_SIDE_ONLY = "hostSideOnly"; |
| 56 | private static final String ATTRIBUTE_VM_HOST_TEST = "vmHostTest"; |
| 57 | private static final String ATTRIBUTE_JAR_PATH = "jarPath"; |
| 58 | private static final String ATTRIBUTE_JAVA_PACKAGE_FILTER = "javaPackageFilter"; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 59 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 60 | private static final String JAR_PATH = "LOCAL_JAR_PATH :="; |
| 61 | private static final String TEST_TYPE = "LOCAL_TEST_TYPE :"; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 62 | |
| 63 | public static void main(String[] args) { |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 64 | if (args.length < 4 || args.length > 6) { |
| 65 | System.err.println("usage: CollectAllTests <output-file> <manifest-file> <jar-file> " |
| 66 | + "<java-package> [expectation-dir [makefile-file]]"); |
| 67 | if (args.length != 0) { |
| 68 | System.err.println("received:"); |
| 69 | for (String arg : args) { |
| 70 | System.err.println(" " + arg); |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 71 | } |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 72 | } |
Urs Grob | fc77f6e | 2009-04-17 02:07:14 -0700 | [diff] [blame] | 73 | System.exit(1); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 76 | final String outputPathPrefix = args[0]; |
| 77 | File manifestFile = new File(args[1]); |
| 78 | String jarFileName = args[2]; |
| 79 | final String javaPackageFilter = args[3]; |
Tsu Chiang Chuang | c87fd6b | 2011-07-13 17:04:40 -0700 | [diff] [blame] | 80 | // Validate the javaPackageFilter value if non null. |
| 81 | if (javaPackageFilter.length() != 0) { |
| 82 | if (!isValidJavaPackage(javaPackageFilter)) { |
| 83 | System.err.println("Invalid " + ATTRIBUTE_JAVA_PACKAGE_FILTER + ": " + |
| 84 | javaPackageFilter); |
| 85 | System.exit(1); |
| 86 | return; |
| 87 | } |
| 88 | } |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 89 | String libcoreExpectationDir = (args.length > 4) ? args[4] : null; |
| 90 | String androidMakeFile = (args.length > 5) ? args[5] : null; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 91 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 92 | final TestType testType = TestType.getTestType(androidMakeFile); |
| 93 | |
| 94 | Document manifest; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 95 | try { |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 96 | manifest = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 97 | new FileInputStream(manifestFile)); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 98 | } catch (Exception e) { |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 99 | System.err.println("cannot open manifest " + manifestFile); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 100 | e.printStackTrace(); |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 101 | System.exit(1); |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 102 | return; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | Element documentElement = manifest.getDocumentElement(); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 106 | documentElement.getAttribute("package"); |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 107 | final String runner = getElementAttribute(documentElement, |
| 108 | "instrumentation", |
| 109 | "android:name"); |
| 110 | final String packageName = documentElement.getAttribute("package"); |
| 111 | final String target = getElementAttribute(documentElement, |
| 112 | "instrumentation", |
| 113 | "android:targetPackage"); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 114 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 115 | String outputXmlFile = outputPathPrefix + ".xml"; |
| 116 | final String xmlName = new File(outputPathPrefix).getName(); |
| 117 | XMLGenerator xmlGenerator; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 118 | try { |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 119 | xmlGenerator = new XMLGenerator(outputXmlFile) { |
| 120 | { |
| 121 | Node testPackageElem = mDoc.getDocumentElement(); |
| 122 | |
| 123 | setAttribute(testPackageElem, ATTRIBUTE_NAME, xmlName); |
| 124 | setAttribute(testPackageElem, ATTRIBUTE_RUNNER, runner); |
| 125 | setAttribute(testPackageElem, ATTRIBUTE_PACKAGE, packageName); |
| 126 | setAttribute(testPackageElem, ATTRIBUTE_NS, packageName); |
| 127 | setAttribute(testPackageElem, ATTRIBUTE_JAVA_PACKAGE_FILTER, javaPackageFilter); |
| 128 | |
| 129 | if (testType.type == TestType.HOST_SIDE_ONLY) { |
| 130 | setAttribute(testPackageElem, ATTRIBUTE_HOST_SIDE_ONLY, "true"); |
| 131 | setAttribute(testPackageElem, ATTRIBUTE_JAR_PATH, testType.jarPath); |
| 132 | } |
| 133 | |
| 134 | if (testType.type == TestType.VM_HOST_TEST) { |
| 135 | setAttribute(testPackageElem, ATTRIBUTE_VM_HOST_TEST, "true"); |
| 136 | setAttribute(testPackageElem, ATTRIBUTE_JAR_PATH, testType.jarPath); |
| 137 | } |
| 138 | |
| 139 | if (!packageName.equals(target)) { |
| 140 | setAttribute(testPackageElem, ATTRIBUTE_TARGET, target); |
| 141 | setAttribute(testPackageElem, ATTRIBUTE_TARGET_BINARY, target); |
| 142 | } |
| 143 | } |
| 144 | }; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 145 | } catch (ParserConfigurationException e) { |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 146 | System.err.println("Can't initialize XML Generator " + outputXmlFile); |
Urs Grob | fc77f6e | 2009-04-17 02:07:14 -0700 | [diff] [blame] | 147 | System.exit(1); |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 148 | return; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 151 | ExpectationStore libcoreVogarExpectationStore; |
| 152 | ExpectationStore ctsVogarExpectationStore; |
| 153 | |
Brett Chabot | 5977e94 | 2010-12-13 16:42:42 -0800 | [diff] [blame] | 154 | try { |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 155 | libcoreVogarExpectationStore |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 156 | = VogarUtils.provideExpectationStore(libcoreExpectationDir); |
Brian Muramatsu | 7f64e85 | 2011-02-17 16:52:16 -0800 | [diff] [blame] | 157 | ctsVogarExpectationStore = VogarUtils.provideExpectationStore(CTS_EXPECTATION_DIR); |
Brett Chabot | 5977e94 | 2010-12-13 16:42:42 -0800 | [diff] [blame] | 158 | } catch (IOException e) { |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 159 | System.err.println("Can't initialize vogar expectation store from " |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 160 | + libcoreExpectationDir); |
Brett Chabot | 5977e94 | 2010-12-13 16:42:42 -0800 | [diff] [blame] | 161 | e.printStackTrace(System.err); |
| 162 | System.exit(1); |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 163 | return; |
Brett Chabot | 5977e94 | 2010-12-13 16:42:42 -0800 | [diff] [blame] | 164 | } |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 165 | ExpectationStore[] expectations = new ExpectationStore[] { |
| 166 | libcoreVogarExpectationStore, ctsVogarExpectationStore |
| 167 | }; |
Brett Chabot | 5977e94 | 2010-12-13 16:42:42 -0800 | [diff] [blame] | 168 | |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 169 | JarFile jarFile = null; |
| 170 | try { |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 171 | jarFile = new JarFile(jarFileName); |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 172 | } catch (Exception e) { |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 173 | System.err.println("cannot open jarfile " + jarFileName); |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 174 | e.printStackTrace(); |
Urs Grob | fc77f6e | 2009-04-17 02:07:14 -0700 | [diff] [blame] | 175 | System.exit(1); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 178 | Map<String,TestClass> testCases = new LinkedHashMap<String, TestClass>(); |
| 179 | |
| 180 | String javaPackagePrefix = javaPackageFilter.isEmpty() ? "" : (javaPackageFilter + "."); |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 181 | |
| 182 | Enumeration<JarEntry> jarEntries = jarFile.entries(); |
| 183 | while (jarEntries.hasMoreElements()) { |
| 184 | JarEntry jarEntry = jarEntries.nextElement(); |
| 185 | String name = jarEntry.getName(); |
| 186 | if (!name.endsWith(".class")) { |
| 187 | continue; |
| 188 | } |
| 189 | String className |
| 190 | = name.substring(0, name.length() - ".class".length()).replace('/', '.'); |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 191 | if (!className.startsWith(javaPackagePrefix)) { |
| 192 | continue; |
| 193 | } |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 194 | try { |
| 195 | Class<?> klass = Class.forName(className, |
| 196 | false, |
| 197 | CollectAllTests.class.getClassLoader()); |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 198 | final int modifiers = klass.getModifiers(); |
| 199 | if (Modifier.isAbstract(modifiers) || !Modifier.isPublic(modifiers)) { |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 200 | continue; |
| 201 | } |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 202 | |
| 203 | final boolean isJunit4Class = isJunit4Class(klass); |
| 204 | if (!isJunit4Class && !isJunit3Test(klass)) { |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 205 | continue; |
| 206 | } |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 207 | |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 208 | try { |
| 209 | klass.getConstructor(new Class<?>[] { String.class } ); |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 210 | addToTests(expectations, testCases, klass); |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 211 | continue; |
| 212 | } catch (NoSuchMethodException e) { |
Brian Carlstrom | d9f4846 | 2011-05-28 18:47:39 -0700 | [diff] [blame] | 213 | } catch (SecurityException e) { |
Tsu Chiang Chuang | c87fd6b | 2011-07-13 17:04:40 -0700 | [diff] [blame] | 214 | System.out.println("Known bug (Working as intended): problem with class " |
| 215 | + className); |
Brian Carlstrom | d9f4846 | 2011-05-28 18:47:39 -0700 | [diff] [blame] | 216 | e.printStackTrace(); |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 217 | } |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 218 | |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 219 | try { |
| 220 | klass.getConstructor(new Class<?>[0]); |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 221 | addToTests(expectations, testCases, klass); |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 222 | continue; |
| 223 | } catch (NoSuchMethodException e) { |
Brian Carlstrom | d9f4846 | 2011-05-28 18:47:39 -0700 | [diff] [blame] | 224 | } catch (SecurityException e) { |
Tsu Chiang Chuang | c87fd6b | 2011-07-13 17:04:40 -0700 | [diff] [blame] | 225 | System.out.println("Known bug (Working as intended): problem with class " |
| 226 | + className); |
Brian Carlstrom | d9f4846 | 2011-05-28 18:47:39 -0700 | [diff] [blame] | 227 | e.printStackTrace(); |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 228 | } |
| 229 | } catch (ClassNotFoundException e) { |
| 230 | System.out.println("class not found " + className); |
| 231 | e.printStackTrace(); |
| 232 | System.exit(1); |
| 233 | } |
| 234 | } |
| 235 | |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 236 | for (Iterator<TestClass> iterator = testCases.values().iterator(); iterator.hasNext();) { |
| 237 | TestClass type = iterator.next(); |
| 238 | xmlGenerator.addTestClass(type); |
| 239 | } |
| 240 | |
| 241 | try { |
| 242 | xmlGenerator.dump(); |
| 243 | } catch (Exception e) { |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 244 | System.err.println("cannot dump xml to " + outputXmlFile); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 245 | e.printStackTrace(); |
Urs Grob | fc77f6e | 2009-04-17 02:07:14 -0700 | [diff] [blame] | 246 | System.exit(1); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 250 | private static class TestType { |
| 251 | private static final int HOST_SIDE_ONLY = 1; |
| 252 | private static final int DEVICE_SIDE_ONLY = 2; |
| 253 | private static final int VM_HOST_TEST = 3; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 254 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 255 | private final int type; |
| 256 | private final String jarPath; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 257 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 258 | private TestType (int type, String jarPath) { |
| 259 | this.type = type; |
| 260 | this.jarPath = jarPath; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 263 | private static TestType getTestType(String makeFileName) { |
| 264 | if (makeFileName == null || makeFileName.isEmpty()) { |
| 265 | return new TestType(DEVICE_SIDE_ONLY, null); |
| 266 | } |
| 267 | int type = TestType.DEVICE_SIDE_ONLY; |
| 268 | String jarPath = null; |
| 269 | try { |
| 270 | BufferedReader reader = new BufferedReader(new FileReader(makeFileName)); |
| 271 | String line; |
| 272 | |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 273 | while ((line = reader.readLine())!=null) { |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 274 | if (line.startsWith(TEST_TYPE)) { |
| 275 | if (line.indexOf(ATTRIBUTE_VM_HOST_TEST) >= 0) { |
| 276 | type = VM_HOST_TEST; |
| 277 | } else { |
| 278 | type = HOST_SIDE_ONLY; |
| 279 | } |
| 280 | } else if (line.startsWith(JAR_PATH)) { |
| 281 | jarPath = line.substring(JAR_PATH.length(), line.length()).trim(); |
| 282 | } |
| 283 | } |
| 284 | reader.close(); |
| 285 | } catch (IOException e) { |
| 286 | } |
| 287 | return new TestType(type, jarPath); |
| 288 | } |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | private static Element getElement(Element element, String tagName) { |
| 292 | NodeList elements = element.getElementsByTagName(tagName); |
| 293 | if (elements.getLength() > 0) { |
| 294 | return (Element) elements.item(0); |
| 295 | } else { |
| 296 | return null; |
| 297 | } |
| 298 | } |
| 299 | |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 300 | private static String getElementAttribute(Element element, |
| 301 | String elementName, |
| 302 | String attributeName) { |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 303 | Element e = getElement(element, elementName); |
| 304 | if (e != null) { |
| 305 | return e.getAttribute(attributeName); |
| 306 | } else { |
| 307 | return ""; |
| 308 | } |
| 309 | } |
| 310 | |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 311 | private static String getKnownFailure(final Class<?> testClass, |
Urs Grob | fc77f6e | 2009-04-17 02:07:14 -0700 | [diff] [blame] | 312 | final String testName) { |
| 313 | return getAnnotation(testClass, testName, KNOWN_FAILURE); |
| 314 | } |
Brian Muramatsu | b8ed316 | 2010-07-13 12:43:52 -0700 | [diff] [blame] | 315 | |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 316 | private static boolean isKnownFailure(final Class<?> testClass, |
Brian Muramatsu | b8ed316 | 2010-07-13 12:43:52 -0700 | [diff] [blame] | 317 | final String testName) { |
| 318 | return getAnnotation(testClass, testName, KNOWN_FAILURE) != null; |
| 319 | } |
| 320 | |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 321 | private static boolean isSuppressed(final Class<?> testClass, |
Brian Muramatsu | 168beb0 | 2010-10-21 12:39:45 -0700 | [diff] [blame] | 322 | final String testName) { |
| 323 | return getAnnotation(testClass, testName, SUPPRESSED_TEST) != null; |
| 324 | } |
| 325 | |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 326 | private static String getAnnotation(final Class<?> testClass, |
Urs Grob | fc77f6e | 2009-04-17 02:07:14 -0700 | [diff] [blame] | 327 | final String testName, final String annotationName) { |
The Android Open Source Project | 7671bec | 2009-03-19 23:08:36 -0700 | [diff] [blame] | 328 | try { |
Urs Grob | fc77f6e | 2009-04-17 02:07:14 -0700 | [diff] [blame] | 329 | Method testMethod = testClass.getMethod(testName, (Class[])null); |
The Android Open Source Project | 7671bec | 2009-03-19 23:08:36 -0700 | [diff] [blame] | 330 | Annotation[] annotations = testMethod.getAnnotations(); |
| 331 | for (Annotation annot : annotations) { |
| 332 | |
Urs Grob | fc77f6e | 2009-04-17 02:07:14 -0700 | [diff] [blame] | 333 | if (annot.annotationType().getName().equals(annotationName)) { |
The Android Open Source Project | 7671bec | 2009-03-19 23:08:36 -0700 | [diff] [blame] | 334 | String annotStr = annot.toString(); |
| 335 | String knownFailure = null; |
| 336 | if (annotStr.contains("(value=")) { |
| 337 | knownFailure = |
Urs Grob | fc77f6e | 2009-04-17 02:07:14 -0700 | [diff] [blame] | 338 | annotStr.substring(annotStr.indexOf("=") + 1, |
| 339 | annotStr.length() - 1); |
The Android Open Source Project | 7671bec | 2009-03-19 23:08:36 -0700 | [diff] [blame] | 340 | |
| 341 | } |
| 342 | |
| 343 | if (knownFailure == null) { |
| 344 | knownFailure = "true"; |
| 345 | } |
| 346 | |
| 347 | return knownFailure; |
| 348 | } |
| 349 | |
| 350 | } |
| 351 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 352 | } catch (NoSuchMethodException e) { |
The Android Open Source Project | 7671bec | 2009-03-19 23:08:36 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | return null; |
| 356 | } |
| 357 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 358 | private static void addToTests(ExpectationStore[] expectations, |
| 359 | Map<String,TestClass> testCases, |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 360 | Class<?> testClass) { |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 361 | Set<String> testNames = new HashSet<String>(); |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 362 | |
| 363 | boolean isJunit3Test = isJunit3Test(testClass); |
| 364 | |
| 365 | Method[] testMethods = testClass.getMethods(); |
| 366 | for (Method testMethod : testMethods) { |
| 367 | String testName = testMethod.getName(); |
| 368 | if (testNames.contains(testName)) { |
| 369 | continue; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 370 | } |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 371 | |
| 372 | /* Make sure the method has the right signature. */ |
| 373 | if (!Modifier.isPublic(testMethod.getModifiers())) { |
| 374 | continue; |
| 375 | } |
| 376 | if (!testMethod.getReturnType().equals(Void.TYPE)) { |
| 377 | continue; |
| 378 | } |
| 379 | if (testMethod.getParameterTypes().length != 0) { |
| 380 | continue; |
| 381 | } |
| 382 | |
| 383 | if ((isJunit3Test && !testName.startsWith("test")) |
| 384 | || (!isJunit3Test && !isJunit4TestMethod(testMethod))) { |
| 385 | continue; |
| 386 | } |
| 387 | |
| 388 | testNames.add(testName); |
| 389 | addToTests(expectations, testCases, testClass, testName); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 390 | } |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 393 | private static void addToTests(ExpectationStore[] expectations, |
| 394 | Map<String,TestClass> testCases, |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 395 | Class<?> test, |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 396 | String testName) { |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 397 | |
| 398 | String testClassName = test.getName(); |
| 399 | String knownFailure = getKnownFailure(test, testName); |
| 400 | |
| 401 | if (isKnownFailure(test, testName)) { |
| 402 | System.out.println("ignoring known failure: " + test + "#" + testName); |
| 403 | return; |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 404 | } else if (isSuppressed(test, testName)) { |
| 405 | System.out.println("ignoring suppressed test: " + test + "#" + testName); |
| 406 | return; |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 407 | } else if (VogarUtils.isVogarKnownFailure(expectations, |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 408 | testClassName, |
| 409 | testName)) { |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 410 | System.out.println("ignoring expectation known failure: " + test |
Brian Carlstrom | 9f2dab8 | 2011-04-01 15:47:17 -0700 | [diff] [blame] | 411 | + "#" + testName); |
| 412 | return; |
| 413 | } |
| 414 | |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 415 | TestClass testClass; |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 416 | if (testCases.containsKey(testClassName)) { |
| 417 | testClass = testCases.get(testClassName); |
| 418 | } else { |
| 419 | testClass = new TestClass(testClassName, new ArrayList<TestMethod>()); |
| 420 | testCases.put(testClassName, testClass); |
| 421 | } |
| 422 | |
Brian Muramatsu | 168beb0 | 2010-10-21 12:39:45 -0700 | [diff] [blame] | 423 | testClass.mCases.add(new TestMethod(testName, "", "", knownFailure, false, false)); |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 424 | } |
Tsu Chiang Chuang | c87fd6b | 2011-07-13 17:04:40 -0700 | [diff] [blame] | 425 | |
Kenny Root | b5b47cb | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 426 | private static boolean isJunit3Test(Class<?> klass) { |
| 427 | return TestCase.class.isAssignableFrom(klass); |
| 428 | } |
| 429 | |
| 430 | private static boolean isJunit4Class(Class<?> klass) { |
| 431 | for (Annotation a : klass.getAnnotations()) { |
| 432 | if (RunWith.class.isAssignableFrom(a.annotationType())) { |
| 433 | // @RunWith is currently not supported for CTS tests because tradefed cannot handle |
| 434 | // a single test spawning other tests with different names. |
| 435 | System.out.println("Skipping test class " + klass.getName() |
| 436 | + ": JUnit4 @RunWith is not supported"); |
| 437 | return false; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | for (Method m : klass.getMethods()) { |
| 442 | if (isJunit4TestMethod(m)) { |
| 443 | return true; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | private static boolean isJunit4TestMethod(Method method) { |
| 451 | for (Annotation a : method.getAnnotations()) { |
| 452 | if (org.junit.Test.class.isAssignableFrom(a.annotationType())) { |
| 453 | return true; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | return false; |
| 458 | } |
| 459 | |
Tsu Chiang Chuang | c87fd6b | 2011-07-13 17:04:40 -0700 | [diff] [blame] | 460 | /** |
| 461 | * Determines if a given string is a valid java package name |
| 462 | * @param javaPackageName |
| 463 | * @return true if it is valid, false otherwise |
| 464 | */ |
| 465 | private static boolean isValidJavaPackage(String javaPackageName) { |
| 466 | String[] strSections = javaPackageName.split("."); |
| 467 | for (String strSection : strSections) { |
| 468 | if (!isValidJavaIdentifier(strSection)) { |
| 469 | return false; |
| 470 | } |
| 471 | } |
| 472 | return true; |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Determines if a given string is a valid java identifier. |
| 477 | * @param javaIdentifier |
| 478 | * @return true if it is a valid identifier, false otherwise |
| 479 | */ |
| 480 | private static boolean isValidJavaIdentifier(String javaIdentifier) { |
| 481 | if (javaIdentifier.length() == 0 || |
| 482 | !Character.isJavaIdentifierStart(javaIdentifier.charAt(0))) { |
| 483 | return false; |
| 484 | } |
| 485 | for (int i = 1; i < javaIdentifier.length(); i++) { |
| 486 | if (!Character.isJavaIdentifierPart(javaIdentifier.charAt(i))) { |
| 487 | return false; |
| 488 | } |
| 489 | } |
| 490 | return true; |
| 491 | } |
The Android Open Source Project | f805710 | 2009-03-15 16:47:16 -0700 | [diff] [blame] | 492 | } |