1 22 package org.jboss.test.jmx.compliance.objectname; 23 import java.util.Hashtable ; 24 25 import javax.management.MalformedObjectNameException ; 26 import javax.management.ObjectName ; 27 28 import junit.framework.Test; 29 import junit.framework.TestCase; 30 import junit.framework.TestSuite; 31 32 45 public class MalformedSUITE extends TestSuite { 46 public static final String GOOD_DOMAIN = "domain"; 47 public static final String GOOD_KEY = "key1"; 48 public static final String GOOD_VALUE = "val1"; 49 50 public static final String [] BAD_KEYS = { 52 "", "som:thing", "som?thing", "som*thing", "som,thing", "som=thing", "som\nthing", }; 60 61 public static final String [] BAD_VALS = { 63 "som:thing", "som?thing", "som*thing", "som,thing", "som=thing", "som\nthing", "som\"thing", "som\"\\b\"thing", "som\"" + '\n' + "\"thing", "som\"\"\"thing", "som\"?\"thing", "som\"*\"thing", }; 76 77 public static final String [] BAD_DOMAINS = { 79 "doma:in", "doma\nin", }; 82 83 public static final String [] BAD_FULLNAMES = { 85 "domain:key=val,key=val2", "domain:=,foo=bar", "domain:key=val,,foo=bar", "domain:,key=val,foo=bar", "domain:key=val,foo=bar,", "domain:key=val, ,foo=bar", "domain:key=val,*,*", "domain:*,key=val,*", "domain:*,key1=val1,*,key2=val2", "domain: *,key1=val1,key2=val2", "domain:key1=val1,key2=val2, *", "domain:key1=val1,key2=val2,* ", "domain:", null, }; 100 101 public static void main(String [] args) 102 { 103 junit.textui.TestRunner.run(suite()); 104 } 105 106 public static Test suite() { 107 TestSuite suite = new TestSuite("All Malformed Tests"); 108 suite.addTest(new DomainKeyValueTEST(null, null, null)); 110 suite.addTest(new DomainKeyValueTEST(null, "key1", "val1")); 111 suite.addTest(new DomainKeyValueTEST("domain", null, "val1")); 112 suite.addTest(new DomainKeyValueTEST("domain", "key1", null)); 113 suite.addTest(new DomainKeyValueTEST("domain", null, null)); 114 suite.addTest(new DomainHashtableTEST(null, "key1", "val1")); 115 116 suite.addTestSuite(DomainHashtableExtraTEST.class); 118 119 for (int i = 0; i < BAD_DOMAINS.length; i++) 121 { 122 suite.addTest(new FullNameTEST(BAD_DOMAINS[i] + ":" + GOOD_KEY + "=" + GOOD_VALUE)); 123 suite.addTest(new DomainKeyValueTEST(BAD_DOMAINS[i], GOOD_KEY, GOOD_VALUE)); 124 suite.addTest(new DomainHashtableTEST(BAD_DOMAINS[i], GOOD_KEY, GOOD_VALUE)); 125 } 126 127 for (int i = 0; i < BAD_KEYS.length; i++) 129 { 130 suite.addTest(new FullNameTEST(GOOD_DOMAIN + ":" + BAD_KEYS[i] + "=" + GOOD_VALUE)); 131 suite.addTest(new DomainKeyValueTEST(GOOD_DOMAIN, BAD_KEYS[i], GOOD_VALUE)); 132 suite.addTest(new DomainHashtableTEST(GOOD_DOMAIN, BAD_KEYS[i], GOOD_VALUE)); 133 } 134 135 for (int i = 0; i < BAD_VALS.length; i++) 137 { 138 suite.addTest(new FullNameTEST(GOOD_DOMAIN + ":" + GOOD_KEY + "=" + BAD_VALS[i])); 139 suite.addTest(new DomainKeyValueTEST(GOOD_DOMAIN, GOOD_KEY, BAD_VALS[i])); 140 suite.addTest(new DomainHashtableTEST(GOOD_DOMAIN, GOOD_KEY, BAD_VALS[i])); 141 } 142 143 for (int i = 0; i < BAD_FULLNAMES.length; i++) 145 { 146 suite.addTest( new FullNameTEST(BAD_FULLNAMES[i])); 147 } 148 149 return suite; 150 } 151 152 public static class FullNameTEST extends TestCase 153 { 154 private String fullName; 155 156 public FullNameTEST(String fullName) 157 { 158 super("testMalformed"); 159 this.fullName = fullName; 160 } 161 162 public void testMalformed() 163 { 164 boolean caught = false; 165 try 166 { 167 new ObjectName (fullName); 168 } 169 catch (MalformedObjectNameException e) 170 { 171 caught = true; 172 } 173 catch (NullPointerException e) 174 { 175 caught = true; 176 if (fullName != null) 177 fail("Unexpected NullPointerException for " + fullName); 178 } 179 if (caught == false) 180 { 181 if (fullName != null) 182 if (fullName.equals("domain:=val1") || 183 fullName.equals("domain:=,foo=bar")) 184 fail("FAILS IN RI: expected a MalformedObjectNameException for: " + fullName); 185 fail("expected a MalformedObjectNameException for: " + fullName); 186 } 187 188 caught = false; 189 try 190 { 191 ObjectName.getInstance(fullName); 192 } 193 catch (MalformedObjectNameException e) 194 { 195 caught = true; 196 } 197 catch (NullPointerException e) 198 { 199 caught = true; 200 if (fullName != null) 201 fail("Unexpected NullPointerException for " + fullName); 202 } 203 if (caught == false) 204 { 205 if (fullName != null) 206 if (fullName.equals("domain:=val1") || 207 fullName.equals("domain:=,foo=bar")) 208 fail("FAILS IN RI: expected a MalformedObjectNameException for: " + fullName); 209 fail("expected a MalformedObjectNameException for: " + fullName); 210 } 211 } 212 } 213 214 public static class DomainKeyValueTEST extends TestCase 215 { 216 private String domain; 217 private String key; 218 private String value; 219 220 public DomainKeyValueTEST(String domain, String key, String value) 221 { 222 super("testMalformed"); 223 this.domain = domain; 224 this.key = key; 225 this.value = value; 226 } 227 228 public void testMalformed() 229 { 230 boolean caught = false; 231 try 232 { 233 new ObjectName (domain, key, value); 234 } 235 catch (MalformedObjectNameException e) 236 { 237 caught = true; 238 } 239 catch (NullPointerException e) 240 { 241 caught = true; 242 if (domain != null && key != null && value != null) 243 fail("Unexpected NullPointerException for " + domain + ":" + key + "=" + value); 244 } 245 if (caught == false) 246 { 247 if (value != null) 248 if (value.equals("som\"thing") || 249 value.equals("som\"\\b\"thing") || 250 value.equals("som\"\"\"thing")) 251 fail("FAILS IN RI: expected a MalformedObjectNameException for: " + domain + ":" + key + "=" + value); 252 fail("expected a MalformedObjectNameException for: " + domain + ":" + key + "=" + value); 253 } 254 255 caught = false; 256 try 257 { 258 ObjectName.getInstance(domain, key, value); 259 } 260 catch (MalformedObjectNameException e) 261 { 262 caught = true; 263 } 264 catch (NullPointerException e) 265 { 266 caught = true; 267 if (domain != null && key != null && value != null) 268 fail("Unexpected NullPointerException for " + domain + ":" + key + "=" + value); 269 } 270 if (caught == false) 271 { 272 if (value != null) 273 if (value.equals("som\"thing") || 274 value.equals("som\"\\b\"thing") || 275 value.equals("som\"\"\"thing")) 276 fail("FAILS IN RI: expected a MalformedObjectNameException for: " + domain + ":" + key + "=" + value); 277 fail("expected a MalformedObjectNameException for: " + domain + ":" + key + "=" + value); 278 } 279 } 280 } 281 282 public static class DomainHashtableTEST extends TestCase 283 { 284 private String domain; 285 private String key; 286 private String value; 287 288 public DomainHashtableTEST(String domain, String key, String value) 289 { 290 super("testMalformed"); 291 this.domain = domain; 292 this.key = key; 293 this.value = value; 294 } 295 296 public void testMalformed() 297 { 298 boolean caught = false; 299 try 300 { 301 Hashtable h = new Hashtable (); 302 h.put(key, value); 303 new ObjectName (domain, h); 304 } 305 catch (MalformedObjectNameException e) 306 { 307 caught = true; 308 } 309 catch (NullPointerException e) 310 { 311 caught = true; 312 if (domain != null && key != null && value != null) 313 fail("Unexpected NullPointerException for " + domain + ":" + key + "=" + value); 314 } 315 if (caught == false) 316 { 317 if (value != null) 318 if (value.equals("som\"thing") || 319 value.equals("som\"\\b\"thing") || 320 value.equals("som\"\"\"thing")) 321 fail("FAILS IN RI: expected a MalformedObjectNameException for: " + domain + ":" + key + "=" + value); 322 fail("expected a MalformedObjectNameException for: " + domain + ":" + key + "=" + value); 323 } 324 325 caught = false; 326 try 327 { 328 Hashtable h = new Hashtable (); 329 h.put(key, value); 330 ObjectName.getInstance(domain, h); 331 } 332 catch (MalformedObjectNameException e) 333 { 334 caught = true; 335 } 336 catch (NullPointerException e) 337 { 338 caught = true; 339 if (domain != null && key != null && value != null) 340 fail("Unexpected NullPointerException for " + domain + ":" + key + "=" + value); 341 } 342 if (caught == false) 343 { 344 if (value != null) 345 if (value.equals("som\"thing") || 346 value.equals("som\"\\b\"thing") || 347 value.equals("som\"\"\"thing")) 348 fail("FAILS IN RI: expected a MalformedObjectNameException for: " + domain + ":" + key + "=" + value); 349 fail("expected a MalformedObjectNameException for: " + domain + ":" + key + "=" + value); 350 } 351 } 352 } 353 354 public static class DomainHashtableExtraTEST extends TestCase 355 { 356 public DomainHashtableExtraTEST(String s) 357 { 358 super(s); 359 } 360 361 public void testNullDomain() 362 { 363 Hashtable h = new Hashtable (); 364 h.put(new Object (), GOOD_VALUE); 365 doCheck(null, h, "<null domain>", true); 366 } 367 368 public void testNullHashtable() 369 { 370 doCheck(GOOD_DOMAIN, null, "<null hashtable>", true); 371 } 372 373 public void testEmptyHashtable() 374 { 375 doCheck(GOOD_DOMAIN, new Hashtable (), "<empty_hashtable>", false); 376 } 377 378 public void testNonStringKey() 379 { 380 Hashtable h = new Hashtable (); 381 h.put(new Object (), GOOD_VALUE); 382 doCheck(GOOD_DOMAIN, h, "<non_string_key>=" + GOOD_VALUE, false); 383 } 384 385 public void testNonStringValue() 386 { 387 Hashtable h = new Hashtable (); 388 h.put(GOOD_KEY, new Object ()); 389 doCheck(GOOD_DOMAIN, h, GOOD_KEY + "=<non_string_value>", false); 390 } 391 392 private void doCheck(String domain, Hashtable h, String failureHint, boolean expectNull) 393 { 394 boolean caught = true; 395 try 396 { 397 new ObjectName (domain, h); 398 } 399 catch (MalformedObjectNameException e) 400 { 401 caught = true; 402 if (expectNull) 403 fail("FAILS IN RI: Expected a NullPointerException for: " + domain + ":" + failureHint); 404 } 405 catch (NullPointerException e) 406 { 407 if (expectNull == false) 408 fail("unexpected a NullPointerException for: " + domain + ":" + failureHint); 409 } 410 if (caught == false) 411 fail("expected a MalformedObjectNameException for: " + domain + ":" + failureHint); 412 413 caught = true; 414 try 415 { 416 ObjectName.getInstance(domain, h); 417 } 418 catch (MalformedObjectNameException e) 419 { 420 caught = true; 421 if (expectNull) 422 fail("FAILS IN RI: Expected a NullPointerException for: " + domain + ":" + failureHint); 423 } 424 catch (NullPointerException e) 425 { 426 if (expectNull == false) 427 fail("unexpected a NullPointerException for: " + domain + ":" + failureHint); 428 } 429 if (caught == false) 430 fail("expected a MalformedObjectNameException for: " + domain + ":" + failureHint); 431 } 432 } 433 } | Popular Tags |