Added higher-level download-blob function and tests for it.

This commit is contained in:
Jim 2010-03-09 16:13:23 -06:00
parent a31a114dd7
commit 1da7c5a6b6
2 changed files with 40 additions and 7 deletions

View File

@ -18,8 +18,9 @@ Here's a quick example of how to view blob resources in rackspace
(pprint (blobs blobstore your_container_name)))
See http://code.google.com/p/jclouds for details."
(:use org.jclouds.core)
(:import java.io.File
(:use [org.jclouds.core]
[clojure.contrib.duck-streams :only [copy]])
(:import [java.io File FileOutputStream OutputStream]
[org.jclouds.blobstore
AsyncBlobStore BlobStore BlobStoreContext BlobStoreContextFactory
domain.BlobMetadata domain.StorageMetadata domain.Blob
@ -241,8 +242,26 @@ container, name, string -> etag
(doto (.newBlob blobstore name)
(.setPayload data)))))
(defmulti #^{:arglists '[[container-name name target]
[blobstore container-name name target]]}
download-blob (fn [& args]
(if (= (count args) 3)
::short-form
(class (last args)))))
(define-accessors StorageMetadata "blob" type id name location-id uri last-modfied)
(defmethod download-blob ::short-form
[container-name name target]
(download-blob *blobstore* container-name name target))
(defmethod download-blob OutputStream [blobstore container-name name target]
(let [blob (get-blob blobstore container-name name)]
(copy (.getContent blob) target)))
(defmethod download-blob File [blobstore container-name name target]
(download-blob (FileOutputStream. target)))
(define-accessors StorageMetadata "blob" type id name
location-id uri last-modfied)
(define-accessors BlobMetadata "blob" content-type)
(defn blob-etag [blob]

View File

@ -1,14 +1,15 @@
(ns org.jclouds.blobstore-test
(:use [org.jclouds.blobstore] :reload-all)
(:use clojure.test)
(:import org.jclouds.blobstore.BlobStoreContextFactory))
(:import [org.jclouds.blobstore BlobStoreContextFactory]
[java.io ByteArrayOutputStream]))
(def stub-context (.createContext (BlobStoreContextFactory.) "transient" "" ""))
(def stub-blobstore (.getBlobStore stub-context))
(defn clean-stub-fixture [f]
(doseq [container (.list stub-blobstore)]
(.deleteContainer stub-blobstore (.getName container)))
(doseq [container (containers)]
(delete-container (.getName container)))
(f))
(use-fixtures :each clean-stub-fixture)
@ -63,7 +64,8 @@
(is (create-blob stub-blobstore "container" "dir/blob2" "blob2"))
(is (= 3 (count (list-container stub-blobstore "container"))))
(is (= 4 (count (list-container stub-blobstore "container" :recursive))))
(is (= 1 (count (list-container stub-blobstore "container" :in-directory "dir")))))
(is (= 1 (count (list-container stub-blobstore "container"
:in-directory "dir")))))
(deftest list-container-with-blobstore-test
(with-blobstore [stub-blobstore]
@ -79,4 +81,16 @@
(is (= 4 (count (list-container "container" :recursive))))
(is (= 1 (count (list-container "container" :in-directory "dir"))))))
(deftest download-blob-test
(with-blobstore [stub-blobstore]
(let [name "test"
container-name "test-container"
data "test content"
baos (ByteArrayOutputStream.)]
(create-container container-name)
(create-blob container-name name data)
(download-blob container-name name baos)
(is (= data (.toString baos))))))
;; TODO: more tests involving blob-specific functions