KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > testbeancluster > test > DBSetup


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.testbeancluster.test;
23
24 import java.sql.DriverManager JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 import junit.extensions.TestSetup;
31 import junit.framework.Test;
32
33 /** A TestSetup that starts hypersonic before the testcase with a tcp
34  * listening port at 1701.
35  *
36  * @author Scott.Stark@jboss.org
37  * @version $Revison:$
38  */

39 public class DBSetup extends TestSetup
40 {
41    public DBSetup(Test test)
42    {
43       super(test);
44    }
45
46    protected void setUp() throws Exception JavaDoc
47    {
48          File JavaDoc hypersoniDir = new File JavaDoc("output/hypersonic");
49          if (!hypersoniDir.exists())
50          {
51             hypersoniDir.mkdirs();
52          }
53
54          if (!hypersoniDir.isDirectory())
55          {
56             throw new IOException JavaDoc("Failed to create directory: " + hypersoniDir);
57          }
58       
59          File JavaDoc dbPath = new File JavaDoc(hypersoniDir, "cif-db");
60
61          // Start DB in new thread, or else it will block us
62
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 JavaDoc 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 JavaDoc
84    {
85       Class.forName("org.hsqldb.jdbcDriver");
86       String JavaDoc dbURL = "jdbc:hsqldb:hsql://localhost:1701";
87       Connection JavaDoc conn = DriverManager.getConnection(dbURL, "sa", "");
88       Statement JavaDoc statement = conn.createStatement();
89       statement.executeQuery("SHUTDOWN COMPACT");
90       
91    }
92
93    public static void main(String JavaDoc[] args) throws Exception JavaDoc
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 JavaDoc
102    {
103       boolean started;
104       File JavaDoc dbPath;
105       
106       DBThread(File JavaDoc 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             // Create startup arguments
122
String JavaDoc[] 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 JavaDoc e)
139          {
140             e.printStackTrace();
141          }
142          finally
143          {
144             started = true;
145          }
146       }
147    }
148 }
149
Popular Tags