01: /*
02: * Copyright (C) The MX4J Contributors.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the MX4J License version 1.0.
06: * See the terms of the MX4J License in the documentation provided with this software.
07: */
08:
09: package test.javax.management.modelmbean;
10:
11: import java.lang.reflect.Constructor;
12: import javax.management.Descriptor;
13: import javax.management.MBeanParameterInfo;
14: import javax.management.RuntimeOperationsException;
15: import javax.management.modelmbean.DescriptorSupport;
16: import javax.management.modelmbean.ModelMBeanConstructorInfo;
17:
18: import junit.framework.TestCase;
19: import junit.textui.TestRunner;
20:
21: /**
22: * @version $Revision: 1.5 $
23: */
24: public class ModelMBeanConstructorInfoTest extends TestCase {
25: public static class Bob {
26: public Bob(String address) {
27: }
28: }
29:
30: public static void main(String[] args) {
31: TestRunner.run(ModelMBeanConstructorInfoTest.class);
32: }
33:
34: public ModelMBeanConstructorInfoTest(String name) {
35: super (name);
36: }
37:
38: public void testValid() throws Exception {
39: Constructor bobctor = ModelMBeanConstructorInfoTest.Bob.class
40: .getConstructor(new Class[] { String.class });
41: String[] fields = { "name", "descriptorType", "displayName",
42: "role" };
43: String[] values = { bobctor.getName(), "operation",
44: "bob maker", "constructor" };
45: DescriptorSupport ds = new DescriptorSupport(fields, values);
46: ModelMBeanConstructorInfo ctorinfo = new ModelMBeanConstructorInfo(
47: "BobBuilder", bobctor, ds);
48: }
49:
50: public void testAddDefaultDisplayName() throws Exception {
51: Constructor bobctor = ModelMBeanConstructorInfoTest.Bob.class
52: .getConstructor(new Class[] { String.class });
53: String[] fields = { "name", "descriptorType", "role" };
54: String[] values = { bobctor.getName(), "operation",
55: "constructor" };
56: DescriptorSupport ds = new DescriptorSupport(fields, values);
57: ModelMBeanConstructorInfo ctorinfo = new ModelMBeanConstructorInfo(
58: "BobBuilder", bobctor, ds);
59: Descriptor d = ctorinfo.getDescriptor();
60: String dispname = (String) d.getFieldValue("displayName");
61: assertTrue("Unexpected displayName", dispname.compareTo(bobctor
62: .getName()) == 0);
63: }
64:
65: public void testBadRole() throws Exception {
66: try {
67: Constructor bobctor = ModelMBeanConstructorInfoTest.Bob.class
68: .getConstructor(new Class[] { String.class });
69: String[] fields = { "name", "descriptorType",
70: "displayName", "role" };
71: String[] values = { bobctor.getName(), "operation",
72: "bob maker", "getter" };
73: DescriptorSupport ds = new DescriptorSupport(fields, values);
74: ModelMBeanConstructorInfo ctorinfo = new ModelMBeanConstructorInfo(
75: "BobBuilder", bobctor, ds);
76: fail("Expecting RuntimeOperationsException");
77: } catch (RuntimeOperationsException x) {
78: if (!(x.getTargetException() instanceof IllegalArgumentException)) {
79: fail("Target exception should be IllegalArgumentException");
80: }
81: assertTrue(true); // success
82: }
83: }
84:
85: public void testCaseInsensitiveDescriptorType() {
86: DescriptorSupport ds = new DescriptorSupport(new String[] {
87: "descriptorType=OPERATION", "role=constructor",
88: "name=BobBuilder", "displayname=bob maker" });
89: ModelMBeanConstructorInfo ctorinfo = new ModelMBeanConstructorInfo(
90: "BobBuilder", "Default Bob Constructor",
91: new MBeanParameterInfo[0], ds);
92: }
93:
94: protected void setUp() {
95: }
96:
97: protected void tearDown() {
98: }
99: }
|