KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > tests > MMBaseTest


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.tests;
11 import junit.framework.TestCase;
12
13 import java.io.File JavaDoc;
14 import java.sql.*;
15 import org.hsqldb.Server;
16 import org.mmbase.util.ResourceLoader;
17 import org.mmbase.util.logging.Logging;
18 import org.mmbase.module.Module;
19 import org.mmbase.module.core.*;
20 import org.mmbase.module.tools.MMAdmin;
21
22 /**
23  * This class contains static methods for MMBase tests.
24  *
25  * @author Michiel Meeuwissen
26  */

27 public abstract class MMBaseTest extends TestCase {
28
29     static MMBase mmb;
30
31     public MMBaseTest() {
32         super();
33     }
34     public MMBaseTest(String JavaDoc name) {
35         super(name);
36     }
37
38     static public void startMMBase() throws Exception JavaDoc {
39         if (System.getProperty("nostartmmbase") == null) {
40             startMMBase(System.getProperty("nostartdb") == null);
41         }
42     }
43     /**
44      * If your test needs a running MMBase. Call this.
45      */

46     static public void startMMBase(boolean startDatabase) throws Exception JavaDoc {
47         if (startDatabase) startDatabase();
48         MMBaseContext.init();
49         mmb = MMBase.getMMBase();
50
51         MMAdmin mmadmin = (MMAdmin) Module.getModule("mmadmin", true);
52         while (! mmadmin.getState()) {
53             Thread.sleep(1000);
54         }
55         System.out.println("================================================================================");
56         System.out.println("Starting test");
57     }
58
59     static public void startDatabase() {
60         // first try if it is running already
61
try {
62             Thread.sleep(5000);
63             Class.forName("org.hsqldb.jdbcDriver" );
64         } catch (Exception JavaDoc e) {
65             System.err.println("ERROR: failed to load HSQLDB JDBC driver." + e.getMessage());
66             return;
67         }
68         while(true) {
69             String JavaDoc database = System.getProperty("test.database");
70             if (database == null) database = "test";
71             try {
72                 Connection c = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/" + database, "sa", "");
73                 // ok!, already running one.
74
return;
75             } catch (SQLException sqe) {
76                 Server server = new Server();
77                 server.setSilent(true);
78                 String JavaDoc dbDir = System.getProperty("test.database.dir");
79                 if (dbDir == null) dbDir = System.getProperty("user.dir") + File.separator + "data";
80                 server.setDatabasePath(0, dbDir + File.separator + database);
81                 server.setDatabaseName(0, database);
82                 server.start();
83                 try {
84                     Thread.sleep(10000);
85                 } catch (Exception JavaDoc e) {
86                     // I hate java
87
}
88             }
89         }
90     }
91
92     /**
93      * If no running MMBase is needed, then you probably want at least to initialize logging.
94      */

95     static public void startLogging() throws Exception JavaDoc {
96         startLogging("log.xml");
97     }
98     /**
99      * If no running MMBase is needed, then you probably want at least to initialize logging.
100      */

101     static public void startLogging(String JavaDoc configure) throws Exception JavaDoc {
102         Logging.configure(ResourceLoader.getConfigurationRoot().getChildResourceLoader("log"), configure);
103     }
104
105     /**
106      * Always useful, an mmbase running outside an app-server, you can talk to it with rmmci.
107      */

108     public static void main(String JavaDoc[] args) {
109         try {
110             startMMBase();
111             while(!mmb.isShutdown()) {
112
113             }
114         } catch (Exception JavaDoc e) {
115         }
116     }
117
118 }
119
Popular Tags