diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/test_clojure/java/io.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,206 @@
     1.4 +;   Copyright (c) Rich Hickey. All rights reserved.
     1.5 +;   The use and distribution terms for this software are covered by the
     1.6 +;   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
     1.7 +;   which can be found in the file epl-v10.html at the root of this distribution.
     1.8 +;   By using this software in any fashion, you are agreeing to be bound by
     1.9 +;   the terms of this license.
    1.10 +;   You must not remove this notice, or any other, from this software.
    1.11 +
    1.12 +(ns clojure.test-clojure.java.io
    1.13 +  (:use clojure.test clojure.java.io)
    1.14 +  (:import (java.io File BufferedInputStream
    1.15 +                    FileInputStream InputStreamReader InputStream
    1.16 +                    FileOutputStream OutputStreamWriter OutputStream
    1.17 +                    ByteArrayInputStream ByteArrayOutputStream)
    1.18 +           (java.net URL URI Socket ServerSocket)))
    1.19 +
    1.20 +(defn temp-file
    1.21 +  [prefix suffix]
    1.22 +  (doto (File/createTempFile prefix suffix)
    1.23 +    (.deleteOnExit)))
    1.24 +
    1.25 +(deftest test-spit-and-slurp
    1.26 +  (let [f (temp-file "clojure.java.io" "test")]
    1.27 +    (spit f "foobar")
    1.28 +    (is (= "foobar" (slurp f)))
    1.29 +    (spit f "foobar" :encoding "UTF-16")
    1.30 +    (is (= "foobar" (slurp f :encoding "UTF-16")))
    1.31 +    (testing "deprecated arity"
    1.32 +      (is (=
    1.33 +           "WARNING: (slurp f enc) is deprecated, use (slurp f :encoding enc).\n"
    1.34 +           (with-out-str
    1.35 +             (is (= "foobar" (slurp f "UTF-16")))))))))
    1.36 +  
    1.37 +(deftest test-streams-defaults
    1.38 +  (let [f (temp-file "clojure.java.io" "test-reader-writer")
    1.39 +        content "testing"]
    1.40 +    (try
    1.41 +      (is (thrown? Exception (reader (Object.))))
    1.42 +      (is (thrown? Exception (writer (Object.))))
    1.43 +
    1.44 +      (are [write-to read-from] (= content (do
    1.45 +                                             (spit write-to content :encoding "UTF-8")
    1.46 +                                             (slurp read-from :encoding "UTF-8")))
    1.47 +           f f
    1.48 +           (.getAbsolutePath f) (.getAbsolutePath f)
    1.49 +           (.toURL f) (.toURL f)
    1.50 +           (.toURI f) (.toURI f)
    1.51 +           (FileOutputStream. f) (FileInputStream. f)
    1.52 +           (OutputStreamWriter. (FileOutputStream. f) "UTF-8") (reader f :encoding "UTF-8")
    1.53 +           f (FileInputStream. f)
    1.54 +           (writer f :encoding "UTF-8") (InputStreamReader. (FileInputStream. f) "UTF-8"))
    1.55 +
    1.56 +      (is (= content (slurp (.getBytes content "UTF-8"))))
    1.57 +      (is (= content (slurp (.toCharArray content))))
    1.58 +      (finally
    1.59 +       (.delete f)))))
    1.60 +
    1.61 +(defn bytes-should-equal [byte-array-1 byte-array-2 msg]
    1.62 +  (is (= @#'clojure.java.io/byte-array-type (class byte-array-1) (class byte-array-2)) msg)
    1.63 +  (is (= (into []  byte-array-1) (into []  byte-array-2)) msg))
    1.64 +
    1.65 +(defn data-fixture
    1.66 +  "in memory fixture data for tests"
    1.67 +  [encoding]
    1.68 +  (let [bs (.getBytes "hello" encoding)
    1.69 +        cs (.toCharArray "hello")
    1.70 +        i (ByteArrayInputStream. bs)
    1.71 +        r (InputStreamReader. i)
    1.72 +        o (ByteArrayOutputStream.)
    1.73 +        w (OutputStreamWriter. o)]
    1.74 +    {:bs bs
    1.75 +     :i i
    1.76 +     :r r
    1.77 +     :o o
    1.78 +     :s "hello"
    1.79 +     :cs cs
    1.80 +     :w w}))
    1.81 +
    1.82 +(deftest test-copy
    1.83 +  (dorun
    1.84 +   (for [{:keys [in out flush] :as test}
    1.85 +         [{:in :i :out :o}
    1.86 +          {:in :i :out :w}
    1.87 +          {:in :r :out :o}
    1.88 +          {:in :r :out :w}
    1.89 +          {:in :cs :out :o}
    1.90 +          {:in :cs :out :w}
    1.91 +          {:in :bs :out :o}
    1.92 +          {:in :bs :out :w}]
    1.93 +         
    1.94 +         opts
    1.95 +         [{} {:buffer-size 256}]]
    1.96 +     (let [{:keys [s o] :as d} (data-fixture "UTF-8")]
    1.97 +       (apply copy (in d) (out d) (flatten (vec opts)))
    1.98 +       #_(when (= out :w) (.flush (:w d)))
    1.99 +       (.flush (out d))
   1.100 +       (bytes-should-equal (.getBytes s "UTF-8")
   1.101 +                           (.toByteArray o)
   1.102 +                           (str "combination " test opts))))))
   1.103 +
   1.104 +(deftest test-copy-encodings
   1.105 +  (testing "from inputstream UTF-16 to writer UTF-8"
   1.106 +    (let [{:keys [i s o w bs]} (data-fixture "UTF-16")]
   1.107 +      (copy i w :encoding "UTF-16")
   1.108 +      (.flush w)
   1.109 +      (bytes-should-equal (.getBytes s "UTF-8") (.toByteArray o) "")))
   1.110 +  (testing "from reader UTF-8 to output-stream UTF-16"
   1.111 +    (let [{:keys [r o s]} (data-fixture "UTF-8")]
   1.112 +      (copy r o :encoding "UTF-16")
   1.113 +      (bytes-should-equal (.getBytes s "UTF-16") (.toByteArray o) ""))))
   1.114 +
   1.115 +(deftest test-as-file
   1.116 +  (are [result input] (= result (as-file input))
   1.117 +       (File. "foo") "foo"
   1.118 +       (File. "bar") (File. "bar")
   1.119 +       (File. "baz") (URL. "file:baz")
   1.120 +       (File. "quux") (URI. "file:quux")
   1.121 +       nil nil))
   1.122 +
   1.123 +(deftest test-file
   1.124 +  (are [result args] (= (File. result) (apply file args))
   1.125 +       "foo" ["foo"]
   1.126 +       "foo/bar" ["foo" "bar"]
   1.127 +       "foo/bar/baz" ["foo" "bar" "baz"]))
   1.128 +(deftest test-as-url
   1.129 +  (are [file-part input] (= (URL. (str "file:" file-part)) (as-url input))
   1.130 +       "foo" "file:foo"
   1.131 +       "baz" (URL. "file:baz")
   1.132 +       "quux" (URI. "file:quux"))
   1.133 +  (is (nil? (as-url nil))))
   1.134 +
   1.135 +(deftest test-delete-file
   1.136 +  (let [file (temp-file "test" "deletion")
   1.137 +        not-file (File. (str (java.util.UUID/randomUUID)))]
   1.138 +    (delete-file (.getAbsolutePath file))
   1.139 +    (is (not (.exists file)))
   1.140 +    (is (thrown? java.io.IOException (delete-file not-file)))
   1.141 +    (is (= :silently (delete-file not-file :silently)))))
   1.142 +
   1.143 +(deftest test-as-relative-path
   1.144 +  (testing "strings"
   1.145 +    (is (= "foo" (as-relative-path "foo"))))
   1.146 +  (testing "absolute path strings are forbidden"
   1.147 +    (is (thrown? IllegalArgumentException (as-relative-path (.getAbsolutePath (File. "baz"))))))
   1.148 +  (testing "relative File paths"
   1.149 +    (is (= "bar" (as-relative-path (File. "bar")))))
   1.150 +  (testing "absolute File paths are forbidden"
   1.151 +    (is (thrown? IllegalArgumentException (as-relative-path (File. (.getAbsolutePath (File. "quux"))))))))
   1.152 +
   1.153 +(defn stream-should-have [stream expected-bytes msg]
   1.154 +  (let [actual-bytes (byte-array (alength expected-bytes))]
   1.155 +    (.read stream actual-bytes)
   1.156 +    (is (= -1 (.read stream)) (str msg " : should be end of stream"))
   1.157 +    (is (= (seq expected-bytes) (seq actual-bytes)) (str msg " : byte arrays should match"))))
   1.158 +
   1.159 +(deftest test-input-stream
   1.160 +  (let [file (temp-file "test-input-stream" "txt")
   1.161 +        bytes (.getBytes "foobar")]
   1.162 +    (spit file "foobar")
   1.163 +    (doseq [[expr msg]
   1.164 +            [[file File]
   1.165 +             [(FileInputStream. file) FileInputStream]
   1.166 +             [(BufferedInputStream. (FileInputStream. file)) BufferedInputStream]
   1.167 +             [(.. file toURI) URI]
   1.168 +             [(.. file toURI toURL) URL]
   1.169 +             [(.. file toURI toURL toString) "URL as String"]
   1.170 +             [(.. file toString) "File as String"]]]
   1.171 +      (with-open [s (input-stream expr)]
   1.172 +        (stream-should-have s bytes msg)))))
   1.173 +
   1.174 +(deftest test-streams-buffering
   1.175 +  (let [data (.getBytes "")]
   1.176 +    (is (instance? java.io.BufferedReader (reader data)))
   1.177 +    (is (instance? java.io.BufferedWriter (writer (java.io.ByteArrayOutputStream.))))
   1.178 +    (is (instance? java.io.BufferedInputStream (input-stream data)))
   1.179 +    (is (instance? java.io.BufferedOutputStream (output-stream (java.io.ByteArrayOutputStream.))))))
   1.180 +
   1.181 +(deftest test-resource
   1.182 +  (is (nil? (resource "non/existent/resource")))
   1.183 +  (is (instance? URL (resource "clojure/core.clj")))
   1.184 +  (let [file (temp-file "test-resource" "txt")
   1.185 +        url (as-url (.getParentFile file))
   1.186 +        loader (java.net.URLClassLoader. (into-array [url]))]
   1.187 +    (is (nil? (resource "non/existent/resource" loader)))
   1.188 +    (is (instance? URL (resource (.getName file) loader)))))
   1.189 +
   1.190 +(deftest test-make-parents
   1.191 +  (let [tmp (System/getProperty "java.io.tmpdir")]
   1.192 +    (delete-file (file tmp "test-make-parents" "child" "grandchild") :silently)
   1.193 +    (delete-file (file tmp "test-make-parents" "child") :silently)
   1.194 +    (delete-file (file tmp "test-make-parents") :silently)
   1.195 +    (make-parents tmp "test-make-parents" "child" "grandchild")
   1.196 +    (is (.isDirectory (file tmp "test-make-parents" "child")))
   1.197 +    (is (not (.isDirectory (file tmp "test-make-parents" "child" "grandchild"))))
   1.198 +    (delete-file (file tmp "test-make-parents" "child"))
   1.199 +    (delete-file (file tmp "test-make-parents"))))
   1.200 +
   1.201 +(deftest test-socket-iofactory
   1.202 +  (let [port 65321
   1.203 +        server-socket (ServerSocket. port)
   1.204 +        client-socket (Socket. "localhost" port)]
   1.205 +    (try
   1.206 +      (is (instance? InputStream (input-stream client-socket)))
   1.207 +      (is (instance? OutputStream (output-stream client-socket)))
   1.208 +      (finally (.close server-socket)
   1.209 +               (.close client-socket)))))