KickJava   Java API By Example, From Geeks To Geeks.

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


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

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