1 /* 2 * Copyright (C) 2017 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.junitxml; 18 19 import org.junit.Test; 20 import org.junit.internal.TextListener; 21 import org.junit.runner.JUnitCore; 22 import org.junit.runner.Result; 23 24 import java.io.File; 25 import java.io.FileNotFoundException; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import java.lang.reflect.Method; 29 import java.util.stream.Stream; 30 31 /** 32 * A JUnit runner that is intended to use as replacement of JUnitCore 33 * which in addition to printing the test failures to stdout, will 34 * write the results in XML format to the path specified in the env 35 * variable XML_OUTPUT_FILE. 36 * 37 * <p>To use this runner: 38 * {@code TEST_WORKSPACE=[...] 39 * XML_OUTPUT_FILE=[...] 40 * java -cp junitxml.jar [...] \ 41 * com.android.junitxml.JUnitXmlRunner [Test classes]} 42 */ 43 public class JUnitXmlRunner { 44 getRunListener()45 private static XmlRunListener getRunListener() { 46 String outputFile = System.getenv("XML_OUTPUT_FILE"); 47 String suiteName = System.getenv("TEST_WORKSPACE"); 48 if (outputFile != null && outputFile.length() > 0) { 49 try { 50 return new XmlRunListener( 51 new FileOutputStream(outputFile), 52 suiteName != null ? suiteName : "Unknown test suite"); 53 } catch (FileNotFoundException e) { 54 e.printStackTrace(); 55 } 56 } 57 return null; 58 } 59 getAtestRunListener(int count)60 private static AtestRunListener getAtestRunListener(int count) { 61 String outputFileStr = System.getenv("EVENT_FILE_ROBOLECTRIC"); 62 String suiteName = System.getenv("TEST_WORKSPACE"); 63 if (outputFileStr != null && outputFileStr.length() > 0) { 64 File outputFile = new File(outputFileStr); 65 if (outputFile.exists()) { 66 return new AtestRunListener(suiteName, outputFile, count); 67 } 68 } 69 return null; 70 } 71 main(String... args)72 public static void main(String... args) { 73 JUnitCore core = new JUnitCore(); 74 try { 75 Class[] as = 76 Stream.of(args) 77 .map( 78 test -> { 79 try { 80 return Class.forName(test); 81 } catch (ClassNotFoundException e) { 82 throw new RuntimeException(e); 83 } 84 }) 85 .toArray(Class[]::new); 86 TextListener textListener = new TextListener(System.out); 87 core.addListener(textListener); 88 XmlRunListener xmlListener = getRunListener(); 89 if (xmlListener != null) { 90 core.addListener(xmlListener); 91 } 92 93 // Add AtestRunListener to communicate with ATest. 94 AtestRunListener atestRunListener = getAtestRunListener(calcTestCount(as)); 95 if (atestRunListener != null) { 96 core.addListener(atestRunListener); 97 } 98 Result result = core.run(as); 99 if (xmlListener != null) { 100 xmlListener.endTestSuite(); 101 } 102 System.exit(result.wasSuccessful() ? 0 : 1); 103 } catch (IOException e) { 104 throw new RuntimeException(e); 105 } 106 } 107 calcTestCount(Class[] as)108 private static int calcTestCount(Class[] as) { 109 int count = 0; 110 for (Class cls : as) { 111 Method[] declaredMethods = cls.getMethods(); 112 for (Method method : declaredMethods) { 113 if (method.isAnnotationPresent(Test.class)) { 114 count++; 115 } 116 } 117 } 118 return count; 119 } 120 } 121 122