1 22 package org.jboss.test.jmx.compliance.relation; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.IOException ; 27 import java.io.ObjectInputStream ; 28 import java.io.ObjectOutputStream ; 29 import java.util.ArrayList ; 30 31 import javax.management.relation.InvalidRelationTypeException ; 32 import javax.management.relation.RelationSupport ; 33 import javax.management.relation.RelationTypeSupport ; 34 import javax.management.relation.RoleInfo ; 35 import javax.management.relation.RoleInfoNotFoundException ; 36 37 import junit.framework.TestCase; 38 39 44 public class RelationTypeSupportTestCase 45 extends TestCase 46 { 47 48 50 52 55 public RelationTypeSupportTestCase(String s) 56 { 57 super(s); 58 } 59 60 62 65 public void testBasic() 66 { 67 RoleInfo roleInfo1 = null; 68 RoleInfo roleInfo2 = null; 69 RoleInfo [] roleInfos = null; 70 RelationTypeSupport support = null; 71 try 72 { 73 roleInfo1 = new RoleInfo ("roleInfo1", RelationSupport .class.getName()); 74 roleInfo2 = new RoleInfo ("roleInfo2", RelationSupport .class.getName()); 75 roleInfos = new RoleInfo [] { roleInfo1, roleInfo2 }; 76 support = new RelationTypeSupport ("name", roleInfos); 77 } 78 catch (Exception e) 79 { 80 fail(e.toString()); 81 } 82 83 assertEquals("name", support.getRelationTypeName()); 85 86 ArrayList result = (ArrayList ) support.getRoleInfos(); 88 assertEquals(2, result.size()); 89 90 try 92 { 93 assertEquals(roleInfo1.toString(), support.getRoleInfo("roleInfo1").toString()); 94 assertEquals(roleInfo2.toString(), support.getRoleInfo("roleInfo2").toString()); 95 } 96 catch (Exception e) 97 { 98 fail(e.toString()); 99 } 100 101 MyRelationTypeSupport mySupport = new MyRelationTypeSupport("myName"); 103 104 assertEquals("myName", mySupport.getRelationTypeName()); 106 result = (ArrayList ) mySupport.getRoleInfos(); 107 assertEquals(0, result.size()); 108 109 try 111 { 112 mySupport.addRoleInfo(roleInfo1); 113 result = (ArrayList ) mySupport.getRoleInfos(); 114 assertEquals(1, result.size()); 115 } 116 catch (Exception e) 117 { 118 fail(e.toString()); 119 } 120 } 121 122 125 public void testErrorHandling() 126 { 127 RoleInfo roleInfo1 = null; 128 RoleInfo roleInfo2 = null; 129 RoleInfo [] roleInfos = null; 130 RelationTypeSupport support = null; 131 132 boolean caught = false; 133 try 134 { 135 roleInfo1 = new RoleInfo ("roleInfo1", RelationSupport .class.getName()); 136 roleInfo2 = new RoleInfo ("roleInfo2", RelationSupport .class.getName()); 137 roleInfos = new RoleInfo [] { roleInfo1, roleInfo2 }; 138 support = new RelationTypeSupport (null, roleInfos); 139 } 140 catch (IllegalArgumentException e) 141 { 142 caught = true; 143 } 144 catch (Exception e) 145 { 146 fail(e.toString()); 147 } 148 if (caught == false) 149 fail("Constructor accepts null relation type name"); 150 151 caught = false; 152 try 153 { 154 support = new RelationTypeSupport ("name", null); 155 } 156 catch (IllegalArgumentException e) 157 { 158 caught = true; 159 } 160 catch (Exception e) 161 { 162 fail(e.toString()); 163 } 164 if (caught == false) 165 fail("Constructor accepts null role infos"); 166 167 caught = false; 168 try 169 { 170 support = new RelationTypeSupport ("name", new RoleInfo [0]); 171 } 172 catch (InvalidRelationTypeException e) 173 { 174 caught = true; 175 } 176 catch (Exception e) 177 { 178 fail(e.toString()); 179 } 180 if (caught == false) 181 fail("Constructor accepts no role infos"); 182 183 caught = false; 184 try 185 { 186 roleInfo1 = new RoleInfo ("roleInfo1", RelationSupport .class.getName()); 187 roleInfos = new RoleInfo [] { roleInfo1, null }; 188 support = new RelationTypeSupport ("name", roleInfos); 189 } 190 catch (InvalidRelationTypeException e) 191 { 192 caught = true; 193 } 194 catch (Exception e) 195 { 196 fail(e.toString()); 197 } 198 if (caught == false) 199 fail("Constructor accepts null role"); 200 201 caught = false; 202 try 203 { 204 roleInfo1 = new RoleInfo ("roleInfo1", RelationSupport .class.getName()); 205 roleInfos = new RoleInfo [] { roleInfo1 }; 206 support = new RelationTypeSupport ("name", roleInfos); 207 support.getRoleInfo(null); 208 } 209 catch (IllegalArgumentException e) 210 { 211 caught = true; 212 } 213 catch (Exception e) 214 { 215 fail(e.toString()); 216 } 217 if (caught == false) 218 fail("getRoleInfo allows a null role info name"); 219 220 caught = false; 221 try 222 { 223 roleInfo1 = new RoleInfo ("roleInfo1", RelationSupport .class.getName()); 224 roleInfos = new RoleInfo [] { roleInfo1 }; 225 support = new RelationTypeSupport ("name", roleInfos); 226 support.getRoleInfo("rubbish"); 227 } 228 catch (RoleInfoNotFoundException e) 229 { 230 caught = true; 231 } 232 catch (Exception e) 233 { 234 fail(e.toString()); 235 } 236 if (caught == false) 237 fail("getRoleInfo returns a not existent role info"); 238 239 MyRelationTypeSupport mySupport = new MyRelationTypeSupport("myName"); 241 242 caught = false; 243 try 244 { 245 mySupport.addRoleInfo(null); 246 } 247 catch (IllegalArgumentException e) 248 { 249 caught = true; 250 } 251 catch (Exception e) 252 { 253 fail(e.toString()); 254 } 255 if (caught == false) 256 fail("addRoleInfo accepts null"); 257 258 caught = false; 259 try 260 { 261 mySupport.addRoleInfo(roleInfo1); 262 mySupport.addRoleInfo(roleInfo1); 263 } 264 catch (InvalidRelationTypeException e) 265 { 266 caught = true; 267 } 268 catch (Exception e) 269 { 270 fail(e.toString()); 271 } 272 if (caught == false) 273 fail("addRoleInfo accepts duplicates role infos"); 274 } 275 276 279 public void testSerialization() 280 { 281 RoleInfo roleInfo1 = null; 283 RoleInfo roleInfo2 = null; 284 RoleInfo [] roleInfos = null; 285 RelationTypeSupport support = null; 286 try 287 { 288 roleInfo1 = new RoleInfo ("roleInfo1", RelationSupport .class.getName()); 289 roleInfo2 = new RoleInfo ("roleInfo2", RelationSupport .class.getName()); 290 roleInfos = new RoleInfo [] { roleInfo1, roleInfo2 }; 291 support = new RelationTypeSupport ("name", roleInfos); 292 } 293 catch (Exception e) 294 { 295 fail(e.toString()); 296 } 297 RelationTypeSupport support2 = null; 298 299 try 300 { 301 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 303 ObjectOutputStream oos = new ObjectOutputStream (baos); 304 oos.writeObject(support); 305 306 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 308 ObjectInputStream ois = new ObjectInputStream (bais); 309 support2 = (RelationTypeSupport ) ois.readObject(); 310 } 311 catch (IOException ioe) 312 { 313 fail(ioe.toString()); 314 } 315 catch (ClassNotFoundException cnfe) 316 { 317 fail(cnfe.toString()); 318 } 319 320 assertEquals("name", support2.getRelationTypeName()); 322 ArrayList result = (ArrayList ) support2.getRoleInfos(); 323 assertEquals(2, result.size()); 324 } 325 326 328 class MyRelationTypeSupport 329 extends RelationTypeSupport 330 { 331 private static final long serialVersionUID = -1; 332 333 public MyRelationTypeSupport(String relationTypeName) 334 { 335 super(relationTypeName); 336 } 337 public void addRoleInfo(RoleInfo roleInfo) 338 throws IllegalArgumentException , InvalidRelationTypeException 339 { 340 super.addRoleInfo(roleInfo); 341 } 342 } 343 } 344 | Popular Tags |