Source

Clj-DBCP / src / test / clj / org / bituf / test_clj_dbcp.clj

(ns org.bituf.test-clj-dbcp
  (:require
    [org.bituf.test-util :as tu]
    [clojure.pprint :as pp]
    [clojure.java.jdbc :as sql])
  (:use org.bituf.clj-dbcp)
  (:use org.bituf.dbserver-util)
  (:use clojure.test)
  (:import (javax.naming.spi.DirectoryManager)))


(defn fail
  [msg]
  (is false msg))


(def dbcred (let [props (tu/str-to-keys
                          (tu/property-map
                            (tu/read-properties "src/test/dbcred.properties")))]
              (println "*** DB-Credentials properties map ***")
              (pp/pprint props)
              props))


(defn test-crud
  [dbspec]
  (let [table :emp
        orig-record {:id 1 :name "Bashir" :age 40}
        updt-record {:id 1 :name "Shabir" :age 50}
        drop-table  #(sql/do-commands "DROP TABLE emp")
        retrieve-fn #(sql/with-query-results rows
                      ["SELECT * FROM emp WHERE id=?" 1]
                      (first rows))
        as-int-num #(reduce into {}
                            (map (fn [[k v]] (if (number? v)
                                               {k (int v)}
                                               {k v})) %))]
    (sql/with-connection dbspec
      ;; drop table if pre-exists
      (try (drop-table)
        (catch Exception _))
      ;; create table
      (sql/do-commands
        "CREATE TABLE emp (id INTEGER, name VARCHAR(50), age INTEGER)")
      ;; insert
      (sql/insert-values table (keys orig-record) (vals orig-record))
      ;; retrieve-check
      (is (= orig-record (as-int-num (retrieve-fn))))
      ;; update
      (sql/update-values table ["id=?" 1] updt-record)
      ;; retrieve-check
      (is (= updt-record (as-int-num (retrieve-fn))))
      ;; delete
      ;; drop table
      (drop-table))))


(defn test-datasource
  "Test a datasource. It creates a dbspec out of the supplied datasource and
  sends for testing."
  [datasource]
  (test-crud (db-spec datasource)))


(deftest test-odbc
  ;; lite ODBC DSNs (e.g. Excel, Access etc - no advanced ODBC features)
  (if (tu/is-true? (:odbc.lite.testconn dbcred))
    (test-datasource
      (lite-odbc-datasource (dbcred :odbc.lite.dsn))))
  ;; regular full-blown ODBC DSNs (e.g. CUBRID, Oracle)
  (test-datasource
    (odbc-datasource (dbcred :odbc.regular.dsn))))


(deftest test-derby
  ;; in-memory (default dbname)
  (test-datasource
    (derby-memory-datasource))
  ;; in-memory
  (test-datasource
    (derby-memory-datasource "somesuch"))
  ;; file system (default dbname)
  (test-datasource
    (derby-filesystem-datasource))
  ;; file system
  (test-datasource
    (derby-filesystem-datasource "derbyfile"))
  ;; network server
  (let [server (start-derby-server)]
    (try
      (test-datasource
        (derby-network-datasource "localhost" "derbyserver"))
      (finally
        (stop-derby-server server)))))


(deftest test-h2
  ;; in-memory (default dbname)
  (test-datasource
    (h2-memory-datasource))
  ;; in-memory
  (test-datasource
    (h2-memory-datasource "somesuch"))
  ;; file system (default dbname)
  (test-datasource
    (h2-filesystem-datasource))
  ;; file system
  (test-datasource
    (h2-filesystem-datasource "h2file"))
  ;; network server
  (let [server (start-h2-server)]
    (try
      (test-datasource
        (h2-network-datasource "localhost" "h2server"))
      (finally
        (stop-h2-server server)))))


(deftest test-hsql
  ;; in-memory (default dbname)
  (test-datasource
    (hsql-memory-datasource))
  ;; in-memory
  (test-datasource
    (hsql-memory-datasource "somesuch"))
  ;; file system (default dbname)
  (test-datasource
    (hsql-filesystem-datasource))
  ;; file system
  (test-datasource
    (hsql-filesystem-datasource "hsqlfile"))
  ;; network server
  (let [server (start-hsql-server "hsqlserver")]
    (try
      (test-datasource
        (hsql-network-datasource "localhost" "hsqlserver"))
      (finally
        (stop-hsql-server server)))))


(deftest test-sqlite
  ;; in-memory (no dbname)
  (test-datasource
    (sqlite-memory-datasource))
  ;; file system (default dbname)
  (test-datasource
    (sqlite-filesystem-datasource))
  ;; file system
  (test-datasource
    (sqlite-filesystem-datasource "sqlitefile")))


