KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > access > MBeanClientInterceptorTests


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.access;
18
19 import java.beans.PropertyDescriptor JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.management.Descriptor JavaDoc;
25 import javax.management.MBeanServerConnection JavaDoc;
26
27 import org.springframework.jmx.AbstractJmxTests;
28 import org.springframework.jmx.IJmxTestBean;
29 import org.springframework.jmx.JmxTestBean;
30 import org.springframework.jmx.export.MBeanExporter;
31 import org.springframework.jmx.export.assembler.AbstractReflectiveMBeanInfoAssembler;
32
33 /**
34  * @author Rob Harrop
35  */

36 public class MBeanClientInterceptorTests extends AbstractJmxTests {
37
38     protected static final String JavaDoc OBJECT_NAME = "spring:test=proxy";
39
40     protected JmxTestBean target;
41
42     public void setUp() throws Exception JavaDoc {
43         super.setUp();
44
45         target = new JmxTestBean();
46         target.setAge(100);
47         target.setName("Rob Harrop");
48
49         MBeanExporter adapter = new MBeanExporter();
50         Map JavaDoc beans = new HashMap JavaDoc();
51         beans.put(OBJECT_NAME, target);
52         adapter.setServer(server);
53         adapter.setBeans(beans);
54         adapter.setAssembler(new ProxyTestAssembler());
55         adapter.afterPropertiesSet();
56     }
57
58     protected MBeanServerConnection JavaDoc getServerConnection() throws Exception JavaDoc {
59         return server;
60     }
61
62     protected IJmxTestBean getProxy() throws Exception JavaDoc {
63         MBeanProxyFactoryBean factory = new MBeanProxyFactoryBean();
64         factory.setServer(getServerConnection());
65         factory.setProxyInterface(IJmxTestBean.class);
66         factory.setObjectName(OBJECT_NAME);
67         factory.afterPropertiesSet();
68         return (IJmxTestBean) factory.getObject();
69     }
70
71     public void testProxyClassIsDifferent() throws Exception JavaDoc {
72         IJmxTestBean proxy = getProxy();
73         assertTrue("The proxy class should be different than the base class",
74                 (proxy.getClass() != IJmxTestBean.class));
75     }
76
77     public void testDifferentProxiesSameClass() throws Exception JavaDoc {
78         IJmxTestBean proxy1 = getProxy();
79         IJmxTestBean proxy2 = getProxy();
80
81         assertNotSame("The proxies should NOT be the same", proxy1, proxy2);
82         assertSame("The proxy classes should be the same", proxy1.getClass(), proxy2.getClass());
83     }
84
85     public void testGetAttributeValue() throws Exception JavaDoc {
86         IJmxTestBean proxy1 = getProxy();
87         int age = proxy1.getAge();
88         assertEquals("The age should be 100", 100, age);
89     }
90
91     public void testSetAttributeValue() throws Exception JavaDoc {
92         IJmxTestBean proxy = getProxy();
93         proxy.setName("Rob Harrop");
94         assertEquals("The name of the bean should have been updated", "Rob Harrop", target.getName());
95     }
96
97     public void testSetReadOnlyAttribute() throws Exception JavaDoc {
98         IJmxTestBean proxy = getProxy();
99         try {
100             proxy.setAge(900);
101             fail("Should not be able to write to a read only attribute");
102         }
103         catch (InvalidInvocationException ex) {
104             // success
105
}
106     }
107
108     public void testInvokeNoArgs() throws Exception JavaDoc {
109         IJmxTestBean proxy = getProxy();
110         long result = proxy.myOperation();
111         assertEquals("The operation should return 1", 1, result);
112     }
113
114     public void testInvokeArgs() throws Exception JavaDoc {
115         IJmxTestBean proxy = getProxy();
116         int result = proxy.add(1, 2);
117         assertEquals("The operation should return 3", 3, result);
118     }
119
120     public void testInvokeUnexposedMethodWithException() throws Exception JavaDoc {
121         IJmxTestBean bean = getProxy();
122         try {
123             bean.dontExposeMe();
124             fail("Method dontExposeMe should throw an exception");
125         }
126         catch (InvalidInvocationException desired) {
127             // success
128
}
129     }
130
131
132     private static class ProxyTestAssembler extends AbstractReflectiveMBeanInfoAssembler {
133
134         protected boolean includeReadAttribute(Method JavaDoc method, String JavaDoc beanKey) {
135             return true;
136         }
137
138         protected boolean includeWriteAttribute(Method JavaDoc method, String JavaDoc beanKey) {
139             if ("setAge".equals(method.getName())) {
140                 return false;
141             }
142             return true;
143         }
144
145         protected boolean includeOperation(Method JavaDoc method, String JavaDoc beanKey) {
146             if ("dontExposeMe".equals(method.getName())) {
147                 return false;
148             }
149             return true;
150         }
151
152         protected String JavaDoc getOperationDescription(Method JavaDoc method) {
153             return method.getName();
154         }
155
156         protected String JavaDoc getAttributeDescription(PropertyDescriptor JavaDoc propertyDescriptor) {
157             return propertyDescriptor.getDisplayName();
158         }
159
160         protected void populateAttributeDescriptor(Descriptor JavaDoc descriptor, Method JavaDoc getter, Method JavaDoc setter) {
161
162         }
163
164         protected void populateOperationDescriptor(Descriptor JavaDoc descriptor, Method JavaDoc method) {
165
166         }
167
168         protected String JavaDoc getDescription(String JavaDoc beanKey, Class JavaDoc beanClass) {
169             return "";
170         }
171
172         protected void populateMBeanDescriptor(Descriptor JavaDoc mbeanDescriptor, String JavaDoc beanKey, Class JavaDoc beanClass) {
173
174         }
175     }
176
177 }
178
Popular Tags