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 import com.sun.javatest.TestDescription;
  32 import com.sun.javatest.TestResult;
  33 import com.sun.javatest.TestRunner;
  34 import com.sun.javatest.WorkDirectory;
  35 import com.sun.javatest.lib.MultiTest;
  36 import com.sun.javatest.util.BackupPolicy;
  37 import java.io.IOException;
  38 import java.util.Iterator;
  39 
  40 /**
  41  *
  42  */
  43 public class JUnitTestRunner extends TestRunner {
  44 
  45     /** Creates a new instance of JUnitTestRunner */
  46     public JUnitTestRunner() {
  47         // could create additional constructors to accept args
  48         // might be useful if there are more settings that need to be passed
  49         // to the MultiTest class.  This class is generally constructed
  50         // by the corresponding TestSuite class.
  51     }
  52 
  53     protected boolean runTests(Iterator testIter) throws InterruptedException {
  54         WorkDirectory wd = getWorkDirectory();
  55         TestDescription td = null;
  56         //for (TestDescription td: testIter) {
  57         for (; testIter.hasNext() ;) {
  58             td = (TestDescription)(testIter.next());
  59             TestResult tr = new TestResult(td);
  60             TestResult.Section outSection = tr.createSection("Main");
  61 
  62             notifyStartingTest(tr);
  63             Status execStatus = getMultiTest(td).run(getTestArgs(td), outSection.createOutput("stdout"), outSection.createOutput("stderr"));
  64             tr.setStatus(execStatus);
  65 
  66             try {
  67                 if (execStatus.getType() != Status.PASSED || jtrIfPassed)
  68                     tr.writeResults(wd, backupPolicy);
  69             } catch (IOException e) {
  70                 // ignore it; the test will have an error status already
  71                 // could log the error using the logging system
  72             }
  73 
  74             notifyFinishedTest(tr);
  75         }   // for
  76 
  77         return false;
  78     }
  79 
  80     public BackupPolicy getBackupPolicy() {
  81         return backupPolicy;
  82     }
  83 
  84     void setClassLoader(ClassLoader loader) {
  85         this.loader = loader;
  86 
  87         try {
  88             Class c = loader.loadClass("com.sun.javatest.junit.JUnitMultiTest");
  89         } catch (ClassNotFoundException e) {
  90             e.printStackTrace();
  91         }
  92     }
  93 
  94     /**
  95      * Get the test class that should be used to execute this test.
  96      * The default implementation returns a JUnitBareMultiTest or
  97      * JUnitAnnotationMultiTest depending on the <tt>junit.finderscantype</tt>
  98      * value in the test description.
  99      *
 100      * Override this method if you wish to return a <tt>JUnitMultiTest</tt> of
 101      * your own.
 102      *
 103      * @see com.sun.javatest.junit.JUnitMultiTest
 104      */
 105     protected MultiTest getMultiTest(TestDescription td) {
 106         String type = td.getParameter("junit.finderscantype");
 107         JUnitMultiTest mt = null;
 108 
 109         if (type != null) {
 110             if (type.equals("superclass")) {
 111                 //mt = new JUnitMultiTest();
 112                 mt = new JUnitBareMultiTest(loader);
 113             } else {
 114                 mt = new JUnitAnnotationMultiTest(loader);
 115             }
 116         }
 117 
 118         return mt;
 119     }
 120 
 121     // could make this protected so that custom test runners could easily
 122     // add args without changing other code
 123     private String[] getTestArgs(TestDescription td) {
 124         return new String[] {td.getParameter("executeClass")};
 125     }
 126 
 127     protected BackupPolicy backupPolicy = BackupPolicy.noBackups(); // optional
 128     protected ClassLoader loader;
 129     // the name of the setting is legacy, but we provide equivalent behavior
 130     private boolean jtrIfPassed =
 131             System.getProperty("javatest.script.jtrIfPassed", "true").equals("true");
 132 }