rlm@10
|
1 (ns clojure.contrib.test-io
|
rlm@10
|
2 (:refer-clojure :exclude (spit))
|
rlm@10
|
3 (:use clojure.test clojure.contrib.io)
|
rlm@10
|
4 (:import (java.io File FileInputStream BufferedInputStream)
|
rlm@10
|
5 (java.net URL URI)))
|
rlm@10
|
6
|
rlm@10
|
7 (deftest file-str-backslash
|
rlm@10
|
8 (is (= (java.io.File.
|
rlm@10
|
9 (str "C:" java.io.File/separator
|
rlm@10
|
10 "Documents" java.io.File/separator
|
rlm@10
|
11 "file.txt"))
|
rlm@10
|
12 (file-str "C:\\Documents\\file.txt"))))
|
rlm@10
|
13
|
rlm@10
|
14 (deftest test-as-file
|
rlm@10
|
15 (testing "strings"
|
rlm@10
|
16 (is (= (File. "foo") (as-file "foo"))))
|
rlm@10
|
17 (testing "Files"
|
rlm@10
|
18 (is (= (File. "bar") (as-file (File. "bar"))))))
|
rlm@10
|
19
|
rlm@10
|
20 (deftest test-as-url
|
rlm@10
|
21 (are [result expr] (= result expr)
|
rlm@10
|
22 (URL. "http://foo") (as-url (URL. "http://foo"))
|
rlm@10
|
23 (URL. "http://foo") (as-url "http://foo")
|
rlm@10
|
24 (URL. "http://foo") (as-url (URI. "http://foo"))
|
rlm@10
|
25 (URL. "file:/foo") (as-url (File. "/foo"))))
|
rlm@10
|
26
|
rlm@10
|
27 (deftest test-delete-file
|
rlm@10
|
28 (let [file (File/createTempFile "test" "deletion")
|
rlm@10
|
29 not-file (File. (str (java.util.UUID/randomUUID)))]
|
rlm@10
|
30 (delete-file (.getAbsolutePath file))
|
rlm@10
|
31 (is (not (.exists file)))
|
rlm@10
|
32 (is (thrown? ArithmeticException (/ 1 0)))
|
rlm@10
|
33 (is (thrown? java.io.IOException (delete-file not-file)))
|
rlm@10
|
34 (is (delete-file not-file :silently))))
|
rlm@10
|
35
|
rlm@10
|
36 (deftest test-relative-path-string
|
rlm@10
|
37 (testing "strings"
|
rlm@10
|
38 (is (= "foo" (relative-path-string "foo"))))
|
rlm@10
|
39 (testing "absolute path strings are forbidden"
|
rlm@10
|
40 (is (thrown? IllegalArgumentException (relative-path-string (str File/separator "baz")))))
|
rlm@10
|
41 (testing "relative File paths"
|
rlm@10
|
42 (is (= "bar" (relative-path-string (File. "bar")))))
|
rlm@10
|
43 (testing "absolute File paths are forbidden"
|
rlm@10
|
44 (is (thrown? IllegalArgumentException (relative-path-string (File. (str File/separator "quux")))))))
|
rlm@10
|
45
|
rlm@10
|
46 (defn stream-should-have [stream expected-bytes msg]
|
rlm@10
|
47 (let [actual-bytes (byte-array (alength expected-bytes))]
|
rlm@10
|
48 (.read stream actual-bytes)
|
rlm@10
|
49 (is (= -1 (.read stream)) (str msg " : should be end of stream"))
|
rlm@10
|
50 (is (= (seq expected-bytes) (seq actual-bytes)) (str msg " : byte arrays should match"))))
|
rlm@10
|
51
|
rlm@10
|
52 (deftest test-input-stream
|
rlm@10
|
53 (let [file (File/createTempFile "test-input-stream" "txt")
|
rlm@10
|
54 bytes (.getBytes "foobar")]
|
rlm@10
|
55 (spit file "foobar")
|
rlm@10
|
56 (doseq [[expr msg]
|
rlm@10
|
57 [[file File]
|
rlm@10
|
58 [(FileInputStream. file) FileInputStream]
|
rlm@10
|
59 [(BufferedInputStream. (FileInputStream. file)) BufferedInputStream]
|
rlm@10
|
60 [(.. file toURI) URI]
|
rlm@10
|
61 [(.. file toURI toURL) URL]
|
rlm@10
|
62 [(.. file toURI toURL toString) "URL as String"]
|
rlm@10
|
63 [(.. file toString) "File as String"]]]
|
rlm@10
|
64 (with-open [s (input-stream expr)]
|
rlm@10
|
65 (stream-should-have s bytes msg)))))
|
rlm@10
|
66
|
rlm@10
|
67 (deftest test-streams-buffering
|
rlm@10
|
68 (let [data (.getBytes "")]
|
rlm@10
|
69 (is (instance? java.io.BufferedReader (reader data)))
|
rlm@10
|
70 (is (instance? java.io.BufferedWriter (writer (java.io.ByteArrayOutputStream.))))
|
rlm@10
|
71 (is (instance? java.io.BufferedInputStream (input-stream data)))
|
rlm@10
|
72 (is (instance? java.io.BufferedOutputStream (output-stream (java.io.ByteArrayOutputStream.))))))
|
rlm@10
|
73
|
rlm@10
|
74 (deftest test-streams-defaults
|
rlm@10
|
75 (let [f (File/createTempFile "clojure.contrib" "test-reader-writer")
|
rlm@10
|
76 content "test\u2099ing"]
|
rlm@10
|
77 (try
|
rlm@10
|
78 (is (thrown? Exception (reader (Object.))))
|
rlm@10
|
79 (is (thrown? Exception (writer (Object.))))
|
rlm@10
|
80
|
rlm@10
|
81 (are [write-to read-from] (= content (do
|
rlm@10
|
82 (spit write-to content)
|
rlm@10
|
83 (slurp* (or read-from write-to))))
|
rlm@10
|
84 f nil
|
rlm@10
|
85 (.getAbsolutePath f) nil
|
rlm@10
|
86 (.toURL f) nil
|
rlm@10
|
87 (.toURI f) nil
|
rlm@10
|
88 (java.io.FileOutputStream. f) f
|
rlm@10
|
89 (java.io.OutputStreamWriter. (java.io.FileOutputStream. f) "UTF-8") f
|
rlm@10
|
90 f (java.io.FileInputStream. f)
|
rlm@10
|
91 f (java.io.InputStreamReader. (java.io.FileInputStream. f) "UTF-8"))
|
rlm@10
|
92
|
rlm@10
|
93 (is (= content (slurp* (.getBytes content "UTF-8"))))
|
rlm@10
|
94 (is (= content (slurp* (.toCharArray content))))
|
rlm@10
|
95 (finally
|
rlm@10
|
96 (.delete f)))))
|