001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jbossmx.compliance.objectname;
023:
024: import org.jboss.test.jbossmx.compliance.TestCase;
025:
026: import javax.management.MalformedObjectNameException;
027: import javax.management.ObjectName;
028:
029: public class PatternTestCase extends TestCase {
030: public PatternTestCase(String s) {
031: super (s);
032: }
033:
034: public void testBasicDomainPattern() {
035: String nameArg = "*:key1=val1,key2=val2";
036: ObjectName name = constructSafely(nameArg);
037: assertTrue("isPattern should be true", name.isPattern());
038: assertEquals("toString should be: '" + nameArg + "'", nameArg,
039: name.toString());
040: assertTrue("isPropertyPattern should be false", !name
041: .isPropertyPattern());
042: assertEquals("*", name.getDomain());
043: }
044:
045: public void testBasicDomainPatternExtra() {
046: String nameArg = "**:key1=val1,key2=val2";
047: ObjectName name = constructSafely(nameArg);
048: assertTrue("isPattern should be true", name.isPattern());
049: assertEquals("toString should be: '" + nameArg + "'", nameArg,
050: name.toString());
051: assertTrue("isPropertyPattern should be false", !name
052: .isPropertyPattern());
053: assertEquals("**", name.getDomain());
054: }
055:
056: public void testPartialDomainPattern() {
057: String nameArg = "*domain:key1=val1,key2=val2";
058: ObjectName name = constructSafely(nameArg);
059: assertTrue("isPattern should be true", name.isPattern());
060: assertEquals("toString should be: '" + nameArg + "'", nameArg,
061: name.toString());
062: assertTrue("isPropertyPattern should be false", !name
063: .isPropertyPattern());
064: assertEquals("*domain", name.getDomain());
065: }
066:
067: public void testHarderPartialDomainPattern() {
068: String nameArg = "d*n:key1=val1,key2=val2";
069: ObjectName name = constructSafely(nameArg);
070: assertTrue("isPattern should be true", name.isPattern());
071: assertEquals("toString should be: '" + nameArg + "'", nameArg,
072: name.toString());
073: assertTrue("isPropertyPattern should be false", !name
074: .isPropertyPattern());
075: assertEquals("d*n", name.getDomain());
076: }
077:
078: public void testHarderPartialDomainPatternExtra() {
079: String nameArg = "d**n:key1=val1,key2=val2";
080: ObjectName name = constructSafely(nameArg);
081: assertTrue("isPattern should be true", name.isPattern());
082: assertEquals("toString should be: '" + nameArg + "'", nameArg,
083: name.toString());
084: assertTrue("isPropertyPattern should be false", !name
085: .isPropertyPattern());
086: assertEquals("d**n", name.getDomain());
087: }
088:
089: public void testPositionalDomainPattern() {
090: String nameArg = "do??in:key1=val1,key2=val2";
091: ObjectName name = constructSafely(nameArg);
092: assertTrue("isPattern should be true", name.isPattern());
093: assertEquals("toString should be: '" + nameArg + "'", nameArg,
094: name.toString());
095: assertTrue("isPropertyPattern should be false", !name
096: .isPropertyPattern());
097: assertEquals("do??in", name.getDomain());
098: }
099:
100: public void testPatternOnly() {
101: String nameArg = "*:*";
102: ObjectName name = constructSafely(nameArg);
103: assertTrue("isPattern should be true", name.isPattern());
104: assertTrue("isPropertyPattern should be true", name
105: .isPropertyPattern());
106: // The RI incorrectly (IMHO) removes the * from propertyPatterns
107: assertEquals("FAILS IN RI", nameArg, name.getCanonicalName());
108: }
109:
110: public void testKeyPatternOnly() {
111: String nameArg = "domain:*";
112: ObjectName name = constructSafely(nameArg);
113: assertTrue("isPattern should be true", name.isPattern());
114: assertTrue("isPropertyPattern should be true", name
115: .isPropertyPattern());
116: // The RI incorrectly (IMHO) removes the * from propertyPatterns
117: assertEquals("FAILS IN RI", nameArg, name.getCanonicalName());
118: assertTrue("key properties hash should be zero size", 0 == name
119: .getKeyPropertyList().size());
120: }
121:
122: public void testPartialKeyPattern() {
123: String nameArg = "domain:key2=val2,*,key1=val1";
124: ObjectName name = constructSafely(nameArg);
125: assertTrue("isPattern should be true", name.isPattern());
126: assertTrue("isPropertyPattern should be true", name
127: .isPropertyPattern());
128: // The RI incorrectly (IMHO) removes the * from propertyPatterns
129: assertEquals("FAILS IN RI", "domain:key1=val1,key2=val2,*",
130: name.getCanonicalName());
131: assertTrue("key properties hash should only have 2 elements",
132: 2 == name.getKeyPropertyList().size());
133: }
134:
135: public void testEquality_a() {
136: ObjectName pat1 = constructSafely("domain:*,key=value");
137: ObjectName pat2 = constructSafely("domain:key=value,*");
138: assertEquals(pat1, pat2);
139: }
140:
141: public void testEquality_b() {
142: ObjectName pat1 = constructSafely("do**main:key=value,*");
143: ObjectName pat2 = constructSafely("do*main:key=value,*");
144: assertTrue(".equals() should return false", !pat1.equals(pat2));
145: }
146:
147: /* FIXME THS - this test fails when run against the RI!
148: public void testEquality_c()
149: {
150: ObjectName conc = constructSafely("domain:key=value");
151: ObjectName pat = constructSafely("domain:key=value,*");
152: assertEquals("toString() should match", conc.toString(), pat.toString());
153: assertTrue("equals() should be false", !conc.equals(pat));
154: }
155: */
156:
157: private ObjectName constructSafely(String nameArg) {
158: ObjectName name = null;
159: try {
160: name = new ObjectName(nameArg);
161: } catch (MalformedObjectNameException e) {
162: fail("spurious MalformedObjectNameException on ('"
163: + nameArg + "')");
164: }
165:
166: return name;
167: }
168: }
|