KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > export > assembler > InterfaceBasedMBeanInfoAssemblerMappedTests


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.export.assembler;
18
19 import java.util.Properties JavaDoc;
20
21 import javax.management.MBeanAttributeInfo JavaDoc;
22 import javax.management.modelmbean.ModelMBeanAttributeInfo JavaDoc;
23 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
24
25 /**
26  * @author Rob Harrop
27  */

28 public class InterfaceBasedMBeanInfoAssemblerMappedTests extends AbstractJmxAssemblerTests {
29
30     protected static final String JavaDoc OBJECT_NAME = "bean:name=testBean4";
31
32     public void testGetAgeIsReadOnly() throws Exception JavaDoc {
33         ModelMBeanInfo JavaDoc info = getMBeanInfoFromAssembler();
34         ModelMBeanAttributeInfo JavaDoc attr = info.getAttribute(AGE_ATTRIBUTE);
35
36         assertTrue("Age is not readable", attr.isReadable());
37         assertFalse("Age is not writable", attr.isWritable());
38     }
39
40     public void testWithUnknownClass() throws Exception JavaDoc {
41         try {
42             InterfaceBasedMBeanInfoAssembler assembler = getWithMapping("com.foo.bar.Unknown");
43             fail("Should have thrown IllegalArgumentException");
44         }
45         catch (IllegalArgumentException JavaDoc ex) {
46             // expected
47
}
48     }
49
50     public void testWithNonInterface() throws Exception JavaDoc {
51         try {
52             InterfaceBasedMBeanInfoAssembler assembler = getWithMapping("JmxTestBean");
53             fail("Should have thrown IllegalArgumentException");
54         }
55         catch (IllegalArgumentException JavaDoc ex) {
56             // expected
57
}
58     }
59
60     public void testWithFallThrough() throws Exception JavaDoc {
61         InterfaceBasedMBeanInfoAssembler assembler =
62                 getWithMapping("foobar", "org.springframework.jmx.export.assembler.ICustomJmxBean");
63         assembler.setManagedInterfaces(new Class JavaDoc[] {IAdditionalTestMethods.class});
64
65         ModelMBeanInfo JavaDoc inf = assembler.getMBeanInfo(getBean(), getObjectName());
66         MBeanAttributeInfo JavaDoc attr = inf.getAttribute("NickName");
67
68         assertNickName(attr);
69     }
70
71     public void testNickNameIsExposed() throws Exception JavaDoc {
72         ModelMBeanInfo JavaDoc inf = (ModelMBeanInfo JavaDoc) getMBeanInfo();
73         MBeanAttributeInfo JavaDoc attr = inf.getAttribute("NickName");
74
75         assertNickName(attr);
76     }
77
78     protected String JavaDoc getObjectName() {
79         return OBJECT_NAME;
80     }
81
82     protected int getExpectedOperationCount() {
83         return 7;
84     }
85
86     protected int getExpectedAttributeCount() {
87         return 3;
88     }
89
90     protected MBeanInfoAssembler getAssembler() throws Exception JavaDoc {
91         return getWithMapping(
92                 "org.springframework.jmx.export.assembler.IAdditionalTestMethods, " +
93                 "org.springframework.jmx.export.assembler.ICustomJmxBean");
94     }
95
96     protected String JavaDoc getApplicationContextPath() {
97         return "org/springframework/jmx/export/assembler/interfaceAssemblerMapped.xml";
98     }
99
100     private InterfaceBasedMBeanInfoAssembler getWithMapping(String JavaDoc mapping) {
101         return getWithMapping(OBJECT_NAME, mapping);
102     }
103
104     private InterfaceBasedMBeanInfoAssembler getWithMapping(String JavaDoc name, String JavaDoc mapping) {
105         InterfaceBasedMBeanInfoAssembler assembler = new InterfaceBasedMBeanInfoAssembler();
106         Properties JavaDoc props = new Properties JavaDoc();
107         props.setProperty(name, mapping);
108         assembler.setInterfaceMappings(props);
109         return assembler;
110     }
111
112     private void assertNickName(MBeanAttributeInfo JavaDoc attr) {
113         assertNotNull("Nick Name should not be null", attr);
114         assertTrue("Nick Name should be writable", attr.isWritable());
115         assertTrue("Nick Name should be readab;e", attr.isReadable());
116     }
117
118 }
119
Popular Tags