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 javax.management.ObjectName;
025:
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: /**
031: * Tests quoting and unquoting
032: *
033: * @author <a href="mailto:trevor@protocool.com">Trevor Squires</a>.
034: */
035: public class QuoteSUITE extends TestSuite {
036: private static final String EMPTY = "";
037: private static final String WHITESPACE = " ";
038: private static final String DOMAIN = "domain";
039: private static final String LETTER = "A";
040: private static final String QUOTE = "\"";
041: private static final String ESCAPE = "\\";
042: private static final String ASTERISK = "*";
043: private static final String QUESTION = "?";
044: private static final String NL = "\n";
045: private static final String NEWLINE = ESCAPE + "n";
046: private static final String COLON = ":";
047: private static final String COMMA = ",";
048: private static final String EQUALS = "=";
049: private static final String KEY = "type";
050: private static final String VALUE = "user";
051: private static final String JMI = "JMImplementation";
052: private static final String TYPE = "type";
053: private static final String DELEGATE = "MBeanServerDelegate";
054:
055: public static void main(String[] args) {
056: junit.textui.TestRunner.run(suite());
057: }
058:
059: public static Test suite() {
060: TestSuite suite = new TestSuite("All Quote Tests");
061:
062: // Characters that need escaping
063: suite.addTest(new QuoteTEST(QUOTE, ESCAPE + QUOTE));
064: suite.addTest(new QuoteTEST(ESCAPE, ESCAPE + ESCAPE));
065: suite.addTest(new QuoteTEST(QUESTION, ESCAPE + QUESTION));
066: suite.addTest(new QuoteTEST(ASTERISK, ESCAPE + ASTERISK));
067: suite.addTest(new QuoteTEST(NL, NEWLINE));
068:
069: // Special ObjectName characters that don't need escaping
070: suite.addTest(new QuoteTEST(COLON, COLON));
071: suite.addTest(new QuoteTEST(COMMA, COMMA));
072: suite.addTest(new QuoteTEST(EQUALS, EQUALS));
073:
074: // Tests with no special considerations
075: suite.addTest(new QuoteTEST(EMPTY, EMPTY));
076: suite.addTest(new QuoteTEST(WHITESPACE, WHITESPACE));
077: suite.addTest(new QuoteTEST(LETTER, LETTER));
078:
079: // Here's the one from the spec
080: suite.addTest(new QuoteTEST(ASTERISK + COLON + KEY + EQUALS
081: + VALUE + COMMA + ASTERISK, ESCAPE + ASTERISK + COLON
082: + KEY + EQUALS + VALUE + COMMA + ESCAPE + ASTERISK));
083:
084: // And the delegate
085: suite.addTest(new QuoteTEST(JMI + COLON + TYPE + EQUALS
086: + DELEGATE, JMI + COLON + TYPE + EQUALS + DELEGATE));
087:
088: // And select everything
089: suite.addTest(new QuoteTEST(ASTERISK + COLON + ASTERISK, ESCAPE
090: + ASTERISK + COLON + ESCAPE + ASTERISK));
091:
092: // Unquote escaped characters
093: suite.addTest(new UnquoteTEST(ESCAPE + QUOTE, QUOTE));
094: suite.addTest(new UnquoteTEST(ESCAPE + ESCAPE, ESCAPE));
095: suite.addTest(new UnquoteTEST(ESCAPE + QUESTION, QUESTION));
096: suite.addTest(new UnquoteTEST(ESCAPE + ASTERISK, ASTERISK));
097:
098: // Unquote special object name characters
099: suite.addTest(new UnquoteTEST(COLON, COLON));
100: suite.addTest(new UnquoteTEST(COMMA, COMMA));
101: suite.addTest(new UnquoteTEST(EQUALS, EQUALS));
102:
103: // Unquote with no special considerations
104: suite.addTest(new UnquoteTEST(EMPTY, EMPTY));
105: suite.addTest(new UnquoteTEST(WHITESPACE, WHITESPACE));
106: suite.addTest(new UnquoteTEST(LETTER, LETTER));
107:
108: // Here's the one from the spec
109: suite.addTest(new UnquoteTEST(ESCAPE + ASTERISK + COLON + KEY
110: + EQUALS + VALUE + COMMA + ESCAPE + ASTERISK, ASTERISK
111: + COLON + KEY + EQUALS + VALUE + COMMA + ASTERISK));
112:
113: // And the delegate
114: suite.addTest(new UnquoteTEST(JMI + COLON + TYPE + EQUALS
115: + DELEGATE, JMI + COLON + TYPE + EQUALS + DELEGATE));
116:
117: // And select everything
118: suite.addTest(new UnquoteTEST(ESCAPE + ASTERISK + COLON
119: + ESCAPE + ASTERISK, ASTERISK + COLON + ASTERISK));
120:
121: // Must be quoted
122: suite.addTest(new UnquoteFailuresTEST(EMPTY));
123: suite.addTest(new UnquoteFailuresTEST(LETTER + QUOTE + LETTER
124: + QUOTE));
125: suite.addTest(new UnquoteFailuresTEST(QUOTE + LETTER + QUOTE
126: + LETTER));
127:
128: // Unterminated quote
129: suite.addTest(new UnquoteFailuresTEST(QUOTE + LETTER));
130:
131: // Characters must be escaped
132: suite.addTest(new UnquoteFailuresTEST(QUOTE + QUOTE + QUOTE));
133: suite.addTest(new UnquoteFailuresTEST(QUOTE + ESCAPE + QUOTE));
134: suite
135: .addTest(new UnquoteFailuresTEST(QUOTE + QUESTION
136: + QUOTE));
137: suite
138: .addTest(new UnquoteFailuresTEST(QUOTE + ASTERISK
139: + QUOTE));
140:
141: return suite;
142: }
143:
144: public static class QuoteTEST extends TestCase {
145: private String original;
146: private String expectedResult;
147:
148: public QuoteTEST(String original, String expectedResult) {
149: super ("testQuote");
150: this .original = original;
151: this .expectedResult = QUOTE + expectedResult + QUOTE;
152: }
153:
154: public void testQuote() throws Exception {
155: String quoted = ObjectName.quote(original);
156: assertTrue("The quoted string for " + original
157: + " should be " + expectedResult + " but got "
158: + quoted, expectedResult.equals(quoted));
159:
160: String quoteUnquote = ObjectName.unquote(quoted);
161: assertTrue(
162: "quote/unquote should produce the original string "
163: + original + " but got " + quoteUnquote,
164: original.equals(quoteUnquote));
165:
166: ObjectName name = new ObjectName(DOMAIN, KEY, quoted);
167: }
168: }
169:
170: public static class UnquoteTEST extends TestCase {
171: private String original;
172: private String expectedResult;
173:
174: public UnquoteTEST(String original, String expectedResult) {
175: super ("testUnquote");
176: this .original = QUOTE + original + QUOTE;
177: this .expectedResult = expectedResult;
178: }
179:
180: public void testUnquote() throws Exception {
181: String unquoted = ObjectName.unquote(original);
182: assertTrue("The unquoted string for " + original
183: + " should be " + expectedResult + " but got "
184: + unquoted, expectedResult.equals(unquoted));
185: }
186: }
187:
188: public static class UnquoteFailuresTEST extends TestCase {
189: private String test;
190:
191: public UnquoteFailuresTEST(String test) {
192: super ("testUnquoteFailures");
193: this .test = test;
194: }
195:
196: public void testUnquoteFailures() throws Exception {
197: boolean caught = false;
198: try {
199: ObjectName.unquote(test);
200: } catch (Exception e) {
201: caught = true;
202: }
203: assertTrue("The value " + test + " should fail in unquote",
204: caught);
205: }
206: }
207: }
|