Skip to content

Instantly share code, notes, and snippets.

@slyphon
Created April 28, 2010 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slyphon/9ddaa20d37a4a4be4d07 to your computer and use it in GitHub Desktop.
Save slyphon/9ddaa20d37a4a4be4d07 to your computer and use it in GitHub Desktop.
(ns mbox.harpo.jms-test
(:use
[clojure
test
(walk :only (keywordize-keys))]
[clojure.contrib
mock
[java-utils :only (set-system-properties with-system-properties as-str)]
[logging :only (debug info warn error fatal spy)]
[except :only (throw-arg)]]
[mbox.harpo
jms-test-helper
[utils :only (to-hashtable camelize enumeration-to-vec)]]
[mbox.harpo.db common])
(:require
[clojure.contrib (sql :as sql)]
[clojure [test :as test]]
[mbox.harpo
[db :as db]
[log :as log]
[sync :as db-sync]
[jms :as jms]
[jndi :as jndi]
[jms-internal :as jms-int]]
[mbox.harpo.db
[jta :as jta]]
)
(:import
[java.util Date Hashtable]
[javax.jms Connection ConnectionFactory MessageConsumer
MessageProducer Queue Session TextMessage] ))
;; (use-fixtures :once mbox.harpo.jms-test-helper/jms-test-env-fixture)
(defmacro try-report [& body]
`(try
(do ~@body)
(catch Exception e#
(error "caught error in try-report" e#)
(throw e#))))
(defn get-queue-count
([]
(get-queue-count *testq-jndi-name*))
([jndi-queue-name]
(let [{:keys [non-xa-connection]
:as jms-state} (jms-int/jms-state)
test-dest (jndi/lookup jndi-queue-name)]
(with-open [session (.createSession non-xa-connection true 0)
browser (.createBrowser session test-dest)]
(count (enumeration-seq (.getEnumeration browser)))))))
(deftest create-message-test
(testing "should fill in defaults"
(let [rval (jms/create-message {})]
(is (= jms/*default-content-type* (rval :content-type)))
(is (= ::jms/MessageStruct (rval :type)))))
(testing "should override argument :type"
(let [rval (jms/create-message {:type ::BOOGABOOGA})]
(is (= (rval :type) ::jms/MessageStruct))))
(testing "should not override :content-type"
(let [content-type "application/clojure"
rval (jms/create-message {:content-type content-type})]
(is (= (rval :content-type) content-type)))))
(deftest with-non-jta-session-STAR-test
(testing "initial state of the system"
(is (jms-int/jms-state) "jms-state should not throw an error")
(is (nil? (deref jms-int/*session-state*))))
(testing "should establish session state and then call provided fn"
(let [non-xa-cnx ((jms-int/jms-state) :non-xa-connection)
fn-called (atom false)
cb-fn (fn cb-fn []
(reset! fn-called true)
(let [{:keys [connection session]
:as session-state} (deref jms-int/*session-state*)]
(is (not (nil? session-state)))
(is (not (nil? session)))
(is (= connection non-xa-cnx))))]
(jms-int/with-non-jta-session* cb-fn)
(is @fn-called "cb-fn should have been called")
))
(testing "should commit session if no error raised"
(let [fn-called (atom false)
msg-sent (atom false)
cb-fn (fn cb-fn []
(reset! fn-called true)
(let [{:keys [session]} (jms-int/session-state)
msg (. session (createTextMessage "hallo!"))]
(jms/with-producer [prod :test]
(.send prod msg)
(reset! msg-sent true))))]
(jms-int/with-non-jta-session* cb-fn)
(is @fn-called "cb-fn should have been called")
(is @msg-sent "message should have been sent")
(is (= (try-report (get-queue-count)) 1) "should be 1 message pending")))
(testing "should not commit if exception thrown"
(let [fn-called (atom false)
msg-sent (atom false)
cb-fn (fn cb-fn []
(reset! fn-called true)
(let [{:keys [session]} (jms-int/session-state)
msg (. session (createTextMessage "hallo!"))]
(jms/with-producer [prod :test]
(.send prod msg)
(reset! msg-sent true)))) ]
)
)
)
(defn test-ns-hook []
(with-jms-test-env
(create-message-test)
(with-non-jta-session-STAR-test)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment