Mercurial > lasercutter
comparison src/clojure/test_clojure/java/io.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9:35cf337adfcf | 10:ef7dbbd6452c |
---|---|
1 ; Copyright (c) Rich Hickey. All rights reserved. | |
2 ; The use and distribution terms for this software are covered by the | |
3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) | |
4 ; which can be found in the file epl-v10.html at the root of this distribution. | |
5 ; By using this software in any fashion, you are agreeing to be bound by | |
6 ; the terms of this license. | |
7 ; You must not remove this notice, or any other, from this software. | |
8 | |
9 (ns clojure.test-clojure.java.io | |
10 (:use clojure.test clojure.java.io) | |
11 (:import (java.io File BufferedInputStream | |
12 FileInputStream InputStreamReader InputStream | |
13 FileOutputStream OutputStreamWriter OutputStream | |
14 ByteArrayInputStream ByteArrayOutputStream) | |
15 (java.net URL URI Socket ServerSocket))) | |
16 | |
17 (defn temp-file | |
18 [prefix suffix] | |
19 (doto (File/createTempFile prefix suffix) | |
20 (.deleteOnExit))) | |
21 | |
22 (deftest test-spit-and-slurp | |
23 (let [f (temp-file "clojure.java.io" "test")] | |
24 (spit f "foobar") | |
25 (is (= "foobar" (slurp f))) | |
26 (spit f "foobar" :encoding "UTF-16") | |
27 (is (= "foobar" (slurp f :encoding "UTF-16"))) | |
28 (testing "deprecated arity" | |
29 (is (= | |
30 "WARNING: (slurp f enc) is deprecated, use (slurp f :encoding enc).\n" | |
31 (with-out-str | |
32 (is (= "foobar" (slurp f "UTF-16"))))))))) | |
33 | |
34 (deftest test-streams-defaults | |
35 (let [f (temp-file "clojure.java.io" "test-reader-writer") | |
36 content "testing"] | |
37 (try | |
38 (is (thrown? Exception (reader (Object.)))) | |
39 (is (thrown? Exception (writer (Object.)))) | |
40 | |
41 (are [write-to read-from] (= content (do | |
42 (spit write-to content :encoding "UTF-8") | |
43 (slurp read-from :encoding "UTF-8"))) | |
44 f f | |
45 (.getAbsolutePath f) (.getAbsolutePath f) | |
46 (.toURL f) (.toURL f) | |
47 (.toURI f) (.toURI f) | |
48 (FileOutputStream. f) (FileInputStream. f) | |
49 (OutputStreamWriter. (FileOutputStream. f) "UTF-8") (reader f :encoding "UTF-8") | |
50 f (FileInputStream. f) | |
51 (writer f :encoding "UTF-8") (InputStreamReader. (FileInputStream. f) "UTF-8")) | |
52 | |
53 (is (= content (slurp (.getBytes content "UTF-8")))) | |
54 (is (= content (slurp (.toCharArray content)))) | |
55 (finally | |
56 (.delete f))))) | |
57 | |
58 (defn bytes-should-equal [byte-array-1 byte-array-2 msg] | |
59 (is (= @#'clojure.java.io/byte-array-type (class byte-array-1) (class byte-array-2)) msg) | |
60 (is (= (into [] byte-array-1) (into [] byte-array-2)) msg)) | |
61 | |
62 (defn data-fixture | |
63 "in memory fixture data for tests" | |
64 [encoding] | |
65 (let [bs (.getBytes "hello" encoding) | |
66 cs (.toCharArray "hello") | |
67 i (ByteArrayInputStream. bs) | |
68 r (InputStreamReader. i) | |
69 o (ByteArrayOutputStream.) | |
70 w (OutputStreamWriter. o)] | |
71 {:bs bs | |
72 :i i | |
73 :r r | |
74 :o o | |
75 :s "hello" | |
76 :cs cs | |
77 :w w})) | |
78 | |
79 (deftest test-copy | |
80 (dorun | |
81 (for [{:keys [in out flush] :as test} | |
82 [{:in :i :out :o} | |
83 {:in :i :out :w} | |
84 {:in :r :out :o} | |
85 {:in :r :out :w} | |
86 {:in :cs :out :o} | |
87 {:in :cs :out :w} | |
88 {:in :bs :out :o} | |
89 {:in :bs :out :w}] | |
90 | |
91 opts | |
92 [{} {:buffer-size 256}]] | |
93 (let [{:keys [s o] :as d} (data-fixture "UTF-8")] | |
94 (apply copy (in d) (out d) (flatten (vec opts))) | |
95 #_(when (= out :w) (.flush (:w d))) | |
96 (.flush (out d)) | |
97 (bytes-should-equal (.getBytes s "UTF-8") | |
98 (.toByteArray o) | |
99 (str "combination " test opts)))))) | |
100 | |
101 (deftest test-copy-encodings | |
102 (testing "from inputstream UTF-16 to writer UTF-8" | |
103 (let [{:keys [i s o w bs]} (data-fixture "UTF-16")] | |
104 (copy i w :encoding "UTF-16") | |
105 (.flush w) | |
106 (bytes-should-equal (.getBytes s "UTF-8") (.toByteArray o) ""))) | |
107 (testing "from reader UTF-8 to output-stream UTF-16" | |
108 (let [{:keys [r o s]} (data-fixture "UTF-8")] | |
109 (copy r o :encoding "UTF-16") | |
110 (bytes-should-equal (.getBytes s "UTF-16") (.toByteArray o) "")))) | |
111 | |
112 (deftest test-as-file | |
113 (are [result input] (= result (as-file input)) | |
114 (File. "foo") "foo" | |
115 (File. "bar") (File. "bar") | |
116 (File. "baz") (URL. "file:baz") | |
117 (File. "quux") (URI. "file:quux") | |
118 nil nil)) | |
119 | |
120 (deftest test-file | |
121 (are [result args] (= (File. result) (apply file args)) | |
122 "foo" ["foo"] | |
123 "foo/bar" ["foo" "bar"] | |
124 "foo/bar/baz" ["foo" "bar" "baz"])) | |
125 (deftest test-as-url | |
126 (are [file-part input] (= (URL. (str "file:" file-part)) (as-url input)) | |
127 "foo" "file:foo" | |
128 "baz" (URL. "file:baz") | |
129 "quux" (URI. "file:quux")) | |
130 (is (nil? (as-url nil)))) | |
131 | |
132 (deftest test-delete-file | |
133 (let [file (temp-file "test" "deletion") | |
134 not-file (File. (str (java.util.UUID/randomUUID)))] | |
135 (delete-file (.getAbsolutePath file)) | |
136 (is (not (.exists file))) | |
137 (is (thrown? java.io.IOException (delete-file not-file))) | |
138 (is (= :silently (delete-file not-file :silently))))) | |
139 | |
140 (deftest test-as-relative-path | |
141 (testing "strings" | |
142 (is (= "foo" (as-relative-path "foo")))) | |
143 (testing "absolute path strings are forbidden" | |
144 (is (thrown? IllegalArgumentException (as-relative-path (.getAbsolutePath (File. "baz")))))) | |
145 (testing "relative File paths" | |
146 (is (= "bar" (as-relative-path (File. "bar"))))) | |
147 (testing "absolute File paths are forbidden" | |
148 (is (thrown? IllegalArgumentException (as-relative-path (File. (.getAbsolutePath (File. "quux")))))))) | |
149 | |
150 (defn stream-should-have [stream expected-bytes msg] | |
151 (let [actual-bytes (byte-array (alength expected-bytes))] | |
152 (.read stream actual-bytes) | |
153 (is (= -1 (.read stream)) (str msg " : should be end of stream")) | |
154 (is (= (seq expected-bytes) (seq actual-bytes)) (str msg " : byte arrays should match")))) | |
155 | |
156 (deftest test-input-stream | |
157 (let [file (temp-file "test-input-stream" "txt") | |
158 bytes (.getBytes "foobar")] | |
159 (spit file "foobar") | |
160 (doseq [[expr msg] | |
161 [[file File] | |
162 [(FileInputStream. file) FileInputStream] | |
163 [(BufferedInputStream. (FileInputStream. file)) BufferedInputStream] | |
164 [(.. file toURI) URI] | |
165 [(.. file toURI toURL) URL] | |
166 [(.. file toURI toURL toString) "URL as String"] | |
167 [(.. file toString) "File as String"]]] | |
168 (with-open [s (input-stream expr)] | |
169 (stream-should-have s bytes msg))))) | |
170 | |
171 (deftest test-streams-buffering | |
172 (let [data (.getBytes "")] | |
173 (is (instance? java.io.BufferedReader (reader data))) | |
174 (is (instance? java.io.BufferedWriter (writer (java.io.ByteArrayOutputStream.)))) | |
175 (is (instance? java.io.BufferedInputStream (input-stream data))) | |
176 (is (instance? java.io.BufferedOutputStream (output-stream (java.io.ByteArrayOutputStream.)))))) | |
177 | |
178 (deftest test-resource | |
179 (is (nil? (resource "non/existent/resource"))) | |
180 (is (instance? URL (resource "clojure/core.clj"))) | |
181 (let [file (temp-file "test-resource" "txt") | |
182 url (as-url (.getParentFile file)) | |
183 loader (java.net.URLClassLoader. (into-array [url]))] | |
184 (is (nil? (resource "non/existent/resource" loader))) | |
185 (is (instance? URL (resource (.getName file) loader))))) | |
186 | |
187 (deftest test-make-parents | |
188 (let [tmp (System/getProperty "java.io.tmpdir")] | |
189 (delete-file (file tmp "test-make-parents" "child" "grandchild") :silently) | |
190 (delete-file (file tmp "test-make-parents" "child") :silently) | |
191 (delete-file (file tmp "test-make-parents") :silently) | |
192 (make-parents tmp "test-make-parents" "child" "grandchild") | |
193 (is (.isDirectory (file tmp "test-make-parents" "child"))) | |
194 (is (not (.isDirectory (file tmp "test-make-parents" "child" "grandchild")))) | |
195 (delete-file (file tmp "test-make-parents" "child")) | |
196 (delete-file (file tmp "test-make-parents")))) | |
197 | |
198 (deftest test-socket-iofactory | |
199 (let [port 65321 | |
200 server-socket (ServerSocket. port) | |
201 client-socket (Socket. "localhost" port)] | |
202 (try | |
203 (is (instance? InputStream (input-stream client-socket))) | |
204 (is (instance? OutputStream (output-stream client-socket))) | |
205 (finally (.close server-socket) | |
206 (.close client-socket))))) |