KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Hashtable JavaDoc;
24
25 import javax.management.MalformedObjectNameException JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27
28 import junit.framework.Test;
29 import junit.framework.TestCase;
30 import junit.framework.TestSuite;
31
32 /**
33  * Hammer ObjectName, making sure it spots all malformed inputs.
34  *
35  * This may look like overkill but it's not. I want each
36  * permutation to run independantly for full test coverage.
37  *
38  * This suite has twice as many tests (about 60) as my last
39  * testcase - and for that it caught one extra bug for me.
40  *
41  * @todo check we are throwing MalformedObjectNameException and NullPointerException
42  * for the right errors
43  * @author <a HREF="mailto:trevor@protocool.com">Trevor Squires</a>.
44  */

45 public class MalformedSUITE extends TestSuite {
46    public static final String JavaDoc GOOD_DOMAIN = "domain";
47    public static final String JavaDoc GOOD_KEY = "key1";
48    public static final String JavaDoc GOOD_VALUE = "val1";
49
50    // strings containing illegal chars to use in keys
51
public static final String JavaDoc[] BAD_KEYS = {
52       "", // cannot be zero sized
53
"som:thing", // cannot contain domain separator
54
"som?thing", // cannot contain pattern chars
55
"som*thing", // cannot contain pattern chars
56
"som,thing", // cannot contain kvp chunk separator
57
"som=thing", // cannot contain kvp separator
58
"som\nthing", // cannot contain \n separator
59
};
60
61    // strings containing illegal chars to use in values
62
public static final String JavaDoc[] BAD_VALS = {
63       "som:thing", // cannot contain domain separator
64
"som?thing", // cannot contain pattern chars
65
"som*thing", // cannot contain pattern chars
66
"som,thing", // cannot contain kvp chunk separator
67
"som=thing", // cannot contain kvp separator
68
"som\nthing", // cannot contain \n separator
69
"som\"thing", // unterminated quote
70
"som\"\\b\"thing", // bad escape inside quote
71
"som\"" + '\n' + "\"thing", // new line inside quote
72
"som\"\"\"thing", // quote inside quote
73
"som\"?\"thing", // question mark inside quote
74
"som\"*\"thing", // asterisk inside quote
75
};
76
77    // domains containing illegal domain chars
78
public static final String JavaDoc[] BAD_DOMAINS = {
79       "doma:in", // : char in domain
80
"doma\nin", // \n char in domain
81
};
82
83    // pre-cooked name strings dealing with structural malformations
84
public static final String JavaDoc[] BAD_FULLNAMES = {
85       "domain:key=val,key=val2", // duplicate key
86
"domain:=,foo=bar", // both key and value empty
87
"domain:key=val,,foo=bar", // missing kvp in middle
88
"domain:,key=val,foo=bar", // missing kvp at beginning
89
"domain:key=val,foo=bar,", // missing kvp at end
90
"domain:key=val, ,foo=bar", // malformed kvp, no = char
91
"domain:key=val,*,*", // duplicate asterisks
92
"domain:*,key=val,*", // duplicate asterisks
93
"domain:*,key1=val1,*,key2=val2", // duplicate asterisks
94
"domain: *,key1=val1,key2=val2", // asterisk with a leading space
95
"domain:key1=val1,key2=val2, *", // asterisk with a leading space
96
"domain:key1=val1,key2=val2,* ", // asterisk with a trailing space
97
"domain:", // no properties
98
null, // null is not allowed
99
};
100
101    public static void main(String JavaDoc[] args)
102    {
103       junit.textui.TestRunner.run(suite());
104    }
105
106    public static Test suite() {
107       TestSuite suite = new TestSuite("All Malformed Tests");
108       // Tests for nulls
109
suite.addTest(new DomainKeyValueTEST(null, null, null));
110       suite.addTest(new DomainKeyValueTEST(null, "key1", "val1"));
111       suite.addTest(new DomainKeyValueTEST("domain", null, "val1"));
112       suite.addTest(new DomainKeyValueTEST("domain", "key1", null));
113       suite.addTest(new DomainKeyValueTEST("domain", null, null));
114       suite.addTest(new DomainHashtableTEST(null, "key1", "val1"));
115
116       // extra stuff related to null or zero sized hashtable
117
suite.addTestSuite(DomainHashtableExtraTEST.class);
118
119       // all illegal domain characters
120
for (int i = 0; i < BAD_DOMAINS.length; i++)
121       {
122          suite.addTest(new FullNameTEST(BAD_DOMAINS[i] + ":" + GOOD_KEY + "=" + GOOD_VALUE));
123          suite.addTest(new DomainKeyValueTEST(BAD_DOMAINS[i], GOOD_KEY, GOOD_VALUE));
124          suite.addTest(new DomainHashtableTEST(BAD_DOMAINS[i], GOOD_KEY, GOOD_VALUE));
125       }
126
127       // all illegal key characters
128
for (int i = 0; i < BAD_KEYS.length; i++)
129       {
130          suite.addTest(new FullNameTEST(GOOD_DOMAIN + ":" + BAD_KEYS[i] + "=" + GOOD_VALUE));
131          suite.addTest(new DomainKeyValueTEST(GOOD_DOMAIN, BAD_KEYS[i], GOOD_VALUE));
132          suite.addTest(new DomainHashtableTEST(GOOD_DOMAIN, BAD_KEYS[i], GOOD_VALUE));
133       }
134
135       // all illegal value characters
136
for (int i = 0; i < BAD_VALS.length; i++)
137       {
138          suite.addTest(new FullNameTEST(GOOD_DOMAIN + ":" + GOOD_KEY + "=" + BAD_VALS[i]));
139          suite.addTest(new DomainKeyValueTEST(GOOD_DOMAIN, GOOD_KEY, BAD_VALS[i]));
140          suite.addTest(new DomainHashtableTEST(GOOD_DOMAIN, GOOD_KEY, BAD_VALS[i]));
141       }
142
143       // all the structurally malformed fullnames
144
for (int i = 0; i < BAD_FULLNAMES.length; i++)
145       {
146          suite.addTest( new FullNameTEST(BAD_FULLNAMES[i]));
147       }
148
149       return suite;
150    }
151
152    public static class FullNameTEST extends TestCase
153    {
154       private String JavaDoc fullName;
155
156       public FullNameTEST(String JavaDoc fullName)
157       {
158          super("testMalformed");
159          this.fullName = fullName;
160       }
161
162       public void testMalformed()
163       {
164          boolean caught = false;
165          try
166          {
167             new ObjectName JavaDoc(fullName);
168          }
169          catch (MalformedObjectNameException JavaDoc e)
170          {
171             caught = true;
172          }
173          catch (NullPointerException JavaDoc e)
174          {
175             caught = true;
176             if (fullName != null)
177                fail("Unexpected NullPointerException for " + fullName);
178          }
179          if (caught == false)
180          {
181             if (fullName != null)
182                if (fullName.equals("domain:=val1") ||
183                    fullName.equals("domain:=,foo=bar"))
184                   fail("FAILS IN RI: expected a MalformedObjectNameException for: " + fullName);
185             fail("expected a MalformedObjectNameException for: " + fullName);
186          }
187
188          caught = false;
189          try
190          {
191             ObjectName.getInstance(fullName);
192          }
193          catch (MalformedObjectNameException JavaDoc e)
194          {
195             caught = true;
196          }
197          catch (NullPointerException JavaDoc e)
198          {
199             caught = true;
200             if (fullName != null)
201                fail("Unexpected NullPointerException for " + fullName);
202          }
203          if (caught == false)
204          {
205             if (fullName != null)
206                if (fullName.equals("domain:=val1") ||
207                    fullName.equals("domain:=,foo=bar"))
208                   fail("FAILS IN RI: expected a MalformedObjectNameException for: " + fullName);
209             fail("expected a MalformedObjectNameException for: " + fullName);
210          }
211       }
212    }
213
214    public static class DomainKeyValueTEST extends TestCase
215    {
216       private String JavaDoc domain;
217       private String JavaDoc key;
218       private String JavaDoc value;
219
220       public DomainKeyValueTEST(String JavaDoc domain, String JavaDoc key, String JavaDoc value)
221       {
222          super("testMalformed");
223          this.domain = domain;
224          this.key = key;
225          this.value = value;
226       }
227
228       public void testMalformed()
229       {
230          boolean caught = false;
231          try
232          {
233             new ObjectName JavaDoc(domain, key, value);
234          }
235          catch (MalformedObjectNameException JavaDoc e)
236          {
237             caught = true;
238          }
239          catch (NullPointerException JavaDoc e)
240          {
241             caught = true;
242             if (domain != null && key != null && value != null)
243                fail("Unexpected NullPointerException for " + domain + ":" + key + "=" + value);
244          }
245          if (caught == false)
246          {
247             if (value != null)
248                if (value.equals("som\"thing") ||
249                    value.equals("som\"\\b\"thing") ||
250                    value.equals("som\"\"\"thing"))
251                   fail("FAILS IN RI: expected a MalformedObjectNameException for: " + domain + ":" + key + "=" + value);
252             fail("expected a MalformedObjectNameException for: " + domain + ":" + key + "=" + value);
253          }
254
255          caught = false;
256          try
257          {
258             ObjectName.getInstance(domain, key, value);
259          }
260          catch (MalformedObjectNameException JavaDoc e)
261          {
262             caught = true;
263          }
264          catch (NullPointerException JavaDoc e)
265          {
266             caught = true;
267             if (domain != null && key != null && value != null)
268                fail("Unexpected NullPointerException for " + domain + ":" + key + "=" + value);
269          }
270          if (caught == false)
271          {
272             if (value != null)
273                if (value.equals("som\"thing") ||
274                    value.equals("som\"\\b\"thing") ||
275                    value.equals("som\"\"\"thing"))
276                   fail("FAILS IN RI: expected a MalformedObjectNameException for: " + domain + ":" + key + "=" + value);
277             fail("expected a MalformedObjectNameException for: " + domain + ":" + key + "=" + value);
278          }
279       }
280    }
281
282    public static class DomainHashtableTEST extends TestCase
283    {
284       private String JavaDoc domain;
285       private String JavaDoc key;
286       private String JavaDoc value;
287
288       public DomainHashtableTEST(String JavaDoc domain, String JavaDoc key, String JavaDoc value)
289       {
290          super("testMalformed");
291          this.domain = domain;
292          this.key = key;
293          this.value = value;
294       }
295
296       public void testMalformed()
297       {
298          boolean caught = false;
299          try
300          {
301             Hashtable JavaDoc h = new Hashtable JavaDoc();
302             h.put(key, value);
303             new ObjectName JavaDoc(domain, h);
304          }
305          catch (MalformedObjectNameException JavaDoc e)
306          {
307             caught = true;
308          }
309          catch (NullPointerException JavaDoc e)
310          {
311             caught = true;
312             if (domain != null && key != null && value != null)
313                fail("Unexpected NullPointerException for " + domain + ":" + key + "=" + value);
314          }
315          if (caught == false)
316          {
317             if (value != null)
318                if (value.equals("som\"thing") ||
319                    value.equals("som\"\\b\"thing") ||
320                    value.equals("som\"\"\"thing"))
321                   fail("FAILS IN RI: expected a MalformedObjectNameException for: " + domain + ":" + key + "=" + value);
322             fail("expected a MalformedObjectNameException for: " + domain + ":" + key + "=" + value);
323          }
324
325          caught = false;
326          try
327          {
328             Hashtable JavaDoc h = new Hashtable JavaDoc();
329             h.put(key, value);
330             ObjectName.getInstance(domain, h);
331          }
332          catch (MalformedObjectNameException JavaDoc e)
333          {
334             caught = true;
335          }
336          catch (NullPointerException JavaDoc e)
337          {
338             caught = true;
339             if (domain != null && key != null && value != null)
340                fail("Unexpected NullPointerException for " + domain + ":" + key + "=" + value);
341          }
342          if (caught == false)
343          {
344             if (value != null)
345                if (value.equals("som\"thing") ||
346                    value.equals("som\"\\b\"thing") ||
347                    value.equals("som\"\"\"thing"))
348                   fail("FAILS IN RI: expected a MalformedObjectNameException for: " + domain + ":" + key + "=" + value);
349             fail("expected a MalformedObjectNameException for: " + domain + ":" + key + "=" + value);
350          }
351       }
352    }
353
354    public static class DomainHashtableExtraTEST extends TestCase
355    {
356       public DomainHashtableExtraTEST(String JavaDoc s)
357       {
358          super(s);
359       }
360
361       public void testNullDomain()
362       {
363          Hashtable JavaDoc h = new Hashtable JavaDoc();
364          h.put(new Object JavaDoc(), GOOD_VALUE);
365          doCheck(null, h, "<null domain>", true);
366       }
367
368       public void testNullHashtable()
369       {
370          doCheck(GOOD_DOMAIN, null, "<null hashtable>", true);
371       }
372
373       public void testEmptyHashtable()
374       {
375          doCheck(GOOD_DOMAIN, new Hashtable JavaDoc(), "<empty_hashtable>", false);
376       }
377
378       public void testNonStringKey()
379       {
380          Hashtable JavaDoc h = new Hashtable JavaDoc();
381          h.put(new Object JavaDoc(), GOOD_VALUE);
382          doCheck(GOOD_DOMAIN, h, "<non_string_key>=" + GOOD_VALUE, false);
383       }
384
385       public void testNonStringValue()
386       {
387          Hashtable JavaDoc h = new Hashtable JavaDoc();
388          h.put(GOOD_KEY, new Object JavaDoc());
389          doCheck(GOOD_DOMAIN, h, GOOD_KEY + "=<non_string_value>", false);
390       }
391
392       private void doCheck(String JavaDoc domain, Hashtable JavaDoc h, String JavaDoc failureHint, boolean expectNull)
393       {
394          boolean caught = true;
395          try
396          {
397             new ObjectName JavaDoc(domain, h);
398          }
399          catch (MalformedObjectNameException JavaDoc e)
400          {
401             caught = true;
402             if (expectNull)
403                fail("FAILS IN RI: Expected a NullPointerException for: " + domain + ":" + failureHint);
404          }
405          catch (NullPointerException JavaDoc e)
406          {
407             if (expectNull == false)
408                fail("unexpected a NullPointerException for: " + domain + ":" + failureHint);
409          }
410          if (caught == false)
411             fail("expected a MalformedObjectNameException for: " + domain + ":" + failureHint);
412
413          caught = true;
414          try
415          {
416             ObjectName.getInstance(domain, h);
417          }
418          catch (MalformedObjectNameException JavaDoc e)
419          {
420             caught = true;
421             if (expectNull)
422                fail("FAILS IN RI: Expected a NullPointerException for: " + domain + ":" + failureHint);
423          }
424          catch (NullPointerException JavaDoc e)
425          {
426             if (expectNull == false)
427                fail("unexpected a NullPointerException for: " + domain + ":" + failureHint);
428          }
429          if (caught == false)
430             fail("expected a MalformedObjectNameException for: " + domain + ":" + failureHint);
431       }
432    }
433 }
Popular Tags