(defn test-network-datasource
  "Test a network JDBC datasource - CUBRID, MySQL, PostgreSQL, Oracle, DB2, jTDS etc.
  dbtoken must be same as first token (before period) in dbcred.properties file.
  Examples of dbtoken: mysql, pgsql etc."
  [^String dbtoken dsfn]
  (let [as-str #(if (keyword? %) (name %) (str %))
        K      (fn [kw] (keyword
                          (str dbtoken "." (as-str kw))))]
    (test-datasource
      (dsfn
        ((K :hostport) dbcred)
        ((K :database) dbcred)
        ((K :username) dbcred)
        ((K :password) dbcred)))))


(deftest test-firebird
  (if (tu/is-true? (:firebird.testconn dbcred))
    (test-network-datasource "firebird" firebird-datasource)
    (fail "Firebird not tested")))


(deftest test-mysql
  (if (tu/is-true? (:mysql.testconn dbcred))
    (test-network-datasource "mysql" mysql-datasource)
    (fail "MySQL not tested")))


(deftest test-cubrid
  (if (tu/is-true? (:cubrid.testconn dbcred))
    (test-network-datasource "cubrid" cubrid-datasource)
    (fail "CUBRID not tested")))


(deftest test-monetdb
  (if (tu/is-true? (:monetdb.testconn dbcred))
    (test-network-datasource "monetdb" monetdb-datasource)
    (fail "monet not tested")))


(deftest test-pgsql
  (if (tu/is-true? (:pgsql.testconn dbcred))
    (test-network-datasource "pgsql" pgsql-datasource)
    (fail "PostgreSQL not tested")))


(deftest test-jndi
  "See also (not used): http://commons.apache.org/dbcp/guide/jndi-howto.html"
  (tu/with-root-context (javax.naming.InitialContext.)
    (tu/print-jndi-tree))
  (let [ds (jndi-datasource "java:comp/env/myDataSource")]
    (test-datasource ds)))


(defmacro ig
  "Ignore exceptions in a block while creating datasource connections"
  [& body]
  `(let []
     (try
       ~@body
       (catch org.apache.commons.dbcp.SQLNestedException ~'_))))


(deftest smoketest-untested-datasources
  ;; Oracle
  (ig (oracle-datasource "localhost" "cljdbcp" "root" "root"))
  ;; IBM DB2
  (ig (db2-datasource "localhost" "cljdbcp" "root" "root"))
  ;; SQL Server
  (ig (sqlserver-datasource "localhost" "cljdbcp" "root" "root" {:k1 true :k2 10 "k3" "x"}))
  (ig (sqlserver-datasource "localhost" "cljdbcp" "root" "root")) ; no properties
  (ig (sqlserver-datasource "localhost" "root" "root")) ; no database name
  ;; Sybase
  (ig (sybase-datasource "localhost" "cljdbcp" "root" "root" {:k1 true :k2 10 "k3" "x"}))
  (ig (sybase-datasource "localhost" "cljdbcp" "root" "root")) ; no properties
  (ig (sybase-datasource "localhost" "root" "root"))) ; no database name


(deftest test-custom-datasource-parameters
  (test-datasource
    (let [ds (h2-memory-datasource)]
      ;; set custom validation query
      (set-validation-query! ds "SELECT 1;")
      ;; set maximum number of active connections on the datasource at a time
      (set-max-active! ds 15)
      ;; set minimum and maximum number of idle connections on the datasource
      (set-min-max-idle! ds 3 8)
      ds)))


(defn test-datasource-from-properties-file-helper
  []
  (doseq [[msg each] [["Default" (make-datasource-from-properties)]
                      ["mydev" (make-datasource-from-properties (load-datasource-args "mydev"))]
                      ["dev2"  (make-datasource-from-properties (load-datasource-args "dev2"))]
                      ["dev3"  (make-datasource-from-properties (load-datasource-args "dev3"))]
                      ["mysql" (make-datasource-from-properties (load-datasource-args "mysql"))]
                      ["jdbc"  (make-datasource-from-properties (load-datasource-args "jdbc"))]]]
    (testing msg
      (test-crud (db-spec each)))))


(deftest test-datasource-from-properties-file
  (let [p (load-properties)]
    (pp/pprint p)
    (is (not (empty? p))))
  (test-datasource-from-properties-file-helper)
  (binding [*properties-file* "test-clj-dbcp.properties"]
    (test-datasource-from-properties-file-helper)))


(defn test-ns-hook []
  (test-odbc)
  (test-derby)
  (test-h2)
  (test-hsql)
  (test-sqlite)
  (test-firebird)
  (test-mysql)
  (test-cubrid)
  (test-monetdb)
  (test-pgsql)
  (smoketest-untested-datasources)
  (test-jndi)
  (test-custom-datasource-parameters)
  (test-datasource-from-properties-file))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.