diff options
author | Stuart Sierra <mail@stuartsierra.com> | 2010-02-11 17:10:16 -0500 |
---|---|---|
committer | Stuart Sierra <mail@stuartsierra.com> | 2010-02-11 17:10:16 -0500 |
commit | 31748856f90ce77c304d5acdde84c25b4688d407 (patch) | |
tree | dbb2b29f0a661cbb4c95158fb950f4098912cb21 /src | |
parent | faa392bb82018203a82d56013aa8b55c89ef2b5f (diff) |
Add test_properties.clj from old test_java.clj
Diffstat (limited to 'src')
-rw-r--r-- | src/test/clojure/clojure/contrib/test_properties.clj | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/test/clojure/clojure/contrib/test_properties.clj b/src/test/clojure/clojure/contrib/test_properties.clj new file mode 100644 index 00000000..0b9b8179 --- /dev/null +++ b/src/test/clojure/clojure/contrib/test_properties.clj @@ -0,0 +1,62 @@ +(ns clojure.contrib.test-properties + (:use clojure.test clojure.contrib.properties + [clojure.contrib.io :only (spit)]) + (:import (java.util Properties) + (java.io File))) + +(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)))) + (testing "treats second arg as default" + (is (= "default" (get-system-property "testing.test-system-property" "default")))) + (testing "returns nil for missing properties" + (is (nil? (get-system-property "testing.test-system-property"))))) + +(deftest test-set-system-properties + (testing "set and then unset a property using keywords" + (let [propname :clojure.contrib.java.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.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.test-with-system-properties + propcount (count (System/getProperties))] + (with-system-properties {propname "foo"} + (is (= (inc propcount) (count (System/getProperties))))) + (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 + (let [f (File/createTempFile "test" "properties")] + (spit f "a=b\nc=d") + (is (= {"a" "b" "c" "d"} + (read-properties f))))) + +(deftest test-write-properties + (let [f (File/createTempFile "test" "properties")] + (write-properties [['a 'b] ['c 'd]] f) + (is (= {"a" "b" "c" "d"} + (read-properties f))))) + |