001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package test.compliance.objectname;
023:
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026: import junit.framework.Test;
027:
028: import javax.management.MalformedObjectNameException;
029: import javax.management.ObjectName;
030: import java.util.Hashtable;
031:
032: /**
033: * Hammer ObjectName, making sure it spots all malformed inputs.
034: *
035: * This may look like overkill but it's not. I want each
036: * permutation to run independantly for full test coverage.
037: *
038: * This suite has twice as many tests (about 60) as my last
039: * testcase - and for that it caught one extra bug for me.
040: *
041: * @todo check we are throwing MalformedObjectNameException and NullPointerException
042: * for the right errors
043: * @author <a href="mailto:trevor@protocool.com">Trevor Squires</a>.
044: */
045: public class MalformedSUITE extends TestSuite {
046: public static final String GOOD_DOMAIN = "domain";
047: public static final String GOOD_KEY = "key1";
048: public static final String GOOD_VALUE = "val1";
049:
050: // strings containing illegal chars to use in keys
051: public static final String[] BAD_KEYS = { "", // cannot be zero sized
052: "som:thing", // cannot contain domain separator
053: "som?thing", // cannot contain pattern chars
054: "som*thing", // cannot contain pattern chars
055: "som,thing", // cannot contain kvp chunk separator
056: "som=thing", // cannot contain kvp separator
057: "som\nthing", // cannot contain \n separator
058: };
059:
060: // strings containing illegal chars to use in values
061: public static final String[] BAD_VALS = { "som:thing", // cannot contain domain separator
062: "som?thing", // cannot contain pattern chars
063: "som*thing", // cannot contain pattern chars
064: "som,thing", // cannot contain kvp chunk separator
065: "som=thing", // cannot contain kvp separator
066: "som\nthing", // cannot contain \n separator
067: "som\"thing", // unterminated quote
068: "som\"\\b\"thing", // bad escape inside quote
069: "som\"" + '\n' + "\"thing", // new line inside quote
070: "som\"\"\"thing", // quote inside quote
071: "som\"?\"thing", // question mark inside quote
072: "som\"*\"thing", // asterisk inside quote
073: };
074:
075: // domains containing illegal domain chars
076: public static final String[] BAD_DOMAINS = { "doma:in", // : char in domain
077: "doma\nin", // \n char in domain
078: };
079:
080: // pre-cooked name strings dealing with structural malformations
081: public static final String[] BAD_FULLNAMES = {
082: "domain:key=val,key=val2", // duplicate key
083: "domain:=,foo=bar", // both key and value empty
084: "domain:key=val,,foo=bar", // missing kvp in middle
085: "domain:,key=val,foo=bar", // missing kvp at beginning
086: "domain:key=val,foo=bar,", // missing kvp at end
087: "domain:key=val, ,foo=bar", // malformed kvp, no = char
088: "domain:key=val,*,*", // duplicate asterisks
089: "domain:*,key=val,*", // duplicate asterisks
090: "domain:*,key1=val1,*,key2=val2", // duplicate asterisks
091: "domain: *,key1=val1,key2=val2", // asterisk with a leading space
092: "domain:key1=val1,key2=val2, *", // asterisk with a leading space
093: "domain:key1=val1,key2=val2,* ", // asterisk with a trailing space
094: "domain:", // no properties
095: null, // null is not allowed
096: };
097:
098: public static void main(String[] args) {
099: junit.textui.TestRunner.run(suite());
100: }
101:
102: public static Test suite() {
103: TestSuite suite = new TestSuite("All Malformed Tests");
104: // Tests for nulls
105: suite.addTest(new DomainKeyValueTEST(null, null, null));
106: suite.addTest(new DomainKeyValueTEST(null, "key1", "val1"));
107: suite.addTest(new DomainKeyValueTEST("domain", null, "val1"));
108: suite.addTest(new DomainKeyValueTEST("domain", "key1", null));
109: suite.addTest(new DomainKeyValueTEST("domain", null, null));
110: suite.addTest(new DomainHashtableTEST(null, "key1", "val1"));
111:
112: // extra stuff related to null or zero sized hashtable
113: suite.addTestSuite(DomainHashtableExtraTEST.class);
114:
115: // all illegal domain characters
116: for (int i = 0; i < BAD_DOMAINS.length; i++) {
117: suite.addTest(new FullNameTEST(BAD_DOMAINS[i] + ":"
118: + GOOD_KEY + "=" + GOOD_VALUE));
119: suite.addTest(new DomainKeyValueTEST(BAD_DOMAINS[i],
120: GOOD_KEY, GOOD_VALUE));
121: suite.addTest(new DomainHashtableTEST(BAD_DOMAINS[i],
122: GOOD_KEY, GOOD_VALUE));
123: }
124:
125: // all illegal key characters
126: for (int i = 0; i < BAD_KEYS.length; i++) {
127: suite.addTest(new FullNameTEST(GOOD_DOMAIN + ":"
128: + BAD_KEYS[i] + "=" + GOOD_VALUE));
129: suite.addTest(new DomainKeyValueTEST(GOOD_DOMAIN,
130: BAD_KEYS[i], GOOD_VALUE));
131: suite.addTest(new DomainHashtableTEST(GOOD_DOMAIN,
132: BAD_KEYS[i], GOOD_VALUE));
133: }
134:
135: // all illegal value characters
136: for (int i = 0; i < BAD_VALS.length; i++) {
137: suite.addTest(new FullNameTEST(GOOD_DOMAIN + ":" + GOOD_KEY
138: + "=" + BAD_VALS[i]));
139: suite.addTest(new DomainKeyValueTEST(GOOD_DOMAIN, GOOD_KEY,
140: BAD_VALS[i]));
141: suite.addTest(new DomainHashtableTEST(GOOD_DOMAIN,
142: GOOD_KEY, BAD_VALS[i]));
143: }
144:
145: // all the structurally malformed fullnames
146: for (int i = 0; i < BAD_FULLNAMES.length; i++) {
147: suite.addTest(new FullNameTEST(BAD_FULLNAMES[i]));
148: }
149:
150: return suite;
151: }
152:
153: public static class FullNameTEST extends TestCase {
154: private String fullName;
155:
156: public FullNameTEST(String fullName) {
157: super ("testMalformed");
158: this .fullName = fullName;
159: }
160:
161: public void testMalformed() {
162: boolean caught = false;
163: try {
164: ObjectName name = new ObjectName(fullName);
165: } catch (MalformedObjectNameException e) {
166: caught = true;
167: } catch (NullPointerException e) {
168: caught = true;
169: if (fullName != null)
170: fail("Unexpected NullPointerException for "
171: + fullName);
172: }
173: if (caught == false) {
174: if (fullName != null)
175: if (fullName.equals("domain:=val1")
176: || fullName.equals("domain:=,foo=bar"))
177: fail("FAILS IN RI: expected a MalformedObjectNameException for: "
178: + fullName);
179: fail("expected a MalformedObjectNameException for: "
180: + fullName);
181: }
182:
183: caught = false;
184: try {
185: ObjectName name = ObjectName.getInstance(fullName);
186: } catch (MalformedObjectNameException e) {
187: caught = true;
188: } catch (NullPointerException e) {
189: caught = true;
190: if (fullName != null)
191: fail("Unexpected NullPointerException for "
192: + fullName);
193: }
194: if (caught == false) {
195: if (fullName != null)
196: if (fullName.equals("domain:=val1")
197: || fullName.equals("domain:=,foo=bar"))
198: fail("FAILS IN RI: expected a MalformedObjectNameException for: "
199: + fullName);
200: fail("expected a MalformedObjectNameException for: "
201: + fullName);
202: }
203: }
204: }
205:
206: public static class DomainKeyValueTEST extends TestCase {
207: private String domain;
208: private String key;
209: private String value;
210:
211: public DomainKeyValueTEST(String domain, String key,
212: String value) {
213: super ("testMalformed");
214: this .domain = domain;
215: this .key = key;
216: this .value = value;
217: }
218:
219: public void testMalformed() {
220: boolean caught = false;
221: try {
222: ObjectName name = new ObjectName(domain, key, value);
223: } catch (MalformedObjectNameException e) {
224: caught = true;
225: } catch (NullPointerException e) {
226: caught = true;
227: if (domain != null && key != null && value != null)
228: fail("Unexpected NullPointerException for "
229: + domain + ":" + key + "=" + value);
230: }
231: if (caught == false) {
232: if (value != null)
233: if (value.equals("som\"thing")
234: || value.equals("som\"\\b\"thing")
235: || value.equals("som\"\"\"thing"))
236: fail("FAILS IN RI: expected a MalformedObjectNameException for: "
237: + domain + ":" + key + "=" + value);
238: fail("expected a MalformedObjectNameException for: "
239: + domain + ":" + key + "=" + value);
240: }
241:
242: caught = false;
243: try {
244: ObjectName name = ObjectName.getInstance(domain, key,
245: value);
246: } catch (MalformedObjectNameException e) {
247: caught = true;
248: } catch (NullPointerException e) {
249: caught = true;
250: if (domain != null && key != null && value != null)
251: fail("Unexpected NullPointerException for "
252: + domain + ":" + key + "=" + value);
253: }
254: if (caught == false) {
255: if (value != null)
256: if (value.equals("som\"thing")
257: || value.equals("som\"\\b\"thing")
258: || value.equals("som\"\"\"thing"))
259: fail("FAILS IN RI: expected a MalformedObjectNameException for: "
260: + domain + ":" + key + "=" + value);
261: fail("expected a MalformedObjectNameException for: "
262: + domain + ":" + key + "=" + value);
263: }
264: }
265: }
266:
267: public static class DomainHashtableTEST extends TestCase {
268: private String domain;
269: private String key;
270: private String value;
271:
272: public DomainHashtableTEST(String domain, String key,
273: String value) {
274: super ("testMalformed");
275: this .domain = domain;
276: this .key = key;
277: this .value = value;
278: }
279:
280: public void testMalformed() {
281: boolean caught = false;
282: try {
283: Hashtable h = new Hashtable();
284: h.put(key, value);
285: ObjectName name = new ObjectName(domain, h);
286: } catch (MalformedObjectNameException e) {
287: caught = true;
288: } catch (NullPointerException e) {
289: caught = true;
290: if (domain != null && key != null && value != null)
291: fail("Unexpected NullPointerException for "
292: + domain + ":" + key + "=" + value);
293: }
294: if (caught == false) {
295: if (value != null)
296: if (value.equals("som\"thing")
297: || value.equals("som\"\\b\"thing")
298: || value.equals("som\"\"\"thing"))
299: fail("FAILS IN RI: expected a MalformedObjectNameException for: "
300: + domain + ":" + key + "=" + value);
301: fail("expected a MalformedObjectNameException for: "
302: + domain + ":" + key + "=" + value);
303: }
304:
305: caught = false;
306: try {
307: Hashtable h = new Hashtable();
308: h.put(key, value);
309: ObjectName name = ObjectName.getInstance(domain, h);
310: } catch (MalformedObjectNameException e) {
311: caught = true;
312: } catch (NullPointerException e) {
313: caught = true;
314: if (domain != null && key != null && value != null)
315: fail("Unexpected NullPointerException for "
316: + domain + ":" + key + "=" + value);
317: }
318: if (caught == false) {
319: if (value != null)
320: if (value.equals("som\"thing")
321: || value.equals("som\"\\b\"thing")
322: || value.equals("som\"\"\"thing"))
323: fail("FAILS IN RI: expected a MalformedObjectNameException for: "
324: + domain + ":" + key + "=" + value);
325: fail("expected a MalformedObjectNameException for: "
326: + domain + ":" + key + "=" + value);
327: }
328: }
329: }
330:
331: public static class DomainHashtableExtraTEST extends TestCase {
332: public DomainHashtableExtraTEST(String s) {
333: super (s);
334: }
335:
336: public void testNullDomain() {
337: Hashtable h = new Hashtable();
338: h.put(new Object(), GOOD_VALUE);
339: doCheck(null, h, "<null domain>", true);
340: }
341:
342: public void testNullHashtable() {
343: doCheck(GOOD_DOMAIN, null, "<null hashtable>", true);
344: }
345:
346: public void testEmptyHashtable() {
347: doCheck(GOOD_DOMAIN, new Hashtable(), "<empty_hashtable>",
348: false);
349: }
350:
351: public void testNonStringKey() {
352: Hashtable h = new Hashtable();
353: h.put(new Object(), GOOD_VALUE);
354: doCheck(GOOD_DOMAIN, h, "<non_string_key>=" + GOOD_VALUE,
355: false);
356: }
357:
358: public void testNonStringValue() {
359: Hashtable h = new Hashtable();
360: h.put(GOOD_KEY, new Object());
361: doCheck(GOOD_DOMAIN, h, GOOD_KEY + "=<non_string_value>",
362: false);
363: }
364:
365: private void doCheck(String domain, Hashtable h,
366: String failureHint, boolean expectNull) {
367: boolean caught = true;
368: try {
369: ObjectName name = new ObjectName(domain, h);
370: } catch (MalformedObjectNameException e) {
371: caught = true;
372: if (expectNull)
373: fail("FAILS IN RI: Expected a NullPointerException for: "
374: + domain + ":" + failureHint);
375: } catch (NullPointerException e) {
376: if (expectNull == false)
377: fail("unexpected a NullPointerException for: "
378: + domain + ":" + failureHint);
379: }
380: if (caught == false)
381: fail("expected a MalformedObjectNameException for: "
382: + domain + ":" + failureHint);
383:
384: caught = true;
385: try {
386: ObjectName name = ObjectName.getInstance(domain, h);
387: } catch (MalformedObjectNameException e) {
388: caught = true;
389: if (expectNull)
390: fail("FAILS IN RI: Expected a NullPointerException for: "
391: + domain + ":" + failureHint);
392: } catch (NullPointerException e) {
393: if (expectNull == false)
394: fail("unexpected a NullPointerException for: "
395: + domain + ":" + failureHint);
396: }
397: if (caught == false)
398: fail("expected a MalformedObjectNameException for: "
399: + domain + ":" + failureHint);
400: }
401: }
402: }
|