001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3.test;
023:
024: import java.sql.DriverManager;
025: import java.sql.Connection;
026: import java.sql.Statement;
027: import java.io.File;
028: import java.io.IOException;
029: import java.lang.reflect.Method;
030:
031: import junit.extensions.TestSetup;
032: import junit.framework.Test;
033:
034: /** A TestSetup that starts hypersonic before the testcase with a tcp
035: * listening port at 1701.
036: *
037: * @author Scott.Stark@jboss.org
038: * @version $Revison: 60233 $
039: */
040: public class DBSetup extends TestSetup {
041: public DBSetup(Test test) {
042: super (test);
043: }
044:
045: protected void setUp() throws Exception {
046: File hypersoniDir = new File("output/hypersonic");
047: if (!hypersoniDir.exists()) {
048: hypersoniDir.mkdirs();
049: }
050:
051: if (!hypersoniDir.isDirectory()) {
052: throw new IOException("Failed to create directory: "
053: + hypersoniDir);
054: }
055:
056: File dbPath = new File(hypersoniDir, "clusteredentity-db");
057:
058: // Start DB in new thread, or else it will block us
059: DBThread serverThread = new DBThread(dbPath);
060: serverThread.start();
061:
062: int elapsed = 0;
063: while (!serverThread.isStarted() && elapsed < 15000) {
064: try {
065: Thread.sleep(100);
066: elapsed += 100;
067: } catch (InterruptedException ie) {
068: System.out
069: .println("Interrupted while waiting for Hypersonic");
070: }
071: }
072:
073: if (!serverThread.isStarted())
074: System.out
075: .println("Hypersonic failed to start in a timely fashion");
076: }
077:
078: protected void tearDown() throws Exception {
079: Class.forName("org.hsqldb.jdbcDriver");
080: String dbURL = "jdbc:hsqldb:hsql://"
081: + System.getProperty("jbosstest.server.host",
082: "localhost") + ":1701";
083: Connection conn = DriverManager.getConnection(dbURL, "sa", "");
084: Statement statement = conn.createStatement();
085: statement.executeQuery("SHUTDOWN COMPACT");
086:
087: }
088:
089: public static void main(String[] args) throws Exception {
090: DBSetup setup = new DBSetup(null);
091: setup.setUp();
092: Thread.sleep(120 * 1000);
093: setup.tearDown();
094: }
095:
096: class DBThread extends Thread {
097: boolean started;
098: File dbPath;
099:
100: DBThread(File dbPath) {
101: super ("hypersonic");
102: this .dbPath = dbPath;
103: }
104:
105: boolean isStarted() {
106: return started;
107: }
108:
109: public void run() {
110: try {
111: // Create startup arguments
112: String[] args = { "-database", dbPath.toString(),
113: "-port", String.valueOf(1701), "-silent",
114: "false", "-trace", "false", "-no_system_exit",
115: "true", };
116: System.out.println("Starting hsqldb");
117: // HACK Do this by reflection for now until we determine how
118: // we want to handle this in EJB3
119: Class clazz = Thread.currentThread()
120: .getContextClassLoader().loadClass(
121: "org.hsqldb.Server");
122: Method main = clazz.getDeclaredMethod("main",
123: new Class[] { String[].class });
124: main.invoke(null, new Object[] { args });
125: // org.hsqldb.Server.main(args);
126: System.out.println("Done");
127: } catch (Exception e) {
128: e.printStackTrace();
129: } finally {
130: started = true;
131: }
132: }
133: }
134: }
|