KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > objectname > PatternTEST


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

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