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