empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From franci...@apache.org
Subject svn commit: r907578 - in /incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire: HsqldbResource.java db/hsql/DBDatabaseDriverHSqlTest.java
Date Mon, 08 Feb 2010 07:34:41 GMT
Author: francisdb
Date: Mon Feb  8 07:34:39 2010
New Revision: 907578

URL: http://svn.apache.org/viewvc?rev=907578&view=rev
Log:
use a junit @Rule for the hsqldb connection

Added:
    incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/HsqldbResource.java
Modified:
    incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/hsql/DBDatabaseDriverHSqlTest.java

Added: incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/HsqldbResource.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/HsqldbResource.java?rev=907578&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/HsqldbResource.java
(added)
+++ incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/HsqldbResource.java
Mon Feb  8 07:34:39 2010
@@ -0,0 +1,64 @@
+package org.apache.empire;
+
+import java.io.File;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.empire.db.DBTools;
+import org.junit.rules.ExternalResource;
+
+public class HsqldbResource extends ExternalResource
+{
+
+    private static final String PATH = "target/hsqldb-unit-test/";
+    
+    public Connection connection;
+    
+    public Connection getConnection()
+    {
+        return connection;
+    }
+    
+    @Override
+    protected void before()
+        throws Throwable
+    {
+        // clean up possible previous test files
+        FileUtils.deleteDirectory(new File(PATH));
+        
+        Class.forName("org.hsqldb.jdbcDriver");
+        connection = DriverManager.getConnection("jdbc:hsqldb:" + PATH, "sa", "");
+    }
+    
+    @Override
+    protected void after()
+    {
+        try
+        {
+            Statement st = connection.createStatement();
+            // properly shutdown hsqldb
+            st.execute("SHUTDOWN");
+        } catch (SQLException ex)
+        {
+            ex.printStackTrace();
+        }
+        try
+        {
+            DBTools.close(connection);
+        } catch (SQLException e)
+        {
+            e.printStackTrace();
+        }
+        try
+        {
+            FileUtils.deleteDirectory(new File(PATH));
+        } catch (IOException e)
+        {
+            e.printStackTrace();
+        }
+    }
+}

Modified: incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/hsql/DBDatabaseDriverHSqlTest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/hsql/DBDatabaseDriverHSqlTest.java?rev=907578&r1=907577&r2=907578&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/hsql/DBDatabaseDriverHSqlTest.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/hsql/DBDatabaseDriverHSqlTest.java
Mon Feb  8 07:34:39 2010
@@ -22,75 +22,37 @@
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
-import java.io.File;
-import java.io.IOException;
 import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.SQLException;
 import java.util.Date;
 
-import org.apache.commons.io.FileUtils;
+import org.apache.empire.HsqldbResource;
 import org.apache.empire.db.CompanyDB;
 import org.apache.empire.db.DBCmdType;
 import org.apache.empire.db.DBDatabaseDriver;
 import org.apache.empire.db.DBRecord;
 import org.apache.empire.db.DBSQLScript;
-import org.apache.empire.db.DBTools;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
+import org.junit.Rule;
 import org.junit.Test;
 
 
 public class DBDatabaseDriverHSqlTest
-{
-    private static final String PATH = "target/hsqldb-unit-test/";
+{;
+ 
+    @Rule
+    public HsqldbResource hsqldb = new HsqldbResource();
     
-    public static CompanyDB db;
-    public static Connection conn;
-    
-    @BeforeClass
-    public static void setup() throws ClassNotFoundException, SQLException, IOException
+    @Test
+    public void test()
     {
-        // clean up possible previous test files
-        FileUtils.deleteDirectory(new File(PATH));
-        
-        Class.forName("org.hsqldb.jdbcDriver");
-        conn = DriverManager.getConnection("jdbc:hsqldb:" + PATH, "sa", "");
+        Connection conn = hsqldb.getConnection();
+     
         DBDatabaseDriver driver = new DBDatabaseDriverHSql();
-        db = new CompanyDB();
-        db.open(driver, conn);
+        CompanyDB db = new CompanyDB();
+        db.open(driver, hsqldb.getConnection());
         DBSQLScript script = new DBSQLScript();
         db.getCreateDDLScript(db.getDriver(), script);
-        script.run(db.getDriver(), conn, false);
+        script.run(db.getDriver(), hsqldb.getConnection(), false);
         
-    }
-    
-    @AfterClass
-    public static void shutdown() throws SQLException
-    {
-        try
-        {
-            DBSQLScript script = new DBSQLScript();
-            db.getDriver().getDDLScript(DBCmdType.DROP, db.EMPLOYEE, script);
-            db.getDriver().getDDLScript(DBCmdType.DROP, db.DEPARTMENT, script);
-            script.run(db.getDriver(), conn, true);
-        }
-        finally
-        {
-            DBTools.close(conn);
-            // clean up
-            try {
-                FileUtils.deleteDirectory(new File(PATH));
-            } catch (IOException e) {
-                // ingore
-            }
-        }
-
-    }
-    
-    @Test
-    public void test()
-    {
         DBRecord dep = new DBRecord();
         dep.create(db.DEPARTMENT);
         dep.setValue(db.DEPARTMENT.NAME, "junit");
@@ -127,5 +89,10 @@
         emp.read(db.EMPLOYEE, id, conn);
         
         assertEquals("123456", emp.getString(db.EMPLOYEE.PHONE_NUMBER));
+        
+        script = new DBSQLScript();
+        db.getDriver().getDDLScript(DBCmdType.DROP, db.EMPLOYEE, script);
+        db.getDriver().getDDLScript(DBCmdType.DROP, db.DEPARTMENT, script);
+        script.run(db.getDriver(), conn, true);
     }
 }



Mime
View raw message