1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.  Oracle designates this
  10  * particular file as subject to the "Classpath" exception as provided
  11  * by Oracle in the LICENSE file that accompanied this code.
  12  *
  13  * This code is distributed in the hope that it will be useful, but WITHOUT
  14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16  * version 2 for more details (a copy is included in the LICENSE file that
  17  * accompanied this code).
  18  *
  19  * You should have received a copy of the GNU General Public License version
  20  * 2 along with this work; if not, write to the Free Software Foundation,
  21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22  *
  23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  24  * or visit www.oracle.com if you need additional information or have any
  25  * questions.
  26  */
  27 
  28 package com.sun.javatest.junit;
  29 
  30 import com.sun.javatest.Status;
  31 
  32 import java.io.PrintWriter;
  33 
  34 import org.junit.runner.JUnitCore;
  35 import org.junit.runner.Result;
  36 
  37 /**
  38  * Class to execute tests using the JUnit annotation style of test execution.
  39  * Or more accurately, JUnit 4.x style execution rather than 3.x style.  The
  40  * JUnitCore class is used to execute the test described in the TestDescription.
  41  */
  42 public class JUnitAnnotationMultiTest extends JUnitMultiTest {
  43 
  44     public JUnitAnnotationMultiTest(ClassLoader cl) {
  45         super(cl);
  46     }
  47 
  48     public Status run(String[] argv, PrintWriter stdout, PrintWriter stderr) {
  49         this.log = stderr;
  50         this.ref = stdout;
  51         setup(argv[0]);
  52         return run0(argv);
  53     }
  54 
  55     /**
  56      * Common method for running the test, used by all entry points.
  57      */
  58     public Status run0(String[] argv) {
  59         JUnitCore core = new JUnitCore();
  60         Result junitresult = core.run(testCaseClass);
  61 
  62         if (junitresult.wasSuccessful())
  63             return Status.passed("All test cases passed.");
  64         else
  65             return Status.failed("Test cases failed: " + junitresult.getFailureCount());
  66     }
  67 
  68 
  69     /**
  70      * Entry point for direct execution, not used by the harness.
  71      */
  72     public static void main(String args[]) {
  73         String executeClass = System.getProperty("javaTestExecuteClass");
  74         JUnitAnnotationMultiTest multiTest = new JUnitAnnotationMultiTest(ClassLoader.getSystemClassLoader());
  75         multiTest.setup(executeClass);
  76         multiTest.run0(args);
  77     }
  78 
  79     /**
  80      * Entry point for standalone mode.
  81      */
  82     protected void setup(String executeClass) {
  83 
  84         try {
  85             Class junitTestCaseClass = getClassLoader().loadClass(executeClass);
  86             testCaseClass = junitTestCaseClass;
  87 
  88 
  89         } catch (ClassNotFoundException e){
  90             log.println("Cannot find test: " + executeClass + " (" + exceptionToString(e) + ")");
  91         }
  92     }
  93 
  94     protected void printStackTrace(Throwable t) {
  95         t.printStackTrace(log);
  96     }
  97 
  98     protected Class testCaseClass;
  99 }