Skip to content

Commit

Permalink
Implement writing to a property file
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelklishin committed Oct 5, 2013
1 parent ed50048 commit ba1234d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
28 changes: 22 additions & 6 deletions src/clojure/clojurewerkz/propertied/properties.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

(ns clojurewerkz.propertied.properties
(:require [clojure.java.io :as io])
(:import [java.util Properties Map ArrayList]))
(:import [java.util Properties Map ArrayList]
java.io.File))

;;
;; Implementation
Expand Down Expand Up @@ -48,12 +49,11 @@
(.setProperty p k v))
p))

(defprotocol PropertySource
"Extensions of this protocol can be used to load and store properties"
(load-from [input] "Instantiates a property list from the input")
(store-to [output] "Stores a property list to the output"))
(defprotocol PropertyReader
"Extensions of this protocol can be used to load properties"
(load-from [input] "Instantiates a property list from the input"))

(extend-protocol PropertySource
(extend-protocol PropertyReader
java.util.Map
(load-from [input]
(map->properties input))
Expand All @@ -67,3 +67,19 @@
(load-from [input]
(doto (Properties.)
(.load (io/input-stream input)))))

(defprotocol PropertyWriter
"Writes a property list to file"
(store-to [input sink] "Writes property list to file"))

(extend-protocol PropertyWriter
java.util.Map
(store-to [input sink]
(let [p (map->properties input)
w (io/writer sink)]
(.store p w nil)))

java.util.Properties
(store-to [input sink]
(let [w (io/writer sink)]
(.store input w nil))))
12 changes: 11 additions & 1 deletion test/clojurewerkz/propertied/properties_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
(ns clojurewerkz.propertied.properties-test
(:require [clojure.test :refer :all]
[clojure.java.io :as io]
[clojurewerkz.propertied.properties :as prop]))
[clojurewerkz.propertied.properties :as prop])
(:import java.io.File))


(deftest test-properties-from-map
Expand Down Expand Up @@ -48,3 +49,12 @@
"org.quartz.threadPool.threadCount" "4"
"org.quartz.plugin.triggHistory.class" "org.quartz.plugins.history.LoggingTriggerHistoryPlugin"
"org.quartz.plugin.jobHistory.class" "org.quartz.plugins.history.LoggingJobHistoryPlugin")))

(deftest test-store-to-file
(let [m {"key 1" "value 1"
"key 2" "2"}
p (prop/map->properties m)
sink (File/createTempFile "propertied" ".properties")]
(prop/store-to p sink)
(is (= p
(prop/load-from sink)))))

0 comments on commit ba1234d

Please sign in to comment.