Commits

Anonymous committed 4cd21db

unit tests for 'file' and 'network-server' mode for Derby, H2, HSQLDB

  • Participants
  • Parent commits 5d07dc6

Comments (0)

Files changed (7)

 
 - [TODO] Move database credentials (unit tests) to properties file
 - [TODO] Support jTDS driver (MS SQL Server, Sybase)
-- [TODO] Add tests-cases for file-mode and server-mode: Derby, HSQLDB, H2
 - [TODO] Add test-cases for untested databases: Oracle, IBM DB2
 - [TODO] API for changing connection pool parameters (as of now it's Java API)
 
 - Support for validation query
 - Auto-select driver classname and validation query for supported database
 - No-arg API for launching in-memory instances of embedded databases
-- Supported+tested in-memory embedded use only: Apache Derby, HSQLDB, H2
-- Supported+tested: MySQL, PostgreSQL
+- Supported+tested: Apache Derby, HSQLDB, H2, MySQL, PostgreSQL
 - Supported but not tested: Oracle, IBM DB2
 * Support for validation query
 * Auto-select driver classname and validation query for supported database
 * No-arg API for launching in-memory instances of embedded databases
-* Supported+tested for in-memory embedding: Apache Derby, HSQLDB, H2
-* Supported+tested: MySQL, PostgreSQL
+* Supported+tested: Apache Derby, HSQLDB, H2, MySQL, PostgreSQL
 * Supported but not tested: Oracle, IBM DB2
 
 
       <version>10.6.1.0</version>
       <scope>test</scope>
     </dependency>
+    <dependency><!-- Apache Derby Client -->
+      <groupId>org.apache.derby</groupId>
+      <artifactId>derbyclient</artifactId>
+      <version>10.6.1.0</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency><!-- Apache Derby Tools (network server)-->
+      <groupId>org.apache.derby</groupId>
+      <artifactId>derbynet</artifactId>
+      <version>10.6.1.0</version>
+      <scope>test</scope>
+    </dependency>
     <dependency><!-- H2 Database -->
       <groupId>com.h2database</groupId>
       <artifactId>h2</artifactId>
       <scope>test</scope>
     </dependency>
     <dependency><!-- HSQLDB -->
-      <groupId>hsqldb</groupId>
+      <groupId>org.hsqldb</groupId>
       <artifactId>hsqldb</artifactId>
-      <version>1.8.0.7</version>
+      <version>2.0.0</version>
       <scope>test</scope>
     </dependency>
     <dependency><!-- MySQL Database -->

File src/main/clj/org/bituf/clj_dbcp.clj

 (ns org.bituf.clj-dbcp
   (:import org.apache.commons.dbcp.BasicDataSource))
 
+(def *show-jdbc-url* false)
+
+
 (defstruct datasource-args
   :classname :url :username :password)
 
     (derby-args \"memory\"    \"sample\")
     (derby-args \"classpath\" \"/db1\") ; 'db1' is a directory in classpath
     (derby-args \"jar\"       \"(C:/dbs.jar)products/boiledfood\")
+    (derby-args nil           \"//localhost/somedb\") ; connects to server
+  Maven/Lein/Cake dependencies (or a later version):
+    org.apache.derby/derby      /10.6.1.0 - driver for memory and file mode
+    org.apache.derby/derbyclient/10.6.1.0 - driver for network mode
+    org.apache.derby/derbynet   /10.6.1.0 - for launching the server itself
   See also:
     http://db.apache.org/derby/docs/dev/devguide/cdevdvlp17453.html
     http://db.apache.org/derby/docs/dev/devguide/rdevdvlp22102.html"
   [db-protocol db-path]
-  (struct datasource-args
-    "org.apache.derby.jdbc.EmbeddedDriver" ; classname: must be in classpath
-    (str "jdbc:derby:" (or (empty? db-protocol)
-                         (str db-protocol ":"))
-      db-path) ; url
-    "" ; username
-    "" ; password
-    ))
+  (let [url (str "jdbc:derby:" (if (empty? db-protocol) ""
+                                 (str db-protocol ":"))
+              db-path)]
+    (if *show-jdbc-url*
+      (println "\n**** Derby JDBC URL ****\n" url))
+    (struct datasource-args
+      "org.apache.derby.jdbc.EmbeddedDriver" ; classname: must be in classpath
+      url ; url
+      "sa" ; username
+      "sx" ; password
+      )))
 
 
 (defn derby-datasource
     (create-datasource
       (derby-args db-protocol db-path)))
   ([db-path]
-    (derby-datasource nil db-path))
+    (derby-datasource nil (str db-path ";create=true;")))
   ([]
-    (derby-datasource "memory" (str default-db-name ";create=true"))))
+    (derby-datasource "memory" (str default-db-name ";create=true;"))))
 
 
 ; === H2 config ===
     (h2-args \"tcp\"  \"//localhost:9092/sample\")
     (h2-args \"tcp\"  \"//localhost/sample\")
     (h2-args \"tcp\"  \"//localhost/home/dir/sample\")
+  Maven/Lein/Cake Dependencies:
+    com.h2database/h2/1.2.140 (or a later version)
   See also:
     http://www.h2database.com/html/features.html"
   [db-protocol db-path]
-  (struct datasource-args
-    "org.h2.Driver" ; classname: must be in classpath
-    (str "jdbc:h2:" db-protocol ":" db-path) ; url
-    "sa" ; username
-    ""   ; password
-    ))
+  (let [url (str "jdbc:h2:" db-protocol ":" db-path)]
+    (if *show-jdbc-url*
+      (println "\n**** H2 JDBC URL ****\n" url))
+    (struct datasource-args
+      "org.h2.Driver" ; classname: must be in classpath
+      url ; url
+      "sa" ; username
+      ""   ; password
+      )))
 
 
 (defn h2-datasource
     (hsql-args \"hsql\" \"//localhost:9001/sample\")
     (hsql-args \"hsql\" \"//localhost/sample\")
     (hsql-args \"hsql\" \"//localhost/home/dir/sample\")
+  Maven/Lein/Cake Dependencies:
+    org.hsqldb/hsqldb/2.0.0 (or a later version)
   See also:
     http://hsqldb.org/doc/guide/ch01.html"
   [db-protocol db-path]
-  (struct datasource-args
-    "org.hsqldb.jdbcDriver" ; classname: must be in classpath
-    (str "jdbc:hsqldb:" db-protocol ":" db-path) ; url
-    "sa" ; username
-    ""   ; password
-    ))
+  (let [url (str "jdbc:hsqldb:" db-protocol ":" db-path)]
+    (if *show-jdbc-url*
+      (println "\n**** HSQL JDBC URL ****\n" url))
+    (struct datasource-args
+      "org.hsqldb.jdbcDriver" ; classname: must be in classpath
+      url ; url
+      "sa" ; username
+      ""   ; password
+      )))
 
 
 (defn hsql-datasource

File src/test/clj/org/bituf/dbserver_util.clj

+(ns org.bituf.dbserver-util
+  "Functions to start and stop embedded database servers."
+  (:import java.io.PrintWriter))
+
+(defn msg
+  [s]
+  (println (format "\n*** %s ***" (str s))))
+
+
+(defn as-vector
+  "Convert anything to a vector and return the result"
+  [anything]
+  (if (vector? anything) anything
+    (if (or (seq? anything) (set? anything)) (into [] anything)
+      (if (map? anything) (into [] (vals anything))
+        [anything]))))
+
+
+(defn start-derby-server
+  "Start the Derby database server on a network port. The default host is
+  localhost and the default port is 1527.
+  See also:
+    http://publib.boulder.ibm.com/infocenter/cscv/v10r1/index.jsp?topic=/com.ibm.cloudscape.doc/radminembeddedserverex.html
+    Short link: http://j.mp/axA6t6"
+  ([host port]
+    (let [server (org.apache.derby.drda.NetworkServerControl.
+                   (java.net.InetAddress/getByName host)
+                   port)]
+      (msg (format "Starting Derby Server at %s:%d" host port))
+      (.start server
+        (PrintWriter. (System/out)))
+      server))
+  ([port]
+    (start-derby-server "localhost" port))
+  ([]
+    (start-derby-server "localhost" 1527)))
+
+
+(defn stop-derby-server
+  "Stop the Derby server"
+  [server]
+  (.shutdown server))
+
+
+(defn start-h2-server
+  "Start the H2 database server on a network port. The default host is
+  localhost and the default port is 9092.
+  See also:
+    http://www.hsqldb.org/doc/src/org/hsqldb/Server.html"
+  ([port]
+    (let [server (org.h2.tools.Server/createTcpServer
+                   (into-array
+                     [(str "-tcp,-tcpAllowOthers,true,-tcpPort," port)]))]
+      (msg (format "Starting H2 Server at port %d" port))
+      (.start server)
+      server))
+  ([]
+    (start-h2-server 9092)))
+
+
+(defn stop-h2-server
+  "Stop the H2 server"
+  [server]
+  (.stop server))
+
+
+(defn start-hsql-server
+  "Start the HSQL database server on a network port. The default host is
+  localhost and the default port is 9001.
+  See also:
+    http://www.hsqldb.org/doc/src/org/hsqldb/Server.html"
+  ([host port dbnames]
+    (let [dbnames-vec (as-vector dbnames)
+          dbnames-cnt (take (count dbnames-vec) (iterate #(inc %) 0))
+          dbnames-map (zipmap dbnames-cnt dbnames-vec)
+          server (org.hsqldb.Server.)]
+      (msg (format "Starting HSQL Server at %s:%d %s" host port
+             (str dbnames-vec)))
+      (doall
+        (map #(doto server
+                (.setDatabaseName (first %) (last %))
+                (.setDatabasePath (first %) (last %)))
+          (seq dbnames-map)))
+      (doto server
+        (.setAddress host)
+        (.setPort port)
+        (.setSilent false)
+        (.setErrWriter (PrintWriter. (System/err)))
+        (.setLogWriter (PrintWriter. (System/out)))
+        (.setNoSystemExit true)
+        (.setRestartOnShutdown false)
+        (.start))
+      server))
+  ([port dbnames]
+    (start-hsql-server "localhost" port dbnames))
+  ([dbnames]
+    (start-hsql-server "localhost" 9001 dbnames)))
+
+
+(defn stop-hsql-server
+  "Stop the HSQL server"
+  [server]
+  (msg "Stopping HSQL Server")
+  (.shutdown server))

File src/test/clj/org/bituf/test_clj_dbcp.clj

 (ns org.bituf.test-clj-dbcp
   (:use org.bituf.clj-dbcp)
   (:use [clojure.contrib.sql :as sql :only ()])
-  (:use clojure.test))
+  (:use clojure.test)
+  (:use org.bituf.dbserver-util))
 
 (defn test-crud
   [dbspec]
 
 
 (deftest test-derby
+  ;; in-memory
   (test-datasource
-    (derby-datasource)))
+    (derby-datasource))
+  ;; file system
+  (test-datasource
+    (derby-datasource "derbyfile"))
+  ;; network server
+  (let [server (start-derby-server)]
+    (try
+      (test-datasource
+        (derby-datasource "//localhost/derbyserver"))
+      (finally
+        (stop-derby-server server)))))
 
 
 (deftest test-hsql
+  ;; in-memory
   (test-datasource
-    (hsql-datasource)))
+    (hsql-datasource))
+  ;; file system
+  (test-datasource
+    (hsql-datasource "file" "hsqlfile"))
+  ;; network server
+  (let [server (start-hsql-server "hsqlserver")]
+    (try
+      (test-datasource
+        (hsql-datasource "hsql" "//localhost/hsqlserver"))
+      (finally
+        (stop-hsql-server server)))))
 
 
 (deftest test-h2
+  ;; in-memory
   (test-datasource
-    (h2-datasource)))
+    (h2-datasource))
+  ;; file system
+  (test-datasource
+    (h2-datasource "file" "h2file"))
+  ;; network server
+  (let [server (start-h2-server)]
+    (try
+      (test-datasource
+        (h2-datasource "tcp" "//localhost/h2server"))
+      (finally
+        (stop-h2-server server)))))
 
 
 (deftest test-mysql

File src/test/script/runtests.clj

   (:use clojure.test))
 
 
-(binding []
+(binding [org.bituf.clj-dbcp/*show-jdbc-url* true]
   (run-tests
     'org.bituf.test-clj-dbcp))