Author: clr Date: Mon Mar 28 10:30:09 2005 New Revision: 159275 URL: http://svn.apache.org/viewcvs?view=rev&rev=159275 Log: JUnit test framework
Added: incubator/jdo/trunk/api20/test/java/javax/jdo/util/ incubator/jdo/trunk/api20/test/java/javax/jdo/util/AbstractTest.java incubator/jdo/trunk/api20/test/java/javax/jdo/util/BatchResultPrinter.java incubator/jdo/trunk/api20/test/java/javax/jdo/util/BatchTestRunner.java Added: incubator/jdo/trunk/api20/test/java/javax/jdo/util/AbstractTest.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/api20/test/java/javax/jdo/util/AbstractTest.java?view=auto&rev=159275 ============================================================================== --- incubator/jdo/trunk/api20/test/java/javax/jdo/util/AbstractTest.java (added) +++ incubator/jdo/trunk/api20/test/java/javax/jdo/util/AbstractTest.java Mon Mar 28 10:30:09 2005 @@ -0,0 +1,47 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package javax.jdo.util; + +import java.io.PrintStream; + +import junit.framework.TestCase; + +/** */ +public class AbstractTest extends TestCase { + + /** */ + protected static PrintStream out = System.out; + + /** If true, print extra messages. */ + protected boolean verbose; + + /** + * Construct and initialize from properties. + */ + protected AbstractTest() { + super(null); + verbose = Boolean.getBoolean("verbose"); + } + + /** + */ + protected void println(String s) { + if (verbose) + out.println(s); + } +} + Added: incubator/jdo/trunk/api20/test/java/javax/jdo/util/BatchResultPrinter.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/api20/test/java/javax/jdo/util/BatchResultPrinter.java?view=auto&rev=159275 ============================================================================== --- incubator/jdo/trunk/api20/test/java/javax/jdo/util/BatchResultPrinter.java (added) +++ incubator/jdo/trunk/api20/test/java/javax/jdo/util/BatchResultPrinter.java Mon Mar 28 10:30:09 2005 @@ -0,0 +1,103 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package javax.jdo.util; + +import java.io.PrintStream; + +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestResult; +import junit.textui.ResultPrinter; + +/** + * Default result printer implementation for running tests in batch mode. + * + * @author Michael Bouschen + */ +public class BatchResultPrinter + extends ResultPrinter +{ + /** */ + public BatchResultPrinter(PrintStream writer) { + super(writer); + } + + /** Called in case of a test error. */ + public void addError(Test test, Throwable t) { + getWriter().print(" ERROR"); + } + + /** Called in case of a test failure. */ + public void addFailure(Test test, AssertionFailedError t) { + getWriter().print(" FAILURE"); + } + + /** Called when a test case is finished. */ + public void endTest(Test test) { + getWriter().println(); + } + + /** Called when a test case is started. */ + public void startTest(Test test) { + String testName; + if (test instanceof TestCase) { + testName = getClassBaseName(test) + "." + ((TestCase)test).getName(); + } + else { + testName = test.toString(); + } + getWriter().print("RUN " + testName); + } + + /** */ + protected void printHeader(long runTime) { + getWriter().println("Time: "+elapsedTimeAsString(runTime)); + } + + /** */ + protected void printFooter(TestResult result) { + if (result.wasSuccessful()) { + getWriter().print("OK"); + getWriter().println (" (" + result.runCount() + " test" + (result.runCount() == 1 ? "": "s") + ")"); + + } else { + getWriter().println("FAILURES!!!"); + getWriter().println("Tests run: "+result.runCount()+ + ", Failures: "+result.failureCount()+ + ", Errors: "+result.errorCount()); + } + } + + // helper method + + /** + * @return Name of the class of the given object without package prefix + */ + private String getClassBaseName(Object obj) { + if (obj == null) return null; + String className = obj.getClass().getName(); + int index = className.lastIndexOf('.'); + if (index != -1) { + className = className.substring(index + 1); + } + return className; + } + +} + + Added: incubator/jdo/trunk/api20/test/java/javax/jdo/util/BatchTestRunner.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/api20/test/java/javax/jdo/util/BatchTestRunner.java?view=auto&rev=159275 ============================================================================== --- incubator/jdo/trunk/api20/test/java/javax/jdo/util/BatchTestRunner.java (added) +++ incubator/jdo/trunk/api20/test/java/javax/jdo/util/BatchTestRunner.java Mon Mar 28 10:30:09 2005 @@ -0,0 +1,163 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package javax.jdo.util; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.io.PrintStream; + +import junit.framework.Test; +import junit.framework.TestResult; +import junit.framework.TestSuite; +import junit.textui.ResultPrinter; +import junit.textui.TestRunner; + +/** + * TestRunner class for running a single test or a test suite in batch + * mode. The format of the test output is specified by the result printer + * class. The main method sets an exit code according to the test result: + * <ul> + * <li><code>0</code>: success + * <li><code>1</code>: failure, the test shows an unexpected behavior + * <li><code>2</code>: exception, the test throws an unhandled excption + * </ul> + * + * @author Michael Bouschen + */ +public class BatchTestRunner + extends TestRunner +{ + /** Name of the system property to specify the result printer class. */ + public static final String RESULTPRINTER_PROPERTY = "ResultPrinterClass"; + + /** Default of the system property ResultPrinterClass. */ + public static final String RESULTPRINTER_DEFAULT = BatchResultPrinter.class.getName(); + + /** + * Constructor. + * It creates a result printer instance based on the system property + * and delegates to the constructor taking a result printer argument. + */ + public BatchTestRunner() { + super(); + setPrinter(getResultPrinter()); + } + + /** + * Constructor. USes teh specified resultPrinter to format the test result. + */ + public BatchTestRunner(ResultPrinter resultPrinter) { + super(resultPrinter); + } + + /** Runs all test methods from the specified class. */ + public static void run(Class clazz) { + run(new TestSuite(clazz)); + } + + /** Runs the specified test. */ + public static TestResult run(Test test) { + return new BatchTestRunner().doRun(test); + } + + /** Runs the specified test and waits until the user types RETURN. */ + public static void runAndWait(Test suite) { + new BatchTestRunner().doRun(suite, true); + } + + /** + * Runs in batch mode and sets an exit code. If the specified String + * array includes a single fully qualified class name, this test class + * is executed. If it is empty it runs the TestListSuite. + */ + public static void main(String args[]) { + BatchTestRunner aTestRunner= new BatchTestRunner(); + try { + /* + if ((args == null) || args.length == 0) + args = new String[] { TestListSuite.class.getName() }; + */ + TestResult r = aTestRunner.start(args); + if (!r.wasSuccessful()) + System.exit(FAILURE_EXIT); + System.exit(SUCCESS_EXIT); + } catch(Exception e) { + System.err.println(e.getMessage()); + System.exit(EXCEPTION_EXIT); + } + } + + /** Returns a result printer instance. n instance of tCheck the system property */ + protected ResultPrinter getResultPrinter() { + String className = System.getProperty(RESULTPRINTER_PROPERTY); + if (className != null) { + className = className.trim(); + if (className.length() != 0) { + String msg = null; + try { + // get class instance + Class clazz = Class.forName(className); + // constructor taking PrintStream arg + Constructor ctor = clazz.getConstructor( + new Class[] { PrintStream.class } ); + // create instance + return (ResultPrinter)ctor.newInstance( + new Object[] { System.out }); + } + catch (ClassNotFoundException ex) { + // specified ResultPrinter class not + msg = "Cannot find specified result printer class " + + className + "."; + } + catch (NoSuchMethodException ex) { + msg = "Class " + className + + " does not provide constructor taking a PrintStream."; + } + catch (InstantiationException ex) { + msg = "Class " + className + " is abstract."; + } + catch (IllegalAccessException ex) { + msg = "Constructor taking a PrintStream of class " + + className + " is not accessible."; + } + catch (InvocationTargetException ex) { + msg = "Constructor call results in exception " + ex + "."; + } + + // ResultPrinter class specified, but not avaiable + System.out.println(msg); + ResultPrinter printer = getDefaultResultPrinter(); + System.out.println("Using default result printer of class " + + printer.getClass().getName()); + } + } + + // ResultPrinter class not specified => use default + return getDefaultResultPrinter(); + } + + /** + * Returns an instance of the default result printer class + * BatchResultPrinter. + */ + protected ResultPrinter getDefaultResultPrinter() { + return new BatchResultPrinter(System.out); + } + + +} +