KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.management.Descriptor JavaDoc;
23 import javax.management.MBeanInfo JavaDoc;
24 import javax.management.MBeanParameterInfo JavaDoc;
25 import javax.management.modelmbean.ModelMBeanAttributeInfo JavaDoc;
26 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
27 import javax.management.modelmbean.ModelMBeanOperationInfo JavaDoc;
28
29 import org.springframework.aop.framework.ProxyFactory;
30 import org.springframework.aop.interceptor.NopInterceptor;
31 import org.springframework.jmx.IJmxTestBean;
32 import org.springframework.jmx.JmxTestBean;
33 import org.springframework.jmx.export.MBeanExporter;
34 import org.springframework.jmx.export.metadata.JmxAttributeSource;
35 import org.springframework.jmx.support.ObjectNameManager;
36
37 /**
38  * @author Rob Harrop
39  */

40 public abstract class AbstractMetadataAssemblerTests extends AbstractJmxAssemblerTests {
41
42     public void testDescription() throws Exception JavaDoc {
43         ModelMBeanInfo JavaDoc info = getMBeanInfoFromAssembler();
44         assertEquals("The descriptions are not the same", "My Managed Bean",
45                 info.getDescription());
46     }
47
48     public void testAttributeDescriptionOnSetter() throws Exception JavaDoc {
49         ModelMBeanInfo JavaDoc inf = getMBeanInfoFromAssembler();
50         ModelMBeanAttributeInfo JavaDoc attr = inf.getAttribute(AGE_ATTRIBUTE);
51         assertEquals("The description for the age attribute is incorrect",
52                 "The Age Attribute", attr.getDescription());
53     }
54
55     public void testAttributeDescriptionOnGetter() throws Exception JavaDoc {
56         ModelMBeanInfo JavaDoc inf = getMBeanInfoFromAssembler();
57         ModelMBeanAttributeInfo JavaDoc attr = inf.getAttribute(NAME_ATTRIBUTE);
58         assertEquals("The description for the name attribute is incorrect",
59                 "The Name Attribute", attr.getDescription());
60     }
61
62     /**
63      * Tests the situation where the attribute is only defined on the getter.
64      */

65     public void testReadOnlyAttribute() throws Exception JavaDoc {
66         ModelMBeanInfo JavaDoc inf = getMBeanInfoFromAssembler();
67         ModelMBeanAttributeInfo JavaDoc attr = inf.getAttribute(AGE_ATTRIBUTE);
68         assertFalse("The age attribute should not be writable", attr.isWritable());
69     }
70
71     public void testReadWriteAttribute() throws Exception JavaDoc {
72         ModelMBeanInfo JavaDoc inf = getMBeanInfoFromAssembler();
73         ModelMBeanAttributeInfo JavaDoc attr = inf.getAttribute(NAME_ATTRIBUTE);
74         assertTrue("The name attribute should be writable", attr.isWritable());
75         assertTrue("The name attribute should be readable", attr.isReadable());
76     }
77
78     /**
79      * Tests the situation where the property only has a getter.
80      */

81     public void testWithOnlyGetter() throws Exception JavaDoc {
82         ModelMBeanInfo JavaDoc inf = getMBeanInfoFromAssembler();
83         ModelMBeanAttributeInfo JavaDoc attr = inf.getAttribute("NickName");
84         assertNotNull("Attribute should not be null", attr);
85     }
86
87     /**
88      * Tests the situation where the property only has a setter.
89      */

90     public void testWithOnlySetter() throws Exception JavaDoc {
91         ModelMBeanInfo JavaDoc info = getMBeanInfoFromAssembler();
92         ModelMBeanAttributeInfo JavaDoc attr = info.getAttribute("Superman");
93         assertNotNull("Attribute should not be null", attr);
94     }
95
96     public void testMBeanDescriptor() throws Exception JavaDoc {
97         ModelMBeanInfo JavaDoc info = getMBeanInfoFromAssembler();
98         Descriptor JavaDoc desc = info.getMBeanDescriptor();
99
100         assertEquals("Logging should be set to true", "true", desc.getFieldValue("log"));
101         assertEquals("Log file should be jmx.log", "jmx.log", desc.getFieldValue("logFile"));
102         assertEquals("Currency Time Limit should be 15", "15", desc.getFieldValue("currencyTimeLimit"));
103         assertEquals("Persist Policy should be OnUpdate", "OnUpdate", desc.getFieldValue("persistPolicy"));
104         assertEquals("Persist Period should be 200", "200", desc.getFieldValue("persistPeriod"));
105         assertEquals("Persist Location should be foo", "./foo", desc.getFieldValue("persistLocation"));
106         assertEquals("Persist Name should be bar", "bar.jmx", desc.getFieldValue("persistName"));
107     }
108
109     public void testAgeAttributeDescriptor() throws Exception JavaDoc {
110         ModelMBeanInfo JavaDoc info = getMBeanInfoFromAssembler();
111         Descriptor JavaDoc desc = info.getAttribute(AGE_ATTRIBUTE).getDescriptor();
112
113         assertEquals("Currency Time Limit should be 15", "15", desc.getFieldValue("currencyTimeLimit"));
114         assertEquals("Persist Policy should be inherited", null, desc.getFieldValue("persistPolicy"));
115         assertEquals("Persist Period should be inherited", null, desc.getFieldValue("persistPeriod"));
116     }
117
118     public void testNameAttributeDescriptor() throws Exception JavaDoc {
119         ModelMBeanInfo JavaDoc info = getMBeanInfoFromAssembler();
120         Descriptor JavaDoc desc = info.getAttribute(NAME_ATTRIBUTE).getDescriptor();
121
122         assertEquals("Default value should be foo", "foo", desc.getFieldValue("default"));
123         assertEquals("Currency Time Limit should be 20", "20", desc.getFieldValue("currencyTimeLimit"));
124         assertEquals("Persist Policy should be OnUpdate", "OnUpdate", desc.getFieldValue("persistPolicy"));
125         assertEquals("Persist Period should be 300", "300", desc.getFieldValue("persistPeriod"));
126     }
127
128     public void testOperationDescriptor() throws Exception JavaDoc {
129         ModelMBeanInfo JavaDoc info = getMBeanInfoFromAssembler();
130         Descriptor JavaDoc desc = info.getOperation("myOperation").getDescriptor();
131
132         assertEquals("Currency Time Limit should be 30", "30", desc.getFieldValue("currencyTimeLimit"));
133         assertEquals("Role should be \"operation\"", "operation", desc.getFieldValue("role"));
134     }
135
136     public void testOperationParameterMetadata() throws Exception JavaDoc {
137         ModelMBeanInfo JavaDoc info = getMBeanInfoFromAssembler();
138         ModelMBeanOperationInfo JavaDoc oper = info.getOperation("add");
139         MBeanParameterInfo JavaDoc[] params = oper.getSignature();
140
141         assertEquals("Invalid number of params", 2, params.length);
142         assertEquals("Incorrect name for x param", "x", params[0].getName());
143         assertEquals("Incorrect type for x param", int.class.getName(), params[0].getType());
144         
145         assertEquals("Incorrect name for y param", "y", params[1].getName());
146         assertEquals("Incorrect type for y param", int.class.getName(), params[1].getType());
147     }
148
149     public void testWithCglibProxy() throws Exception JavaDoc {
150         IJmxTestBean tb = createJmxTestBean();
151         ProxyFactory pf = new ProxyFactory();
152         pf.setTarget(tb);
153         pf.addAdvice(new NopInterceptor());
154         Object JavaDoc proxy = pf.getProxy();
155
156         MetadataMBeanInfoAssembler assembler = (MetadataMBeanInfoAssembler) getAssembler();
157
158         MBeanExporter exporter = new MBeanExporter();
159         exporter.setBeanFactory(getContext());
160         exporter.setAssembler(assembler);
161
162         String JavaDoc objectName = "spring:bean=test,proxy=true";
163
164         Map JavaDoc beans = new HashMap JavaDoc();
165         beans.put(objectName, proxy);
166         exporter.setBeans(beans);
167         exporter.afterPropertiesSet();
168
169         MBeanInfo JavaDoc inf = server.getMBeanInfo(ObjectNameManager.getInstance(objectName));
170         assertEquals("Incorrect number of operations", getExpectedOperationCount(), inf.getOperations().length);
171         assertEquals("Incorrect number of attributes", getExpectedAttributeCount(), inf.getAttributes().length);
172
173         assertTrue("Not included in autodetection", assembler.includeBean(proxy.getClass(), "some bean name"));
174     }
175
176     public void testWithJdkProxy() throws Exception JavaDoc {
177         IJmxTestBean tb = createJmxTestBean();
178         ProxyFactory pf = new ProxyFactory();
179         pf.setTarget(tb);
180         pf.addInterface(IJmxTestBean.class);
181         pf.addAdvice(new NopInterceptor());
182         Object JavaDoc proxy = pf.getProxy();
183
184         MetadataMBeanInfoAssembler assembler = (MetadataMBeanInfoAssembler) getAssembler();
185
186         MBeanExporter exporter = new MBeanExporter();
187         exporter.setBeanFactory(getContext());
188         exporter.setAssembler(assembler);
189
190         String JavaDoc objectName = "spring:bean=test,proxy=true";
191
192         Map JavaDoc beans = new HashMap JavaDoc();
193         beans.put(objectName, proxy);
194         exporter.setBeans(beans);
195
196         try {
197             exporter.afterPropertiesSet();
198             fail("Should have thrown an IllegalArgumentException");
199         }
200         catch (IllegalArgumentException JavaDoc ex) {
201             // expected
202
}
203     }
204
205     protected abstract String JavaDoc getObjectName();
206
207     protected int getExpectedAttributeCount() {
208         return 4;
209     }
210
211     protected int getExpectedOperationCount() {
212         return 7;
213     }
214
215     protected IJmxTestBean createJmxTestBean() {
216         return new JmxTestBean();
217     }
218
219     protected MBeanInfoAssembler getAssembler() {
220         MetadataMBeanInfoAssembler assembler = new MetadataMBeanInfoAssembler();
221         assembler.setAttributeSource(getAttributeSource());
222         return assembler;
223     }
224
225     protected abstract JmxAttributeSource getAttributeSource();
226
227 }
228
Popular Tags