001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package test.javax.management;
010:
011: import java.util.Hashtable;
012:
013: import javax.management.MalformedObjectNameException;
014: import javax.management.ObjectName;
015:
016: import test.MX4JTestCase;
017:
018: /**
019: * @version $Revision: 1.15 $
020: */
021: public class ObjectNameTest extends MX4JTestCase {
022: public ObjectNameTest(String s) {
023: super (s);
024: }
025:
026: public void testInvalidDomain() throws Exception {
027: try {
028: new ObjectName("missingColon");
029: fail("Wrong ObjectName");
030: } catch (MalformedObjectNameException x) {
031: }
032:
033: try {
034: new ObjectName("newLinePresent" + '\n' + ":k=v");
035: fail("Wrong ObjectName");
036: } catch (MalformedObjectNameException x) {
037: }
038: }
039:
040: public void testValidNonPatternDomain() throws Exception {
041: ObjectName name = new ObjectName(":k=v");
042: if (name.isDomainPattern())
043: fail("Not a domain pattern");
044:
045: name = new ObjectName("domain:k=v");
046: if (name.isDomainPattern())
047: fail("Not a domain pattern");
048: }
049:
050: public void testValidPatternDomain() throws Exception {
051: ObjectName name = new ObjectName("*:k=v");
052: if (!name.isDomainPattern())
053: fail("Domain is a pattern");
054:
055: name = new ObjectName("?:k=v");
056: if (!name.isDomainPattern())
057: fail("Domain is a pattern");
058:
059: name = new ObjectName("*domain:k=v");
060: if (!name.isDomainPattern())
061: fail("Domain is a pattern");
062:
063: name = new ObjectName("?domain:k=v");
064: if (!name.isDomainPattern())
065: fail("Domain is a pattern");
066:
067: name = new ObjectName("dom*ain:k=v");
068: if (!name.isDomainPattern())
069: fail("Domain is a pattern");
070:
071: name = new ObjectName("dom?ain:k=v");
072: if (!name.isDomainPattern())
073: fail("Domain is a pattern");
074:
075: name = new ObjectName("domain*:k=v");
076: if (!name.isDomainPattern())
077: fail("Domain is a pattern");
078:
079: name = new ObjectName("domain?:k=v");
080: if (!name.isDomainPattern())
081: fail("Domain is a pattern");
082: }
083:
084: public void testInvalidProperties() throws Exception {
085: try {
086: new ObjectName("noProps:");
087: fail("Wrong ObjectName");
088: } catch (MalformedObjectNameException x) {
089: }
090:
091: try {
092: new ObjectName("noPropsWithBlank: ");
093: fail("Wrong ObjectName");
094: } catch (MalformedObjectNameException x) {
095: }
096:
097: try {
098: new ObjectName("noPropsWithGarbage: abc ");
099: fail("Wrong ObjectName");
100: } catch (MalformedObjectNameException x) {
101: }
102:
103: try {
104: new ObjectName("noKey:=value");
105: fail("Wrong ObjectName");
106: } catch (MalformedObjectNameException x) {
107: }
108:
109: try {
110: new ObjectName("domain:trailingSlash=Invalid,");
111: fail("Wrong ObjectName");
112: } catch (MalformedObjectNameException x) {
113: }
114:
115: try {
116: new ObjectName("keyWithInvalidChar:key,invalid=value");
117: fail("Wrong ObjectName");
118: } catch (MalformedObjectNameException x) {
119: }
120:
121: try {
122: new ObjectName("keyWithInvalidChar:key:invalid=value");
123: fail("Wrong ObjectName");
124: } catch (MalformedObjectNameException x) {
125: }
126:
127: try {
128: new ObjectName("keyWithInvalidChar:key*invalid=value");
129: fail("Wrong ObjectName");
130: } catch (MalformedObjectNameException x) {
131: }
132:
133: try {
134: new ObjectName("keyWithInvalidChar:key?invalid=value");
135: fail("Wrong ObjectName");
136: } catch (MalformedObjectNameException x) {
137: }
138:
139: try {
140: new ObjectName("keyWithInvalidChar:?=value");
141: fail("Wrong ObjectName");
142: } catch (MalformedObjectNameException x) {
143: }
144:
145: try {
146: new ObjectName("keyWithInvalidChar:*=value");
147: fail("Wrong ObjectName");
148: } catch (MalformedObjectNameException x) {
149: }
150:
151: try {
152: new ObjectName("keyWithInvalidChar:,=value");
153: fail("Wrong ObjectName");
154: } catch (MalformedObjectNameException x) {
155: }
156:
157: try {
158: new ObjectName("duplicateKey:key=value,key=value1");
159: fail("Wrong ObjectName");
160: } catch (MalformedObjectNameException x) {
161: }
162: }
163:
164: public void testValidPatternProperties() throws Exception {
165: ObjectName name = new ObjectName("domain:*");
166: if (!name.isPropertyPattern())
167: fail("Properties are pattern");
168:
169: name = new ObjectName("domain:k=v,*");
170: if (!name.isPropertyPattern())
171: fail("Properties are pattern");
172:
173: name = new ObjectName("domain:*,k=v");
174: if (!name.isPropertyPattern())
175: fail("Properties are pattern");
176:
177: name = new ObjectName("domain:k=v,*,k1=v1");
178: if (!name.isPropertyPattern())
179: fail("Properties are pattern");
180: }
181:
182: public void testValidNonPatternProperties() throws Exception {
183: ObjectName name = new ObjectName("domain:k=v");
184: if (name.isPropertyPattern())
185: fail("Properties are not pattern");
186:
187: name = new ObjectName("domain:k=v, k1=v1");
188: if (name.isPropertyPattern())
189: fail("Properties are not pattern");
190:
191: name = new ObjectName("domain:k=\"\\*\"");
192: if (name.isPropertyPattern())
193: fail("Properties are not pattern");
194:
195: name = new ObjectName("domain:k=\",\\*\"");
196: if (name.isPropertyPattern())
197: fail("Properties are not pattern");
198: }
199:
200: public void testInvalidValue() throws Exception {
201: try {
202: new ObjectName("domain:key=newLinePresent" + '\n');
203: fail("Wrong ObjectName");
204: } catch (MalformedObjectNameException x) {
205: }
206:
207: try {
208: new ObjectName("domain:key=\"quotedNewLinePresent" + '\n'
209: + "\"");
210: fail("Wrong ObjectName");
211: } catch (MalformedObjectNameException x) {
212: }
213:
214: try {
215: // Just one quote
216: new ObjectName("domain:key=\"");
217: fail("Wrong ObjectName");
218: } catch (MalformedObjectNameException x) {
219: }
220:
221: try {
222: // Just one quote with a space
223: new ObjectName("domain:key=\" ");
224: fail("Wrong ObjectName");
225: } catch (MalformedObjectNameException x) {
226: }
227:
228: try {
229: // Just one quote with a chars
230: new ObjectName("domain:key=\"unterminatedQuote");
231: fail("Wrong ObjectName");
232: } catch (MalformedObjectNameException x) {
233: }
234:
235: try {
236: // Just one quote with an escaped quote at the end
237: new ObjectName("domain:key=\"\\\"");
238: fail("Wrong ObjectName");
239: } catch (MalformedObjectNameException x) {
240: }
241:
242: try {
243: // Just one quote with a chars and an escaped quote at the end
244: new ObjectName("domain:key=\"unterminatedQuote\\\"");
245: fail("Wrong ObjectName");
246: } catch (MalformedObjectNameException x) {
247: }
248: }
249:
250: public void testEmptyValue() throws Exception {
251: try {
252: new ObjectName("domain:key=");
253: fail("Expecting a MalformedObjectNameException");
254: } catch (MalformedObjectNameException x) {
255: // success;
256: }
257: }
258:
259: public void testInvalidUnquotedValue() throws Exception {
260: try {
261: new ObjectName("invalidValueChar:k=,");
262: fail("Wrong ObjectName");
263: } catch (MalformedObjectNameException x) {
264: }
265:
266: try {
267: new ObjectName("invalidValueChar:k=v=");
268: fail("Wrong ObjectName");
269: } catch (MalformedObjectNameException x) {
270: }
271:
272: try {
273: new ObjectName("invalidValueChar:k=v:");
274: fail("Wrong ObjectName");
275: } catch (MalformedObjectNameException x) {
276: }
277:
278: try {
279: new ObjectName("invalidValueChar:k=v\"");
280: fail("Wrong ObjectName");
281: } catch (MalformedObjectNameException x) {
282: }
283:
284: try {
285: new ObjectName("invalidValueChar:k=v*");
286: fail("Wrong ObjectName");
287: } catch (MalformedObjectNameException x) {
288: }
289:
290: try {
291: new ObjectName("invalidValueChar:k=v?");
292: fail("Wrong ObjectName");
293: } catch (MalformedObjectNameException x) {
294: }
295: }
296:
297: public void testInvalidQuotedValue() throws Exception {
298: try {
299: new ObjectName("invalidQuotedValueChar:k=\"v?\"");
300: fail("Wrong ObjectName");
301: } catch (MalformedObjectNameException x) {
302: }
303:
304: try {
305: new ObjectName("invalidQuotedValueChar:k=\"v*\"");
306: fail("Wrong ObjectName");
307: } catch (MalformedObjectNameException x) {
308: }
309:
310: try {
311: new ObjectName(
312: "invalidQuotedValueChar:evenNumberOfBackslashes=\"v"
313: + '\\' + '\\' + "*\"");
314: fail("Wrong ObjectName");
315: } catch (MalformedObjectNameException x) {
316: }
317:
318: try {
319: new ObjectName("garbage:afterQuoted=\"value\"garbage");
320: fail("Wrong ObjectName");
321: } catch (MalformedObjectNameException x) {
322: }
323:
324: try {
325: new ObjectName("invalidEscapedChar:k=\"\\x\"");
326: fail("Wrong ObjectName");
327: } catch (MalformedObjectNameException x) {
328: }
329:
330: try {
331: new ObjectName("invalidEscapedChar:k=\"\\\"");
332: fail("Wrong ObjectName");
333: } catch (MalformedObjectNameException x) {
334: }
335:
336: try {
337: new ObjectName("invalidEscapedChar:k=\"\\\\\\\"");
338: fail("Wrong ObjectName");
339: } catch (MalformedObjectNameException x) {
340: }
341:
342: try {
343: new ObjectName("invalidEscapedChar:k=\"value\\\"");
344: fail("Wrong ObjectName");
345: } catch (MalformedObjectNameException x) {
346: }
347:
348: try {
349: new ObjectName("domain", "x", "\"unterminated");
350: fail("Wrong ObjectName");
351: } catch (MalformedObjectNameException x) {
352: }
353: }
354:
355: public void testValidQuotedObjectName() throws Exception {
356: ObjectName name = new ObjectName("domain:key=\"\"");
357: name = new ObjectName("domain:key=\"\\\\\"");
358: name = new ObjectName("domain:key=\":\"");
359: name = new ObjectName("domain:key=\",\"");
360: name = new ObjectName("domain:key=\"=\"");
361: name = new ObjectName("domain:key=\"\\\"\"");
362: name = new ObjectName("domain:key=\"\\*\"");
363: name = new ObjectName("domain:key=\"\\?\"");
364:
365: name = new ObjectName("domain:key1=\"v1,v2\",key2=value2");
366: if (name.getKeyPropertyList().size() != 2)
367: fail("Too many properties");
368:
369: name = new ObjectName(
370: "domain:key1=\"k1=v1,k2=v2\", key2= value2");
371: if (name.getKeyPropertyList().size() != 2)
372: fail("Too many properties");
373:
374: name = new ObjectName("domain:key1=\"v1,\\*,v2\",*,key2=value2");
375: if (!name.isPropertyPattern())
376: fail("ObjectName is property pattern");
377: if (name.getKeyPropertyList().size() != 2)
378: fail("Too many properties");
379: }
380:
381: public void testValidObjectNameWithSpaces() throws Exception {
382: String key = " key ";
383: String value = " value ";
384: ObjectName name = new ObjectName("domain:" + key + "=" + value);
385: String val = name.getKeyProperty(key.trim());
386: if (val != null)
387: fail("Key is not present");
388: val = name.getKeyProperty(key);
389: if (!value.equals(val))
390: fail("Wrong value");
391: }
392:
393: public void testValidObjectNames() throws Exception {
394: ObjectName name = new ObjectName(
395: "domain:property1=value1,property2=value2");
396: if (name.getKeyPropertyList().size() != 2)
397: fail("Wrong properties number");
398:
399: name = new ObjectName("*:*");
400: if (!name.isPattern())
401: fail("ObjectName is a pattern");
402: if (!name.isDomainPattern())
403: fail("ObjectName is a pattern");
404: if (!name.isPropertyPattern())
405: fail("ObjectName is a pattern");
406: if (name.getKeyPropertyList().size() != 0)
407: fail("Wrong properties number");
408: if (name.getKeyPropertyListString().length() != 0)
409: fail("Wrong properties string");
410: if (name.getCanonicalKeyPropertyListString().length() != 0)
411: fail("Wrong properties string");
412:
413: name = new ObjectName("");
414: if (!name.isPattern())
415: fail("ObjectName is a pattern");
416: if (!name.isDomainPattern())
417: fail("ObjectName is a pattern");
418: if (!name.isPropertyPattern())
419: fail("ObjectName is a pattern");
420: if (name.getKeyPropertyList().size() != 0)
421: fail("Wrong properties number");
422: if (name.getKeyPropertyListString().length() != 0)
423: fail("Wrong properties string");
424: if (name.getCanonicalKeyPropertyListString().length() != 0)
425: fail("Wrong properties string");
426:
427: name = new ObjectName(":*");
428: if (!name.isPattern())
429: fail("ObjectName is a pattern");
430: if (name.isDomainPattern())
431: fail("ObjectName is not a pattern");
432: if (!name.isPropertyPattern())
433: fail("ObjectName is a pattern");
434: if (name.getKeyPropertyList().size() != 0)
435: fail("Wrong properties number");
436: if (name.getKeyPropertyListString().length() != 0)
437: fail("Wrong properties string");
438: if (name.getCanonicalKeyPropertyListString().length() != 0)
439: fail("Wrong properties string");
440:
441: name = new ObjectName(":*,property=value");
442: if (!name.isPattern())
443: fail("ObjectName is a pattern");
444: if (!name.isPropertyPattern())
445: fail("ObjectName is a pattern");
446: if (name.getKeyPropertyList().size() != 1)
447: fail("Wrong properties number");
448: if (!"property=value".equals(name.getKeyPropertyListString()))
449: fail("Wrong properties string");
450:
451: name = new ObjectName(":property=value,*");
452: if (!name.isPattern())
453: fail("ObjectName is a pattern");
454: if (!name.isPropertyPattern())
455: fail("ObjectName is a pattern");
456: if (name.getKeyPropertyList().size() != 1)
457: fail("Wrong properties number");
458: if (!"property=value".equals(name.getKeyPropertyListString()))
459: fail("Wrong properties string");
460:
461: name = new ObjectName(":property2=value2,*,property1=value1");
462: if (!name.isPattern())
463: fail("ObjectName is a pattern");
464: if (!name.isPropertyPattern())
465: fail("ObjectName is a pattern");
466: if (name.getKeyPropertyList().size() != 2)
467: fail("Wrong properties number");
468: if (!"property2=value2,property1=value1".equals(name
469: .getKeyPropertyListString()))
470: fail("Wrong properties string");
471: if (!"property1=value1,property2=value2".equals(name
472: .getCanonicalKeyPropertyListString()))
473: fail("Wrong properties string");
474:
475: name = new ObjectName("*uu*:*");
476: if (!name.isDomainPattern())
477: fail("ObjectName is a domain pattern");
478: name = new ObjectName("*domain:property=value,*");
479: if (!name.isDomainPattern())
480: fail("ObjectName is a domain pattern");
481: name = new ObjectName("??Domain:*");
482: if (!name.isDomainPattern())
483: fail("ObjectName is a domain pattern");
484: name = new ObjectName(
485: "JMImplementation:type=MBeanServerDelegate");
486: if (name.isPattern())
487: fail("ObjectName is not a pattern");
488:
489: name = new ObjectName("domain", "key", "value");
490: if (name.isPattern())
491: fail("ObjectName is not a pattern");
492: if (name.isPropertyPattern())
493: fail("ObjectName is not a pattern");
494: if (name.getKeyPropertyList().size() != 1)
495: fail("Wrong properties number");
496: }
497:
498: public void testProperties() throws Exception {
499: String properties = "b=1,a=2,d=0,c=3,aa=4";
500: String canonicals = "a=2,aa=4,b=1,c=3,d=0";
501: ObjectName name = new ObjectName(":" + properties);
502: assertEquals(properties, name.getKeyPropertyListString());
503: assertEquals(canonicals, name
504: .getCanonicalKeyPropertyListString());
505:
506: // One of the values is a quoted value with an asterisk
507: properties = "b=1,a=\"\\*2\",d=0,c=3,aa=4";
508: canonicals = "a=\"\\*2\",aa=4,b=1,c=3,d=0";
509: name = new ObjectName(":" + properties);
510: assertEquals(properties, name.getKeyPropertyListString());
511: assertEquals(canonicals, name
512: .getCanonicalKeyPropertyListString());
513:
514: // Adding the wildcard
515: name = new ObjectName(":b=1,a=\"\\*2\",d=0,*,c=3,aa=4");
516: assertEquals(properties, name.getKeyPropertyListString());
517: assertEquals(canonicals, name
518: .getCanonicalKeyPropertyListString());
519: }
520:
521: public void testCanonicalName() throws Exception {
522: String origin = "domain:a=1,b=1,c=1,*";
523: ObjectName name = new ObjectName(origin);
524: String canonical = name.getCanonicalName();
525: assertEquals(canonical, origin);
526: }
527:
528: public void testNullConstructorParameters() throws Exception {
529: try {
530: new ObjectName(null);
531: fail("Expecting a NullPointerException on null 'name'");
532: } catch (NullPointerException x) {
533: }
534:
535: try {
536: new ObjectName("domain", null);
537: fail("Expecting a NullPointerException on null 'table'");
538: } catch (NullPointerException x) {
539: }
540:
541: try {
542: new ObjectName(null, new Hashtable());
543: fail("Expecting a NullPointerException on null 'domain'");
544: } catch (NullPointerException x) {
545: }
546:
547: try {
548: new ObjectName(null, "key", "value");
549: fail("Expecting a NullPointerException on null 'domain'");
550: } catch (NullPointerException x) {
551: }
552:
553: try {
554: new ObjectName("domain", null, "value");
555: fail("Expecting a NullPointerException on null 'key'");
556: } catch (NullPointerException x) {
557: }
558:
559: try {
560: new ObjectName("domain", "key", null);
561: fail("Expecting a NullPointerException on null 'value'");
562: } catch (NullPointerException x) {
563: }
564: }
565:
566: public void testApply() throws Exception {
567: ObjectName notpatone = new ObjectName("names:id=notpatone");
568: ObjectName notpattwo = new ObjectName("names:id=notpattwo");
569: ObjectName patone = new ObjectName("names:*");
570: ObjectName pattwo = new ObjectName("names/patterns:id=pattwo,*");
571:
572: assertTrue("Expecting true on notpatone.apply(notpatone)",
573: notpatone.apply(notpatone));
574: assertTrue("Expecting true on patone.apply(notpatone)", patone
575: .apply(notpatone));
576: assertFalse("Expecting false on notpattwo.apply(notpatone)",
577: notpattwo.apply(notpatone));
578: assertFalse("Expecting false on notpat.apply(patone)",
579: notpatone.apply(patone));
580: assertFalse("Expecting false on patone.apply(pattwo)", patone
581: .apply(pattwo));
582: assertFalse("Expecting false on patone.apply(patone)", patone
583: .apply(patone));
584: }
585:
586: public void testEmptyHashtable() throws Exception {
587: try {
588: Hashtable ht = new Hashtable();
589: new ObjectName("afinedomain", ht);
590: fail("Expecting MalformedObjectNameException");
591: } catch (MalformedObjectNameException x) {
592: }
593: }
594:
595: public void testNonStringProperties() throws Exception {
596: try {
597: Hashtable ht = new Hashtable();
598: ht.put("key", new Integer(42));
599: new ObjectName("afinedomain", ht);
600: fail("Expecting MalformedObjectNameException");
601: } catch (MalformedObjectNameException x) {
602: }
603: }
604: }
|