1 22 package org.jboss.test.testbeancluster.test; 23 24 import java.sql.DriverManager ; 25 import java.sql.Connection ; 26 import java.sql.Statement ; 27 import java.io.File ; 28 import java.io.IOException ; 29 30 import junit.extensions.TestSetup; 31 import junit.framework.Test; 32 33 39 public class DBSetup extends TestSetup 40 { 41 public DBSetup(Test test) 42 { 43 super(test); 44 } 45 46 protected void setUp() throws Exception 47 { 48 File hypersoniDir = new File ("output/hypersonic"); 49 if (!hypersoniDir.exists()) 50 { 51 hypersoniDir.mkdirs(); 52 } 53 54 if (!hypersoniDir.isDirectory()) 55 { 56 throw new IOException ("Failed to create directory: " + hypersoniDir); 57 } 58 59 File dbPath = new File (hypersoniDir, "cif-db"); 60 61 DBThread serverThread = new DBThread(dbPath); 63 serverThread.start(); 64 65 int elapsed = 0; 66 while (!serverThread.isStarted() && elapsed < 15000) 67 { 68 try 69 { 70 Thread.sleep(100); 71 elapsed += 100; 72 } 73 catch (InterruptedException ie) 74 { 75 System.out.println("Interrupted while waiting for Hypersonic"); 76 } 77 } 78 79 if (!serverThread.isStarted()) 80 System.out.println("Hypersonic failed to start in a timely fashion"); 81 } 82 83 protected void tearDown() throws Exception 84 { 85 Class.forName("org.hsqldb.jdbcDriver"); 86 String dbURL = "jdbc:hsqldb:hsql://localhost:1701"; 87 Connection conn = DriverManager.getConnection(dbURL, "sa", ""); 88 Statement statement = conn.createStatement(); 89 statement.executeQuery("SHUTDOWN COMPACT"); 90 91 } 92 93 public static void main(String [] args) throws Exception 94 { 95 DBSetup setup = new DBSetup(null); 96 setup.setUp(); 97 Thread.sleep(120*1000); 98 setup.tearDown(); 99 } 100 101 class DBThread extends Thread 102 { 103 boolean started; 104 File dbPath; 105 106 DBThread(File dbPath) 107 { 108 super("hypersonic"); 109 this.dbPath = dbPath; 110 } 111 112 boolean isStarted() 113 { 114 return started; 115 } 116 117 public void run() 118 { 119 try 120 { 121 String [] args = { 123 "-database", 124 dbPath.toString(), 125 "-port", 126 String.valueOf(1701), 127 "-silent", 128 "false", 129 "-trace", 130 "false", 131 "-no_system_exit", 132 "true", 133 }; 134 System.out.println("Starting hsqldb"); 135 org.hsqldb.Server.main(args); 136 System.out.println("Done"); 137 } 138 catch (Exception e) 139 { 140 e.printStackTrace(); 141 } 142 finally 143 { 144 started = true; 145 } 146 } 147 } 148 } 149 | Popular Tags |