rlm@10
|
1 (ns clojure.contrib.test-properties
|
rlm@10
|
2 (:refer-clojure :exclude (spit))
|
rlm@10
|
3 (:use clojure.test clojure.contrib.properties
|
rlm@10
|
4 [clojure.contrib.io :only (spit)])
|
rlm@10
|
5 (:import (java.util Properties)
|
rlm@10
|
6 (java.io File)))
|
rlm@10
|
7
|
rlm@10
|
8 (deftest test-get-system-property
|
rlm@10
|
9 (testing "works the same with keywords, symbols, and strings"
|
rlm@10
|
10 (is (= (get-system-property "java.home") (get-system-property 'java.home)))
|
rlm@10
|
11 (is (= (get-system-property "java.home") (get-system-property :java.home))))
|
rlm@10
|
12 (testing "treats second arg as default"
|
rlm@10
|
13 (is (= "default" (get-system-property "testing.test-system-property" "default"))))
|
rlm@10
|
14 (testing "returns nil for missing properties"
|
rlm@10
|
15 (is (nil? (get-system-property "testing.test-system-property")))))
|
rlm@10
|
16
|
rlm@10
|
17 (deftest test-set-system-properties
|
rlm@10
|
18 (testing "set and then unset a property using keywords"
|
rlm@10
|
19 (let [propname :clojure.contrib.java.test-set-system-properties]
|
rlm@10
|
20 (is (nil? (get-system-property propname)))
|
rlm@10
|
21 (set-system-properties {propname :foo})
|
rlm@10
|
22 (is (= "foo") (get-system-property propname))
|
rlm@10
|
23 (set-system-properties {propname nil})
|
rlm@10
|
24 (is (nil? (get-system-property propname))))))
|
rlm@10
|
25
|
rlm@10
|
26 (deftest test-with-system-properties
|
rlm@10
|
27 (let [propname :clojure.contrib.java.test-with-system-properties]
|
rlm@10
|
28 (testing "sets a property only for the duration of a block"
|
rlm@10
|
29 (is (= "foo"
|
rlm@10
|
30 (with-system-properties {propname "foo"}
|
rlm@10
|
31 (get-system-property propname))))
|
rlm@10
|
32 (is (nil? (get-system-property propname)))))
|
rlm@10
|
33 (testing "leaves other properties alone"
|
rlm@10
|
34 ; TODO: write this test better, using a properties -> map function
|
rlm@10
|
35 (let [propname :clojure.contrib.java.test-with-system-properties
|
rlm@10
|
36 propcount (count (System/getProperties))]
|
rlm@10
|
37 (with-system-properties {propname "foo"}
|
rlm@10
|
38 (is (= (inc propcount) (count (System/getProperties)))))
|
rlm@10
|
39 (is (= propcount (count (System/getProperties)))))))
|
rlm@10
|
40
|
rlm@10
|
41 (deftest test-as-properties
|
rlm@10
|
42 (let [expected (doto (Properties.)
|
rlm@10
|
43 (.setProperty "a" "b")
|
rlm@10
|
44 (.setProperty "c" "d"))]
|
rlm@10
|
45 (testing "with a map"
|
rlm@10
|
46 (is (= expected
|
rlm@10
|
47 (as-properties {:a "b" :c "d"}))))
|
rlm@10
|
48 (testing "with a sequence of pairs"
|
rlm@10
|
49 (is (= expected
|
rlm@10
|
50 (as-properties [[:a :b] [:c :d]]))))))
|
rlm@10
|
51
|
rlm@10
|
52 (deftest test-read-properties
|
rlm@10
|
53 (let [f (File/createTempFile "test" "properties")]
|
rlm@10
|
54 (spit f "a=b\nc=d")
|
rlm@10
|
55 (is (= {"a" "b" "c" "d"}
|
rlm@10
|
56 (read-properties f)))))
|
rlm@10
|
57
|
rlm@10
|
58 (deftest test-write-properties
|
rlm@10
|
59 (let [f (File/createTempFile "test" "properties")]
|
rlm@10
|
60 (write-properties [['a 'b] ['c 'd]] f)
|
rlm@10
|
61 (is (= {"a" "b" "c" "d"}
|
rlm@10
|
62 (read-properties f)))))
|
rlm@10
|
63
|