1 22 package org.jboss.test.jmx.compliance.metadata; 23 24 25 import java.lang.reflect.Method ; 26 27 import javax.management.IntrospectionException ; 28 import javax.management.MBeanAttributeInfo ; 29 30 import org.jboss.test.jmx.compliance.metadata.support.Trivial; 31 32 import junit.framework.AssertionFailedError; 33 import junit.framework.TestCase; 34 35 41 public class MBeanAttributeInfoTEST extends TestCase 42 { 43 public MBeanAttributeInfoTEST(String s) 44 { 45 super(s); 46 } 47 48 51 public void testConstructorWithAccessorMethods() 52 { 53 try 54 { 55 Class c = Trivial.class; 56 Method getter = c.getMethod("getSomething", new Class [0]); 57 Method setter = c.getMethod("setSomething", new Class [] { String .class }); 58 59 MBeanAttributeInfo info = new MBeanAttributeInfo ("Something", "a description", getter, setter); 60 61 assertTrue(info.getDescription().equals("a description")); 62 assertTrue(info.getName().equals("Something")); 63 assertTrue(info.getType().equals("java.lang.String")); 64 assertTrue(info.isReadable() == true); 65 assertTrue(info.isWritable() == true); 66 assertTrue(info.isIs() == false); 67 } 68 catch (AssertionFailedError e) 69 { 70 throw e; 71 } 72 catch (Throwable t) 73 { 74 t.printStackTrace(); 75 fail("Unexpected error: " + t.toString()); 76 } 77 } 78 79 82 public void testConstructorWithMisplacedAccessorMethods() 83 { 84 try 85 { 86 Class c = Trivial.class; 87 Method getter = c.getMethod("getSomething", new Class [0]); 88 Method setter = c.getMethod("setSomething", new Class [] { String .class }); 89 90 new MBeanAttributeInfo ("Something", "a description", setter, getter); 91 92 fail("Introspection exception should have been thrown."); 94 } 95 catch (IntrospectionException e) 96 { 97 } 99 catch (AssertionFailedError e) 100 { 101 throw e; 102 } 103 catch (Throwable t) 104 { 105 t.printStackTrace(); 106 fail("Unexpected error: " + t.toString()); 107 } 108 } 109 110 113 public void testConstructorWithInvalidGetterMethod() 114 { 115 try 116 { 117 Class c = Trivial.class; 118 Method getter = c.getMethod("getSomethingInvalid", new Class [] { Object .class }); 119 Method setter = c.getMethod("setSomethingInvalid", new Class [] { String .class }); 120 121 new MBeanAttributeInfo ("Something", "a description", getter, setter); 122 123 fail("Introspection exception should have been thrown."); 125 } 126 catch (IntrospectionException e) 127 { 128 } 130 catch (AssertionFailedError e) 131 { 132 throw e; 133 } 134 catch (Throwable t) 135 { 136 t.printStackTrace(); 137 fail("Unexpected error: " + t.toString()); 138 } 139 } 140 141 144 public void testConstructorWithInvalidGetterMethod2() 145 { 146 try 147 { 148 Class c = Trivial.class; 149 Method getter = c.getMethod("getSomethingInvalid2", new Class [] { } ); 150 Method setter = c.getMethod("setSomethingInvalid2", new Class [] { String .class }); 151 152 new MBeanAttributeInfo ("Something", "a description", getter, setter); 153 154 fail("Introspection exception should have been thrown."); 156 } 157 catch (IntrospectionException e) 158 { 159 } 161 catch (AssertionFailedError e) 162 { 163 throw e; 164 } 165 catch (Throwable t) 166 { 167 t.printStackTrace(); 168 fail("Unexpected error: " + t.toString()); 169 } 170 } 171 172 public void testConstructorWithNonBooleanIsIs() 173 throws Exception 174 { 175 try 176 { 177 new MBeanAttributeInfo ("name", "type", "description", true, true, true); 178 } 179 catch (Exception e) 180 { 181 return; 182 } 183 fail("isIs is only allowed for boolean types"); 184 } 185 186 public void testConstructorWithPrimitiveBooleanIsIs() 187 throws Exception 188 { 189 new MBeanAttributeInfo ("name", Boolean.TYPE.getName(), "description", true, true, true); 190 } 191 192 public void testConstructorWithObjectBooleanIsIs() 193 throws Exception 194 { 195 new MBeanAttributeInfo ("name", Boolean .class.getName(), "description", true, true, true); 196 } 197 198 public void testHashCode() 199 throws Exception 200 { 201 MBeanAttributeInfo info1 = new MBeanAttributeInfo ("name", "type", "description", true, true, false); 202 MBeanAttributeInfo info2 = new MBeanAttributeInfo ("name", "type", "description", true, true, false); 203 204 assertTrue("Different instances with the same hashcode are equal", info1.hashCode() == info2.hashCode()); 205 } 206 207 public void testEquals() 208 throws Exception 209 { 210 MBeanAttributeInfo info = new MBeanAttributeInfo ("name", "type", "description", true, true, false); 211 212 assertTrue("Null should not be equal", info.equals(null) == false); 213 assertTrue("Only MBeanAttributeInfo should be equal", info.equals(new Object ()) == false); 214 215 MBeanAttributeInfo info2 = new MBeanAttributeInfo ("name", "type", "description", true, true, false); 216 217 assertTrue("Different instances of the same data are equal", info.equals(info2)); 218 assertTrue("Different instances of the same data are equal", info2.equals(info)); 219 220 info2 = new MBeanAttributeInfo ("name2", "type", "description", true, true, false); 221 222 assertTrue("Different instances with different names are not equal", info.equals(info2) == false); 223 assertTrue("Different instances with different names are not equal", info2.equals(info) == false); 224 225 info2 = new MBeanAttributeInfo ("name", "type2", "description", true, true, false); 226 227 assertTrue("Different instances with different types are not equal", info.equals(info2) == false); 228 assertTrue("Different instances with different types are not equal", info2.equals(info) == false); 229 230 info2 = new MBeanAttributeInfo ("name", "type", "description2", true, true, false); 231 232 assertTrue("Different instances with different descriptions are not equal", info.equals(info2) == false); 233 assertTrue("Different instances with different descritpions are not equal", info2.equals(info) == false); 234 235 info2 = new MBeanAttributeInfo ("name", "type", "description", false, true, false); 236 237 assertTrue("Different instances with different readables are not equal", info.equals(info2) == false); 238 assertTrue("Different instances with different readables are not equal", info2.equals(info) == false); 239 240 info2 = new MBeanAttributeInfo ("name", "type", "description", true, false, false); 241 242 assertTrue("Different instances with different writables are not equal", info.equals(info2) == false); 243 assertTrue("Different instances with different writables are not equal", info2.equals(info) == false); 244 245 info2 = new MBeanAttributeInfo ("name", Boolean.TYPE.getName(), "description", true, true, true); 246 247 assertTrue("Different instances with different isIs are not equal", info.equals(info2) == false); 248 assertTrue("Different instances with different isIs are not equal", info2.equals(info) == false); 249 } 250 } 251 | Popular Tags |