KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > objectname > PatternTEST


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.jmx.compliance.objectname;
23
24 import javax.management.MalformedObjectNameException JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26
27 import junit.framework.TestCase;
28
29 public class PatternTEST extends TestCase
30 {
31    public PatternTEST(String JavaDoc s)
32    {
33       super(s);
34    }
35
36    public void testBasicDomainPattern()
37    {
38       String JavaDoc nameArg = "*:key1=val1,key2=val2";
39       ObjectName JavaDoc name = constructSafely(nameArg);
40       assertTrue("isPattern should be true", name.isPattern());
41       assertEquals("toString should be: '" + nameArg + "'", nameArg, name.toString());
42       assertTrue("isPropertyPattern should be false", !name.isPropertyPattern());
43       assertTrue("isDomainPattern should be true", name.isDomainPattern());
44       assertEquals("*", name.getDomain());
45    }
46
47    public void testBasicDomainPatternExtra()
48    {
49       String JavaDoc nameArg = "**:key1=val1,key2=val2";
50       ObjectName JavaDoc name = constructSafely(nameArg);
51       assertTrue("isPattern should be true", name.isPattern());
52       assertEquals("toString should be: '" + nameArg + "'", nameArg, name.toString());
53       assertTrue("isPropertyPattern should be false", !name.isPropertyPattern());
54       assertTrue("isDomainPattern should be true", name.isDomainPattern());
55       assertEquals("**", name.getDomain());
56    }
57
58    public void testPartialDomainPattern()
59    {
60       String JavaDoc nameArg = "*domain:key1=val1,key2=val2";
61       ObjectName JavaDoc name = constructSafely(nameArg);
62       assertTrue("isPattern should be true", name.isPattern());
63       assertEquals("toString should be: '" + nameArg + "'", nameArg, name.toString());
64       assertTrue("isPropertyPattern should be false", !name.isPropertyPattern());
65       assertTrue("isDomainPattern should be true", name.isDomainPattern());
66       assertEquals("*domain", name.getDomain());
67    }
68
69    public void testHarderPartialDomainPattern()
70    {
71       String JavaDoc nameArg = "d*n:key1=val1,key2=val2";
72       ObjectName JavaDoc name = constructSafely(nameArg);
73       assertTrue("isPattern should be true", name.isPattern());
74       assertEquals("toString should be: '" + nameArg + "'", nameArg, name.toString());
75       assertTrue("isPropertyPattern should be false", !name.isPropertyPattern());
76       assertTrue("isDomainPattern should be true", name.isDomainPattern());
77       assertEquals("d*n", name.getDomain());
78    }
79
80    public void testHarderPartialDomainPatternExtra()
81    {
82       String JavaDoc nameArg = "d**n:key1=val1,key2=val2";
83       ObjectName JavaDoc name = constructSafely(nameArg);
84       assertTrue("isPattern should be true", name.isPattern());
85       assertEquals("toString should be: '" + nameArg + "'", nameArg, name.toString());
86       assertTrue("isPropertyPattern should be false", !name.isPropertyPattern());
87       assertTrue("isDomainPattern should be true", name.isDomainPattern());
88       assertEquals("d**n", name.getDomain());
89    }
90
91    public void testPositionalDomainPattern()
92    {
93       String JavaDoc nameArg = "do??in:key1=val1,key2=val2";
94       ObjectName JavaDoc name = constructSafely(nameArg);
95       assertTrue("isPattern should be true", name.isPattern());
96       assertEquals("toString should be: '" + nameArg + "'", nameArg, name.toString());
97       assertTrue("isPropertyPattern should be false", !name.isPropertyPattern());
98       assertTrue("isDomainPattern should be true", name.isDomainPattern());
99       assertEquals("do??in", name.getDomain());
100    }
101
102    public void testPatternOnly()
103    {
104       String JavaDoc nameArg = "*:*";
105       ObjectName JavaDoc name = constructSafely(nameArg);
106       assertTrue("isPattern should be true", name.isPattern());
107       assertTrue("isPropertyPattern should be true", name.isPropertyPattern());
108       assertTrue("isDomainPattern should be true", name.isDomainPattern());
109       // The RI incorrectly (IMHO) removes the * from propertyPatterns
110
assertEquals("FAILS IN RI", nameArg, name.getCanonicalName());
111    }
112
113    public void testKeyPatternOnly()
114    {
115       String JavaDoc nameArg = "domain:*";
116       ObjectName JavaDoc name = constructSafely(nameArg);
117       assertTrue("isPattern should be true", name.isPattern());
118       assertTrue("isPropertyPattern should be true", name.isPropertyPattern());
119       assertTrue("isDomainPattern should be false", !name.isDomainPattern());
120       // The RI incorrectly (IMHO) removes the * from propertyPatterns
121
assertEquals("FAILS IN RI", nameArg, name.getCanonicalName());
122       assertTrue("key properties hash should be zero size", 0 == name.getKeyPropertyList().size());
123    }
124
125    public void testPartialKeyPattern()
126    {
127       String JavaDoc nameArg = "domain:key2=val2,*,key1=val1";
128       ObjectName JavaDoc name = constructSafely(nameArg);
129       assertTrue("isPattern should be true", name.isPattern());
130       assertTrue("isPropertyPattern should be true", name.isPropertyPattern());
131       assertTrue("isDomainPattern should be false", !name.isDomainPattern());
132       // The RI incorrectly (IMHO) removes the * from propertyPatterns
133
assertEquals("FAILS IN RI", "domain:key1=val1,key2=val2,*", name.getCanonicalName());
134       assertTrue("key properties hash should only have 2 elements", 2 == name.getKeyPropertyList().size());
135    }
136
137    public void testEquality_a()
138    {
139       ObjectName JavaDoc pat1 = constructSafely("domain:*,key=value");
140       ObjectName JavaDoc pat2 = constructSafely("domain:key=value,*");
141       assertEquals(pat1, pat2);
142    }
143
144    public void testEquality_b()
145    {
146       ObjectName JavaDoc pat1 = constructSafely("do**main:key=value,*");
147       ObjectName JavaDoc pat2 = constructSafely("do*main:key=value,*");
148       assertTrue(".equals() should return false", !pat1.equals(pat2));
149    }
150
151    public void testEquality_c()
152    {
153       ObjectName JavaDoc conc = constructSafely("domain:key=value");
154       ObjectName JavaDoc pat = constructSafely("domain:key=value,*");
155       assertTrue("toString() should not match", conc.toString().equals(pat.toString()) == false);
156       assertTrue("equals() should be false", !conc.equals(pat));
157    }
158
159    private ObjectName JavaDoc constructSafely(String JavaDoc nameArg)
160    {
161       ObjectName JavaDoc name = null;
162       try
163       {
164          name = new ObjectName JavaDoc(nameArg);
165       }
166       catch (MalformedObjectNameException JavaDoc e)
167       {
168          fail("spurious MalformedObjectNameException on ('" + nameArg + "')");
169       }
170
171       return name;
172    }
173 }
174
Popular Tags