KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.management.ObjectName;
9
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13
14 /**
15  * Tests quoting and unquoting
16  *
17  * @author <a HREF="mailto:trevor@protocool.com">Trevor Squires</a>.
18  */

19 public class QuoteSUITE
20    extends TestSuite
21 {
22    private static final String EMPTY = "";
23    private static final String WHITESPACE = " ";
24    private static final String DOMAIN = "domain";
25    private static final String LETTER = "A";
26    private static final String QUOTE = "\"";
27    private static final String ESCAPE = "\\";
28    private static final String ASTERISK = "*";
29    private static final String QUESTION = "?";
30    private static final String NL = "\n";
31    private static final String NEWLINE = ESCAPE + "n";
32    private static final String COLON = ":";
33    private static final String COMMA = ",";
34    private static final String EQUALS = "=";
35    private static final String KEY = "type";
36    private static final String VALUE = "user";
37    private static final String JMI = "JMImplementation";
38    private static final String TYPE = "type";
39    private static final String DELEGATE = "MBeanServerDelegate";
40
41    public static void main(String[] args)
42    {
43       junit.textui.TestRunner.run(suite());
44    }
45
46    public static Test suite()
47    {
48       TestSuite suite = new TestSuite("All Quote Tests");
49
50       // Characters that need escaping
51
suite.addTest(new QuoteTEST(QUOTE, ESCAPE + QUOTE));
52       suite.addTest(new QuoteTEST(ESCAPE, ESCAPE + ESCAPE));
53       suite.addTest(new QuoteTEST(QUESTION, ESCAPE + QUESTION));
54       suite.addTest(new QuoteTEST(ASTERISK, ESCAPE + ASTERISK));
55       suite.addTest(new QuoteTEST(NL, NEWLINE));
56
57       // Special ObjectName characters that don't need escaping
58
suite.addTest(new QuoteTEST(COLON, COLON));
59       suite.addTest(new QuoteTEST(COMMA, COMMA));
60       suite.addTest(new QuoteTEST(EQUALS, EQUALS));
61
62       // Tests with no special considerations
63
suite.addTest(new QuoteTEST(EMPTY, EMPTY));
64       suite.addTest(new QuoteTEST(WHITESPACE, WHITESPACE));
65       suite.addTest(new QuoteTEST(LETTER, LETTER));
66
67       // Here's the one from the spec
68
suite.addTest(new QuoteTEST(ASTERISK + COLON + KEY + EQUALS + VALUE + COMMA + ASTERISK,
69                                   ESCAPE + ASTERISK + COLON + KEY + EQUALS + VALUE + COMMA + ESCAPE + ASTERISK));
70
71       // And the delegate
72
suite.addTest(new QuoteTEST(JMI + COLON + TYPE + EQUALS + DELEGATE,
73                                   JMI + COLON + TYPE + EQUALS + DELEGATE));
74
75       // And select everything
76
suite.addTest(new QuoteTEST(ASTERISK + COLON + ASTERISK,
77                                   ESCAPE + ASTERISK + COLON + ESCAPE + ASTERISK));
78
79       // Unquote escaped characters
80
suite.addTest(new UnquoteTEST(ESCAPE + QUOTE, QUOTE));
81       suite.addTest(new UnquoteTEST(ESCAPE + ESCAPE, ESCAPE));
82       suite.addTest(new UnquoteTEST(ESCAPE + QUESTION, QUESTION));
83       suite.addTest(new UnquoteTEST(ESCAPE + ASTERISK, ASTERISK));
84
85       // Unquote special object name characters
86
suite.addTest(new UnquoteTEST(COLON, COLON));
87       suite.addTest(new UnquoteTEST(COMMA, COMMA));
88       suite.addTest(new UnquoteTEST(EQUALS, EQUALS));
89
90       // Unquote with no special considerations
91
suite.addTest(new UnquoteTEST(EMPTY, EMPTY));
92       suite.addTest(new UnquoteTEST(WHITESPACE, WHITESPACE));
93       suite.addTest(new UnquoteTEST(LETTER, LETTER));
94
95       // Here's the one from the spec
96
suite.addTest(new UnquoteTEST(ESCAPE + ASTERISK + COLON + KEY + EQUALS + VALUE + COMMA + ESCAPE + ASTERISK,
97                                     ASTERISK + COLON + KEY + EQUALS + VALUE + COMMA + ASTERISK));
98
99       // And the delegate
100
suite.addTest(new UnquoteTEST(JMI + COLON + TYPE + EQUALS + DELEGATE,
101                                     JMI + COLON + TYPE + EQUALS + DELEGATE));
102
103       // And select everything
104
suite.addTest(new UnquoteTEST(ESCAPE + ASTERISK + COLON + ESCAPE + ASTERISK,
105                                     ASTERISK + COLON + ASTERISK));
106
107       // Must be quoted
108
suite.addTest(new UnquoteFailuresTEST(EMPTY));
109       suite.addTest(new UnquoteFailuresTEST(LETTER + QUOTE + LETTER + QUOTE));
110       suite.addTest(new UnquoteFailuresTEST(QUOTE + LETTER + QUOTE + LETTER));
111
112       // Unterminated quote
113
suite.addTest(new UnquoteFailuresTEST(QUOTE + LETTER));
114
115       // Characters must be escaped
116
suite.addTest(new UnquoteFailuresTEST(QUOTE + QUOTE + QUOTE));
117       suite.addTest(new UnquoteFailuresTEST(QUOTE + ESCAPE + QUOTE));
118       suite.addTest(new UnquoteFailuresTEST(QUOTE + QUESTION + QUOTE));
119       suite.addTest(new UnquoteFailuresTEST(QUOTE + ASTERISK + QUOTE));
120
121       return suite;
122    }
123
124    public static class QuoteTEST
125       extends TestCase
126    {
127       private String original;
128       private String expectedResult;
129
130       public QuoteTEST(String original, String expectedResult)
131       {
132          super("testQuote");
133          this.original = original;
134          this.expectedResult = QUOTE + expectedResult + QUOTE;
135       }
136
137       public void testQuote()
138          throws Exception
139       {
140          String quoted = ObjectName.quote(original);
141          assertTrue("The quoted string for " + original + " should be " +
142                     expectedResult + " but got " + quoted, expectedResult.equals(quoted));
143
144          String quoteUnquote = ObjectName.unquote(quoted);
145          assertTrue("quote/unquote should produce the original string " +
146                     original + " but got " + quoteUnquote,
147                     original.equals(quoteUnquote));
148
149          ObjectName name = new ObjectName(DOMAIN, KEY, quoted);
150       }
151    }
152
153    public static class UnquoteTEST
154       extends TestCase
155    {
156       private String original;
157       private String expectedResult;
158
159       public UnquoteTEST(String original, String expectedResult)
160       {
161          super("testUnquote");
162          this.original = QUOTE + original + QUOTE;
163          this.expectedResult = expectedResult;
164       }
165
166       public void testUnquote()
167          throws Exception
168       {
169          String unquoted = ObjectName.unquote(original);
170          assertTrue("The unquoted string for " + original + " should be " +
171                     expectedResult + " but got " + unquoted, expectedResult.equals(unquoted));
172       }
173    }
174
175    public static class UnquoteFailuresTEST
176       extends TestCase
177    {
178       private String test;
179
180       public UnquoteFailuresTEST(String test)
181       {
182          super("testUnquoteFailures");
183          this.test = test;
184       }
185
186       public void testUnquoteFailures()
187          throws Exception
188       {
189          boolean caught = false;
190          try
191          {
192             ObjectName.unquote(test);
193          }
194          catch (Exception e)
195          {
196             caught = true;
197          }
198          assertTrue("The value " + test + " should fail in unquote", caught);
199       }
200    }
201 }
Popular Tags