diff src/clojure/contrib/test_contrib/test_properties.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/test_contrib/test_properties.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,63 @@
     1.4 +(ns clojure.contrib.test-properties
     1.5 +  (:refer-clojure :exclude (spit))
     1.6 +  (:use clojure.test clojure.contrib.properties
     1.7 +        [clojure.contrib.io :only (spit)])
     1.8 +  (:import (java.util Properties)
     1.9 +           (java.io File)))
    1.10 +
    1.11 +(deftest test-get-system-property
    1.12 +  (testing "works the same with keywords, symbols, and strings"
    1.13 +    (is (= (get-system-property "java.home") (get-system-property 'java.home)))
    1.14 +    (is (= (get-system-property "java.home") (get-system-property :java.home))))
    1.15 +  (testing "treats second arg as default"
    1.16 +    (is (= "default" (get-system-property "testing.test-system-property" "default"))))
    1.17 +  (testing "returns nil for missing properties"
    1.18 +    (is (nil? (get-system-property "testing.test-system-property")))))
    1.19 +
    1.20 +(deftest test-set-system-properties 
    1.21 +  (testing "set and then unset a property using keywords"
    1.22 +           (let [propname :clojure.contrib.java.test-set-system-properties]
    1.23 +             (is (nil? (get-system-property propname)))
    1.24 +             (set-system-properties {propname :foo})
    1.25 +             (is (= "foo") (get-system-property propname))
    1.26 +             (set-system-properties {propname nil})
    1.27 +             (is (nil? (get-system-property propname))))))
    1.28 +
    1.29 +(deftest test-with-system-properties
    1.30 +  (let [propname :clojure.contrib.java.test-with-system-properties]
    1.31 +    (testing "sets a property only for the duration of a block"
    1.32 +      (is (= "foo" 
    1.33 +	     (with-system-properties {propname "foo"}
    1.34 +	       (get-system-property propname))))
    1.35 +      (is (nil? (get-system-property propname)))))
    1.36 +  (testing "leaves other properties alone"
    1.37 +    ; TODO: write this test better, using a properties -> map function
    1.38 +    (let [propname :clojure.contrib.java.test-with-system-properties
    1.39 +          propcount (count (System/getProperties))]
    1.40 +      (with-system-properties {propname "foo"}
    1.41 +        (is (= (inc propcount) (count (System/getProperties)))))
    1.42 +      (is (= propcount (count (System/getProperties)))))))
    1.43 +
    1.44 +(deftest test-as-properties
    1.45 +  (let [expected (doto (Properties.)
    1.46 +		   (.setProperty "a" "b")
    1.47 +		   (.setProperty "c" "d"))]
    1.48 +    (testing "with a map"
    1.49 +      (is (= expected
    1.50 +	     (as-properties {:a "b" :c "d"}))))
    1.51 +    (testing "with a sequence of pairs"
    1.52 +      (is (= expected
    1.53 +	     (as-properties [[:a :b] [:c :d]]))))))
    1.54 +
    1.55 +(deftest test-read-properties
    1.56 +  (let [f (File/createTempFile "test" "properties")]
    1.57 +    (spit f "a=b\nc=d")
    1.58 +    (is (= {"a" "b" "c" "d"}
    1.59 +	   (read-properties f)))))
    1.60 +	   
    1.61 +(deftest test-write-properties
    1.62 +  (let [f (File/createTempFile "test" "properties")]
    1.63 +    (write-properties [['a 'b] ['c 'd]] f)
    1.64 +    (is (= {"a" "b" "c" "d"}
    1.65 +	   (read-properties f)))))
    1.66 +