• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.internal.TextListener;
20 import org.junit.runner.JUnitCore;
21 import org.junit.runner.Result;
22 
23 import java.io.FileNotFoundException;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.util.stream.Stream;
27 
28 /**
29  * A JUnit runner that is intended to use as replacement of JUnitCore
30  * which in addition to printing the test failures to stdout, will
31  * write the results in XML format to the path specified in the env
32  * variable XML_OUTPUT_FILE.
33  *
34  * <p>To use this runner:
35  *     {@code TEST_WORKSPACE=[...]
36  *            XML_OUTPUT_FILE=[...]
37  *            java -cp junitxml.jar [...] \
38  *            com.android.junitxml.JUnitXmlRunner [Test classes]}
39  */
40 public class JUnitXmlRunner {
41 
getRunListener()42     private static XmlRunListener getRunListener() {
43         String outputFile = System.getenv("XML_OUTPUT_FILE");
44         String suiteName = System.getenv("TEST_WORKSPACE");
45         if (outputFile != null && outputFile.length() > 0) {
46             try {
47                 return new XmlRunListener(
48                         new FileOutputStream(outputFile),
49                         suiteName != null ? suiteName : "Unknown test suite");
50             } catch (FileNotFoundException e) {
51                 e.printStackTrace();
52             }
53         }
54         return null;
55     }
56 
main(String... args)57     public static void main(String... args) {
58         JUnitCore core = new JUnitCore();
59         try {
60             TextListener textListener = new TextListener(System.out);
61             core.addListener(textListener);
62             XmlRunListener xmlListener = getRunListener();
63             if (xmlListener != null) {
64                 core.addListener(xmlListener);
65             }
66             Result result = core.run(Stream.of(args)
67                     .map(test -> {
68                         try {
69                             return Class.forName(test);
70                         } catch (ClassNotFoundException e) {
71                             throw new RuntimeException(e);
72                         }
73                     })
74                     .toArray(Class[]::new));
75             if (xmlListener != null) {
76                 xmlListener.endTestSuite();
77             }
78             System.exit(result.wasSuccessful() ? 0 : 1);
79         } catch (IOException e) {
80             throw new RuntimeException(e);
81         }
82     }
83 }
84 
85