aboutsummaryrefslogtreecommitdiff
path: root/src/clojure
diff options
context:
space:
mode:
authorStuart Halloway <stu@thinkrelevance.com>2009-04-05 16:02:37 +0000
committerStuart Halloway <stu@thinkrelevance.com>2009-04-05 16:02:37 +0000
commit47024875854f8bed0301f9927d7faa6e0e83b595 (patch)
treec7daeb1071a26f59ceed2416a34f7914536f0a9e /src/clojure
parent57571874b35cd8df77ccdc96a4e158c74478280e (diff)
java-util: the-str and with-system-properties
Diffstat (limited to 'src/clojure')
-rw-r--r--src/clojure/contrib/java_utils.clj47
-rw-r--r--src/clojure/contrib/sql/internal.clj11
-rw-r--r--src/clojure/contrib/test_clojure/clojure_main.clj28
-rw-r--r--src/clojure/contrib/test_contrib/test_java_utils.clj45
4 files changed, 97 insertions, 34 deletions
diff --git a/src/clojure/contrib/java_utils.clj b/src/clojure/contrib/java_utils.clj
index 1ea420ea..a60f68c8 100644
--- a/src/clojure/contrib/java_utils.clj
+++ b/src/clojure/contrib/java_utils.clj
@@ -1,4 +1,4 @@
-; Copyright (c) Stuart Halloway, April 2009. All rights reserved.
+; Copyright (c) Stuart Halloway & Contributors, April 2009. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
@@ -23,6 +23,12 @@
;
; If something in this module violates the principle of least surprise, please
; let me (Stu) and the Clojure community know via the mailing list.
+;
+; Contributors:
+;
+; Stuart Halloway
+; Stephen C. Gilardi
+; Shawn Hoover
(ns clojure.contrib.java-utils
(:import [java.io File]))
@@ -56,5 +62,44 @@
(File. (as-file parent) (relative-path-string child)))
([parent child & more]
(reduce file (file parent child) more)))
+
+(defn the-str
+ "Returns the name or string representation of x"
+ [x]
+ (if (instance? clojure.lang.Named x)
+ (name x)
+ (str x)))
+
+(defn get-system-property [stringable]
+ (System/getProperty (the-str stringable)))
+
+(defn set-system-properties
+ [settings]
+ "Set some system properties. Nil clears a property."
+ (doseq [[name val] settings]
+ (if val
+ (System/setProperty (the-str name) val)
+ (System/clearProperty (the-str name)))))
+
+(defmacro with-system-properties
+ "setting => property-name value
+
+ Sets the system properties to the supplied values, executes the body, and
+ sets the properties back to their original values. Values of nil are
+ translated to a clearing of the property."
+ [settings & body]
+ `(let [settings# ~settings
+ current# (reduce (fn [coll# k#]
+ (assoc coll# k# (get-system-property k#)))
+ {}
+ (keys settings#))]
+ (set-system-properties settings#)
+ (try
+ ~@body
+ (finally
+ (set-system-properties current#)))))
+
+
+
\ No newline at end of file
diff --git a/src/clojure/contrib/sql/internal.clj b/src/clojure/contrib/sql/internal.clj
index 730bc84b..9d9667ca 100644
--- a/src/clojure/contrib/sql/internal.clj
+++ b/src/clojure/contrib/sql/internal.clj
@@ -12,17 +12,12 @@
;; Created 3 October 2008
(ns clojure.contrib.sql.internal
- (:use [clojure.contrib.except :only (throw-arg)]))
+ (:use [clojure.contrib.except :only (throw-arg)]
+ [clojure.contrib.java-utils :only (the-str)]))
(def *db* {:connection nil :level 0})
-(defn the-str
- "Returns the name or string representation of x"
- [x]
- (if (instance? clojure.lang.Named x)
- (name x)
- (str x)))
-
+; TODO: test and move to clojure.contrib.java-utils?
(defn properties
"Converts a map from keywords, symbols, or strings to values into a
java.util.Properties object that maps the key names to the values with
diff --git a/src/clojure/contrib/test_clojure/clojure_main.clj b/src/clojure/contrib/test_clojure/clojure_main.clj
index 80629a3d..f1a96465 100644
--- a/src/clojure/contrib/test_clojure/clojure_main.clj
+++ b/src/clojure/contrib/test_clojure/clojure_main.clj
@@ -7,34 +7,12 @@
;; from this software.
(ns clojure.contrib.test-clojure.clojure-main
- (:use clojure.contrib.test-is)
+ (:use [clojure.contrib.java-utils :only (with-system-properties)]
+ clojure.contrib.test-is)
(:require [clojure.main :as main]))
-(defn- set-properties
- [settings]
- (doseq [[name val] settings]
- (if val
- (System/setProperty name val)
- (System/clearProperty name))))
-
-(defmacro with-properties
- "setting => property-name value
-
- Sets the system properties to the supplied values, executes the body, and
- sets the properties back to their original values. Values of nil are
- translated to a clearing of the property."
- [settings & body]
- `(let [settings# ~(apply hash-map settings)
- current# (map (fn [p#] [p# (System/getProperty p#)])
- (keys settings#))]
- (set-properties settings#)
- (try
- ~@body
- (finally
- (set-properties current#)))))
-
(deftest compile-path-respects-java-property
;; Bug fixed in r1177; previously was hardwired to the compile-time path.
- (with-properties ["clojure.compile.path" "compile path test"]
+ (with-system-properties {:clojure.compile.path "compile path test"}
(main/with-bindings
(is (= "compile path test" *compile-path*)))))
diff --git a/src/clojure/contrib/test_contrib/test_java_utils.clj b/src/clojure/contrib/test_contrib/test_java_utils.clj
index 9c536322..427babca 100644
--- a/src/clojure/contrib/test_contrib/test_java_utils.clj
+++ b/src/clojure/contrib/test_contrib/test_java_utils.clj
@@ -31,6 +31,51 @@
(testing "no sneaking in absolute paths!"
(is (thrown? IllegalArgumentException (file "foo" "bar" "/boom" "baz" "quux"))))
)
+
+(deftest test-the-str
+ (testing "keyword to string"
+ (is (= "foo") (the-str :foo)))
+ (testing "symbol to string"
+ (is (= "foo") (the-str 'foo)))
+ (testing "string to string"
+ (is (= "foo") (the-str "foo")))
+ (testing "stringifying non-namish things"
+ (is (= "42") (the-str 42)))
+)
+
+(deftest test-get-system-property
+ (testing "works the same with keywords, symbols, and strings"
+ (is (= (get-system-property "java.home") (get-system-property 'java.home)))
+ (is (= (get-system-property "java.home") (get-system-property :java.home))))
+)
+
+(deftest test-set-system-properties
+ (testing "set and then unset a property"
+ (let [propname :clojure.contrib.java-utils.test-set-system-properties]
+ (is (nil? (get-system-property propname)))
+ (set-system-properties {propname "foo"})
+ (is (= "foo") (get-system-property propname))
+ (set-system-properties {propname nil})
+ (is (nil? (get-system-property propname)))))
+)
+
+(deftest test-with-system-properties
+ (let [propname :clojure.contrib.java-utils.test-with-system-properties]
+ (testing "sets a property only for the duration of a block"
+ (is (= "foo"
+ (with-system-properties {propname "foo"}
+ (get-system-property propname))))
+ (is (nil? (get-system-property propname)))))
+ (testing "leaves other properties alone"
+ ; TODO: write this test better, using a properties -> map function
+ (let [propname :clojure.contrib.java-utils.test-with-system-properties
+ propcount (count (System/getProperties))]
+ (with-system-properties {propname "foo"}
+ (is (= (inc propcount) (count (System/getProperties)))))
+ (is (= propcount (count (System/getProperties))))))
+)
+
+