1 22 package org.jboss.test.jmx.compliance.openmbean; 23 24 import junit.framework.TestCase; 25 26 import java.io.ByteArrayInputStream ; 27 import java.io.ByteArrayOutputStream ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectOutputStream ; 30 import java.util.Arrays ; 31 import java.util.Set ; 32 33 import javax.management.openmbean.OpenMBeanParameterInfo ; 34 import javax.management.openmbean.OpenMBeanParameterInfoSupport ; 35 import javax.management.openmbean.OpenMBeanConstructorInfoSupport ; 36 import javax.management.openmbean.OpenType ; 37 import javax.management.openmbean.SimpleType ; 38 39 44 public class OpenMBeanConstructorInfoSupportTestCase 45 extends TestCase 46 { 47 49 51 53 56 public OpenMBeanConstructorInfoSupportTestCase(String s) 57 { 58 super(s); 59 } 60 61 63 public void testOpenMBeanConstructorInfoSupport() 64 throws Exception 65 { 66 OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport ( 67 "name", "description", null); 68 assertEquals("name", info.getName()); 69 assertEquals("description", info.getDescription()); 70 assertEquals(0, info.getSignature().length); 71 72 info = new OpenMBeanConstructorInfoSupport ( 73 "name", "description", new OpenMBeanParameterInfoSupport [0]); 74 assertEquals("name", info.getName()); 75 assertEquals("description", info.getDescription()); 76 assertEquals(0, info.getSignature().length); 77 78 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 79 { 80 new OpenMBeanParameterInfoSupport ( 81 "name", "description", SimpleType.STRING) 82 }; 83 info = new OpenMBeanConstructorInfoSupport ( 84 "name", "description", parms); 85 assertEquals("name", info.getName()); 86 assertEquals("description", info.getDescription()); 87 assertEquals(1, info.getSignature().length); 88 } 89 90 public void testEquals() 91 throws Exception 92 { 93 OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport ( 94 "name", "description", null); 95 96 assertTrue("Null should not be equal", info.equals(null) == false); 97 assertTrue("Only OpenMBeanConstructorInfo should be equal", info.equals(new Object ()) == false); 98 99 OpenMBeanConstructorInfoSupport info2 = new OpenMBeanConstructorInfoSupport ( 100 "name", "description", null); 101 102 assertTrue("Different instances of the same data are equal", info.equals(info2)); 103 assertTrue("Different instances of the same data are equal", info2.equals(info)); 104 105 info2 = new OpenMBeanConstructorInfoSupport ( 106 "name", "description2", null); 107 108 assertTrue("Different instances with different descriptions are equal", info.equals(info2)); 109 assertTrue("Different instances with different descritpions are equal", info2.equals(info)); 110 111 info2 = new OpenMBeanConstructorInfoSupport ( 112 "name2", "description", null); 113 114 assertTrue("Instances with different names are not equal", info.equals(info2) == false); 115 assertTrue("Instances with different names are not equal", info2.equals(info) == false); 116 117 OpenMBeanParameterInfoSupport param1 = new OpenMBeanParameterInfoSupport ( 118 "name", "description", SimpleType.STRING); 119 OpenMBeanParameterInfoSupport param2 = new OpenMBeanParameterInfoSupport ( 120 "name2", "description", SimpleType.STRING); 121 122 info = new OpenMBeanConstructorInfoSupport ( 123 "name", "description", new OpenMBeanParameterInfoSupport [] { param1, param2 }); 124 info2 = new OpenMBeanConstructorInfoSupport ( 125 "name", "description", new OpenMBeanParameterInfoSupport [] { param1, param2 }); 126 127 assertTrue("Different instances with the same parameters are equal", info.equals(info2)); 128 assertTrue("Different instances with the same parameters are equal", info2.equals(info)); 129 130 info = new OpenMBeanConstructorInfoSupport ( 131 "name", "description", new OpenMBeanParameterInfoSupport [] { param1, param2 }); 132 info2 = new OpenMBeanConstructorInfoSupport ( 133 "name", "description", new OpenMBeanParameterInfoSupport [] { param2, param1 }); 134 135 assertTrue("Different instances with the same signature but different parameters are not equal", 136 info.equals(info2) == false); 137 assertTrue("Different instances with the same signature but different parameters are not equal", 138 info2.equals(info) == false); 139 140 param2 = new OpenMBeanParameterInfoSupport ( 141 "name2", "description", SimpleType.INTEGER); 142 info2 = new OpenMBeanConstructorInfoSupport ( 143 "name", "description", new OpenMBeanParameterInfoSupport [] { param1, param2 }); 144 145 assertTrue("Different instances with different signatures are not equal", 146 info.equals(info2) == false); 147 assertTrue("Different instances with different signatures are not equal", 148 info2.equals(info) == false); 149 150 info2 = new OpenMBeanConstructorInfoSupport ( 151 "name", "description", new OpenMBeanParameterInfoSupport [] { param1 }); 152 153 assertTrue("Different instances with different numbers of paramters are not equal", 154 info.equals(info2) == false); 155 assertTrue("Different instances with different numbers of parameters are not equal", 156 info2.equals(info) == false); 157 } 158 159 public void testHashCode() 160 throws Exception 161 { 162 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 163 { 164 new OpenMBeanParameterInfoSupport ( 165 "name", "description", SimpleType.STRING) 166 }; 167 OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport ( 168 "name", "description", parms); 169 170 int myHash = "name".hashCode() + Arrays.asList(parms).hashCode(); 171 assertEquals(myHash, info.hashCode()); 172 } 173 174 public void testToString() 175 throws Exception 176 { 177 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 178 { 179 new OpenMBeanParameterInfoSupport ( 180 "name", "description", SimpleType.STRING) 181 }; 182 OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport ( 183 "NAME", "DESCRIPTION", parms); 184 185 String toString = info.toString(); 186 187 assertTrue("info.toString() should contain NAME", 188 toString.indexOf("NAME") != -1); 189 assertTrue("info.toString() should contain the parameters", 190 toString.indexOf(Arrays.asList(parms).toString()) != -1); 191 } 192 193 public void testSerialization() 194 throws Exception 195 { 196 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 197 { 198 new OpenMBeanParameterInfoSupport ( 199 "name", "description", SimpleType.STRING) 200 }; 201 OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport ( 202 "name", "description", parms); 203 204 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 206 ObjectOutputStream oos = new ObjectOutputStream (baos); 207 oos.writeObject(info); 208 209 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 211 ObjectInputStream ois = new ObjectInputStream (bais); 212 Object result = ois.readObject(); 213 214 assertEquals(info, result); 215 } 216 217 public void testErrors() 218 throws Exception 219 { 220 boolean caught = false; 221 try 222 { 223 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 224 { 225 new OpenMBeanParameterInfoSupport ( 226 "name", "description", SimpleType.STRING) 227 }; 228 new OpenMBeanConstructorInfoSupport ( 229 null, "description", parms); 230 } 231 catch (IllegalArgumentException e) 232 { 233 caught = true; 234 } 235 if (caught == false) 236 fail("Expected IllegalArgumentException for null name"); 237 238 caught = false; 239 try 240 { 241 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 242 { 243 new OpenMBeanParameterInfoSupport ( 244 "name", "description", SimpleType.STRING) 245 }; 246 new OpenMBeanConstructorInfoSupport ( 247 "", "description", parms); 248 } 249 catch (IllegalArgumentException e) 250 { 251 caught = true; 252 } 253 if (caught == false) 254 fail("Expected IllegalArgumentException for empty name"); 255 256 caught = false; 257 try 258 { 259 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 260 { 261 new OpenMBeanParameterInfoSupport ( 262 "name", "description", SimpleType.STRING) 263 }; 264 new OpenMBeanConstructorInfoSupport ( 265 "name", null, parms); 266 } 267 catch (IllegalArgumentException e) 268 { 269 caught = true; 270 } 271 if (caught == false) 272 fail("Expected IllegalArgumentException for null description"); 273 274 caught = false; 275 try 276 { 277 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 278 { 279 new OpenMBeanParameterInfoSupport ( 280 "name", "description", SimpleType.STRING) 281 }; 282 new OpenMBeanConstructorInfoSupport ( 283 "name", "", parms); 284 } 285 catch (IllegalArgumentException e) 286 { 287 caught = true; 288 } 289 if (caught == false) 290 fail("Expected IllegalArgumentException for empty description"); 291 292 caught = false; 293 try 294 { 295 new OpenMBeanConstructorInfoSupport ( 296 "name", "description", new MyOpenMBeanParameterInfo[] { new MyOpenMBeanParameterInfo() }); 297 } 298 catch (ArrayStoreException e) 299 { 300 caught = true; 301 } 302 if (caught == false) 303 fail("Expected ArrayStoreException for non MBeanParameterInfo array"); 304 } 305 306 public static class MyOpenMBeanParameterInfo 307 implements OpenMBeanParameterInfo 308 { 309 public boolean equals(Object o) { return false; } 310 public Object getDefaultValue() { return null; } 311 public String getDescription() { return null; } 312 public Set getLegalValues() { return null; } 313 public Comparable getMaxValue() { return null; } 314 public Comparable getMinValue() { return null; } 315 public String getName() { return null; } 316 public OpenType getOpenType() { return null; } 317 public boolean hasDefaultValue() { return false; } 318 public boolean hasLegalValues() { return false; } 319 public int hashCode() { return 0; } 320 public boolean hasMaxValue() { return false; } 321 public boolean hasMinValue() { return false; } 322 public boolean isValue(Object o) { return false; } 323 public String toString() { return null; } 324 } 325 } 326 | Popular Tags |