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.RoleStatus; 21 import javax.management.relation.RoleUnresolved; 22 23 import junit.framework.TestCase; 24 25 35 public class RoleUnresolvedTestCase 36 extends TestCase 37 { 38 40 42 45 public RoleUnresolvedTestCase(String s) 46 { 47 super(s); 48 } 49 50 52 55 public void testBasic() 56 { 57 try 58 { 59 ArrayList roleValue = new ArrayList(); 61 ObjectName a = new ObjectName(":a=a"); 62 ObjectName b = new ObjectName(":b=b"); 63 roleValue.add(a); 64 roleValue.add(b); 65 RoleUnresolved roleUnresolved = new RoleUnresolved("RoleName", roleValue, 66 RoleStatus.NO_ROLE_WITH_NAME); 67 68 assertEquals("RoleName", roleUnresolved.getRoleName()); 70 71 assertEquals(roleValue, roleUnresolved.getRoleValue()); 73 74 assertEquals(RoleStatus.NO_ROLE_WITH_NAME, roleUnresolved.getProblemType()); 76 77 roleUnresolved.setRoleName("Changed"); 79 assertEquals("Changed", roleUnresolved.getRoleName()); 80 81 ArrayList roleValue2 = new ArrayList(); 83 ObjectName c = new ObjectName(":c=c"); 84 ObjectName d = new ObjectName(":d=d"); 85 roleValue2.add(c); 86 roleValue2.add(d); 87 roleUnresolved.setRoleValue(roleValue2); 88 89 assertEquals(roleValue2, roleUnresolved.getRoleValue()); 91 92 roleUnresolved.setProblemType(RoleStatus.ROLE_NOT_READABLE); 94 assertEquals(RoleStatus.ROLE_NOT_READABLE, roleUnresolved.getProblemType()); 95 } 96 catch (MalformedObjectNameException mfone) 97 { 98 fail(mfone.toString()); 99 } 100 } 101 102 105 public void testToString() 106 { 107 try 108 { 109 ArrayList roleValue = new ArrayList(); 111 ObjectName a = new ObjectName(":a=a"); 112 ObjectName b = new ObjectName(":b=b"); 113 roleValue.add(a); 114 roleValue.add(b); 115 RoleUnresolved roleUnresolved = new RoleUnresolved("XYZZY", roleValue, 116 RoleStatus.NO_ROLE_WITH_NAME); 117 118 String result = roleUnresolved.toString(); 120 if (result.lastIndexOf("XYZZY") == -1) 121 fail("Missing role name in toString"); 122 if (result.lastIndexOf(":a=a") == -1) 123 fail("Missing object name :a=a in toString"); 124 if (result.lastIndexOf(":b=b") == -1) 125 fail("Missing object name :b=b in toString"); 126 127 } 129 catch (MalformedObjectNameException mfone) 130 { 131 fail(mfone.toString()); 132 } 133 } 134 135 138 public void testErrorHandling() 139 { 140 try 141 { 142 ArrayList roleValue = new ArrayList(); 144 ObjectName a = new ObjectName(":a=a"); 145 ObjectName b = new ObjectName(":b=b"); 146 roleValue.add(a); 147 roleValue.add(b); 148 149 boolean caught = false; 151 try 152 { 153 new RoleUnresolved(null, roleValue, RoleStatus.NO_ROLE_WITH_NAME); 154 } 155 catch (IllegalArgumentException e) 156 { 157 caught = true; 158 } 159 if (caught == false) 160 fail ("Constructor accepts null role name"); 161 162 caught = false; 164 try 165 { 166 new RoleUnresolved("RoleName", roleValue, -1000); 167 } 168 catch (IllegalArgumentException e) 169 { 170 caught = true; 171 } 172 if (caught == false) 173 fail ("Constructor accepts an invalid problem type"); 174 175 RoleUnresolved roleUnresolved = new RoleUnresolved("RoleName", roleValue, 176 RoleStatus.NO_ROLE_WITH_NAME); 177 178 caught = false; 180 try 181 { 182 roleUnresolved.setRoleName(null); 183 } 184 catch (IllegalArgumentException e) 185 { 186 caught = true; 187 } 188 if (caught == false) 189 fail ("setRoleName accepts null"); 190 191 caught = false; 193 try 194 { 195 roleUnresolved.setProblemType(-1000); 196 } 197 catch (IllegalArgumentException e) 198 { 199 caught = true; 200 } 201 if (caught == false) 202 fail ("setProblemType accepts an invalid problem type"); 203 } 204 catch (MalformedObjectNameException mfone) 205 { 206 fail(mfone.toString()); 207 } 208 } 209 210 213 public void testClone() 214 { 215 ArrayList roleValue = new ArrayList(); 217 try 218 { 219 roleValue.add(new ObjectName(":a=a")); 220 roleValue.add(new ObjectName(":b=b")); 221 } 222 catch (MalformedObjectNameException mfone) 223 { 224 fail(mfone.toString()); 225 } 226 RoleUnresolved roleUnresolved = new RoleUnresolved("RoleName", roleValue, 227 RoleStatus.NO_ROLE_WITH_NAME); 228 RoleUnresolved roleUnresolved2 = (RoleUnresolved) roleUnresolved.clone(); 229 230 assertEquals(roleUnresolved.getRoleName(), roleUnresolved2.getRoleName()); 232 assertEquals(roleUnresolved.getRoleValue(), roleUnresolved2.getRoleValue()); 233 assertEquals(roleUnresolved.getProblemType(), roleUnresolved2.getProblemType()); 234 if(roleUnresolved.getRoleValue() == roleUnresolved2.getRoleValue()) 235 fail("RoleUnresolved.clone() didn't clone"); 236 } 237 238 241 public void testSerialization() 242 { 243 ArrayList roleValue = new ArrayList(); 245 try 246 { 247 roleValue.add(new ObjectName(":a=a")); 248 roleValue.add(new ObjectName(":b=b")); 249 } 250 catch (MalformedObjectNameException mfone) 251 { 252 fail(mfone.toString()); 253 } 254 RoleUnresolved roleUnresolved = new RoleUnresolved("RoleName", roleValue, 255 RoleStatus.NO_ROLE_WITH_NAME); 256 RoleUnresolved roleUnresolved2 = null; 257 258 try 259 { 260 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 262 ObjectOutputStream oos = new ObjectOutputStream(baos); 263 oos.writeObject(roleUnresolved); 264 265 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 267 ObjectInputStream ois = new ObjectInputStream(bais); 268 roleUnresolved2 = (RoleUnresolved) ois.readObject(); 269 } 270 catch (IOException ioe) 271 { 272 fail(ioe.toString()); 273 } 274 catch (ClassNotFoundException cnfe) 275 { 276 fail(cnfe.toString()); 277 } 278 279 assertEquals(roleUnresolved.getRoleName(), roleUnresolved2.getRoleName()); 281 assertEquals(roleUnresolved.getRoleValue(), roleUnresolved2.getRoleValue()); 282 } 283 } 284 | Popular Tags |