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.MalformedObjectNameException ; 32 import javax.management.ObjectName ; 33 import javax.management.relation.RoleStatus ; 34 import javax.management.relation.RoleUnresolved ; 35 36 import junit.framework.TestCase; 37 38 48 public class RoleUnresolvedTestCase 49 extends TestCase 50 { 51 53 55 58 public RoleUnresolvedTestCase(String s) 59 { 60 super(s); 61 } 62 63 65 68 public void testBasic() 69 { 70 try 71 { 72 ArrayList roleValue = new ArrayList (); 74 ObjectName a = new ObjectName (":a=a"); 75 ObjectName b = new ObjectName (":b=b"); 76 roleValue.add(a); 77 roleValue.add(b); 78 RoleUnresolved roleUnresolved = new RoleUnresolved ("RoleName", roleValue, 79 RoleStatus.NO_ROLE_WITH_NAME); 80 81 assertEquals("RoleName", roleUnresolved.getRoleName()); 83 84 assertEquals(roleValue, roleUnresolved.getRoleValue()); 86 87 assertEquals(RoleStatus.NO_ROLE_WITH_NAME, roleUnresolved.getProblemType()); 89 90 roleUnresolved.setRoleName("Changed"); 92 assertEquals("Changed", roleUnresolved.getRoleName()); 93 94 ArrayList roleValue2 = new ArrayList (); 96 ObjectName c = new ObjectName (":c=c"); 97 ObjectName d = new ObjectName (":d=d"); 98 roleValue2.add(c); 99 roleValue2.add(d); 100 roleUnresolved.setRoleValue(roleValue2); 101 102 assertEquals(roleValue2, roleUnresolved.getRoleValue()); 104 105 roleUnresolved.setProblemType(RoleStatus.ROLE_NOT_READABLE); 107 assertEquals(RoleStatus.ROLE_NOT_READABLE, roleUnresolved.getProblemType()); 108 } 109 catch (MalformedObjectNameException mfone) 110 { 111 fail(mfone.toString()); 112 } 113 } 114 115 118 public void testToString() 119 { 120 try 121 { 122 ArrayList roleValue = new ArrayList (); 124 ObjectName a = new ObjectName (":a=a"); 125 ObjectName b = new ObjectName (":b=b"); 126 roleValue.add(a); 127 roleValue.add(b); 128 RoleUnresolved roleUnresolved = new RoleUnresolved ("XYZZY", roleValue, 129 RoleStatus.NO_ROLE_WITH_NAME); 130 131 String result = roleUnresolved.toString(); 133 if (result.lastIndexOf("XYZZY") == -1) 134 fail("Missing role name in toString"); 135 if (result.lastIndexOf(":a=a") == -1) 136 fail("Missing object name :a=a in toString"); 137 if (result.lastIndexOf(":b=b") == -1) 138 fail("Missing object name :b=b in toString"); 139 140 } 142 catch (MalformedObjectNameException mfone) 143 { 144 fail(mfone.toString()); 145 } 146 } 147 148 151 public void testErrorHandling() 152 { 153 try 154 { 155 ArrayList roleValue = new ArrayList (); 157 ObjectName a = new ObjectName (":a=a"); 158 ObjectName b = new ObjectName (":b=b"); 159 roleValue.add(a); 160 roleValue.add(b); 161 162 boolean caught = false; 164 try 165 { 166 new RoleUnresolved (null, roleValue, RoleStatus.NO_ROLE_WITH_NAME); 167 } 168 catch (IllegalArgumentException e) 169 { 170 caught = true; 171 } 172 if (caught == false) 173 fail ("Constructor accepts null role name"); 174 175 caught = false; 177 try 178 { 179 new RoleUnresolved ("RoleName", roleValue, -1000); 180 } 181 catch (IllegalArgumentException e) 182 { 183 caught = true; 184 } 185 if (caught == false) 186 fail ("Constructor accepts an invalid problem type"); 187 188 RoleUnresolved roleUnresolved = new RoleUnresolved ("RoleName", roleValue, 189 RoleStatus.NO_ROLE_WITH_NAME); 190 191 caught = false; 193 try 194 { 195 roleUnresolved.setRoleName(null); 196 } 197 catch (IllegalArgumentException e) 198 { 199 caught = true; 200 } 201 if (caught == false) 202 fail ("setRoleName accepts null"); 203 204 caught = false; 206 try 207 { 208 roleUnresolved.setProblemType(-1000); 209 } 210 catch (IllegalArgumentException e) 211 { 212 caught = true; 213 } 214 if (caught == false) 215 fail ("setProblemType accepts an invalid problem type"); 216 } 217 catch (MalformedObjectNameException mfone) 218 { 219 fail(mfone.toString()); 220 } 221 } 222 223 226 public void testClone() 227 { 228 ArrayList roleValue = new ArrayList (); 230 try 231 { 232 roleValue.add(new ObjectName (":a=a")); 233 roleValue.add(new ObjectName (":b=b")); 234 } 235 catch (MalformedObjectNameException mfone) 236 { 237 fail(mfone.toString()); 238 } 239 RoleUnresolved roleUnresolved = new RoleUnresolved ("RoleName", roleValue, 240 RoleStatus.NO_ROLE_WITH_NAME); 241 RoleUnresolved roleUnresolved2 = (RoleUnresolved ) roleUnresolved.clone(); 242 243 assertEquals(roleUnresolved.getRoleName(), roleUnresolved2.getRoleName()); 245 assertEquals(roleUnresolved.getRoleValue(), roleUnresolved2.getRoleValue()); 246 assertEquals(roleUnresolved.getProblemType(), roleUnresolved2.getProblemType()); 247 if(roleUnresolved.getRoleValue() == roleUnresolved2.getRoleValue()) 248 fail("RoleUnresolved.clone() didn't clone"); 249 } 250 251 254 public void testSerialization() 255 { 256 ArrayList roleValue = new ArrayList (); 258 try 259 { 260 roleValue.add(new ObjectName (":a=a")); 261 roleValue.add(new ObjectName (":b=b")); 262 } 263 catch (MalformedObjectNameException mfone) 264 { 265 fail(mfone.toString()); 266 } 267 RoleUnresolved roleUnresolved = new RoleUnresolved ("RoleName", roleValue, 268 RoleStatus.NO_ROLE_WITH_NAME); 269 RoleUnresolved roleUnresolved2 = null; 270 271 try 272 { 273 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 275 ObjectOutputStream oos = new ObjectOutputStream (baos); 276 oos.writeObject(roleUnresolved); 277 278 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 280 ObjectInputStream ois = new ObjectInputStream (bais); 281 roleUnresolved2 = (RoleUnresolved ) ois.readObject(); 282 } 283 catch (IOException ioe) 284 { 285 fail(ioe.toString()); 286 } 287 catch (ClassNotFoundException cnfe) 288 { 289 fail(cnfe.toString()); 290 } 291 292 assertEquals(roleUnresolved.getRoleName(), roleUnresolved2.getRoleName()); 294 assertEquals(roleUnresolved.getRoleValue(), roleUnresolved2.getRoleValue()); 295 } 296 } 297 | Popular Tags |