[logback-dev] svn commit: r1533 - logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/db

noreply.ceki at qos.ch noreply.ceki at qos.ch
Tue May 22 19:57:44 CEST 2007


Author: ceki
Date: Tue May 22 19:57:44 2007
New Revision: 1533

Modified:
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/db/DBAppenderTestBase.java

Log:
have tests go into a memory only dababase

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/db/DBAppenderTestBase.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/db/DBAppenderTestBase.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/db/DBAppenderTestBase.java	Tue May 22 19:57:44 2007
@@ -1,23 +1,28 @@
 package ch.qos.logback.classic.db;
 
+import java.io.PrintWriter;
 import java.sql.Connection;
-import java.sql.DriverManager;
 import java.sql.SQLException;
 import java.sql.Statement;
+import java.util.Properties;
 
 import junit.framework.TestCase;
 
 import org.hsqldb.Server;
+import org.hsqldb.ServerConstants;
+import org.hsqldb.jdbcDriver;
 
 public abstract class DBAppenderTestBase extends TestCase {
 
   public static final String DRIVER_CLASS = "org.hsqldb.jdbcDriver";
-  String serverProps;
-  String url;
+  // String serverProps;
+  String url = null;
   String user = "sa";
   String password = "";
   Server server;
-  boolean isNetwork = true;
+
+  // boolean isNetwork = true;
+  HsqlMode mode = HsqlMode.MEM;
 
   public DBAppenderTestBase(String name) {
     super(name);
@@ -25,53 +30,71 @@
 
   protected void setUp() throws SQLException {
 
-    if (isNetwork) {
-      if (url == null) {
-        url = "jdbc:hsqldb:hsql://localhost/test";
-      }
-
+    switch (mode) {
+    case NET:
+      url = "jdbc:hsqldb:hsql://localhost:4808/test";
+      break;
+    case MEM:
+      url = "jdbc:hsqldb:mem:test;sql.enforce_strict_size=true";
       server = new Server();
-
       server.setDatabaseName(0, "test");
-      server.setDatabasePath(0, "mem:test;sql.enforce_strict_size=true");
-      server.setLogWriter(null);
-      server.setErrWriter(null);
+      server.setDatabasePath(0, url);
+      server.setLogWriter(new PrintWriter(System.out));
+      server.setErrWriter(new PrintWriter(System.out));
       server.setTrace(false);
-      server.setSilent(true);
+      server.setSilent(false);
       server.start();
-    } else {
-      if (url == null) {
-        url = "jdbc:hsqldb:file:test;sql.enforce_strict_size=true";
-      }
+
+      break;
+    case FILE:
+      url = "jdbc:hsqldb:file:test;sql.enforce_strict_size=true";
+      break;
+
     }
 
-    try {
-      Class.forName(DRIVER_CLASS);
-    } catch (Exception e) {
-      e.printStackTrace();
-      System.out.println(this + ".setUp() error: " + e.getMessage());
+    // try {
+    // Class.forName(DRIVER_CLASS);
+    // } catch (Exception e) {
+    // e.printStackTrace();
+    // System.out.println(this + ".setUp() error: " + e.getMessage());
+    // }
+    // Thread.yield();
+    System.out.println(server.getState());
+
+    int waitCount = 0;
+    while (server.getState() != ServerConstants.SERVER_STATE_ONLINE
+        && waitCount < 5) {
+      try {
+        waitCount++;
+        Thread.sleep(1);
+      } catch (InterruptedException e) {
+      }
     }
-    Thread.yield();
-    
     createTables();
   }
 
   protected void tearDown() throws SQLException {
     dropTables();
-    
-    if (isNetwork) {
-      server.stop();
 
+    if (mode == HsqlMode.MEM) {
+      server.stop();
       server = null;
     }
   }
 
   Connection newConnection() throws SQLException {
-    return DriverManager.getConnection(url, user, password);
+    jdbcDriver driver = new jdbcDriver();
+    Properties props = new Properties();
+    props.setProperty("user", user);
+    props.setProperty("password", password);
+    return driver.connect(url, props);
+
+    // return DriverManager.getConnection(url, user, password);
   }
 
   void createTables() throws SQLException {
     Connection conn = newConnection();
+    assertNotNull(conn);
     StringBuffer buf = new StringBuffer();
     buf.append("CREATE TABLE logging_event (");
     buf.append("timestmp BIGINT NOT NULL,");
@@ -105,17 +128,17 @@
     buf.append("FOREIGN KEY (event_id) REFERENCES logging_event(event_id));");
     query(conn, buf.toString());
   }
-  
+
   void dropTables() throws SQLException {
     Connection conn = newConnection();
     StringBuffer buf = new StringBuffer();
     buf.append("DROP TABLE logging_event_exception IF EXISTS;");
     query(conn, buf.toString());
-    
+
     buf = new StringBuffer();
     buf.append("DROP TABLE logging_event_property IF EXISTS;");
     query(conn, buf.toString());
-    
+
     buf = new StringBuffer();
     buf.append("DROP TABLE logging_event IF EXISTS;");
     query(conn, buf.toString());
@@ -124,15 +147,15 @@
   void query(Connection conn, String expression) throws SQLException {
 
     Statement st = null;
-
     st = conn.createStatement();
-
     int i = st.executeUpdate(expression);
-
     if (i == -1) {
       System.out.println("db error : " + expression);
     }
-
     st.close();
   }
+
+  public enum HsqlMode {
+    MEM, FILE, NET;
+  }
 }



More information about the logback-dev mailing list