KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > modelmbean > ModelMBeanAttributeInfoTest


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.javax.management.modelmbean;
10
11 import java.lang.reflect.Method JavaDoc;
12 import java.util.Arrays JavaDoc;
13 import java.util.List JavaDoc;
14 import javax.management.Descriptor JavaDoc;
15 import javax.management.IntrospectionException JavaDoc;
16 import javax.management.modelmbean.DescriptorSupport JavaDoc;
17 import javax.management.modelmbean.ModelMBeanAttributeInfo JavaDoc;
18
19 import test.MX4JTestCase;
20
21 /**
22  * Test case of ModelMBeanAttributeInfo. It will try to verify an appropriate
23  * behaviour in particular with respect to the descriptor values
24  *
25  * @version $Revision: 1.7 $
26  * @see
27  */

28
29 public class ModelMBeanAttributeInfoTest extends MX4JTestCase
30 {
31    public static class BogusNIC
32    {
33       public String JavaDoc getMAC()
34       {
35          return null;
36       }
37
38       public void setMAC(int mac)
39       {
40       }
41    }
42
43
44    public ModelMBeanAttributeInfoTest(String JavaDoc s)
45    {
46       super(s);
47    }
48
49    public void setUp() throws Exception JavaDoc
50    {
51       super.setUp();
52    }
53
54    public void tearDown() throws Exception JavaDoc
55    {
56       super.tearDown();
57    }
58
59    public void testValidDescriptorFields() throws Exception JavaDoc
60    {
61       // testcase for bug #794320
62
// Test that only name and descriptorType are mandatory
63
Descriptor JavaDoc descriptor = new DescriptorSupport JavaDoc(new String JavaDoc[]{"name", "descriptortype", "default"},
64                                                     new String JavaDoc[]{"attribute1", "attribute", "default"});
65       ModelMBeanAttributeInfo JavaDoc attribute
66               = new ModelMBeanAttributeInfo JavaDoc("attribute1", "java.lang.String", "An attribute", true, true, false, descriptor);
67       // in case of bug #794320 the descriptor is overrided
68
assertEquals(attribute.getDescriptor().getFieldValue("default"), "default");
69       assertNull(attribute.getDescriptor().getFieldValue("value"));
70    }
71
72    public void testBadCtor() throws Exception JavaDoc
73    {
74       try
75       {
76          Method JavaDoc macgetter = BogusNIC.class.getMethod("getMAC", new Class JavaDoc[0]);
77          Method JavaDoc macsetter =
78                  BogusNIC.class.getMethod("setMAC", new Class JavaDoc[]{int.class});
79          ModelMBeanAttributeInfo JavaDoc attrinfo =
80                  new ModelMBeanAttributeInfo JavaDoc("MAC",
81                                              "MAC Address",
82                                              macgetter,
83                                              macsetter);
84          fail("Expecting an IntrospectionException");
85       }
86       catch (IntrospectionException JavaDoc x)
87       {
88          assertTrue(true); // success;
89
}
90    }
91
92    public void testValuelessAttribute() throws Exception JavaDoc
93    {
94       ModelMBeanAttributeInfo JavaDoc attrinfo =
95               new ModelMBeanAttributeInfo JavaDoc("SerialNo",
96                                           "NIC Card's serial number",
97                                           null,
98                                           null);
99       attrinfo =
100       new ModelMBeanAttributeInfo JavaDoc("SerialNo",
101                                   "String",
102                                   "NIC Card's serial number",
103                                   false,
104                                   false,
105                                   false);
106    }
107
108    public void testCaseInsenstiveDescriptorType()
109    {
110       DescriptorSupport JavaDoc ds = new DescriptorSupport JavaDoc(new String JavaDoc[]{
111          "name=PreferredWine",
112          "descriptorType=Attribute",
113          "value=Amarone",
114          "default=Red"
115       });
116       ModelMBeanAttributeInfo JavaDoc attrinfo =
117               new ModelMBeanAttributeInfo JavaDoc("PreferredWine",
118                                           "String",
119                                           "Wine of choice",
120                                           true,
121                                           false,
122                                           false,
123                                           ds);
124    }
125
126    public void testGetDescriptor()
127    {
128       DescriptorSupport JavaDoc defds =
129               new DescriptorSupport JavaDoc(new String JavaDoc[]{
130                  "name=PreferredWine",
131                  "descriptorType=Attribute",
132                  "displayName=PreferredWine"});
133       DescriptorSupport JavaDoc ds = new DescriptorSupport JavaDoc(new String JavaDoc[]{
134          "name=PreferredWine",
135          "descriptorType=Attribute",
136          "value=Amarone",
137          "displayName=PreferredWine",
138          "default=Red"
139       });
140
141       ModelMBeanAttributeInfo JavaDoc attrinfo =
142               new ModelMBeanAttributeInfo JavaDoc("PreferredWine",
143                                           "String",
144                                           "Wine of choice",
145                                           true,
146                                           false,
147                                           false);
148       Descriptor JavaDoc d = attrinfo.getDescriptor();
149       assertTrue("Expecting default descriptor", descriptorsEqual(d, defds));
150
151       attrinfo =
152       new ModelMBeanAttributeInfo JavaDoc("PreferredWine",
153                                   "String",
154                                   "Wine of choice",
155                                   true,
156                                   false,
157                                   false,
158                                   ds);
159       d = attrinfo.getDescriptor();
160       assertTrue("Expecting copy of ds", descriptorsEqual(d, ds));
161    }
162
163    private boolean descriptorsEqual(Descriptor JavaDoc done, Descriptor JavaDoc dtwo)
164    {
165       List JavaDoc cifields = Arrays.asList(new String JavaDoc[]{"descriptortype", "persistpolicy", "log"});
166       String JavaDoc[] fields = done.getFieldNames();
167       boolean result = done.getFields().length == dtwo.getFields().length;
168       for (int i = 0; i < fields.length && result == true; i++)
169       {
170          String JavaDoc field = fields[i];
171          Object JavaDoc vone = done.getFieldValue(field);
172          Object JavaDoc vtwo = done.getFieldValue(field);
173          if (vtwo == null)
174          {
175             result = false;
176          }
177          else if (cifields.contains(field))
178          {
179             if (!(vone instanceof String JavaDoc) || !(vtwo instanceof String JavaDoc))
180             {
181                result = false;
182             }
183             else
184             {
185                result = ((String JavaDoc)vone).compareToIgnoreCase((String JavaDoc)vtwo) == 0;
186             }
187          }
188          else
189          {
190             vone.equals(vtwo);
191          }
192       }
193       return result;
194    }
195 }
196
Popular Tags