diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/test_contrib/test_jmx.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,178 @@
     1.4 +;; Tests for JMX support for Clojure (see also clojure/contrib/jmx.clj)
     1.5 +
     1.6 +;; by Stuart Halloway
     1.7 +
     1.8 +;; Copyright (c) Stuart Halloway, 2009. All rights reserved.  The use
     1.9 +;; and distribution terms for this software are covered by the Eclipse
    1.10 +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
    1.11 +;; which can be found in the file epl-v10.html at the root of this
    1.12 +;; distribution.  By using this software in any fashion, you are
    1.13 +;; agreeing to be bound by the terms of this license.  You must not
    1.14 +;; remove this notice, or any other, from this software.
    1.15 +
    1.16 +(ns clojure.contrib.test-jmx
    1.17 +  (:import javax.management.openmbean.CompositeDataSupport
    1.18 +           [javax.management MBeanAttributeInfo AttributeList]
    1.19 +           [java.util.logging LogManager Logger]
    1.20 +           clojure.contrib.jmx.Bean)
    1.21 +  (:use clojure.test)
    1.22 +  (:require [clojure.contrib [jmx :as jmx]]))
    1.23 +
    1.24 +
    1.25 +(defn =set [a b]
    1.26 +  (= (set a) (set b)))
    1.27 +
    1.28 +(defn seq-contains-all?
    1.29 +  "Does container contain every item in containee?
    1.30 +   Not fast. Testing use only"
    1.31 +  [container containee]
    1.32 +  (let [container (set container)]
    1.33 +    (every? #(contains? container %) containee)))
    1.34 +
    1.35 +(deftest finding-mbeans
    1.36 +  (testing "as-object-name"
    1.37 +           (are [cname object-name]
    1.38 +                (= cname (.getCanonicalName object-name))
    1.39 +                "java.lang:type=Memory" (jmx/as-object-name "java.lang:type=Memory")))
    1.40 +  (testing "mbean-names"
    1.41 +           (are [cnames object-name]
    1.42 +                (= cnames (map #(.getCanonicalName %) object-name))
    1.43 +                ["java.lang:type=Memory"] (jmx/mbean-names "java.lang:type=Memory"))))
    1.44 +
    1.45 +; These actual beans may differ on different JVM platforms.
    1.46 +; Tested April 2010 to work on Sun and IBM JDKs.
    1.47 +(deftest testing-actual-beans
    1.48 +  (testing "reflecting on capabilities"
    1.49 +    (are [attr-list mbean-name]
    1.50 +         (seq-contains-all? (jmx/attribute-names mbean-name) attr-list)
    1.51 +         [:Verbose :ObjectPendingFinalizationCount :HeapMemoryUsage :NonHeapMemoryUsage] "java.lang:type=Memory")
    1.52 +    (are [op-list mbean-name]
    1.53 +         (seq-contains-all? (jmx/operation-names mbean-name) op-list)
    1.54 +         [:gc] "java.lang:type=Memory"))
    1.55 +  (testing "mbean-from-oname"
    1.56 +    (are [key-names oname]
    1.57 +         (seq-contains-all? (keys (jmx/mbean oname)) key-names)
    1.58 +         [:Verbose :ObjectPendingFinalizationCount :HeapMemoryUsage :NonHeapMemoryUsage]  "java.lang:type=Memory")))
    1.59 +
    1.60 +(deftest raw-reading-attributes
    1.61 +  (let [mem "java.lang:type=Memory"
    1.62 +        log "java.util.logging:type=Logging"]
    1.63 +    (testing "simple scalar attributes"
    1.64 +             (are [a b] (= a b)
    1.65 +                  false (jmx/raw-read mem :Verbose))
    1.66 +             (are [type attr] (instance? type attr)
    1.67 +                  Number (jmx/raw-read mem :ObjectPendingFinalizationCount)))))
    1.68 +
    1.69 +(deftest reading-attributes
    1.70 +  (testing "simple scalar attributes"
    1.71 +           (are [type attr] (instance? type attr)
    1.72 +                Number (jmx/read "java.lang:type=Memory" :ObjectPendingFinalizationCount)))
    1.73 +  (testing "composite attributes"
    1.74 +           (are [ks attr] (=set ks (keys attr))
    1.75 +                [:used :max :init :committed] (jmx/read "java.lang:type=Memory" :HeapMemoryUsage)))
    1.76 +  (testing "tabular attributes"
    1.77 +           (is (map? (jmx/read "java.lang:type=Runtime" :SystemProperties)))))
    1.78 +
    1.79 +(deftest writing-attributes
    1.80 +  (let [mem "java.lang:type=Memory"]
    1.81 +    (jmx/write! mem :Verbose true)
    1.82 +    (is (true? (jmx/raw-read mem :Verbose)))
    1.83 +    (jmx/write! mem :Verbose false)))
    1.84 +
    1.85 +(deftest test-invoke-operations
    1.86 +  (testing "without arguments"
    1.87 +           (jmx/invoke "java.lang:type=Memory" :gc))
    1.88 +  (testing "with arguments"
    1.89 +           (.addLogger (LogManager/getLogManager) (Logger/getLogger "clojure.contrib.test_contrib.test_jmx"))
    1.90 +           (jmx/invoke "java.util.logging:type=Logging" :setLoggerLevel "clojure.contrib.test_contrib.test_jmx" "WARNING")))
    1.91 +
    1.92 +(deftest test-jmx->clj
    1.93 +  (testing "it works recursively on maps"
    1.94 +           (let [some-map {:foo (jmx/raw-read "java.lang:type=Memory" :HeapMemoryUsage)}]
    1.95 +             (is (map? (:foo (jmx/jmx->clj some-map))))))
    1.96 +  (testing "it leaves everything else untouched"
    1.97 +           (is (= "foo" (jmx/jmx->clj "foo")))))
    1.98 +  
    1.99 +  
   1.100 +(deftest test-composite-data->map
   1.101 +  (let [data (jmx/raw-read "java.lang:type=Memory" :HeapMemoryUsage)
   1.102 +        prox (jmx/composite-data->map data)]
   1.103 +    (testing "returns a map with keyword keys"
   1.104 +             (is (= (set [:committed :init :max :used]) (set (keys prox)))))))
   1.105 +
   1.106 +(deftest test-tabular-data->map
   1.107 +  (let [raw-props (jmx/raw-read "java.lang:type=Runtime" :SystemProperties)
   1.108 +        props (jmx/tabular-data->map raw-props)]
   1.109 +    (are [k] (contains? props k)
   1.110 +         :java.class.path
   1.111 +         :path.separator)))
   1.112 +
   1.113 +(deftest test-creating-attribute-infos
   1.114 +  (let [infos (jmx/map->attribute-infos [[:a 1] [:b 2]])
   1.115 +        info (first infos)]
   1.116 +    (testing "generates the right class"
   1.117 +             (is (= (class (into-array MBeanAttributeInfo [])) (class infos))))
   1.118 +    (testing "generates the right instance data"
   1.119 +             (are [result expr] (= result expr)
   1.120 +                  "a" (.getName info)
   1.121 +                  "a" (.getDescription info)))))
   1.122 +
   1.123 +(deftest various-beans-are-readable
   1.124 +  (testing "that all java.lang beans can be read without error"
   1.125 +           (doseq [mb (jmx/mbean-names "*:*")]
   1.126 +             (is (map? (jmx/mbean mb)) mb))))
   1.127 +
   1.128 +(deftest test-jmx-url
   1.129 +  (testing "creates default url"
   1.130 +    (is (= "service:jmx:rmi:///jndi/rmi://localhost:3000/jmxrmi"
   1.131 +           (jmx/jmx-url))))
   1.132 +  (testing "creates custom url"
   1.133 +    (is (= "service:jmx:rmi:///jndi/rmi://example.com:4000/jmxrmi"
   1.134 +           (jmx/jmx-url {:host "example.com" :port 4000}))))
   1.135 +  (testing "creates custom jndi path"
   1.136 +    (is (= "service:jmx:rmi:///jndi/rmi://example.com:4000/jmxconnector"
   1.137 +           (jmx/jmx-url {:host "example.com" :port 4000 :jndi-path "jmxconnector"})))))
   1.138 +
   1.139 +;; ----------------------------------------------------------------------
   1.140 +;; tests for clojure.contrib.jmx.Bean.
   1.141 +
   1.142 +(deftest dynamic-mbean-from-compiled-class
   1.143 +  (let [mbean-name "clojure.contrib.test_contrib.test_jmx:name=Foo"]
   1.144 +    (jmx/register-mbean
   1.145 +     (Bean.
   1.146 +      (ref {:string-attribute "a-string"}))
   1.147 +     mbean-name)
   1.148 +    (are [result expr] (= result expr)
   1.149 +         "a-string" (jmx/read mbean-name :string-attribute)
   1.150 +         {:string-attribute "a-string"} (jmx/mbean mbean-name)
   1.151 +         )))
   1.152 +
   1.153 +(deftest test-getAttribute
   1.154 +  (doseq [reftype [ref atom agent]]
   1.155 +    (let [state (reftype {:a 1 :b 2})
   1.156 +          bean (Bean. state)]
   1.157 +      (testing (str "accessing values from a " (class state))
   1.158 +               (are [result expr] (= result expr)
   1.159 +                    1 (.getAttribute bean "a"))))))
   1.160 +
   1.161 +(deftest test-bean-info
   1.162 +  (let [state (ref {:a 1 :b 2})
   1.163 +        bean (Bean. state)
   1.164 +        info (.getMBeanInfo bean)]
   1.165 +    (testing "accessing info"
   1.166 +             (are [result expr] (= result expr)
   1.167 +                  "clojure.contrib.jmx.Bean" (.getClassName info)))))
   1.168 +
   1.169 +(deftest test-getAttributes
   1.170 +  (let [bean (Bean. (ref {:r 5 :d 4}))
   1.171 +        atts (.getAttributes bean (into-array ["r" "d"]))]
   1.172 +    (are [x y] (= x y)
   1.173 +         AttributeList (class atts)
   1.174 +         [5 4] (seq atts))))
   1.175 +
   1.176 +(deftest test-guess-attribute-typename
   1.177 +  (are [x y] (= x (jmx/guess-attribute-typename y))
   1.178 +;       "long" 10
   1.179 +       "boolean" false
   1.180 +       "java.lang.String" "foo"
   1.181 +       "long" (Long/valueOf (long 10))))