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.Role ; 34 35 import junit.framework.TestCase; 36 37 47 public class RoleTestCase 48 extends TestCase 49 { 50 52 54 57 public RoleTestCase(String s) 58 { 59 super(s); 60 } 61 62 64 67 public void testBasic() 68 { 69 try 70 { 71 ArrayList roleValue = new ArrayList (); 73 ObjectName a = new ObjectName (":a=a"); 74 ObjectName b = new ObjectName (":b=b"); 75 roleValue.add(a); 76 roleValue.add(b); 77 Role role = new Role ("RoleName", roleValue); 78 79 assertEquals("RoleName", role.getRoleName()); 81 82 assertEquals(roleValue, role.getRoleValue()); 84 85 role.setRoleName("Changed"); 87 assertEquals("Changed", role.getRoleName()); 88 89 ArrayList roleValue2 = new ArrayList (); 91 ObjectName c = new ObjectName (":c=c"); 92 ObjectName d = new ObjectName (":d=d"); 93 roleValue2.add(c); 94 roleValue2.add(d); 95 role.setRoleValue(roleValue2); 96 97 assertEquals(roleValue2, role.getRoleValue()); 99 } 100 catch (MalformedObjectNameException mfone) 101 { 102 fail(mfone.toString()); 103 } 104 } 105 106 109 public void testToString() 110 { 111 try 112 { 113 ArrayList roleValue = new ArrayList (); 115 ObjectName a = new ObjectName (":a=a"); 116 ObjectName b = new ObjectName (":b=b"); 117 roleValue.add(a); 118 roleValue.add(b); 119 Role role = new Role ("XYZZY", roleValue); 120 121 String result = Role.roleValueToString(roleValue); 123 if (result.lastIndexOf(":a=a") == -1) 124 fail("Missing object name :a=a in roleValueToString"); 125 if (result.lastIndexOf(":b=b") == -1) 126 fail("Missing object name :b=b in roleValueToString"); 127 128 result = role.toString(); 130 if (result.lastIndexOf("XYZZY") == -1) 131 fail("Missing role name in toString"); 132 if (result.lastIndexOf(":a=a") == -1) 133 fail("Missing object name :a=a in toString"); 134 if (result.lastIndexOf(":b=b") == -1) 135 fail("Missing object name :b=b in toString"); 136 } 137 catch (MalformedObjectNameException mfone) 138 { 139 fail(mfone.toString()); 140 } 141 } 142 143 146 public void testErrorHandling() 147 { 148 try 149 { 150 ArrayList roleValue = new ArrayList (); 152 ObjectName a = new ObjectName (":a=a"); 153 ObjectName b = new ObjectName (":b=b"); 154 roleValue.add(a); 155 roleValue.add(b); 156 157 boolean caught = false; 159 try 160 { 161 new Role (null, roleValue); 162 } 163 catch (IllegalArgumentException e) 164 { 165 caught = true; 166 } 167 if (caught == false) 168 fail ("Constructor accepts null role name"); 169 170 caught = false; 172 try 173 { 174 new Role ("RoleName", null); 175 } 176 catch (IllegalArgumentException e) 177 { 178 caught = true; 179 } 180 if (caught == false) 181 fail ("Constructor accepts null role value"); 182 183 Role role = new Role ("RoleName", roleValue); 184 185 caught = false; 187 try 188 { 189 role.setRoleName(null); 190 } 191 catch (IllegalArgumentException e) 192 { 193 caught = true; 194 } 195 if (caught == false) 196 fail ("setRoleName accepts null"); 197 198 caught = false; 200 try 201 { 202 role.setRoleValue(null); 203 } 204 catch (IllegalArgumentException e) 205 { 206 caught = true; 207 } 208 if (caught == false) 209 fail ("setRoleValue accepts null"); 210 211 caught = false; 213 try 214 { 215 Role.roleValueToString(null); 216 } 217 catch (IllegalArgumentException e) 218 { 219 caught = true; 220 } 221 if (caught == false) 222 fail ("roleValueToString accepts null"); 223 } 224 catch (MalformedObjectNameException mfone) 225 { 226 fail(mfone.toString()); 227 } 228 } 229 230 233 public void testClone() 234 { 235 ArrayList roleValue = new ArrayList (); 237 try 238 { 239 roleValue.add(new ObjectName (":a=a")); 240 roleValue.add(new ObjectName (":b=b")); 241 } 242 catch (MalformedObjectNameException mfone) 243 { 244 fail(mfone.toString()); 245 } 246 Role role = new Role ("RoleName", roleValue); 247 Role role2 = (Role ) role.clone(); 248 249 assertEquals(role.getRoleName(), role2.getRoleName()); 251 assertEquals(role.getRoleValue(), role2.getRoleValue()); 252 if(role.getRoleValue() == role2.getRoleValue()) 253 fail("Role.clone() didn't clone"); 254 } 255 256 259 public void testSerialization() 260 { 261 ArrayList roleValue = new ArrayList (); 263 try 264 { 265 roleValue.add(new ObjectName (":a=a")); 266 roleValue.add(new ObjectName (":b=b")); 267 } 268 catch (MalformedObjectNameException mfone) 269 { 270 fail(mfone.toString()); 271 } 272 Role role = new Role ("RoleName", roleValue); 273 Role role2 = null; 274 275 try 276 { 277 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 279 ObjectOutputStream oos = new ObjectOutputStream (baos); 280 oos.writeObject(role); 281 282 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 284 ObjectInputStream ois = new ObjectInputStream (bais); 285 role2 = (Role ) ois.readObject(); 286 } 287 catch (IOException ioe) 288 { 289 fail(ioe.toString()); 290 } 291 catch (ClassNotFoundException cnfe) 292 { 293 fail(cnfe.toString()); 294 } 295 296 assertEquals(role.getRoleName(), role2.getRoleName()); 298 assertEquals(role.getRoleValue(), role2.getRoleValue()); 299 } 300 } 301 | Popular Tags |