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.ObjectName; 19 import javax.management.MalformedObjectNameException; 20 import javax.management.relation.Role; 21 22 import junit.framework.TestCase; 23 24 34 public class RoleTestCase 35 extends TestCase 36 { 37 39 41 44 public RoleTestCase(String s) 45 { 46 super(s); 47 } 48 49 51 54 public void testBasic() 55 { 56 try 57 { 58 ArrayList roleValue = new ArrayList(); 60 ObjectName a = new ObjectName(":a=a"); 61 ObjectName b = new ObjectName(":b=b"); 62 roleValue.add(a); 63 roleValue.add(b); 64 Role role = new Role("RoleName", roleValue); 65 66 assertEquals("RoleName", role.getRoleName()); 68 69 assertEquals(roleValue, role.getRoleValue()); 71 72 role.setRoleName("Changed"); 74 assertEquals("Changed", role.getRoleName()); 75 76 ArrayList roleValue2 = new ArrayList(); 78 ObjectName c = new ObjectName(":c=c"); 79 ObjectName d = new ObjectName(":d=d"); 80 roleValue2.add(c); 81 roleValue2.add(d); 82 role.setRoleValue(roleValue2); 83 84 assertEquals(roleValue2, role.getRoleValue()); 86 } 87 catch (MalformedObjectNameException mfone) 88 { 89 fail(mfone.toString()); 90 } 91 } 92 93 96 public void testToString() 97 { 98 try 99 { 100 ArrayList roleValue = new ArrayList(); 102 ObjectName a = new ObjectName(":a=a"); 103 ObjectName b = new ObjectName(":b=b"); 104 roleValue.add(a); 105 roleValue.add(b); 106 Role role = new Role("XYZZY", roleValue); 107 108 String result = Role.roleValueToString(roleValue); 110 if (result.lastIndexOf(":a=a") == -1) 111 fail("Missing object name :a=a in roleValueToString"); 112 if (result.lastIndexOf(":b=b") == -1) 113 fail("Missing object name :b=b in roleValueToString"); 114 115 result = role.toString(); 117 if (result.lastIndexOf("XYZZY") == -1) 118 fail("Missing role name in toString"); 119 if (result.lastIndexOf(":a=a") == -1) 120 fail("Missing object name :a=a in toString"); 121 if (result.lastIndexOf(":b=b") == -1) 122 fail("Missing object name :b=b in toString"); 123 } 124 catch (MalformedObjectNameException mfone) 125 { 126 fail(mfone.toString()); 127 } 128 } 129 130 133 public void testErrorHandling() 134 { 135 try 136 { 137 ArrayList roleValue = new ArrayList(); 139 ObjectName a = new ObjectName(":a=a"); 140 ObjectName b = new ObjectName(":b=b"); 141 roleValue.add(a); 142 roleValue.add(b); 143 144 boolean caught = false; 146 try 147 { 148 new Role(null, roleValue); 149 } 150 catch (IllegalArgumentException e) 151 { 152 caught = true; 153 } 154 if (caught == false) 155 fail ("Constructor accepts null role name"); 156 157 caught = false; 159 try 160 { 161 new Role("RoleName", null); 162 } 163 catch (IllegalArgumentException e) 164 { 165 caught = true; 166 } 167 if (caught == false) 168 fail ("Constructor accepts null role value"); 169 170 Role role = new Role("RoleName", roleValue); 171 172 caught = false; 174 try 175 { 176 role.setRoleName(null); 177 } 178 catch (IllegalArgumentException e) 179 { 180 caught = true; 181 } 182 if (caught == false) 183 fail ("setRoleName accepts null"); 184 185 caught = false; 187 try 188 { 189 role.setRoleValue(null); 190 } 191 catch (IllegalArgumentException e) 192 { 193 caught = true; 194 } 195 if (caught == false) 196 fail ("setRoleValue accepts null"); 197 198 caught = false; 200 try 201 { 202 Role.roleValueToString(null); 203 } 204 catch (IllegalArgumentException e) 205 { 206 caught = true; 207 } 208 if (caught == false) 209 fail ("roleValueToString accepts null"); 210 } 211 catch (MalformedObjectNameException mfone) 212 { 213 fail(mfone.toString()); 214 } 215 } 216 217 220 public void testClone() 221 { 222 ArrayList roleValue = new ArrayList(); 224 try 225 { 226 roleValue.add(new ObjectName(":a=a")); 227 roleValue.add(new ObjectName(":b=b")); 228 } 229 catch (MalformedObjectNameException mfone) 230 { 231 fail(mfone.toString()); 232 } 233 Role role = new Role("RoleName", roleValue); 234 Role role2 = (Role) role.clone(); 235 236 assertEquals(role.getRoleName(), role2.getRoleName()); 238 assertEquals(role.getRoleValue(), role2.getRoleValue()); 239 if(role.getRoleValue() == role2.getRoleValue()) 240 fail("Role.clone() didn't clone"); 241 } 242 243 246 public void testSerialization() 247 { 248 ArrayList roleValue = new ArrayList(); 250 try 251 { 252 roleValue.add(new ObjectName(":a=a")); 253 roleValue.add(new ObjectName(":b=b")); 254 } 255 catch (MalformedObjectNameException mfone) 256 { 257 fail(mfone.toString()); 258 } 259 Role role = new Role("RoleName", roleValue); 260 Role role2 = null; 261 262 try 263 { 264 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 266 ObjectOutputStream oos = new ObjectOutputStream(baos); 267 oos.writeObject(role); 268 269 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 271 ObjectInputStream ois = new ObjectInputStream(bais); 272 role2 = (Role) ois.readObject(); 273 } 274 catch (IOException ioe) 275 { 276 fail(ioe.toString()); 277 } 278 catch (ClassNotFoundException cnfe) 279 { 280 fail(cnfe.toString()); 281 } 282 283 assertEquals(role.getRoleName(), role2.getRoleName()); 285 assertEquals(role.getRoleValue(), role2.getRoleValue()); 286 } 287 } 288 | Popular Tags |