KickJava   Java API By Example, From Geeks To Geeks.

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


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 import junit.framework.TestCase;
9 import junit.framework.TestSuite;
10 import junit.framework.Test;
11
12 import javax.management.MalformedObjectNameException;
13 import javax.management.ObjectName;
14 import java.util.Hashtable;
15
16 /**
17  * Hammer ObjectName, making sure it spots all malformed inputs.
18  *
19  * This may look like overkill but it's not. I want each
20  * permutation to run independantly for full test coverage.
21  *
22  * This suite has twice as many tests (about 60) as my last
23  * testcase - and for that it caught one extra bug for me.
24  *
25  * @todo check we are throwing MalformedObjectNameException and NullPointerException
26  * for the right errors
27  * @author <a HREF="mailto:trevor@protocool.com">Trevor Squires</a>.
28  */

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