1 22 package org.jboss.test.jmx.compliance.objectname; 23 24 import java.util.Hashtable ; 25 26 import javax.management.MalformedObjectNameException ; 27 import javax.management.ObjectName ; 28 29 import junit.framework.TestCase; 30 31 public class BasicTEST extends TestCase 32 { 33 public static final String STD_DOMAIN = "domain"; 34 public static final String WHITESPACE = " "; 35 public static final String COMMA = ","; 36 public static final String EQUALS = "="; 37 public static final String COLON = ":"; 38 public static final String EMPTY = ""; 39 public static final String ASTERISK = "*"; 40 public static final String QUESTION = "?"; 41 public static final String QUOTE = "\""; 42 public static final String ESCAPE = "\\"; 43 public static final String STD_KEYPROP_STRING = "key1=val1,key2=val2"; 44 public static final String REV_KEYPROP_STRING = "key2=val2,key1=val1"; 45 public static final String KEY1 = "key1"; 46 public static final String KEY2 = "key2"; 47 public static final String VAL1 = "val1"; 48 public static final String VAL2 = "val2"; 49 50 public static final int JMX1_0 = 0; 51 public static final int JMX1_2 = 1; 52 public static final int QUOTED = 100; 53 public static final int QUOTEDBACKSLASH = 101; 54 55 public BasicTEST(String s) 56 { 57 super(s); 58 } 59 60 public void testStringNoDomain() 61 { 62 String nameArg = COLON + STD_KEYPROP_STRING; 63 try 64 { 65 ObjectName name = new ObjectName (nameArg); 66 String domain = name.getDomain(); 67 if (null == domain) 68 { 69 fail("getDomain() should return empty string rather than null"); 70 } 71 assertTrue("domain should have been zero size", domain.length() == 0); 72 assertEquals("value for key: " + KEY1 + " should be: " + VAL1, VAL1, name.getKeyProperty(KEY1)); 73 assertEquals("value for key: " + KEY2 + " should be: " + VAL2, VAL2, name.getKeyProperty(KEY2)); 74 } 75 catch (MalformedObjectNameException e) 76 { 77 fail("spurious MalformedObjectNameException on ('" + nameArg + "')"); 78 } 79 } 80 81 public void testStringWithDomain() 82 { 83 String nameArg = STD_DOMAIN + COLON + STD_KEYPROP_STRING; 84 try 85 { 86 ObjectName name = new ObjectName (nameArg); 87 assertEquals("domain should be equivalent", STD_DOMAIN, name.getDomain()); 88 assertEquals("value for key: " + KEY1 + " should be: " + VAL1, VAL1, name.getKeyProperty(KEY1)); 89 assertEquals("value for key: " + KEY2 + " should be: " + VAL2, VAL2, name.getKeyProperty(KEY2)); 90 } 91 catch (MalformedObjectNameException e) 92 { 93 fail("spurious MalformedObjectNameException on ('" + nameArg + "')"); 94 } 95 } 96 97 public void testSingleKVP() 98 { 99 try 100 { 101 ObjectName name = new ObjectName (STD_DOMAIN, KEY1, VAL1); 102 assertEquals("domain should be equivalent", STD_DOMAIN, name.getDomain()); 103 assertEquals("value for key: " + KEY1 + " should be: " + VAL1, VAL1, name.getKeyProperty(KEY1)); 104 assertNull("should return NULL key property for: " + KEY2, name.getKeyProperty(KEY2)); 105 106 String kplistString = name.getKeyPropertyListString(); 107 if (null == kplistString) 108 { 109 fail("key property list string was null;"); 110 } 111 assertTrue("KeyPropertyListString should match", 112 kplistString.equals("key1=val1")); 113 114 } 115 catch (MalformedObjectNameException e) 116 { 117 fail("spurious MalformedObjectNameException on ('" + STD_DOMAIN + "','" + KEY1 + "','" + VAL1 + "')"); 118 } 119 } 120 121 public void testHashtable() 122 { 123 try 124 { 125 Hashtable properties = new Hashtable (); 126 properties.put(KEY1, VAL1); 127 properties.put(KEY2, VAL2); 128 ObjectName name = new ObjectName (STD_DOMAIN, properties); 129 assertEquals("domain should be equivalent", STD_DOMAIN, name.getDomain()); 130 assertEquals("value for key: " + KEY1 + " should be: " + VAL1, VAL1, name.getKeyProperty(KEY1)); 131 assertEquals("value for key: " + KEY2 + " should be: " + VAL2, VAL2, name.getKeyProperty(KEY2)); 132 133 String kplistString = name.getKeyPropertyListString(); 134 if (null == kplistString) 135 { 136 fail("key property list string was null;"); 137 } 138 assertTrue("KeyPropertyListString should match", 139 (kplistString.equals(STD_KEYPROP_STRING) || kplistString.equals(REV_KEYPROP_STRING))); 140 } 141 catch (MalformedObjectNameException e) 142 { 143 fail("spurious MalformedObjectNameException on ('" + STD_DOMAIN + "','" + KEY1 + "','" + VAL1 + "')"); 144 } 145 } 146 147 public void testWhitespaceDomain() 148 { 149 domainTest(WHITESPACE, JMX1_0); 150 } 151 152 public void testCommaDomain() 153 { 154 domainTest(COMMA, JMX1_2); 155 } 156 157 public void testEqualsDomain() 158 { 159 domainTest(EQUALS, JMX1_2); 160 } 161 162 public void testQuestionValue() 163 { 164 valueTest(QUESTION, QUOTEDBACKSLASH); 165 } 166 167 public void testAsteriskValue() 168 { 169 valueTest(ASTERISK, QUOTEDBACKSLASH); 170 } 171 172 public void testQuoteValue() 173 { 174 valueTest(QUOTE, QUOTEDBACKSLASH); 175 } 176 177 public void testEqualsValue() 178 { 179 valueTest(EQUALS, QUOTED); 180 } 181 182 public void testCommaValue() 183 { 184 valueTest(COMMA, QUOTED); 185 } 186 187 public void testColonValue() 188 { 189 valueTest(COLON, QUOTED); 190 } 191 192 public void testEscapeValue() 193 { 194 valueTest(ESCAPE, QUOTEDBACKSLASH); 195 valueTest(ESCAPE, JMX1_0); 196 } 197 198 public void testEmptyQuotesValue() 199 { 200 valueTest(QUOTE + QUOTE, JMX1_0); 201 } 202 203 public void testKeyPropertyList() 204 { 205 String nameArg = ":" + STD_KEYPROP_STRING; 206 try 207 { 208 ObjectName name = new ObjectName (nameArg); 209 String kplistString = name.getKeyPropertyListString(); 210 if (null == kplistString) 211 { 212 fail("key property list string was null;"); 213 } 214 assertTrue("KeyPropertyListString should match", 215 (kplistString.equals(STD_KEYPROP_STRING) || kplistString.equals(REV_KEYPROP_STRING))); 216 217 } 218 catch (MalformedObjectNameException e) 219 { 220 fail("spurious MalformedObjectNameException on ('" + nameArg + "')"); 221 } 222 } 223 224 public void testToString() 225 { 226 String nameArg1 = ":key1=val1"; 227 String nameArg2 = "domain:key1=val1"; 228 229 try 230 { 231 ObjectName name1 = new ObjectName (nameArg1); 232 assertEquals("toString should match", nameArg1, name1.toString()); 233 } 234 catch (MalformedObjectNameException e) 235 { 236 fail("spurious MalformedObjectNameException on ('" + nameArg1 + "')"); 237 } 238 239 try 240 { 241 ObjectName name2 = new ObjectName (nameArg2); 242 assertEquals("toString should match", nameArg2, name2.toString()); 243 } 244 catch (MalformedObjectNameException e) 245 { 246 fail("spurious MalformedObjectNameException on ('" + nameArg2 + "')"); 247 } 248 } 249 250 private void domainTest(String domain, int version) 251 { 252 String nameArg = domain + COLON + STD_KEYPROP_STRING; 253 try 254 { 255 ObjectName name = new ObjectName (nameArg); 256 assertEquals("domain should be equivalent", domain, name.getDomain()); 257 } 258 catch (MalformedObjectNameException e) 259 { 260 if (version == JMX1_2) 261 fail("spurious MalformedObjectNameException on ('" + nameArg + "') as of JMX 1.2 " + 262 domain + " is allowed in a domain"); 263 else 264 fail("spurious MalformedObjectNameException on ('" + nameArg + "')"); 265 } 266 267 try 268 { 269 ObjectName name = new ObjectName (domain, KEY1, VAL1); 270 assertEquals("domain should be equivalent", domain, name.getDomain()); 271 } 272 catch (MalformedObjectNameException e) 273 { 274 if (version == JMX1_2) 275 fail("spurious MalformedObjectNameException on ('" + 276 domain + "','" + KEY1 + "','" + VAL1 + "') as of JMX 1.2 " + 277 domain + " is allowed in a domain"); 278 else 279 fail("spurious MalformedObjectNameException on ('" + 280 domain + "','" + KEY1 + "','" + VAL1 + "')"); 281 } 282 283 Hashtable table = new Hashtable (); 284 try 285 { 286 table.put(KEY1, VAL1); 287 ObjectName name = new ObjectName (domain, table); 288 assertEquals("domain should be equivalent", domain, name.getDomain()); 289 } 290 catch (MalformedObjectNameException e) 291 { 292 if (version == JMX1_2) 293 fail("spurious MalformedObjectNameException on ('" + 294 domain + " " + table + "') as of JMX 1.2 " + 295 domain + " is allowed in a domain"); 296 else 297 fail("spurious MalformedObjectNameException on ('" + 298 domain + " " + table + "')"); 299 } 300 } 301 302 public void valueTest(String value, int type) 303 { 304 String name = null; 305 if (type == QUOTEDBACKSLASH) 306 name = STD_DOMAIN + COLON + KEY1 + EQUALS + QUOTE + ESCAPE + value + QUOTE; 307 else if (type == QUOTED) 308 name = STD_DOMAIN + COLON + KEY1 + EQUALS + QUOTE + value + QUOTE; 309 else 310 name = STD_DOMAIN + COLON + KEY1 + EQUALS + value; 311 try 312 { 313 new ObjectName (name); 314 } 315 catch (MalformedObjectNameException e) 316 { 317 if (type == QUOTEDBACKSLASH) 318 fail("spurious MalformedObjectNameException on ('" + name + 319 "') as of JMX 1.2 " + value + " is allowed inside quotes escaped by a backslash"); 320 else if (type == QUOTED) 321 fail("spurious MalformedObjectNameException on ('" + name + 322 "') as of JMX 1.2 " + value + " is allowed inside quotes"); 323 else 324 fail("FAILS IN RI: spurious MalformedObjectNameException on ('" + name + "')"); 325 } 326 327 String test = null; 328 if (type == QUOTEDBACKSLASH) 329 test = QUOTE + ESCAPE + value + QUOTE; 330 else if (type == QUOTED) 331 test = QUOTE + value + QUOTE; 332 else 333 test = value; 334 try 335 { 336 new ObjectName (STD_DOMAIN, KEY1, test); 337 } 338 catch (MalformedObjectNameException e) 339 { 340 if (type == QUOTEDBACKSLASH) 341 fail("spurious MalformedObjectNameException on ('" + 342 STD_DOMAIN + "','" + KEY1 + "','" + value + "') as of JMX 1.2 " + 343 STD_DOMAIN + " is allowed inside quotes escaped by a backslah"); 344 if (type == QUOTED) 345 fail("spurious MalformedObjectNameException on ('" + 346 STD_DOMAIN + "','" + KEY1 + "','" + value + "') as of JMX 1.2 " + 347 STD_DOMAIN + " is allowed inside quotes"); 348 else 349 fail("spurious MalformedObjectNameException on ('" + 350 STD_DOMAIN + "','" + KEY1 + "','" + value + "')"); 351 } 352 353 Hashtable table = new Hashtable (); 354 if (type == QUOTEDBACKSLASH) 355 table.put(KEY1, QUOTE + ESCAPE + value + QUOTE); 356 else if (type == QUOTED) 357 table.put(KEY1, QUOTE + value + QUOTE); 358 else 359 table.put(KEY1, value); 360 try 361 { 362 new ObjectName (STD_DOMAIN, table); 363 } 364 catch (MalformedObjectNameException e) 365 { 366 if (type == QUOTEDBACKSLASH) 367 fail("spurious MalformedObjectNameException on ('" + 368 STD_DOMAIN + " " + table + "') as of JMX 1.2 " + 369 STD_DOMAIN + " is allowed inside quotes escaped by a backslah"); 370 if (type == QUOTED) 371 fail("spurious MalformedObjectNameException on ('" + 372 STD_DOMAIN + " " + table + "') as of JMX 1.2 " + 373 STD_DOMAIN + " is allowed inside quotes"); 374 else 375 fail("spurious MalformedObjectNameException on ('" + 376 STD_DOMAIN + " " + table + "')"); 377 } 378 } 379 } 380 | Popular Tags |