KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > objectname > QuoteSUITE


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.jmx.compliance.objectname;
23 import javax.management.ObjectName JavaDoc;
24
25 import junit.framework.Test;
26 import junit.framework.TestCase;
27 import junit.framework.TestSuite;
28
29 /**
30  * Tests quoting and unquoting
31  *
32  * @author <a HREF="mailto:trevor@protocool.com">Trevor Squires</a>.
33  */

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