From jdo-commits-return-294-apmail-db-jdo-commits-archive=www.apache.org@db.apache.org Mon Mar 28 18:30:12 2005 Return-Path: Delivered-To: apmail-db-jdo-commits-archive@www.apache.org Received: (qmail 64920 invoked from network); 28 Mar 2005 18:30:12 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 28 Mar 2005 18:30:12 -0000 Received: (qmail 68340 invoked by uid 500); 28 Mar 2005 18:30:12 -0000 Mailing-List: contact jdo-commits-help@db.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: jdo-dev@db.apache.org Delivered-To: mailing list jdo-commits@db.apache.org Received: (qmail 68327 invoked by uid 99); 28 Mar 2005 18:30:12 -0000 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from minotaur.apache.org (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Mon, 28 Mar 2005 10:30:11 -0800 Received: (qmail 64910 invoked by uid 65534); 28 Mar 2005 18:30:10 -0000 Message-ID: <20050328183010.64909.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Mailer: svnmailer-1.0.0-dev Date: Mon, 28 Mar 2005 18:30:10 -0000 Subject: svn commit: r159275 - in incubator/jdo/trunk/api20/test/java/javax/jdo/util: ./ AbstractTest.java BatchResultPrinter.java BatchTestRunner.java To: jdo-commits@db.apache.org From: clr@apache.org X-Virus-Checked: Checked X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: clr Date: Mon Mar 28 10:30:09 2005 New Revision: 159275 URL: http://svn.apache.org/viewcvs?view=3Drev&rev=3D159275 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.j= ava 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/java= x/jdo/util/AbstractTest.java?view=3Dauto&rev=3D159275 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jdo/trunk/api20/test/java/javax/jdo/util/AbstractTest.java (a= dded) +++ incubator/jdo/trunk/api20/test/java/javax/jdo/util/AbstractTest.java Mo= n Mar 28 10:30:09 2005 @@ -0,0 +1,47 @@ +/* + * Copyright 2005 The Apache Software Foundation. + *=20 + * 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=20 + *=20 + * http://www.apache.org/licenses/LICENSE-2.0 + *=20 + * Unless required by applicable law or agreed to in writing, software=20 + * distributed under the License is distributed on an "AS IS" BASIS,=20 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied= .=20 + * See the License for the specific language governing permissions and=20 + * 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 =3D System.out; + =20 + /** If true, print extra messages. */ + protected boolean verbose; + + /** + * Construct and initialize from properties. + */ + protected AbstractTest() { + super(null); + verbose =3D Boolean.getBoolean("verbose"); + } + =20 + /** + */ + protected void println(String s) { + if (verbose)=20 + out.println(s); + } +} + Added: incubator/jdo/trunk/api20/test/java/javax/jdo/util/BatchResultPrinte= r=2Ejava URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/api20/test/java/java= x/jdo/util/BatchResultPrinter.java?view=3Dauto&rev=3D159275 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jdo/trunk/api20/test/java/javax/jdo/util/BatchResultPrinter.j= ava (added) +++ incubator/jdo/trunk/api20/test/java/javax/jdo/util/BatchResultPrinter.j= ava Mon Mar 28 10:30:09 2005 @@ -0,0 +1,103 @@ +/* + * Copyright 2005 The Apache Software Foundation. + *=20 + * 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=20 + *=20 + * http://www.apache.org/licenses/LICENSE-2.0 + *=20 + * Unless required by applicable law or agreed to in writing, software=20 + * distributed under the License is distributed on an "AS IS" BASIS,=20 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied= .=20 + * See the License for the specific language governing permissions and=20 + * 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. + *=20 + * @author Michael Bouschen + */ +public class BatchResultPrinter + extends ResultPrinter +{ + /** */ + public BatchResultPrinter(PrintStream writer) { + super(writer); + } + =20 + /** Called in case of a test error. */ + public void addError(Test test, Throwable t) { + getWriter().print(" ERROR"); + } + =20 + /** Called in case of a test failure. */=20 + public void addFailure(Test test, AssertionFailedError t) { + getWriter().print(" FAILURE"); + } + =20 + /** Called when a test case is finished. */ + public void endTest(Test test) { + getWriter().println(); + } + =20 + /** Called when a test case is started. */ + public void startTest(Test test) { + String testName; + if (test instanceof TestCase) { + testName =3D getClassBaseName(test) + "." + ((TestCase)test).g= etName(); + } + else { + testName =3D test.toString(); + } + getWriter().print("RUN " + testName); + } + =20 + /** */ + protected void printHeader(long runTime) { + getWriter().println("Time: "+elapsedTimeAsString(runTime)); + } + =20 + /** */ + protected void printFooter(TestResult result) { + if (result.wasSuccessful()) { + getWriter().print("OK"); + getWriter().println (" (" + result.runCount() + " test" + (res= ult.runCount() =3D=3D 1 ? "": "s") + ")"); + =20 + } else { + getWriter().println("FAILURES!!!"); + getWriter().println("Tests run: "+result.runCount()+=20 + ", Failures: "+result.failureCount()+ + ", Errors: "+result.errorCount()); + } + } + =20 + // helper method + =20 + /**=20 + * @return Name of the class of the given object without package prefix + */ + private String getClassBaseName(Object obj) { + if (obj =3D=3D null) return null; + String className =3D obj.getClass().getName(); + int index =3D className.lastIndexOf('.'); + if (index !=3D -1) { + className =3D className.substring(index + 1); + } + return className; + } + =20 +} + + Added: incubator/jdo/trunk/api20/test/java/javax/jdo/util/BatchTestRunner.j= ava URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/api20/test/java/java= x/jdo/util/BatchTestRunner.java?view=3Dauto&rev=3D159275 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- 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. + *=20 + * 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=20 + *=20 + * http://www.apache.org/licenses/LICENSE-2.0 + *=20 + * Unless required by applicable law or agreed to in writing, software=20 + * distributed under the License is distributed on an "AS IS" BASIS,=20 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied= .=20 + * See the License for the specific language governing permissions and=20 + * 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: + *
    + *
  • 0: success + *
  • 1: failure, the test shows an unexpected behavior + *
  • 2: exception, the test throws an unhandled excption=20 + *
+ *=20 + * @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 =3D "ResultPrinterCl= ass";=20 + =20 + /** Default of the system property ResultPrinterClass. */ + public static final String RESULTPRINTER_DEFAULT =3D BatchResultPrinte= r=2Eclass.getName(); + =20 + /**=20 + * Constructor.=20 + * It creates a result printer instance based on the system property + * and delegates to the constructor taking a result printer argument.=20 + */ + public BatchTestRunner() { + super(); + setPrinter(getResultPrinter()); + } + =20 + /** =20 + * Constructor. USes teh specified resultPrinter to format the test re= sult. + */ + public BatchTestRunner(ResultPrinter resultPrinter) { + super(resultPrinter); + } + + /** Runs all test methods from the specified class. */ + public static void run(Class clazz) { + run(new TestSuite(clazz)); + } + =20 + /** 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); + } + + /**=20 + * 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=3D new BatchTestRunner(); + try { + /* + if ((args =3D=3D null) || args.length =3D=3D 0) + args =3D new String[] { TestListSuite.class.getName() }; + */ + TestResult r =3D aTestRunner.start(args); + if (!r.wasSuccessful())=20 + System.exit(FAILURE_EXIT); + System.exit(SUCCESS_EXIT); + } catch(Exception e) { + System.err.println(e.getMessage()); + System.exit(EXCEPTION_EXIT); + } + } + =20 + /** Returns a result printer instance. n instance of tCheck the system= property */ + protected ResultPrinter getResultPrinter() { + String className =3D System.getProperty(RESULTPRINTER_PROPERTY); + if (className !=3D null) { + className =3D className.trim(); + if (className.length() !=3D 0) { + String msg =3D null; + try { + // get class instance + Class clazz =3D Class.forName(className); + // constructor taking PrintStream arg + Constructor ctor =3D clazz.getConstructor( + new Class[] { PrintStream.class } ); + // create instance + return (ResultPrinter)ctor.newInstance( + new Object[] { System.out }); + } + catch (ClassNotFoundException ex) { + // specified ResultPrinter class not=20 + msg =3D "Cannot find specified result printer class " = +=20 + className + "."; + } + catch (NoSuchMethodException ex) { + msg =3D "Class " + className +=20 + " does not provide constructor taking a PrintStrea= m=2E"; + } + catch (InstantiationException ex) { + msg =3D "Class " + className + " is abstract."; + } + catch (IllegalAccessException ex) { + msg =3D "Constructor taking a PrintStream of class " +=20 + className + " is not accessible."; + } + catch (InvocationTargetException ex) { + msg =3D "Constructor call results in exception " + ex = + "."; + } + + // ResultPrinter class specified, but not avaiable + System.out.println(msg); + ResultPrinter printer =3D getDefaultResultPrinter(); + System.out.println("Using default result printer of class = " +=20 + printer.getClass().getName()); + } + } + =20 + // ResultPrinter class not specified =3D> use default + return getDefaultResultPrinter(); + } + + /**=20 + * Returns an instance of the default result printer class + * BatchResultPrinter. + */ + protected ResultPrinter getDefaultResultPrinter() { + return new BatchResultPrinter(System.out); + } + =20 + +} +