KickJava   Java API By Example, From Geeks To Geeks.

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


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