KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > support > JmxUtilsTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16
17 package org.springframework.jmx.support;
18
19 import java.beans.PropertyDescriptor JavaDoc;
20
21 import javax.management.DynamicMBean JavaDoc;
22 import javax.management.NotCompliantMBeanException JavaDoc;
23 import javax.management.StandardMBean JavaDoc;
24
25 import junit.framework.TestCase;
26
27 import org.springframework.beans.BeanWrapperImpl;
28 import org.springframework.jmx.IJmxTestBean;
29 import org.springframework.jmx.JmxTestBean;
30 import org.springframework.jmx.export.TestDynamicMBean;
31
32 /**
33  * @author Rob Harrop
34  */

35 public class JmxUtilsTests extends TestCase {
36
37     public void testIsMBeanWithDynamicMBean() throws Exception JavaDoc {
38         DynamicMBean JavaDoc mbean = new TestDynamicMBean();
39         assertTrue("Dynamic MBean not detected correctly", JmxUtils.isMBean(mbean.getClass()));
40     }
41
42     public void testIsMBeanWithStandardMBeanWrapper() throws Exception JavaDoc {
43         StandardMBean JavaDoc mbean = new StandardMBean JavaDoc(new JmxTestBean(), IJmxTestBean.class);
44         assertTrue("Standard MBean not detected correctly", JmxUtils.isMBean(mbean.getClass()));
45     }
46
47     public void testIsMBeanWithStandardMBeanInherited() throws Exception JavaDoc {
48         StandardMBean JavaDoc mbean = new StandardMBeanImpl();
49         assertTrue("Standard MBean not detected correctly", JmxUtils.isMBean(mbean.getClass()));
50     }
51
52     public void testNotAnMBean() throws Exception JavaDoc {
53         assertFalse("Object incorrectly identified as an MBean", JmxUtils.isMBean(Object JavaDoc.class));
54     }
55
56     public void testSimpleMBean() throws Exception JavaDoc {
57         Foo foo = new Foo();
58         assertTrue("Simple MBean not detected correctly", JmxUtils.isMBean(foo.getClass()));
59     }
60
61     public void testSimpleMBeanThroughInheritance() throws Exception JavaDoc {
62         Bar bar = new Bar();
63         Abc abc = new Abc();
64         assertTrue("Simple MBean (through inheritance) not detected correctly",
65                 JmxUtils.isMBean(bar.getClass()));
66         assertTrue("Simple MBean (through 2 levels of inheritance) not detected correctly",
67                 JmxUtils.isMBean(abc.getClass()));
68     }
69
70     public void testGetAttributeNameWithStrictCasing() {
71         PropertyDescriptor JavaDoc pd = new BeanWrapperImpl(AttributeTest.class).getPropertyDescriptor("name");
72         String JavaDoc attributeName = JmxUtils.getAttributeName(pd, true);
73         assertEquals("Incorrect casing on attribute name", "Name", attributeName);
74     }
75
76     public void testGetAttributeNameWithoutStrictCasing() {
77         PropertyDescriptor JavaDoc pd = new BeanWrapperImpl(AttributeTest.class).getPropertyDescriptor("name");
78         String JavaDoc attributeName = JmxUtils.getAttributeName(pd, false);
79         assertEquals("Incorrect casing on attribute name", "name", attributeName);
80     }
81
82
83     public static class AttributeTest {
84
85         private String JavaDoc name;
86
87         public String JavaDoc getName() {
88             return name;
89         }
90
91         public void setName(String JavaDoc name) {
92             this.name = name;
93         }
94     }
95
96
97     public static class StandardMBeanImpl extends StandardMBean JavaDoc implements IJmxTestBean {
98
99         public StandardMBeanImpl() throws NotCompliantMBeanException JavaDoc {
100             super(IJmxTestBean.class);
101         }
102
103         public int add(int x, int y) {
104             return 0;
105         }
106
107         public long myOperation() {
108             return 0;
109         }
110
111         public int getAge() {
112             return 0;
113         }
114
115         public void setAge(int age) {
116         }
117
118         public void setName(String JavaDoc name) {
119         }
120
121         public String JavaDoc getName() {
122             return null;
123         }
124
125         public void dontExposeMe() {
126         }
127     }
128
129
130     public static interface FooMBean {
131
132         String JavaDoc getName();
133     }
134
135
136     public static class Foo implements FooMBean {
137
138         public String JavaDoc getName() {
139             return "Rob Harrop";
140         }
141     }
142
143
144     public static class Bar extends Foo {
145
146     }
147
148
149     public static class Abc extends Bar {
150
151     }
152
153 }
154
Popular Tags