001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.test;
032:
033: import java.lang.reflect.Constructor;
034: import java.sql.Connection;
035: import java.sql.DriverManager;
036: import java.sql.SQLException;
037:
038: import org.hsqldb.Server;
039:
040: import junit.framework.TestCase;
041: import junit.framework.TestResult;
042:
043: /**
044: * HSQLDB TestBugBase Junit test case. <p>
045: *
046: * @author boucherb@users
047: * @version 1.7.2
048: * @since 1.7.2
049: */
050: public abstract class TestBase extends TestCase {
051:
052: // change the url to reflect your preferred db location and name
053: String serverProps;
054: String url;
055: String user = "sa";
056: String password = "";
057: Server server;
058: boolean isNetwork = true;
059:
060: public TestBase(String name) {
061: super (name);
062: }
063:
064: public TestBase(String name, String connectionUrl, boolean network) {
065:
066: super (name);
067:
068: this .isNetwork = network;
069: this .url = connectionUrl;
070: }
071:
072: protected void setUp() {
073:
074: if (isNetwork) {
075: if (url == null) {
076: url = "jdbc:hsqldb:hsql://localhost/test";
077: }
078:
079: server = new Server();
080:
081: server.setDatabaseName(0, "test");
082: server.setDatabasePath(0,
083: "mem:test;sql.enforce_strict_size=true");
084: server.setLogWriter(null);
085: server.setErrWriter(null);
086: server.start();
087: } else {
088: if (url == null) {
089: url = "jdbc:hsqldb:file:test;sql.enforce_strict_size=true";
090: }
091: }
092:
093: try {
094: Class.forName("org.hsqldb.jdbcDriver");
095: } catch (Exception e) {
096: e.printStackTrace();
097: System.out.println(this + ".setUp() error: "
098: + e.getMessage());
099: }
100: }
101:
102: protected void tearDown() {
103:
104: if (isNetwork) {
105: server.stop();
106:
107: server = null;
108: }
109: }
110:
111: Connection newConnection() throws SQLException {
112: return DriverManager.getConnection(url, user, password);
113: }
114:
115: public static void runWithResult(Class testCaseClass,
116: String testName) {
117:
118: try {
119: Constructor ctor = testCaseClass
120: .getConstructor(new Class[] { String.class });
121: TestBase theTest = (TestBase) ctor
122: .newInstance(new Object[] { testName });
123:
124: theTest.runWithResult();
125: } catch (Exception ex) {
126: System.err.println("couldn't execute test:");
127: ex.printStackTrace(System.err);
128: }
129: }
130:
131: public void runWithResult() {
132:
133: TestResult result = run();
134: String testName = this .getClass().getName();
135:
136: if (testName.startsWith("org.hsqldb.test.")) {
137: testName = testName.substring(16);
138: }
139:
140: testName += "." + getName();
141:
142: int failureCount = result.failureCount();
143:
144: System.out
145: .println(testName + " failure count: " + failureCount);
146:
147: java.util.Enumeration failures = result.failures();
148:
149: while (failures.hasMoreElements()) {
150: System.err.println(failures.nextElement());
151: }
152: }
153: }
|