1 /* Copyright (c) 2001-2016, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 package org.hsqldb.test;
33 
34 import java.lang.reflect.Constructor;
35 import java.sql.Connection;
36 import java.sql.DriverManager;
37 import java.sql.SQLException;
38 
39 import org.hsqldb.Database;
40 import org.hsqldb.server.Server;
41 import org.hsqldb.server.WebServer;
42 
43 import junit.framework.TestCase;
44 import junit.framework.TestResult;
45 
46 /**
47  * HSQLDB TestBugBase Junit test case. <p>
48  *
49  * By setting the booleans isNetwork, isHTTP, isUseTestServlet below, you can execute all tests that derive from this TestBase class
50  * using either the embedded HSQL server mode in both HSQL or HTTP protocol, or target the HSQL-Servlet mode running in for example
51  * Tomcat.
52  *
53  * When running against the Servlet: This assumes you have a WebApplication called HSQLwebApp running in for example Tomcat, with hsqldb.jar
54  * (or better hsqldbtest.jar renamed to hsqldb.jar) in the WEB-INF/lib directory and web.xml containing something like this:<p>
55  *      <code>
56  * {@literal
57  *    <servlet>
58  *      <servlet-name>test</servlet-name>
59  *      <servlet-class>org.hsqldb.server.Servlet</servlet-class>
60  *      <init-param>
61  *            <param-name>hsqldb.server.database</param-name>
62  *            <param-value>mem:test</param-value>
63  *      </init-param>
64  *      <load-on-startup>1</load-on-startup>
65  *    </servlet>
66  *
67  *    <servlet-mapping>
68  *      <servlet-name>test</servlet-name>
69  *      <url-pattern>/test</url-pattern>
70  *    </servlet-mapping>
71  * }</code>
72  * @author  boucherb@users
73  * @version 1.7.2
74  * @since 1.7.2
75  */
76 public abstract class TestBase extends TestCase {
77 
78     String  dbPath = "mem:test;sql.enforce_strict_size=true";
79     String  serverProps;
80     String  url;
81     String  user     = "sa";
82     String  password = "";
83     Server  server;
84     boolean isNetwork = true;
85     boolean isHTTP    = false;    // Set false to test HSQL protocol, true to test HTTP, in which case you can use isUseTestServlet to target either HSQL's webserver, or the Servlet server-mode
86     boolean isServlet = false;
87 
TestBase(String name)88     public TestBase(String name) {
89         super(name);
90     }
91 
TestBase(String name, boolean isNetwork, boolean isHTTP)92     public TestBase(String name, boolean isNetwork,
93                     boolean isHTTP) {
94 
95         super(name);
96 
97         this.isNetwork = isNetwork;
98         this.isHTTP    = isHTTP;
99     }
100 
TestBase(String name, String url, boolean isNetwork, boolean isHTTP)101     public TestBase(String name, String url, boolean isNetwork,
102                     boolean isHTTP) {
103 
104         super(name);
105 
106         if (url != null) {
107             this.url = url;
108         }
109 
110         this.isNetwork = isNetwork;
111         this.isHTTP    = isHTTP;
112     }
113 
setUp()114     protected void setUp() throws Exception {
115 
116         if (isNetwork) {
117 
118             //  change the url to reflect your preferred db location and name
119             if (url == null) {
120                 if (isServlet) {
121                     url = "jdbc:hsqldb:http://localhost:8080/HSQLwebApp/test";
122                 } else if (isHTTP) {
123                     url = "jdbc:hsqldb:http://localhost:8085/test";
124                 } else {
125                     url = "jdbc:hsqldb:hsql://localhost/test";
126                 }
127             }
128 
129             if (!isServlet) {
130                 server = isHTTP ? new WebServer()
131                                 : new Server();
132 
133                 if (isHTTP) {
134                     server.setPort(8085);
135                 }
136 
137                 server.setDatabaseName(0, "test");
138                 server.setDatabasePath(0, dbPath);
139                 server.setLogWriter(null);
140                 server.setErrWriter(null);
141                 server.start();
142             }
143         } else {
144             if (url == null) {
145                 url = "jdbc:hsqldb:" + dbPath;
146             }
147         }
148 
149         try {
150             Class.forName("org.hsqldb.jdbc.JDBCDriver");
151         } catch (Exception e) {
152             e.printStackTrace();
153             System.out.println(this + ".setUp() error: " + e.getMessage());
154         }
155     }
156 
tearDown()157     protected void tearDown() {
158 
159         if (isNetwork && !isServlet) {
160             server.shutdownWithCatalogs(Database.CLOSEMODE_IMMEDIATELY);
161 
162             server = null;
163         }
164     }
165 
newConnection()166     protected Connection newConnection() throws SQLException {
167         return DriverManager.getConnection(url, user, password);
168     }
169 
runWithResult(Class testCaseClass, String testName)170     public static void runWithResult(Class testCaseClass, String testName) {
171 
172         try {
173             Constructor ctor = testCaseClass.getConstructor(new Class[]{
174                 String.class });
175             TestBase theTest = (TestBase) ctor.newInstance(new Object[]{
176                 testName });
177 
178             theTest.runWithResult();
179         } catch (Exception ex) {
180             System.err.println("couldn't execute test:");
181             ex.printStackTrace(System.err);
182         }
183     }
184 
runWithResult()185     public void runWithResult() {
186 
187         TestResult result   = run();
188         String     testName = this.getClass().getName();
189 
190         if (testName.startsWith("org.hsqldb.test.")) {
191             testName = testName.substring(16);
192         }
193 
194         testName += "." + getName();
195 
196         int failureCount = result.failureCount();
197 
198         System.out.println(testName + " failure count: " + failureCount);
199 
200         java.util.Enumeration failures = result.failures();
201 
202         while (failures.hasMoreElements()) {
203             System.err.println(failures.nextElement());
204         }
205     }
206 }
207