001: // ========================================================================
002: // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.util;
016:
017: import junit.framework.TestCase;
018:
019: /**
020: * @author gregw
021: *
022: */
023: public class QuotedStringTokenizerTest extends TestCase {
024:
025: /**
026: * Constructor for QuotedStringTokenizerTest.
027: * @param arg0
028: */
029: public QuotedStringTokenizerTest(String arg0) {
030: super (arg0);
031: }
032:
033: /*
034: * Test for String nextToken()
035: */
036: public void testTokenizer0() {
037: QuotedStringTokenizer tok = new QuotedStringTokenizer(
038: "abc\n\"d\\\"'\"\n'p\\',y'\nz");
039: checkTok(tok, false, false);
040: }
041:
042: /*
043: * Test for String nextToken()
044: */
045: public void testTokenizer1() {
046: QuotedStringTokenizer tok = new QuotedStringTokenizer(
047: "abc, \"d\\\"'\",'p\\',y' z", " ,");
048: checkTok(tok, false, false);
049: }
050:
051: /*
052: * Test for String nextToken()
053: */
054: public void testTokenizer2() {
055: QuotedStringTokenizer tok = new QuotedStringTokenizer(
056: "abc, \"d\\\"'\",'p\\',y' z", " ,", false);
057: checkTok(tok, false, false);
058:
059: tok = new QuotedStringTokenizer("abc, \"d\\\"'\",'p\\',y' z",
060: " ,", true);
061: checkTok(tok, true, false);
062: }
063:
064: /*
065: * Test for String nextToken()
066: */
067: public void testTokenizer3() {
068: QuotedStringTokenizer tok;
069:
070: tok = new QuotedStringTokenizer("abc, \"d\\\"'\",'p\\',y' z",
071: " ,", false, false);
072: checkTok(tok, false, false);
073:
074: tok = new QuotedStringTokenizer("abc, \"d\\\"'\",'p\\',y' z",
075: " ,", false, true);
076: checkTok(tok, false, true);
077:
078: tok = new QuotedStringTokenizer("abc, \"d\\\"'\",'p\\',y' z",
079: " ,", true, false);
080: checkTok(tok, true, false);
081:
082: tok = new QuotedStringTokenizer("abc, \"d\\\"'\",'p\\',y' z",
083: " ,", true, true);
084: checkTok(tok, true, true);
085: }
086:
087: /*
088: * Test for String nextToken()
089: */
090: public void testTokenizer4() {
091: QuotedStringTokenizer tok = new QuotedStringTokenizer(
092: "abc'def,ghi'jkl", ",");
093: tok.setSingle(false);
094: assertEquals("abc'def", tok.nextToken());
095: assertEquals("ghi'jkl", tok.nextToken());
096: tok = new QuotedStringTokenizer("abc'def,ghi'jkl", ",");
097: tok.setSingle(true);
098: assertEquals("abcdef,ghijkl", tok.nextToken());
099: }
100:
101: private void checkTok(QuotedStringTokenizer tok, boolean delim,
102: boolean quotes) {
103: assertTrue(tok.hasMoreElements());
104: assertTrue(tok.hasMoreTokens());
105: assertEquals("abc", tok.nextToken());
106: if (delim)
107: assertEquals(",", tok.nextToken());
108: if (delim)
109: assertEquals(" ", tok.nextToken());
110:
111: assertEquals(quotes ? "\"d\\\"'\"" : "d\"'", tok.nextElement());
112: if (delim)
113: assertEquals(",", tok.nextToken());
114: assertEquals(quotes ? "'p\\',y'" : "p',y", tok.nextToken());
115: if (delim)
116: assertEquals(" ", tok.nextToken());
117: assertEquals("z", tok.nextToken());
118: assertFalse(tok.hasMoreTokens());
119: }
120:
121: /*
122: * Test for String quote(String, String)
123: */
124: public void testQuoteString() {
125: assertEquals("abc", QuotedStringTokenizer.quote("abc", " ,"));
126: assertEquals("\"a c\"", QuotedStringTokenizer
127: .quote("a c", " ,"));
128: assertEquals("\"a'c\"", QuotedStringTokenizer
129: .quote("a'c", " ,"));
130: assertEquals("\"a\\n\\r\\t\"", QuotedStringTokenizer
131: .quote("a\n\r\t"));
132: }
133:
134: public void testUnquote() {
135: assertEquals("abc", QuotedStringTokenizer.unquote("abc"));
136: assertEquals("a\"c", QuotedStringTokenizer
137: .unquote("\"a\\\"c\""));
138: assertEquals("a'c", QuotedStringTokenizer.unquote("\"a'c\""));
139: assertEquals("a\n\r\t", QuotedStringTokenizer
140: .unquote("\"a\\n\\r\\t\""));
141: }
142:
143: }
|