/* // $Id: //guest/julian_hyde/saffron/src/examples/saffron/test/Main.java#2 $ // Saffron preprocessor and data engine // Copyright (C) 2002 Julian Hyde <julian.hyde@mail.com> // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public // License as published by the Free Software Foundation; either // version 2 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Library General Public License for more details. // // You should have received a copy of the GNU Library General Public // License along with this library; if not, write to the // Free Software Foundation, Inc., 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. // // See the COPYING file located in the top-level-directory of // the archive of this library for complete text of license. // // jhyde, 18 April, 2002 */ package saffron.test; import junit.framework.*; import saffron.util.Util; import java.io.PrintWriter; import java.io.StringWriter; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.Properties; import java.util.Vector; /** * <code>Main</code> is the main test suite for saffron. * * @author jhyde * @since 18 April, 2002 * @version $Id: //guest/julian_hyde/saffron/src/examples/saffron/test/Main.java#2 $ **/ public class Main extends TestCase { static public void main(String[] args) throws Exception { new Main().run(args); } Main() { super("saffron"); } public Main(String s) { super(s); } /** * Creates and runs the root test suite. */ void run(String[] args) throws Exception { Test test = suite(); new Harness().run(test, new SaffronListener()); } /** * <code>Harness</code> is a simple harness for JUnit tests. */ static class Harness { /** * Runs a test. */ void run(Test test, TestListener listener) { Util.applyProperties(); TestResult result = new TestResult(); result.addListener(listener); test.run(result); } } /** * <code>Listener</code> is a simple listener. */ static class SaffronListener implements TestListener { PrintWriter pw; SaffronListener() { this.pw = TestContext.instance().getPrintWriter(); } private void report(Test test, String s, Throwable throwable) { pw.print(s + " in test '" + test + "': "); String[] msgs = convertStackToString(throwable); for (int i = 0; i < msgs.length; i++) { String msg = msgs[i]; pw.println(msg); } } public void addError(Test test, Throwable throwable) { report(test, "Error", throwable); } public void addFailure(Test test, AssertionFailedError error) { report(test, "Failure", error); } public void endTest(Test test) { } public void startTest(Test test) { } } /** * Converts an error into an array of strings, the most recent error first. * * @param e the error; may be null. Errors are chained if the error * implmements {@link ChainableThrowable}. **/ public static String[] convertStackToString(Throwable e) { Vector v = new Vector(); while (e != null) { String sMsg = getErrorMessage(e); v.addElement(sMsg); // if (e instanceof ChainableThrowable) { // e = ((ChainableThrowable) e).getNextThrowable(); // } else { e = null; // } } String[] msgs = new String[v.size()]; v.copyInto(msgs); return msgs; } /** * @see #getErrorMessage(Throwable,boolean) **/ public static String getErrorMessage(Throwable err) { boolean prependClassName = !(err instanceof java.sql.SQLException || err.getClass() == java.lang.Exception.class); return getErrorMessage(err, prependClassName); } /** * Constructs the message associated with an arbitrary Java error, making * up one based on the stack trace if there is none. * * @param err the error * @param prependClassName should the error be preceded by the * class name of the Java exception? defaults to false, unless the error * is derived from {@link java.sql.SQLException} or is exactly a {@link * java.lang.Exception} */ public static String getErrorMessage( Throwable err, boolean prependClassName) { String errMsg = err.getMessage(); if ((errMsg == null) || (err instanceof RuntimeException)) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); err.printStackTrace(pw); return sw.toString(); } else { if (prependClassName) { return err.getClass().getName() + ": " + errMsg; } else { return errMsg; } } } /** * Creates the main saffron test suite. This method has a special meaning * to JUnit; see {@link junit.framework.ScheduleTestCase}. */ public static Test suite() throws Exception { Properties properties = Util.getProperties(); String testName = properties.getProperty("saffron.test.Name"), testClass = properties.getProperty("saffron.test.Class"), testSuite = properties.getProperty("saffron.test.Suite"); if (testClass != null) { Class clazz = Class.forName(testClass); if (testName != null) { // e.g. testName = "testExistsEmptyFrom", // testClass = "sales.InMemorySalesTestCase" Constructor constructor = clazz.getConstructor( new Class[] {String.class}); Object o = constructor.newInstance( new Object[] {testName}); return (Test) o; } else { // e.g. testClass = "saffron.runtime.BufferedIterator$Test", // the name of a class which extends ScheduleTestCase. We will invoke // every method which starts with 'test'. TestSuite suite = new TestSuite(); suite.addTestSuite(clazz); return suite; } } if (testSuite != null) { // e.g. testSuite = "saffron.ext.ObjectSchema". Class does not // necessarily implement Test. We call its 'public static Test // suite()' method. Class clazz = Class.forName(testSuite); Method method = clazz.getMethod("suite", new Class[0]); Object o = method.invoke(null, new Object[0]); return (Test) o; } TestSuite suite = new TestSuite(); suite.addTest(sales.SalesTestCase.suite()); suite.addTestSuite(saffron.runtime.BufferedIterator.Test.class); suite.addTestSuite(saffron.runtime.ThreadIterator.Test.class); suite.addTest(saffron.ext.ObjectSchema.suite()); return suite; } /** * Call <code><i>className</i>.suite()</code> and add the * resulting {@link Test}. */ static void addSuite(TestSuite suite, String className) throws Exception { Class clazz = Class.forName(className); Method method = clazz.getMethod("suite", new Class[0]); Object o = method.invoke(null, new Object[0]); suite.addTest((Test) o); } } // End Main.java
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 1853 | Julian Hyde |
saffron: Further improve binding of rows to variables. |
||
#2 | 1801 | Julian Hyde |
saffron: add ObjectSchema; rules can now be matched more than once; started to implement correlations in queries in from list. |
||
#1 | 1779 | Julian Hyde |
saffron: Move class saffron.test.Main to under src/examples/. |
||
//guest/julian_hyde/saffron/src/main/saffron/test/Main.java | |||||
#1 | 1749 | Julian Hyde |
saffron: Oops, forgot some files. |