001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package test.javax.management;
010:
011: import java.lang.reflect.Constructor;
012: import java.lang.reflect.Method;
013: import java.util.ArrayList;
014: import java.util.Arrays;
015: import java.util.List;
016:
017: import mx4j.MX4JSystemKeys;
018: import mx4j.server.MBeanIntrospector;
019: import mx4j.server.MBeanMetaData;
020: import test.MX4JTestCase;
021: import test.javax.management.support.ComplianceSupport;
022:
023: import javax.management.MBeanConstructorInfo;
024:
025: /**
026: * @version $Revision: 1.10 $
027: */
028: public class MBeanComplianceTest extends MX4JTestCase {
029: private MBeanIntrospector introspector;
030:
031: public MBeanComplianceTest(String s) {
032: super (s);
033: }
034:
035: protected void setUp() throws Exception {
036: String property = MX4JSystemKeys.MX4J_STRICT_MBEAN_INTERFACE;
037: System.setProperty(property, "no");
038: introspector = new MBeanIntrospector();
039: }
040:
041: protected void tearDown() throws Exception {
042: introspector = null;
043: }
044:
045: private MBeanMetaData createMBeanMetaData(Object mbean)
046: throws Exception {
047: MBeanMetaData metadata = MBeanMetaData.Factory.create();
048: metadata.setMBean(mbean);
049: metadata.setClassLoader(mbean.getClass().getClassLoader());
050: return metadata;
051: }
052:
053: private boolean isCompliant(Object mbean) throws Exception {
054: Object metadata = createMBeanMetaData(mbean);
055: Method method = introspector.getClass().getDeclaredMethod(
056: "testCompliance", new Class[] { MBeanMetaData.class });
057: method.setAccessible(true);
058: Boolean value = (Boolean) method.invoke(introspector,
059: new Object[] { metadata });
060: return value.booleanValue();
061: }
062:
063: private boolean isStandardCompliant(Object mbean) throws Exception {
064: MBeanMetaData metadata = createMBeanMetaData(mbean);
065: Method method = introspector.getClass().getDeclaredMethod(
066: "testCompliance", new Class[] { MBeanMetaData.class });
067: method.setAccessible(true);
068: Boolean value = (Boolean) method.invoke(introspector,
069: new Object[] { metadata });
070: return value.booleanValue() && metadata.isMBeanStandard()
071: && !metadata.isMBeanDynamic()
072: && metadata.getMBeanInterface() != null;
073: }
074:
075: private boolean isDynamicCompliant(Object mbean) throws Exception {
076: MBeanMetaData metadata = createMBeanMetaData(mbean);
077: Method method = introspector.getClass().getDeclaredMethod(
078: "testCompliance", new Class[] { MBeanMetaData.class });
079: method.setAccessible(true);
080: Boolean value = (Boolean) method.invoke(introspector,
081: new Object[] { metadata });
082: return value.booleanValue() && !metadata.isMBeanStandard()
083: && metadata.isMBeanDynamic();
084: }
085:
086: private Class getStandardManagementInterface(Object mbean)
087: throws Exception {
088: MBeanMetaData metadata = createMBeanMetaData(mbean);
089: introspector.introspect(metadata);
090: return metadata.getMBeanInterface();
091: }
092:
093: private Method[] getStandardManagementMethods(Object mbean)
094: throws Exception {
095: Class intf = getStandardManagementInterface(mbean);
096: return intf.getMethods();
097: }
098:
099: public void testNotCompliantNoManagement() throws Exception {
100: Object bad = new ComplianceSupport.NoManagement();
101: if (isCompliant(bad))
102: fail("MBean is not compliant");
103: }
104:
105: public void testNotCompliantLexicalPatternNotRespected()
106: throws Exception {
107: Object bad = new ComplianceSupport.DoesntRespectLexicalPattern();
108: if (isCompliant(bad))
109: fail("MBean is not compliant");
110: }
111:
112: public void testNotCompliantOverloadedAttributeSetSet()
113: throws Exception {
114: Object bad = new ComplianceSupport.OverloadedAttributeSetSet();
115: if (isCompliant(bad))
116: fail("MBean is not compliant");
117: }
118:
119: public void testNotCompliantOverloadedAttributeGetGet()
120: throws Exception {
121: // This is guaranteed by the Java compiler: a class with 2 getters that return different types
122: // does not compile
123: }
124:
125: public void testNotCompliantOverloadedAttribute3() throws Exception {
126: Object bad = new ComplianceSupport.OverloadedAttributeGetSet();
127: if (isCompliant(bad))
128: fail("MBean is not compliant");
129: }
130:
131: public void testNotCompliantOverloadedAttribute4() throws Exception {
132: Object bad = new ComplianceSupport.OverloadedAttributeIsGet();
133: if (isCompliant(bad))
134: fail("MBean is not compliant");
135: }
136:
137: public void testCompliantBasicStandard() throws Exception {
138: Object good = new ComplianceSupport.BasicStandard();
139: if (!isStandardCompliant(good))
140: fail("MBean is compliant");
141:
142: Method[] methods = ComplianceSupport.BasicStandardMBean.class
143: .getMethods();
144: List list = Arrays.asList(methods);
145: Method[] management = getStandardManagementMethods(good);
146: List list2 = Arrays.asList(management);
147: assertTrue("Different management interface", list
148: .containsAll(list2)
149: && list2.containsAll(list));
150: }
151:
152: public void testCompliantDerived() throws Exception {
153: Object good = new ComplianceSupport.Derived();
154: if (!isStandardCompliant(good))
155: fail("MBean is compliant");
156:
157: Method[] methods = ComplianceSupport.BasicStandardMBean.class
158: .getMethods();
159: List list = Arrays.asList(methods);
160: Method[] management = getStandardManagementMethods(good);
161: List list2 = Arrays.asList(management);
162: assertTrue("Different management interface", list
163: .containsAll(list2)
164: && list2.containsAll(list));
165: }
166:
167: public void testCompliantInherited() throws Exception {
168: Object good = new ComplianceSupport.Inherited();
169: if (!isStandardCompliant(good))
170: fail("MBean is compliant");
171:
172: Method[] methods = ComplianceSupport.InheritedMBean.class
173: .getMethods();
174: List list = Arrays.asList(methods);
175: Method[] management = getStandardManagementMethods(good);
176: List list2 = Arrays.asList(management);
177: assertTrue("Different management interface", list
178: .containsAll(list2)
179: && list2.containsAll(list));
180: }
181:
182: public void testCompliantNotInherited() throws Exception {
183: Object good = new ComplianceSupport.NotInherited();
184: if (!isStandardCompliant(good))
185: fail("MBean is compliant");
186:
187: Method[] methods = ComplianceSupport.BasicStandardMBean.class
188: .getMethods();
189: List list = Arrays.asList(methods);
190: Method[] management = getStandardManagementMethods(good);
191: List list2 = Arrays.asList(management);
192: assertTrue("Different management interface", list
193: .containsAll(list2)
194: && list2.containsAll(list));
195: }
196:
197: public void testCompliantMulti() throws Exception {
198: Object good = new ComplianceSupport.Multi();
199: if (!isStandardCompliant(good))
200: fail("MBean is compliant");
201:
202: Method[] methods = ComplianceSupport.BasicStandardMBean.class
203: .getMethods();
204: List list = new ArrayList();
205: list.addAll(Arrays.asList(methods));
206: methods = ComplianceSupport.InheritedMBean.class.getMethods();
207: list.addAll(Arrays.asList(methods));
208: Method[] management = getStandardManagementMethods(good);
209: List list2 = Arrays.asList(management);
210: assertTrue("Different management interface", list
211: .containsAll(list2)
212: && list2.containsAll(list));
213: }
214:
215: public void testCompliantPackagePrivate() throws Exception {
216: String clsName = "test.javax.management.support.ComplianceSupport$PackagePrivate";
217: Class cls = getClass().getClassLoader().loadClass(clsName);
218: Constructor ctor = cls.getDeclaredConstructor(new Class[0]);
219: ctor.setAccessible(true);
220: Object good = ctor.newInstance(new Object[0]);
221: if (!isStandardCompliant(good))
222: fail("MBean is compliant");
223: }
224:
225: public void testNotCompliantDynamicNoClassName() throws Exception {
226: // In JMX 1.2 it is not possible to create an MBeanInfo with null class name
227: Object mbean = new ComplianceSupport.NoClassNameDynamicMBean();
228: if (isCompliant(mbean))
229: fail();
230: }
231:
232: public void testCompliantBasicDynamic() throws Exception {
233: Object mbean = new ComplianceSupport.BasicDynamic();
234: if (!isDynamicCompliant(mbean))
235: fail("MBean is compliant");
236: }
237:
238: public void testCompliantStandardAndDynamic() throws Exception {
239: // JMX 1.0, this is invalid. For JMX 1.1 this is a dynamic MBean
240: Object mbean = new ComplianceSupport.StandardAndDynamic();
241: if (!isDynamicCompliant(mbean))
242: fail("MBean is compliant");
243:
244: Class intf = getStandardManagementInterface(mbean);
245: if (intf != null)
246: fail("MBean is dynamic");
247: }
248:
249: public void testCompliantStandardDynamic() throws Exception {
250: // In JMX 1.0 this is an invalid MBean; in JMX 1.1 is a dynamic MBean
251: Object mbean = new ComplianceSupport.StandardDynamic();
252: if (!isDynamicCompliant(mbean))
253: fail("MBean is compliant");
254:
255: Class intf = getStandardManagementInterface(mbean);
256: if (intf != null)
257: fail("MBean is dynamic");
258: }
259:
260: public void testDynamicFromStandard() throws Exception {
261: // A standard mbean subclassed to be dynamic
262: Object mbean = new ComplianceSupport.DynamicFromStandard();
263: if (!isDynamicCompliant(mbean))
264: fail("MBean is compliant");
265:
266: Class intf = getStandardManagementInterface(mbean);
267: if (intf != null)
268: fail("MBean is dynamic");
269: }
270:
271: public void testStandardFromDynamic() throws Exception {
272: // A dynamic mbean subclassed to be standard, it remains a dynamic (as of JMX 1.1)
273: Object mbean = new ComplianceSupport.StandardFromDynamic();
274: if (!isDynamicCompliant(mbean))
275: fail("MBean is compliant");
276:
277: Class intf = getStandardManagementInterface(mbean);
278: if (intf != null)
279: fail("MBean is dynamic");
280: }
281:
282: public void testStandardConstructorInfo() throws Exception {
283: Object mbean = new ComplianceSupport.BasicStandard();
284: MBeanMetaData md = createMBeanMetaData(mbean);
285: introspector.introspect(md);
286: MBeanConstructorInfo[] constructors = md.getMBeanInfo()
287: .getConstructors();
288: assertEquals(1, constructors.length);
289: MBeanConstructorInfo info = constructors[0];
290: assertEquals(mbean.getClass().getName(), info.getName());
291: }
292: }
|