001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util;
038:
039: import edu.rice.cs.drjava.DrJavaTestCase;
040:
041: import java.util.Arrays;
042: import java.util.List;
043:
044: /**
045: * Tests that an ArgumentTokenizer can correctly divide up a string
046: * into command line-style arguments. Tries to follow the precedent
047: * set by a Unix bash shell in most cases.
048: * @version $Id: ArgumentTokenizerTest.java 4255 2007-08-28 19:17:37Z mgricken $
049: */
050: public class ArgumentTokenizerTest extends DrJavaTestCase {
051:
052: /**
053: * Creates a new ArgumentTokenizer to be used in every test.
054: */
055: public ArgumentTokenizerTest(String name) {
056: super (name);
057: }
058:
059: /**
060: * Asserts that the given string is tokenized to become the
061: * given array of string arguments.
062: * @param typed A string containing all arguments (as typed by a user)
063: * @param expected What the tokenizer should return
064: */
065: protected void _assertTokenized(String typed, String[] expected) {
066: _assertTokenized(typed, expected, false);
067: }
068:
069: /**
070: * Asserts that the given string is tokenized to become the
071: * given array of string arguments.
072: * @param typed A string containing all arguments (as typed by a user)
073: * @param expected What the tokenizer should return
074: * @param stringify Whether to format the resulting arguments to print
075: * out as Strings.
076: */
077: protected void _assertTokenized(String typed, String[] expected,
078: boolean stringify) {
079: List<String> actual = ArgumentTokenizer.tokenize(typed,
080: stringify);
081: List expectedList = Arrays.asList(expected);
082: assertEquals("tokenized argument list should match expected",
083: expectedList, actual);
084: }
085:
086: /**
087: * Tests that the argument tokenizer can split up a simple list of arguments.
088: */
089: public void testTokenizeArguments() {
090: // a b c
091: // [a, b, c]
092: _assertTokenized("a b c", new String[] { "a", "b", "c" });
093: // "a b c"
094: // [a b c]
095: _assertTokenized("\"a b c\"", new String[] { "a b c" });
096:
097: // "a b"c d
098: // [a bc, d]
099: // This behavior seems unintuitive, but it's the way both DOS and Unix
100: // handle command-line arguments.
101: _assertTokenized("\"a b\"c d", new String[] { "a bc", "d" });
102:
103: // 'a b'c d
104: // [a bc, d]
105: // This behavior seems unintuitive, but it's the way both DOS and Unix
106: // handle command-line arguments.
107: _assertTokenized("'a b'c d", new String[] { "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\"", new String[] { "a", "bc d" });
114:
115: // a b'c d'
116: // [a, bc d]
117: // This behavior seems unintuitive, but it's the way both DOS and Unix
118: // handle command-line arguments.
119: _assertTokenized("a b'c d'", new String[] { "a", "bc d" });
120:
121: // a b'c d'"e f" g "hi "
122: // [a, bc de f, g, hi ]
123: _assertTokenized("a b'c d'\"e f\" g \"hi \"", new String[] {
124: "a", "bc de f", "g", "hi " });
125:
126: // c:\\file.txt
127: // [c:\file.txt]
128: _assertTokenized("c:\\\\file.txt",
129: new String[] { "c:\\file.txt" });
130:
131: // /home/user/file
132: // [/home/user/file]
133: _assertTokenized("/home/user/file",
134: new String[] { "/home/user/file" });
135:
136: // "asdf
137: // [asdf]
138: _assertTokenized("\"asdf", new String[] { "asdf" });
139: }
140:
141: /**
142: * Tests that escaped characters just return the character itself.
143: * Escaped whitespace is considered a character, not a delimiter.
144: * (This is how Unix behaves.)
145: *
146: * not currently enforcing any behavior for a simple implementation
147: * using a StreamTokenizer
148: */
149: public void testTokenizeEscapedArgs() {
150: // \j
151: // [j]
152: _assertTokenized("\\j", new String[] { "j" });
153: // \"
154: // ["]
155: _assertTokenized("\\\"", new String[] { "\"" });
156: // \\
157: // [\]
158: _assertTokenized("\\\\", new String[] { "\\" });
159: // a\ b
160: // [a b]
161: _assertTokenized("a\\ b", new String[] { "a b" });
162: }
163:
164: /**
165: * Tests that within a quote, everything is correctly escaped.
166: * (Special characters are passed to the program correctly.)
167: */
168: public void testTokenizeQuotedEscapedArgs() {
169: // "a \" b"
170: // [a " b]
171: _assertTokenized("\"a \\\" b\"", new String[] { "a \" b" });
172: // "\'"
173: // [\']
174: _assertTokenized("\"'\"", new String[] { "'" });
175: // "\\"
176: // [\]
177: _assertTokenized("\\\\", new String[] { "\\" });
178: // "\" \d"
179: // [" \d]
180: _assertTokenized("\"\\\" \\d\"", new String[] { "\" \\d" });
181: // "\n"
182: // [\n]
183: _assertTokenized("\"\\n\"", new String[] { "\\n" });
184: // "\t"
185: // [\t]
186: _assertTokenized("\"\\t\"", new String[] { "\\t" });
187: // "\r"
188: // [\r]
189: _assertTokenized("\"\\r\"", new String[] { "\\r" });
190: // "\f"
191: // [\f]
192: _assertTokenized("\"\\f\"", new String[] { "\\f" });
193: // "\b"
194: // [\b]
195: _assertTokenized("\"\\b\"", new String[] { "\\b" });
196: }
197:
198: /**
199: * Tests that single quotes can be used as argument delimiters.
200: * This is consistent with Unix, not with DOS.
201: */
202: public void testTokenizeSingleQuotedArgs() {
203: // 'asdf'
204: // [asdf]
205: _assertTokenized("'asdf'", new String[] { "asdf" });
206: // 'a b c'
207: // [a b c]
208: _assertTokenized("'a b c'", new String[] { "a b c" });
209: // '\'
210: // [\]
211: _assertTokenized("'\\'", new String[] { "\\" });
212: }
213:
214: /**
215: * Tests that arguments can be "stringified" properly.
216: * (ie. formatted to be printed as a String)
217: */
218: public void testTokenizeAndStringify() {
219: // a b c
220: // ["a", "b", "c"]
221: _assertTokenized("a b c", new String[] { "\"a\"", "\"b\"",
222: "\"c\"" }, true);
223: // \\
224: // ["\\"]
225: _assertTokenized("\\", new String[] { "\"\\\\\"" }, true);
226: // \"
227: // ["\""]
228: _assertTokenized("\\\"", new String[] { "\"\\\"\"" }, true);
229: // "\n"
230: // ["\\n"]
231: _assertTokenized("\"\\n\"", new String[] { "\"\\\\n\"" }, true);
232: }
233: }
|