diff options
-rw-r--r-- | build.xml | 2 | ||||
-rw-r--r-- | src/clojure/contrib/java_utils.clj | 31 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib/test_java_utils.clj | 22 | ||||
-rw-r--r-- | test/fixtures/test_java_utils.properties | 2 |
4 files changed, 54 insertions, 3 deletions
@@ -8,6 +8,7 @@ <property name="src" location="src"/> <property name="build" location="classes"/> + <property name="test.temp.dir" location="test/tmp"/> <available property="hasclojure" file="${clojure.jar}"/> @@ -48,6 +49,7 @@ <target name="test_contrib" description = "Run contrib tests" if="hasclojure"> + <mkdir dir="${test.temp.dir}"/> <java classname="clojure.main"> <classpath> <path location="${build}"/> diff --git a/src/clojure/contrib/java_utils.clj b/src/clojure/contrib/java_utils.clj index 097afd9c..c5e120bb 100644 --- a/src/clojure/contrib/java_utils.clj +++ b/src/clojure/contrib/java_utils.clj @@ -29,9 +29,11 @@ ; Stuart Halloway ; Stephen C. Gilardi ; Shawn Hoover +; Perry Trolard (ns clojure.contrib.java-utils - (:import [java.io File])) + (:import [java.io File] + [java.util Properties])) (defmulti relative-path-string "Interpret a String or java.io.File as a relative path string. @@ -100,6 +102,33 @@ (set-system-properties current#))))) +; Not there is no corresponding props->map. Just destructure! +(defn as-properties + "Convert any seq of pairs to a java.utils.Properties instance. + Uses as-str to convert both keys and values into strings." + {:tag Properties} + [m] + (let [p (Properties.)] + (doseq [[k v] m] + (.setProperty p (as-str k) (as-str v))) + p)) + +(defn read-properties + "Read properties from file-able." + [file-able] + (with-open [f (java.io.FileInputStream. (file file-able))] + (doto (Properties.) + (.load f)))) + +(defn write-properties + "Write properties to file-able." + {:tag Properties} + ([m file-able] (write-properties m file-able nil)) + ([m file-able comments] + (with-open [f (java.io.FileOutputStream. (file file-able))] + (doto (as-properties m) + (.store f comments))))) +
\ No newline at end of file diff --git a/src/clojure/contrib/test_contrib/test_java_utils.clj b/src/clojure/contrib/test_contrib/test_java_utils.clj index 21a4addc..5c2588a3 100644 --- a/src/clojure/contrib/test_contrib/test_java_utils.clj +++ b/src/clojure/contrib/test_contrib/test_java_utils.clj @@ -1,7 +1,8 @@ (ns clojure.contrib.test-contrib.test-java-utils (:use clojure.contrib.test-is clojure.contrib.java-utils) - (:import [java.io File])) + (:import [java.io File] + [java.util Properties])) (deftest test-relative-path-string (testing "strings" @@ -75,8 +76,25 @@ (is (= propcount (count (System/getProperties)))))) ) +(deftest test-as-properties + (let [expected (doto (Properties.) + (.setProperty "a" "b") + (.setProperty "c" "d"))] + (testing "with a map" + (is (= expected + (as-properties {:a "b" :c "d"})))) + (testing "with a sequence of pairs" + (is (= expected + (as-properties [[:a :b] [:c :d]])))))) + +(deftest test-read-properties + (is (= {"a" "b" "c" "d"} + (read-properties (file "test/fixtures/test_java_utils.properties"))))) - +(deftest test-write-properties + (write-properties [['a 'b] ['c 'd]] (file "test/tmp/test_java_utils_write_properties.properties")) + (is (= {"a" "b" "c" "d"} + (read-properties (file "test/tmp/test_java_utils_write_properties.properties"))))) diff --git a/test/fixtures/test_java_utils.properties b/test/fixtures/test_java_utils.properties new file mode 100644 index 00000000..20c0243a --- /dev/null +++ b/test/fixtures/test_java_utils.properties @@ -0,0 +1,2 @@ +a=b +c=d |