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 org.jboss.test.jbossmx.compliance.objectname;
023:
024: import java.util.Hashtable;
025:
026: import javax.management.MalformedObjectNameException;
027: import javax.management.ObjectName;
028:
029: import junit.framework.Test;
030: import junit.framework.TestSuite;
031:
032: import org.jboss.test.jbossmx.compliance.TestCase;
033: import org.jboss.util.platform.Java;
034:
035: /**
036: * Hammer ObjectName, making sure it spots all malformed inputs.
037: * <p/>
038: * This may look like overkill but it's not. I want each
039: * permutation to run independantly for full test coverage.
040: * <p/>
041: * This suite has twice as many tests (about 60) as my last
042: * testcase - and for that it caught one extra bug for me.
043: *
044: * @author <a href="mailto:trevor@protocool.com">Trevor Squires</a>
045: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
046: * @version $Revision: 59455 $
047: */
048: public class MalformedTestCase extends TestSuite {
049: public static final String GOOD_DOMAIN = "domain";
050: public static final String GOOD_KEY = "key1";
051: public static final String GOOD_VALUE = "val1";
052:
053: // strings containing illegal chars to use in key or value positions
054: public static final String[] BAD_KEYVALS = { "som:thing", // cannot contain domain separator
055: "som?thing", // cannot contain pattern chars
056: "som*thing", // cannot contain pattern chars
057: "som,thing", // cannot contain kvp chunk separator
058: "som=thing", // cannot contain kvp separator
059: };
060:
061: // domains containing illegal domain chars
062: public static final String[] BAD_DOMAINS = { "doma:in", // : char in domain
063: };
064:
065: // pre-cooked name strings dealing with structural malformations
066: public static final String[] BAD_FULLNAMES = {
067: "domain:key=val,key=val2", // duplicate key
068: "domain:key=val,,foo=bar", // missing kvp in middle
069: "domain:,key=val,foo=bar", // missing kvp at beginning
070: "domain:key=val,foo=bar,", // missing kvp at end
071: "domain:key=val, ,foo=bar", // malformed kvp, no = char
072: };
073:
074: public MalformedTestCase(String s) {
075: super (s);
076: }
077:
078: public static Test suite() {
079: TestSuite suite = new TestSuite("All Malformed Tests");
080:
081: // Tests for nulls
082: suite.addTest(new DomainKeyValueTEST(null, null, null));
083: suite.addTest(new DomainKeyValueTEST(null, "key1", "val1"));
084: suite.addTest(new DomainKeyValueTEST("domain", null, "val1"));
085: suite.addTest(new DomainKeyValueTEST("domain", "key1", null));
086: suite.addTest(new DomainKeyValueTEST("domain", null, null));
087: suite.addTest(new DomainHashtableTEST(null, "key1", "val1"));
088:
089: // extra stuff related to null or zero sized hashtable
090: suite.addTestSuite(DomainHashtableExtraTEST.class);
091:
092: // all illegal domain characters
093: for (int i = 0; i < BAD_DOMAINS.length; i++) {
094: suite.addTest(new FullNameTEST(BAD_DOMAINS[i] + ":"
095: + GOOD_KEY + "=" + GOOD_VALUE));
096: suite.addTest(new DomainKeyValueTEST(BAD_DOMAINS[i],
097: GOOD_KEY, GOOD_VALUE));
098: suite.addTest(new DomainHashtableTEST(BAD_DOMAINS[i],
099: GOOD_KEY, GOOD_VALUE));
100: }
101:
102: // all illegal key value characters
103: for (int i = 0; i < BAD_KEYVALS.length; i++) {
104: suite.addTest(new FullNameTEST(GOOD_DOMAIN + ":"
105: + BAD_KEYVALS[i] + "=" + GOOD_VALUE));
106: suite.addTest(new FullNameTEST(GOOD_DOMAIN + ":" + GOOD_KEY
107: + "=" + BAD_KEYVALS[i]));
108: suite.addTest(new DomainKeyValueTEST(GOOD_DOMAIN,
109: BAD_KEYVALS[i], GOOD_VALUE));
110: suite.addTest(new DomainKeyValueTEST(GOOD_DOMAIN, GOOD_KEY,
111: BAD_KEYVALS[i]));
112: suite.addTest(new DomainHashtableTEST(GOOD_DOMAIN,
113: BAD_KEYVALS[i], GOOD_VALUE));
114: suite.addTest(new DomainHashtableTEST(GOOD_DOMAIN,
115: GOOD_KEY, BAD_KEYVALS[i]));
116: }
117:
118: // all the structurally malformed fullnames
119: for (int i = 0; i < BAD_FULLNAMES.length; i++) {
120: suite.addTest(new FullNameTEST(BAD_FULLNAMES[i]));
121: }
122: if (Java.isVersion(Java.VERSION_1_5) == false) {
123: // exclude test when running under jdk5 - it's broken
124: suite.addTest(new FullNameTEST("domain:=val,foo=bar")); // JBAS-3615, empty key
125: }
126:
127: return suite;
128: }
129:
130: public static class FullNameTEST extends TestCase {
131: private String fullName;
132:
133: public FullNameTEST(String fullName) {
134: super ("testMalformed");
135: this .fullName = fullName;
136: }
137:
138: public void testMalformed() {
139: try {
140: ObjectName name = new ObjectName(fullName);
141: } catch (MalformedObjectNameException e) {
142: if (fullName != null) {
143: return;
144: }
145: } catch (NullPointerException e) {
146: if (fullName == null) {
147: return;
148: }
149: }
150: fail("invalid object name: " + fullName);
151: }
152: }
153:
154: public static class DomainKeyValueTEST extends TestCase {
155: private String domain;
156: private String key;
157: private String value;
158:
159: public DomainKeyValueTEST(String domain, String key,
160: String value) {
161: super ("testMalformed");
162: this .domain = domain;
163: this .key = key;
164: this .value = value;
165: }
166:
167: public void testMalformed() {
168: try {
169: ObjectName name = new ObjectName(domain, key, value);
170: } catch (MalformedObjectNameException e) {
171: if (domain != null && key != null && value != null) {
172: return;
173: }
174: } catch (NullPointerException e) {
175: if (domain == null || key == null || value == null) {
176: return;
177: }
178: }
179: fail("invalid object name: " + domain + ":" + key + "="
180: + value);
181: }
182: }
183:
184: public static class DomainHashtableTEST extends TestCase {
185: private String domain;
186: private String key;
187: private String value;
188:
189: public DomainHashtableTEST(String domain, String key,
190: String value) {
191: super ("testMalformed");
192: this .domain = domain;
193: this .key = key;
194: this .value = value;
195: }
196:
197: public void testMalformed() {
198: try {
199: Hashtable h = new Hashtable();
200: h.put(key, value);
201: ObjectName name = new ObjectName(domain, h);
202: } catch (MalformedObjectNameException e) {
203: if (domain != null && key != null && value != null) {
204: return;
205: }
206: } catch (NullPointerException e) {
207: if (domain == null || key == null || value == null) {
208: return;
209: }
210: }
211: fail("invalid object name: " + domain + ":" + key + "="
212: + value);
213: }
214: }
215:
216: public static class DomainHashtableExtraTEST extends TestCase {
217: public DomainHashtableExtraTEST(String s) {
218: super (s);
219: }
220:
221: public void testNullHashtable() {
222: doCheck(GOOD_DOMAIN, null, "<null>");
223: }
224:
225: public void testEmptyHashtable() {
226: doCheck(GOOD_DOMAIN, new Hashtable(), "<empty_hashtable>");
227: }
228:
229: public void testNonStringKey() {
230: Hashtable h = new Hashtable();
231: h.put(new Object(), GOOD_VALUE);
232: doCheck(GOOD_DOMAIN, h, "<non_string_key>=" + GOOD_VALUE);
233: }
234:
235: public void testNonStringValue() {
236: Hashtable h = new Hashtable();
237: h.put(GOOD_KEY, new Object());
238: doCheck(GOOD_DOMAIN, h, GOOD_KEY + "=<non_string_value>");
239: }
240:
241: private void doCheck(String domain, Hashtable table,
242: String failureHint) {
243: try {
244: ObjectName name = new ObjectName(domain, table);
245: } catch (MalformedObjectNameException e) {
246: if (domain != null && table != null) {
247: return;
248: }
249: } catch (NullPointerException e) {
250: if (domain == null || table == null) {
251: return;
252: }
253: }
254: fail("invalid object name: " + domain + ":" + failureHint);
255: }
256: }
257: }
|