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