1 8 9 package test.javax.management.modelmbean; 10 11 import java.lang.reflect.Method ; 12 import java.util.Arrays ; 13 import java.util.List ; 14 import javax.management.Descriptor ; 15 import javax.management.IntrospectionException ; 16 import javax.management.modelmbean.DescriptorSupport ; 17 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 18 19 import test.MX4JTestCase; 20 21 28 29 public class ModelMBeanAttributeInfoTest extends MX4JTestCase 30 { 31 public static class BogusNIC 32 { 33 public String getMAC() 34 { 35 return null; 36 } 37 38 public void setMAC(int mac) 39 { 40 } 41 } 42 43 44 public ModelMBeanAttributeInfoTest(String s) 45 { 46 super(s); 47 } 48 49 public void setUp() throws Exception 50 { 51 super.setUp(); 52 } 53 54 public void tearDown() throws Exception 55 { 56 super.tearDown(); 57 } 58 59 public void testValidDescriptorFields() throws Exception 60 { 61 Descriptor descriptor = new DescriptorSupport (new String []{"name", "descriptortype", "default"}, 64 new String []{"attribute1", "attribute", "default"}); 65 ModelMBeanAttributeInfo attribute 66 = new ModelMBeanAttributeInfo ("attribute1", "java.lang.String", "An attribute", true, true, false, descriptor); 67 assertEquals(attribute.getDescriptor().getFieldValue("default"), "default"); 69 assertNull(attribute.getDescriptor().getFieldValue("value")); 70 } 71 72 public void testBadCtor() throws Exception 73 { 74 try 75 { 76 Method macgetter = BogusNIC.class.getMethod("getMAC", new Class [0]); 77 Method macsetter = 78 BogusNIC.class.getMethod("setMAC", new Class []{int.class}); 79 ModelMBeanAttributeInfo attrinfo = 80 new ModelMBeanAttributeInfo ("MAC", 81 "MAC Address", 82 macgetter, 83 macsetter); 84 fail("Expecting an IntrospectionException"); 85 } 86 catch (IntrospectionException x) 87 { 88 assertTrue(true); } 90 } 91 92 public void testValuelessAttribute() throws Exception 93 { 94 ModelMBeanAttributeInfo attrinfo = 95 new ModelMBeanAttributeInfo ("SerialNo", 96 "NIC Card's serial number", 97 null, 98 null); 99 attrinfo = 100 new ModelMBeanAttributeInfo ("SerialNo", 101 "String", 102 "NIC Card's serial number", 103 false, 104 false, 105 false); 106 } 107 108 public void testCaseInsenstiveDescriptorType() 109 { 110 DescriptorSupport ds = new DescriptorSupport (new String []{ 111 "name=PreferredWine", 112 "descriptorType=Attribute", 113 "value=Amarone", 114 "default=Red" 115 }); 116 ModelMBeanAttributeInfo attrinfo = 117 new ModelMBeanAttributeInfo ("PreferredWine", 118 "String", 119 "Wine of choice", 120 true, 121 false, 122 false, 123 ds); 124 } 125 126 public void testGetDescriptor() 127 { 128 DescriptorSupport defds = 129 new DescriptorSupport (new String []{ 130 "name=PreferredWine", 131 "descriptorType=Attribute", 132 "displayName=PreferredWine"}); 133 DescriptorSupport ds = new DescriptorSupport (new String []{ 134 "name=PreferredWine", 135 "descriptorType=Attribute", 136 "value=Amarone", 137 "displayName=PreferredWine", 138 "default=Red" 139 }); 140 141 ModelMBeanAttributeInfo attrinfo = 142 new ModelMBeanAttributeInfo ("PreferredWine", 143 "String", 144 "Wine of choice", 145 true, 146 false, 147 false); 148 Descriptor d = attrinfo.getDescriptor(); 149 assertTrue("Expecting default descriptor", descriptorsEqual(d, defds)); 150 151 attrinfo = 152 new ModelMBeanAttributeInfo ("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 done, Descriptor dtwo) 164 { 165 List cifields = Arrays.asList(new String []{"descriptortype", "persistpolicy", "log"}); 166 String [] 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 field = fields[i]; 171 Object vone = done.getFieldValue(field); 172 Object 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 ) || !(vtwo instanceof String )) 180 { 181 result = false; 182 } 183 else 184 { 185 result = ((String )vone).compareToIgnoreCase((String )vtwo) == 0; 186 } 187 } 188 else 189 { 190 vone.equals(vtwo); 191 } 192 } 193 return result; 194 } 195 } 196 | Popular Tags |