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