annotate src/clojure/contrib/test_contrib/test_jmx.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 ;; Tests for JMX support for Clojure (see also clojure/contrib/jmx.clj)
rlm@10 2
rlm@10 3 ;; by Stuart Halloway
rlm@10 4
rlm@10 5 ;; Copyright (c) Stuart Halloway, 2009. All rights reserved. The use
rlm@10 6 ;; and distribution terms for this software are covered by the Eclipse
rlm@10 7 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
rlm@10 8 ;; which can be found in the file epl-v10.html at the root of this
rlm@10 9 ;; distribution. By using this software in any fashion, you are
rlm@10 10 ;; agreeing to be bound by the terms of this license. You must not
rlm@10 11 ;; remove this notice, or any other, from this software.
rlm@10 12
rlm@10 13 (ns clojure.contrib.test-jmx
rlm@10 14 (:import javax.management.openmbean.CompositeDataSupport
rlm@10 15 [javax.management MBeanAttributeInfo AttributeList]
rlm@10 16 [java.util.logging LogManager Logger]
rlm@10 17 clojure.contrib.jmx.Bean)
rlm@10 18 (:use clojure.test)
rlm@10 19 (:require [clojure.contrib [jmx :as jmx]]))
rlm@10 20
rlm@10 21
rlm@10 22 (defn =set [a b]
rlm@10 23 (= (set a) (set b)))
rlm@10 24
rlm@10 25 (defn seq-contains-all?
rlm@10 26 "Does container contain every item in containee?
rlm@10 27 Not fast. Testing use only"
rlm@10 28 [container containee]
rlm@10 29 (let [container (set container)]
rlm@10 30 (every? #(contains? container %) containee)))
rlm@10 31
rlm@10 32 (deftest finding-mbeans
rlm@10 33 (testing "as-object-name"
rlm@10 34 (are [cname object-name]
rlm@10 35 (= cname (.getCanonicalName object-name))
rlm@10 36 "java.lang:type=Memory" (jmx/as-object-name "java.lang:type=Memory")))
rlm@10 37 (testing "mbean-names"
rlm@10 38 (are [cnames object-name]
rlm@10 39 (= cnames (map #(.getCanonicalName %) object-name))
rlm@10 40 ["java.lang:type=Memory"] (jmx/mbean-names "java.lang:type=Memory"))))
rlm@10 41
rlm@10 42 ; These actual beans may differ on different JVM platforms.
rlm@10 43 ; Tested April 2010 to work on Sun and IBM JDKs.
rlm@10 44 (deftest testing-actual-beans
rlm@10 45 (testing "reflecting on capabilities"
rlm@10 46 (are [attr-list mbean-name]
rlm@10 47 (seq-contains-all? (jmx/attribute-names mbean-name) attr-list)
rlm@10 48 [:Verbose :ObjectPendingFinalizationCount :HeapMemoryUsage :NonHeapMemoryUsage] "java.lang:type=Memory")
rlm@10 49 (are [op-list mbean-name]
rlm@10 50 (seq-contains-all? (jmx/operation-names mbean-name) op-list)
rlm@10 51 [:gc] "java.lang:type=Memory"))
rlm@10 52 (testing "mbean-from-oname"
rlm@10 53 (are [key-names oname]
rlm@10 54 (seq-contains-all? (keys (jmx/mbean oname)) key-names)
rlm@10 55 [:Verbose :ObjectPendingFinalizationCount :HeapMemoryUsage :NonHeapMemoryUsage] "java.lang:type=Memory")))
rlm@10 56
rlm@10 57 (deftest raw-reading-attributes
rlm@10 58 (let [mem "java.lang:type=Memory"
rlm@10 59 log "java.util.logging:type=Logging"]
rlm@10 60 (testing "simple scalar attributes"
rlm@10 61 (are [a b] (= a b)
rlm@10 62 false (jmx/raw-read mem :Verbose))
rlm@10 63 (are [type attr] (instance? type attr)
rlm@10 64 Number (jmx/raw-read mem :ObjectPendingFinalizationCount)))))
rlm@10 65
rlm@10 66 (deftest reading-attributes
rlm@10 67 (testing "simple scalar attributes"
rlm@10 68 (are [type attr] (instance? type attr)
rlm@10 69 Number (jmx/read "java.lang:type=Memory" :ObjectPendingFinalizationCount)))
rlm@10 70 (testing "composite attributes"
rlm@10 71 (are [ks attr] (=set ks (keys attr))
rlm@10 72 [:used :max :init :committed] (jmx/read "java.lang:type=Memory" :HeapMemoryUsage)))
rlm@10 73 (testing "tabular attributes"
rlm@10 74 (is (map? (jmx/read "java.lang:type=Runtime" :SystemProperties)))))
rlm@10 75
rlm@10 76 (deftest writing-attributes
rlm@10 77 (let [mem "java.lang:type=Memory"]
rlm@10 78 (jmx/write! mem :Verbose true)
rlm@10 79 (is (true? (jmx/raw-read mem :Verbose)))
rlm@10 80 (jmx/write! mem :Verbose false)))
rlm@10 81
rlm@10 82 (deftest test-invoke-operations
rlm@10 83 (testing "without arguments"
rlm@10 84 (jmx/invoke "java.lang:type=Memory" :gc))
rlm@10 85 (testing "with arguments"
rlm@10 86 (.addLogger (LogManager/getLogManager) (Logger/getLogger "clojure.contrib.test_contrib.test_jmx"))
rlm@10 87 (jmx/invoke "java.util.logging:type=Logging" :setLoggerLevel "clojure.contrib.test_contrib.test_jmx" "WARNING")))
rlm@10 88
rlm@10 89 (deftest test-jmx->clj
rlm@10 90 (testing "it works recursively on maps"
rlm@10 91 (let [some-map {:foo (jmx/raw-read "java.lang:type=Memory" :HeapMemoryUsage)}]
rlm@10 92 (is (map? (:foo (jmx/jmx->clj some-map))))))
rlm@10 93 (testing "it leaves everything else untouched"
rlm@10 94 (is (= "foo" (jmx/jmx->clj "foo")))))
rlm@10 95
rlm@10 96
rlm@10 97 (deftest test-composite-data->map
rlm@10 98 (let [data (jmx/raw-read "java.lang:type=Memory" :HeapMemoryUsage)
rlm@10 99 prox (jmx/composite-data->map data)]
rlm@10 100 (testing "returns a map with keyword keys"
rlm@10 101 (is (= (set [:committed :init :max :used]) (set (keys prox)))))))
rlm@10 102
rlm@10 103 (deftest test-tabular-data->map
rlm@10 104 (let [raw-props (jmx/raw-read "java.lang:type=Runtime" :SystemProperties)
rlm@10 105 props (jmx/tabular-data->map raw-props)]
rlm@10 106 (are [k] (contains? props k)
rlm@10 107 :java.class.path
rlm@10 108 :path.separator)))
rlm@10 109
rlm@10 110 (deftest test-creating-attribute-infos
rlm@10 111 (let [infos (jmx/map->attribute-infos [[:a 1] [:b 2]])
rlm@10 112 info (first infos)]
rlm@10 113 (testing "generates the right class"
rlm@10 114 (is (= (class (into-array MBeanAttributeInfo [])) (class infos))))
rlm@10 115 (testing "generates the right instance data"
rlm@10 116 (are [result expr] (= result expr)
rlm@10 117 "a" (.getName info)
rlm@10 118 "a" (.getDescription info)))))
rlm@10 119
rlm@10 120 (deftest various-beans-are-readable
rlm@10 121 (testing "that all java.lang beans can be read without error"
rlm@10 122 (doseq [mb (jmx/mbean-names "*:*")]
rlm@10 123 (is (map? (jmx/mbean mb)) mb))))
rlm@10 124
rlm@10 125 (deftest test-jmx-url
rlm@10 126 (testing "creates default url"
rlm@10 127 (is (= "service:jmx:rmi:///jndi/rmi://localhost:3000/jmxrmi"
rlm@10 128 (jmx/jmx-url))))
rlm@10 129 (testing "creates custom url"
rlm@10 130 (is (= "service:jmx:rmi:///jndi/rmi://example.com:4000/jmxrmi"
rlm@10 131 (jmx/jmx-url {:host "example.com" :port 4000}))))
rlm@10 132 (testing "creates custom jndi path"
rlm@10 133 (is (= "service:jmx:rmi:///jndi/rmi://example.com:4000/jmxconnector"
rlm@10 134 (jmx/jmx-url {:host "example.com" :port 4000 :jndi-path "jmxconnector"})))))
rlm@10 135
rlm@10 136 ;; ----------------------------------------------------------------------
rlm@10 137 ;; tests for clojure.contrib.jmx.Bean.
rlm@10 138
rlm@10 139 (deftest dynamic-mbean-from-compiled-class
rlm@10 140 (let [mbean-name "clojure.contrib.test_contrib.test_jmx:name=Foo"]
rlm@10 141 (jmx/register-mbean
rlm@10 142 (Bean.
rlm@10 143 (ref {:string-attribute "a-string"}))
rlm@10 144 mbean-name)
rlm@10 145 (are [result expr] (= result expr)
rlm@10 146 "a-string" (jmx/read mbean-name :string-attribute)
rlm@10 147 {:string-attribute "a-string"} (jmx/mbean mbean-name)
rlm@10 148 )))
rlm@10 149
rlm@10 150 (deftest test-getAttribute
rlm@10 151 (doseq [reftype [ref atom agent]]
rlm@10 152 (let [state (reftype {:a 1 :b 2})
rlm@10 153 bean (Bean. state)]
rlm@10 154 (testing (str "accessing values from a " (class state))
rlm@10 155 (are [result expr] (= result expr)
rlm@10 156 1 (.getAttribute bean "a"))))))
rlm@10 157
rlm@10 158 (deftest test-bean-info
rlm@10 159 (let [state (ref {:a 1 :b 2})
rlm@10 160 bean (Bean. state)
rlm@10 161 info (.getMBeanInfo bean)]
rlm@10 162 (testing "accessing info"
rlm@10 163 (are [result expr] (= result expr)
rlm@10 164 "clojure.contrib.jmx.Bean" (.getClassName info)))))
rlm@10 165
rlm@10 166 (deftest test-getAttributes
rlm@10 167 (let [bean (Bean. (ref {:r 5 :d 4}))
rlm@10 168 atts (.getAttributes bean (into-array ["r" "d"]))]
rlm@10 169 (are [x y] (= x y)
rlm@10 170 AttributeList (class atts)
rlm@10 171 [5 4] (seq atts))))
rlm@10 172
rlm@10 173 (deftest test-guess-attribute-typename
rlm@10 174 (are [x y] (= x (jmx/guess-attribute-typename y))
rlm@10 175 ; "long" 10
rlm@10 176 "boolean" false
rlm@10 177 "java.lang.String" "foo"
rlm@10 178 "long" (Long/valueOf (long 10))))