Commits

Shantanu Kumar committed dcd2a43

copy required functions from Clj-MiscUtil into test_util.clj
update dbserver_util.clj test_clj_dbcp.clj to use test_util.clj instead of Clj-MiscUtil

Comments (0)

Files changed (3)

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

   "Functions to start and stop embedded database servers."
   (:import java.io.PrintWriter)
   (:require
-    [org.bituf.clj-miscutil :as mu]))
+    [org.bituf.test-util :as tu]))
 
 
 (defn msg
   See also:
     http://www.hsqldb.org/doc/src/org/hsqldb/Server.html"
   ([host port dbnames]
-    (let [dbnames-vec (mu/as-vector dbnames)
+    (let [dbnames-vec (tu/as-vector dbnames)
           dbnames-cnt (take (count dbnames-vec) (iterate #(inc %) 0))
           dbnames-map (zipmap dbnames-cnt dbnames-vec)
           server (org.hsqldb.Server.)]

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

 (ns org.bituf.test-clj-dbcp
+  (:require
+    [org.bituf.test-util :as tu])
   (:use org.bituf.clj-dbcp)
-  (:use org.bituf.clj-miscutil)
   (:use org.bituf.dbserver-util)
   (:use [clojure.contrib.sql :as sql :only ()])
   (:use clojure.contrib.java-utils)
   (is false msg))
 
 
-(def dbcred (let [props (str-to-keys
-                          (property-map
+(def dbcred (let [props (tu/str-to-keys
+                          (tu/property-map
                             (read-properties "src/test/dbcred.properties")))]
               (println "*** DB-Credentials properties map ***")
               (pprint props)
 
 
 (deftest test-mysql
-  (if (is-true? (:mysql.testconn dbcred))
+  (if (tu/is-true? (:mysql.testconn dbcred))
     (test-network-datasource "mysql" mysql-datasource)
     (fail "MySQL not tested")))
 
 
 (deftest test-cubrid
-  (if (is-true? (:cubrid.testconn dbcred))
+  (if (tu/is-true? (:cubrid.testconn dbcred))
     (test-network-datasource "cubrid" cubrid-datasource)
     (fail "CUBRID not tested")))
 
 
 (deftest test-pgsql
-  (if (is-true? (:pgsql.testconn dbcred))
+  (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"
-  (with-root-context (javax.naming.InitialContext.)
-    (print-jndi-tree))
+  (tu/with-root-context (javax.naming.InitialContext.)
+    (tu/print-jndi-tree))
   (let [ds (jndi-datasource "java:comp/env/myDataSource")]
     (test-datasource ds)))
 

src/test/clj/org/bituf/test_util.clj

+(ns org.bituf.test-util
+  "This file has some utility functions pulled from Clj-MiscUtil project. It
+  makes sense to do so, so that Clj-DBCP doesn't need to depend on a certain
+  version of Clj-MiscUtil."
+  (:import
+    (java.util    List Map Properties)
+    (javax.naming Binding Context InitialContext
+                  NameClassPair NamingEnumeration)))
+
+
+(defn property-map
+  "Transform a given Properties instance to a map."
+  [^Properties properties]
+  (let [ks (into [] (.stringPropertyNames properties))
+        vs (into [] (map #(.getProperty properties %) ks))]
+    (zipmap ks vs)))
+
+
+(defn is-true?
+  "Tell whether a given value is equivalent to true."
+  [any]
+  (cond
+    (string? any)  (let [v (.toLowerCase ^String any)]
+                     (or
+                       (= "true" v)
+                       (= "yes"  v)
+                       (= "on"   v)))
+    (keyword? any) (let [v (keyword (.toLowerCase ^String (name any)))]
+                     (or
+                       (= :true v)
+                       (= :yes  v)
+                       (= :on   v)))
+    (number? any)  (> any 0)
+    :else (true? any)))
+
+
+(defn as-vector
+  "Convert/wrap given argument as a vector."
+  [anything]
+  (if (vector? anything) anything
+    (if (or (seq? anything) (set? anything)) (into [] anything)
+      (if (map? anything) (into [] (vals anything))
+        (if (nil? anything) []
+          [anything])))))
+
+
+(defn ^List coll-as-keys
+  "Convert each element in a collection to keyword and return a vector."
+  [ks]
+  (as-vector (map keyword ks)))
+
+
+(defn ^Map str-to-keys
+  "Given a map with every key a string, convert keys to keywords.
+  Input: {\"a\" 10 \"b\" \"20\"}
+  Returns: {:a 10 :b \"20\"}"
+  [m]
+  (let [ks (keys m)
+        vs (vals m)]
+    (zipmap (coll-as-keys ks) vs)))
+
+
+;; ===== JNDI functions =====
+
+
+(def ^{:doc "Typically bound to javax.naming.Context"
+       :tag Context}
+      *root-context* nil)
+
+
+(def ^{:doc "Typically bound to an integer wrapped in an atom, e.g. (atom 0)"}
+      *indent* nil)
+
+
+(defmacro with-root-context
+  [root-context & body]
+  `(do
+    (assert (not (nil? ~root-context)))
+    (assert (instance? Context ~root-context))
+    (binding [*root-context* ~root-context]
+      ~@body)))
+
+
+(defn- increase-indent []
+  (swap! *indent* #(+ % 4)))
+
+
+(defn- decrease-indent []
+  (swap! *indent* #(- % 4)))
+
+
+(defn- print-entry
+  [^NameClassPair next-elem]
+  (let [indent-str (apply str
+                     (take @*indent* (repeat " ")))]
+    (if (nil? next-elem) (println indent-str "--> <nil>")
+      (println indent-str "-->"
+        (.getName next-elem)
+        " (" (type next-elem) "->" (.getClassName next-elem) ")"))))
+
+
+(declare do-print-jndi-tree)
+
+
+(defn- print-ne
+  [^NamingEnumeration ne ^String parent-ctx]
+  (loop []
+    (when (.hasMoreElements ne)
+      (let [^NameClassPair next-elem (.nextElement ne)]
+        (print-entry next-elem)
+        (increase-indent)
+        (if (or (instance? Context next-elem)
+              (and (instance? NameClassPair next-elem)
+                (instance? Context (.getObject ^Binding next-elem))))
+          (do-print-jndi-tree
+            (if (zero? (.length parent-ctx))
+              (.getName next-elem)
+              (str parent-ctx "/" (.getName next-elem))))
+          (println "** Not drilling "
+            (type (.getObject ^Binding next-elem))))
+        (decrease-indent))
+      (recur))))
+
+
+(defn- do-print-jndi-tree
+  [^String ct]
+  (assert (not (nil? ct)))
+  (if (instance? Context *root-context*)
+    (print-ne (.list *root-context* ct) ct)
+    (print-entry *root-context*)))
+
+
+(defn print-jndi-tree
+  "Print JNDI tree. You should have JNDI environment configured beforehand."
+  ([^String ct]
+    (binding [*indent* (atom 0)]
+      (do-print-jndi-tree ct)))
+  ([]
+   (print-jndi-tree "")))
+
+