KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > ArgumentTokenizerTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util;
35
36 import edu.rice.cs.drjava.DrJavaTestCase;
37
38 import java.util.Arrays JavaDoc;
39 import java.util.List JavaDoc;
40
41 /**
42  * Tests that an ArgumentTokenizer can correctly divide up a string
43  * into command line-style arguments. Tries to follow the precedent
44  * set by a Unix bash shell in most cases.
45  * @version $Id: ArgumentTokenizerTest.java 3553 2006-02-20 21:22:09Z rcartwright $
46  */

47 public class ArgumentTokenizerTest extends DrJavaTestCase {
48
49   /**
50    * Creates a new ArgumentTokenizer to be used in every test.
51    */

52   public ArgumentTokenizerTest(String JavaDoc name) {
53     super(name);
54   }
55
56   /**
57    * Asserts that the given string is tokenized to become the
58    * given array of string arguments.
59    * @param typed A string containing all arguments (as typed by a user)
60    * @param expected What the tokenizer should return
61    */

62   protected void _assertTokenized(String JavaDoc typed, String JavaDoc[] expected) {
63     _assertTokenized(typed, expected, false);
64   }
65
66   /**
67    * Asserts that the given string is tokenized to become the
68    * given array of string arguments.
69    * @param typed A string containing all arguments (as typed by a user)
70    * @param expected What the tokenizer should return
71    * @param stringify Whether to format the resulting arguments to print
72    * out as Strings.
73    */

74   protected void _assertTokenized(String JavaDoc typed, String JavaDoc[] expected,
75                                   boolean stringify) {
76     List JavaDoc<String JavaDoc> actual = ArgumentTokenizer.tokenize(typed, stringify);
77     List JavaDoc expectedList = Arrays.asList(expected);
78     assertEquals("tokenized argument list should match expected",
79                  expectedList, actual);
80   }
81
82   /**
83    * Tests that the argument tokenizer can split up a simple list of arguments.
84    */

85   public void testTokenizeArguments() {
86     // a b c
87
// [a, b, c]
88
_assertTokenized("a b c",
89                      new String JavaDoc[]{"a","b","c"});
90     // "a b c"
91
// [a b c]
92
_assertTokenized("\"a b c\"",
93                      new String JavaDoc[]{"a b c"});
94
95     // "a b"c d
96
// [a bc, d]
97
// This behavior seems unintuitive, but it's the way both DOS and Unix
98
// handle command-line arguments.
99
_assertTokenized("\"a b\"c d",
100                      new String JavaDoc[]{"a bc","d"});
101
102     // 'a b'c d
103
// [a bc, d]
104
// This behavior seems unintuitive, but it's the way both DOS and Unix
105
// handle command-line arguments.
106
_assertTokenized("'a b'c d",
107                      new String JavaDoc[]{"a bc","d"});
108
109     // a b"c d"
110
// [a, bc d]
111
// This behavior seems unintuitive, but it's the way both DOS and Unix
112
// handle command-line arguments.
113
_assertTokenized("a b\"c d\"",
114                      new String JavaDoc[]{"a","bc d"});
115
116     // a b'c d'
117
// [a, bc d]
118
// This behavior seems unintuitive, but it's the way both DOS and Unix
119
// handle command-line arguments.
120
_assertTokenized("a b'c d'",
121                      new String JavaDoc[]{"a","bc d"});
122
123     // a b'c d'"e f" g "hi "
124
// [a, bc de f, g, hi ]
125
_assertTokenized("a b'c d'\"e f\" g \"hi \"",
126                      new String JavaDoc[]{"a","bc de f","g","hi "});
127
128     // c:\\file.txt
129
// [c:\file.txt]
130
_assertTokenized("c:\\\\file.txt",
131                      new String JavaDoc[]{"c:\\file.txt"});
132
133     // /home/user/file
134
// [/home/user/file]
135
_assertTokenized("/home/user/file",
136                      new String JavaDoc[]{"/home/user/file"});
137
138     // "asdf
139
// [asdf]
140
_assertTokenized("\"asdf",
141                      new String JavaDoc[]{"asdf"});
142   }
143
144   /**
145    * Tests that escaped characters just return the character itself.
146    * Escaped whitespace is considered a character, not a delimiter.
147    * (This is how Unix behaves.)
148    *
149    * not currently enforcing any behavior for a simple implementation
150    * using a StreamTokenizer
151    */

152   public void testTokenizeEscapedArgs() {
153     // \j
154
// [j]
155
_assertTokenized("\\j",
156                      new String JavaDoc[]{"j"});
157     // \"
158
// ["]
159
_assertTokenized("\\\"",
160                      new String JavaDoc[]{"\""});
161     // \\
162
// [\]
163
_assertTokenized("\\\\",
164                      new String JavaDoc[]{"\\"});
165     // a\ b
166
// [a b]
167
_assertTokenized("a\\ b",
168                      new String JavaDoc[]{"a b"});
169   }
170
171   /**
172    * Tests that within a quote, everything is correctly escaped.
173    * (Special characters are passed to the program correctly.)
174    */

175   public void testTokenizeQuotedEscapedArgs() {
176     // "a \" b"
177
// [a " b]
178
_assertTokenized("\"a \\\" b\"",
179                      new String JavaDoc[]{"a \" b"});
180     // "\'"
181
// [\']
182
_assertTokenized("\"'\"",
183                      new String JavaDoc[]{"'"});
184     // "\\"
185
// [\]
186
_assertTokenized("\\\\",
187                      new String JavaDoc[]{"\\"});
188     // "\" \d"
189
// [" \d]
190
_assertTokenized("\"\\\" \\d\"",
191                      new String JavaDoc[]{"\" \\d"});
192     // "\n"
193
// [\n]
194
_assertTokenized("\"\\n\"",
195                      new String JavaDoc[]{"\\n"});
196     // "\t"
197
// [\t]
198
_assertTokenized("\"\\t\"",
199                      new String JavaDoc[]{"\\t"});
200     // "\r"
201
// [\r]
202
_assertTokenized("\"\\r\"",
203                      new String JavaDoc[]{"\\r"});
204     // "\f"
205
// [\f]
206
_assertTokenized("\"\\f\"",
207                      new String JavaDoc[]{"\\f"});
208     // "\b"
209
// [\b]
210
_assertTokenized("\"\\b\"",
211                      new String JavaDoc[]{"\\b"});
212   }
213
214   /**
215    * Tests that single quotes can be used as argument delimiters.
216    * This is consistent with Unix, not with DOS.
217    */

218   public void testTokenizeSingleQuotedArgs() {
219     // 'asdf'
220
// [asdf]
221
_assertTokenized("'asdf'",
222                      new String JavaDoc[]{"asdf"});
223     // 'a b c'
224
// [a b c]
225
_assertTokenized("'a b c'",
226                      new String JavaDoc[]{"a b c"});
227     // '\'
228
// [\]
229
_assertTokenized("'\\'",
230                      new String JavaDoc[]{"\\"});
231   }
232
233   /**
234    * Tests that arguments can be "stringified" properly.
235    * (ie. formatted to be printed as a String)
236    */

237   public void testTokenizeAndStringify() {
238     // a b c
239
// ["a", "b", "c"]
240
_assertTokenized("a b c",
241                      new String JavaDoc[]{"\"a\"", "\"b\"", "\"c\""},
242                      true);
243     // \\
244
// ["\\"]
245
_assertTokenized("\\",
246                      new String JavaDoc[]{"\"\\\\\""},
247                      true);
248     // \"
249
// ["\""]
250
_assertTokenized("\\\"",
251                      new String JavaDoc[]{"\"\\\"\""},
252                      true);
253     // "\n"
254
// ["\\n"]
255
_assertTokenized("\"\\n\"",
256                      new String JavaDoc[]{"\"\\\\n\""},
257                      true);
258   }
259 }
260
Popular Tags