KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmx > compliance > objectname > MalformedTestCase


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.jbossmx.compliance.objectname;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26 import org.jboss.test.jbossmx.compliance.TestCase;
27
28 import javax.management.MalformedObjectNameException JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import java.util.Hashtable JavaDoc;
31
32 /**
33  * Hammer ObjectName, making sure it spots all malformed inputs.
34  * <p/>
35  * This may look like overkill but it's not. I want each
36  * permutation to run independantly for full test coverage.
37  * <p/>
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  * @author <a HREF="mailto:trevor@protocool.com">Trevor Squires</a>.
42  */

43 public class MalformedTestCase
44         extends TestSuite
45 {
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 key or value positions
51
public static final String JavaDoc[] BAD_KEYVALS = {
52       "som:thing", // cannot contain domain separator
53
"som?thing", // cannot contain pattern chars
54
"som*thing", // cannot contain pattern chars
55
"som,thing", // cannot contain kvp chunk separator
56
"som=thing", // cannot contain kvp separator
57
};
58
59    // domains containing illegal domain chars
60
public static final String JavaDoc[] BAD_DOMAINS = {
61       "doma:in", // : char in domain
62
};
63
64    // pre-cooked name strings dealing with structural malformations
65
public static final String JavaDoc[] BAD_FULLNAMES = {
66       "domain:key=val,key=val2", // duplicate key
67
"domain:=,foo=bar", // both key and value empty, fails on jdk5, sun-bug #338585
68
"domain:key=val,,foo=bar", // missing kvp in middle
69
"domain:,key=val,foo=bar", // missing kvp at beginning
70
"domain:key=val,foo=bar,", // missing kvp at end
71
"domain:key=val, ,foo=bar", // malformed kvp, no = char
72
};
73
74    public MalformedTestCase(String JavaDoc s)
75    {
76       super(s);
77    }
78
79    public static Test suite()
80    {
81       TestSuite suite = new TestSuite("All Malformed Tests");
82
83       // Tests for nulls
84
suite.addTest(new DomainKeyValueTEST(null, null, null));
85       suite.addTest(new DomainKeyValueTEST(null, "key1", "val1"));
86       suite.addTest(new DomainKeyValueTEST("domain", null, "val1"));
87       suite.addTest(new DomainKeyValueTEST("domain", "key1", null));
88       suite.addTest(new DomainKeyValueTEST("domain", null, null));
89       suite.addTest(new DomainHashtableTEST(null, "key1", "val1"));
90
91       // extra stuff related to null or zero sized hashtable
92
suite.addTestSuite(DomainHashtableExtraTEST.class);
93
94       // all illegal domain characters
95
for (int i = 0; i < BAD_DOMAINS.length; i++)
96       {
97          suite.addTest(new FullNameTEST(BAD_DOMAINS[i] + ":" + GOOD_KEY + "=" + GOOD_VALUE));
98          suite.addTest(new DomainKeyValueTEST(BAD_DOMAINS[i], GOOD_KEY, GOOD_VALUE));
99          suite.addTest(new DomainHashtableTEST(BAD_DOMAINS[i], GOOD_KEY, GOOD_VALUE));
100       }
101
102       // all illegal key value characters
103
for (int i = 0; i < BAD_KEYVALS.length; i++)
104       {
105          suite.addTest(new FullNameTEST(GOOD_DOMAIN + ":" + BAD_KEYVALS[i] + "=" + GOOD_VALUE));
106          suite.addTest(new FullNameTEST(GOOD_DOMAIN + ":" + GOOD_KEY + "=" + BAD_KEYVALS[i]));
107          suite.addTest(new DomainKeyValueTEST(GOOD_DOMAIN, BAD_KEYVALS[i], GOOD_VALUE));
108          suite.addTest(new DomainKeyValueTEST(GOOD_DOMAIN, GOOD_KEY, BAD_KEYVALS[i]));
109          suite.addTest(new DomainHashtableTEST(GOOD_DOMAIN, BAD_KEYVALS[i], GOOD_VALUE));
110          suite.addTest(new DomainHashtableTEST(GOOD_DOMAIN, GOOD_KEY, BAD_KEYVALS[i]));
111       }
112
113       // all the structurally malformed fullnames
114
for (int i = 0; i < BAD_FULLNAMES.length; i++)
115       {
116          suite.addTest(new FullNameTEST(BAD_FULLNAMES[i]));
117       }
118
119       return suite;
120    }
121
122    public static class FullNameTEST extends TestCase
123    {
124       private String JavaDoc fullName;
125
126       public FullNameTEST(String JavaDoc fullName)
127       {
128          super("testMalformed");
129          this.fullName = fullName;
130       }
131
132       public void testMalformed()
133       {
134          try
135          {
136             ObjectName JavaDoc name = new ObjectName JavaDoc(fullName);
137          }
138          catch (MalformedObjectNameException JavaDoc e)
139          {
140             if (fullName != null)
141             {
142                return;
143             }
144          }
145          catch (NullPointerException JavaDoc e)
146          {
147             if (fullName == null)
148             {
149                return;
150             }
151          }
152          fail("invalid object name: " + fullName);
153       }
154    }
155
156    public static class DomainKeyValueTEST extends TestCase
157    {
158       private String JavaDoc domain;
159       private String JavaDoc key;
160       private String JavaDoc value;
161
162       public DomainKeyValueTEST(String JavaDoc domain, String JavaDoc key, String JavaDoc value)
163       {
164          super("testMalformed");
165          this.domain = domain;
166          this.key = key;
167          this.value = value;
168       }
169
170       public void testMalformed()
171       {
172          try
173          {
174             ObjectName JavaDoc name = new ObjectName JavaDoc(domain, key, value);
175          }
176          catch (MalformedObjectNameException JavaDoc e)
177          {
178             if (domain != null && key != null && value != null)
179             {
180                return;
181             }
182          }
183          catch (NullPointerException JavaDoc e)
184          {
185             if (domain == null || key == null || value == null)
186             {
187                return;
188             }
189          }
190          fail("invalid object name: " + domain + ":" + key + "=" + value);
191       }
192    }
193
194    public static class DomainHashtableTEST extends TestCase
195    {
196       private String JavaDoc domain;
197       private String JavaDoc key;
198       private String JavaDoc value;
199
200       public DomainHashtableTEST(String JavaDoc domain, String JavaDoc key, String JavaDoc value)
201       {
202          super("testMalformed");
203          this.domain = domain;
204          this.key = key;
205          this.value = value;
206       }
207
208       public void testMalformed()
209       {
210          try
211          {
212             Hashtable JavaDoc h = new Hashtable JavaDoc();
213             h.put(key, value);
214             ObjectName JavaDoc name = new ObjectName JavaDoc(domain, h);
215          }
216          catch (MalformedObjectNameException JavaDoc e)
217          {
218             if (domain != null && key != null && value != null)
219             {
220                return;
221             }
222          }
223          catch (NullPointerException JavaDoc e)
224          {
225             if (domain == null || key == null || value == null)
226             {
227                return;
228             }
229          }
230          fail("invalid object name: " + domain + ":" + key + "=" + value);
231       }
232    }
233
234    public static class DomainHashtableExtraTEST extends TestCase
235    {
236       public DomainHashtableExtraTEST(String JavaDoc s)
237       {
238          super(s);
239       }
240
241       public void testNullHashtable()
242       {
243          doCheck(GOOD_DOMAIN, null, "<null>");
244       }
245
246       public void testEmptyHashtable()
247       {
248          doCheck(GOOD_DOMAIN, new Hashtable JavaDoc(), "<empty_hashtable>");
249       }
250
251       public void testNonStringKey()
252       {
253          Hashtable JavaDoc h = new Hashtable JavaDoc();
254          h.put(new Object JavaDoc(), GOOD_VALUE);
255          doCheck(GOOD_DOMAIN, h, "<non_string_key>=" + GOOD_VALUE);
256       }
257
258       public void testNonStringValue()
259       {
260          Hashtable JavaDoc h = new Hashtable JavaDoc();
261          h.put(GOOD_KEY, new Object JavaDoc());
262          doCheck(GOOD_DOMAIN, h, GOOD_KEY + "=<non_string_value>");
263       }
264
265       private void doCheck(String JavaDoc domain, Hashtable JavaDoc table, String JavaDoc failureHint)
266       {
267          try
268          {
269             ObjectName JavaDoc name = new ObjectName JavaDoc(domain, table);
270          }
271          catch (MalformedObjectNameException JavaDoc e)
272          {
273             if (domain != null && table != null)
274             {
275                return;
276             }
277          }
278          catch (NullPointerException JavaDoc e)
279          {
280             if (domain == null || table == null)
281             {
282                return;
283             }
284          }
285          fail("invalid object name: " + domain + ":" + failureHint);
286       }
287    }
288 }
289
Popular Tags