001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.util;
019:
020: import java.util.NoSuchElementException;
021: import java.util.StringTokenizer;
022:
023: public class StringTokenizerTest extends junit.framework.TestCase {
024:
025: /**
026: * @tests java.util.StringTokenizer#StringTokenizer(java.lang.String)
027: */
028: public void test_ConstructorLjava_lang_String() {
029: // Test for method java.util.StringTokenizer(java.lang.String)
030: assertTrue("Used in tests", true);
031: }
032:
033: /**
034: * @tests java.util.StringTokenizer#StringTokenizer(java.lang.String,
035: * java.lang.String)
036: */
037: public void test_ConstructorLjava_lang_StringLjava_lang_String() {
038: // Test for method java.util.StringTokenizer(java.lang.String,
039: // java.lang.String)
040: StringTokenizer st = new StringTokenizer(
041: "This:is:a:test:String", ":");
042: assertTrue("Created incorrect tokenizer", st.countTokens() == 5
043: && (st.nextElement().equals("This")));
044: }
045:
046: /**
047: * @tests java.util.StringTokenizer#StringTokenizer(java.lang.String,
048: * java.lang.String, boolean)
049: */
050: public void test_ConstructorLjava_lang_StringLjava_lang_StringZ() {
051: // Test for method java.util.StringTokenizer(java.lang.String,
052: // java.lang.String, boolean)
053: StringTokenizer st = new StringTokenizer(
054: "This:is:a:test:String", ":", true);
055: st.nextElement();
056: assertTrue("Created incorrect tokenizer", st.countTokens() == 8
057: && (st.nextElement().equals(":")));
058: }
059:
060: /**
061: * @tests java.util.StringTokenizer#countTokens()
062: */
063: public void test_countTokens() {
064: // Test for method int java.util.StringTokenizer.countTokens()
065: StringTokenizer st = new StringTokenizer(
066: "This is a test String");
067:
068: assertEquals("Incorrect token count returned", 5, st
069: .countTokens());
070: }
071:
072: /**
073: * @tests java.util.StringTokenizer#hasMoreElements()
074: */
075: public void test_hasMoreElements() {
076: // Test for method boolean java.util.StringTokenizer.hasMoreElements()
077:
078: StringTokenizer st = new StringTokenizer(
079: "This is a test String");
080: st.nextElement();
081: assertTrue("hasMoreElements returned incorrect value", st
082: .hasMoreElements());
083: st.nextElement();
084: st.nextElement();
085: st.nextElement();
086: st.nextElement();
087: assertTrue("hasMoreElements returned incorrect value", !st
088: .hasMoreElements());
089: }
090:
091: /**
092: * @tests java.util.StringTokenizer#hasMoreTokens()
093: */
094: public void test_hasMoreTokens() {
095: // Test for method boolean java.util.StringTokenizer.hasMoreTokens()
096: StringTokenizer st = new StringTokenizer(
097: "This is a test String");
098: for (int counter = 0; counter < 5; counter++) {
099: assertTrue(
100: "StringTokenizer incorrectly reports it has no more tokens",
101: st.hasMoreTokens());
102: st.nextToken();
103: }
104: assertTrue(
105: "StringTokenizer incorrectly reports it has more tokens",
106: !st.hasMoreTokens());
107: }
108:
109: /**
110: * @tests java.util.StringTokenizer#nextElement()
111: */
112: public void test_nextElement() {
113: // Test for method java.lang.Object
114: // java.util.StringTokenizer.nextElement()
115: StringTokenizer st = new StringTokenizer(
116: "This is a test String");
117: assertEquals("nextElement returned incorrect value", "This",
118: ((String) st.nextElement()));
119: assertEquals("nextElement returned incorrect value", "is",
120: ((String) st.nextElement()));
121: assertEquals("nextElement returned incorrect value", "a",
122: ((String) st.nextElement()));
123: assertEquals("nextElement returned incorrect value", "test",
124: ((String) st.nextElement()));
125: assertEquals("nextElement returned incorrect value", "String",
126: ((String) st.nextElement()));
127: try {
128: st.nextElement();
129: fail("nextElement failed to throw a NoSuchElementException when it should have been out of elements");
130: } catch (NoSuchElementException e) {
131: return;
132: }
133: }
134:
135: /**
136: * @tests java.util.StringTokenizer#nextToken()
137: */
138: public void test_nextToken() {
139: // Test for method java.lang.String
140: // java.util.StringTokenizer.nextToken()
141: StringTokenizer st = new StringTokenizer(
142: "This is a test String");
143: assertEquals("nextToken returned incorrect value", "This", st
144: .nextToken());
145: assertEquals("nextToken returned incorrect value", "is", st
146: .nextToken());
147: assertEquals("nextToken returned incorrect value", "a", st
148: .nextToken());
149: assertEquals("nextToken returned incorrect value", "test", st
150: .nextToken());
151: assertEquals("nextToken returned incorrect value", "String", st
152: .nextToken());
153: try {
154: st.nextToken();
155: fail("nextToken failed to throw a NoSuchElementException when it should have been out of elements");
156: } catch (NoSuchElementException e) {
157: return;
158: }
159: }
160:
161: /**
162: * @tests java.util.StringTokenizer#nextToken(java.lang.String)
163: */
164: public void test_nextTokenLjava_lang_String() {
165: // Test for method java.lang.String
166: // java.util.StringTokenizer.nextToken(java.lang.String)
167: StringTokenizer st = new StringTokenizer(
168: "This is a test String");
169: assertEquals(
170: "nextToken(String) returned incorrect value with normal token String",
171: "This", st.nextToken(" "));
172: assertEquals(
173: "nextToken(String) returned incorrect value with custom token String",
174: " is a ", st.nextToken("tr"));
175: assertEquals(
176: "calling nextToken() did not use the new default delimiter list",
177: "es", st.nextToken());
178: }
179:
180: /**
181: * Sets up the fixture, for example, open a network connection. This method
182: * is called before a test is executed.
183: */
184: protected void setUp() {
185: }
186:
187: /**
188: * Tears down the fixture, for example, close a network connection. This
189: * method is called after a test is executed.
190: */
191: protected void tearDown() {
192: }
193: }
|