1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.tests.java.util;
19
20import java.util.NoSuchElementException;
21import java.util.StringTokenizer;
22
23public class StringTokenizerTest extends junit.framework.TestCase {
24
25    /**
26     * java.util.StringTokenizer#StringTokenizer(java.lang.String)
27     */
28    public void test_ConstructorLjava_lang_String() {
29        // Test for method java.util.StringTokenizer(java.lang.String)
30        try {
31            new StringTokenizer(null);
32            fail("NullPointerException is not thrown.");
33        } catch(NullPointerException npe) {
34          //expected
35        }
36    }
37
38    /**
39     * java.util.StringTokenizer#StringTokenizer(java.lang.String,
40     *        java.lang.String)
41     */
42    public void test_ConstructorLjava_lang_StringLjava_lang_String() {
43        // Test for method java.util.StringTokenizer(java.lang.String,
44        // java.lang.String)
45        StringTokenizer st = new StringTokenizer("This:is:a:test:String", ":");
46        assertTrue("Created incorrect tokenizer", st.countTokens() == 5
47                && (st.nextElement().equals("This")));
48        st = new StringTokenizer("This:is:a:test:String", null);
49
50        try {
51            new StringTokenizer(null, ":");
52            fail("NullPointerException expected");
53        } catch (NullPointerException e) {
54            //expected
55        }
56    }
57
58    /**
59     * java.util.StringTokenizer#StringTokenizer(java.lang.String,
60     *        java.lang.String, boolean)
61     */
62    public void test_ConstructorLjava_lang_StringLjava_lang_StringZ() {
63        // Test for method java.util.StringTokenizer(java.lang.String,
64        // java.lang.String, boolean)
65        StringTokenizer st = new StringTokenizer("This:is:a:test:String", ":",
66                true);
67        st.nextElement();
68        assertTrue("Created incorrect tokenizer", st.countTokens() == 8
69                && (st.nextElement().equals(":")));
70        st = new StringTokenizer("This:is:a:test:String", null, true);
71        st = new StringTokenizer("This:is:a:test:String", null, false);
72
73        try {
74            new StringTokenizer(null, ":", true);
75            fail("NullPointerException expected");
76        } catch (NullPointerException e) {
77            //expected
78        }
79    }
80
81    /**
82     * java.util.StringTokenizer#countTokens()
83     */
84    public void test_countTokens() {
85        // Test for method int java.util.StringTokenizer.countTokens()
86        StringTokenizer st = new StringTokenizer("This is a test String");
87
88        assertEquals("Incorrect token count returned", 5, st.countTokens());
89    }
90
91    /**
92     * java.util.StringTokenizer#hasMoreElements()
93     */
94    public void test_hasMoreElements() {
95        // Test for method boolean java.util.StringTokenizer.hasMoreElements()
96
97        StringTokenizer st = new StringTokenizer("This is a test String");
98        st.nextElement();
99        assertTrue("hasMoreElements returned incorrect value", st
100                .hasMoreElements());
101        st.nextElement();
102        st.nextElement();
103        st.nextElement();
104        st.nextElement();
105        assertTrue("hasMoreElements returned incorrect value", !st
106                .hasMoreElements());
107    }
108
109    /**
110     * java.util.StringTokenizer#hasMoreTokens()
111     */
112    public void test_hasMoreTokens() {
113        // Test for method boolean java.util.StringTokenizer.hasMoreTokens()
114        StringTokenizer st = new StringTokenizer("This is a test String");
115        for (int counter = 0; counter < 5; counter++) {
116            assertTrue(
117                    "StringTokenizer incorrectly reports it has no more tokens",
118                    st.hasMoreTokens());
119            st.nextToken();
120        }
121        assertTrue("StringTokenizer incorrectly reports it has more tokens",
122                !st.hasMoreTokens());
123    }
124
125    /**
126     * java.util.StringTokenizer#nextElement()
127     */
128    public void test_nextElement() {
129        // Test for method java.lang.Object
130        // java.util.StringTokenizer.nextElement()
131        StringTokenizer st = new StringTokenizer("This is a test String");
132        assertEquals("nextElement returned incorrect value", "This", ((String) st
133                .nextElement()));
134        assertEquals("nextElement returned incorrect value", "is", ((String) st
135                .nextElement()));
136        assertEquals("nextElement returned incorrect value", "a", ((String) st
137                .nextElement()));
138        assertEquals("nextElement returned incorrect value", "test", ((String) st
139                .nextElement()));
140        assertEquals("nextElement returned incorrect value", "String", ((String) st
141                .nextElement()));
142        try {
143            st.nextElement();
144            fail(
145                    "nextElement failed to throw a NoSuchElementException when it should have been out of elements");
146        } catch (NoSuchElementException e) {
147            return;
148        }
149    }
150
151    /**
152     * java.util.StringTokenizer#nextToken()
153     */
154    public void test_nextToken() {
155        // Test for method java.lang.String
156        // java.util.StringTokenizer.nextToken()
157        StringTokenizer st = new StringTokenizer("This is a test String");
158        assertEquals("nextToken returned incorrect value",
159                "This", st.nextToken());
160        assertEquals("nextToken returned incorrect value",
161                "is", st.nextToken());
162        assertEquals("nextToken returned incorrect value",
163                "a", st.nextToken());
164        assertEquals("nextToken returned incorrect value",
165                "test", st.nextToken());
166        assertEquals("nextToken returned incorrect value",
167                "String", st.nextToken());
168        try {
169            st.nextToken();
170            fail(
171                    "nextToken failed to throw a NoSuchElementException when it should have been out of elements");
172        } catch (NoSuchElementException e) {
173            return;
174        }
175    }
176
177    /**
178     * java.util.StringTokenizer#nextToken(java.lang.String)
179     */
180    public void test_nextTokenLjava_lang_String() {
181        // Test for method java.lang.String
182        // java.util.StringTokenizer.nextToken(java.lang.String)
183        StringTokenizer st = new StringTokenizer("This is a test String");
184        assertEquals("nextToken(String) returned incorrect value with normal token String",
185                "This", st.nextToken(" "));
186        assertEquals("nextToken(String) returned incorrect value with custom token String",
187                " is a ", st.nextToken("tr"));
188        assertEquals("calling nextToken() did not use the new default delimiter list",
189                "es", st.nextToken());
190        st = new StringTokenizer("This:is:a:test:String", " ");
191        assertTrue(st.nextToken(":").equals("This"));
192        assertTrue(st.nextToken(":").equals("is"));
193        assertTrue(st.nextToken(":").equals("a"));
194        assertTrue(st.nextToken(":").equals("test"));
195        assertTrue(st.nextToken(":").equals("String"));
196
197        try {
198            st.nextToken(":");
199            fail("NoSuchElementException expected");
200        } catch (NoSuchElementException e) {
201            //expected
202        }
203
204        try {
205            st.nextToken(null);
206            fail("NullPointerException expected");
207        } catch (NullPointerException e) {
208            //expected
209        }
210    }
211
212    public void test_hasMoreElements_NPE() {
213        StringTokenizer stringTokenizer = new StringTokenizer(new String(),
214                (String) null, true);
215        try {
216            stringTokenizer.hasMoreElements();
217            fail("should throw NullPointerException");
218        } catch (NullPointerException e) {
219            // Expected
220        }
221
222        stringTokenizer = new StringTokenizer(new String(), (String) null);
223        try {
224            stringTokenizer.hasMoreElements();
225            fail("should throw NullPointerException");
226        } catch (NullPointerException e) {
227            // Expected
228        }
229    }
230
231    public void test_hasMoreTokens_NPE() {
232        StringTokenizer stringTokenizer = new StringTokenizer(new String(),
233                (String) null, true);
234        try {
235            stringTokenizer.hasMoreTokens();
236            fail("should throw NullPointerException");
237        } catch (NullPointerException e) {
238            // Expected
239        }
240
241        stringTokenizer = new StringTokenizer(new String(), (String) null);
242        try {
243            stringTokenizer.hasMoreTokens();
244            fail("should throw NullPointerException");
245        } catch (NullPointerException e) {
246            // Expected
247        }
248    }
249
250    public void test_nextElement_NPE() {
251        StringTokenizer stringTokenizer = new StringTokenizer(new String(),
252                (String) null, true);
253        try {
254            stringTokenizer.nextElement();
255            fail("should throw NullPointerException");
256        } catch (NullPointerException e) {
257            // Expected
258        }
259
260        stringTokenizer = new StringTokenizer(new String(), (String) null);
261        try {
262            stringTokenizer.nextElement();
263            fail("should throw NullPointerException");
264        } catch (NullPointerException e) {
265            // Expected
266        }
267    }
268
269    public void test_nextToken_NPE() {
270        StringTokenizer stringTokenizer = new StringTokenizer(new String(),
271                (String) null, true);
272        try {
273            stringTokenizer.nextToken();
274            fail("should throw NullPointerException");
275        } catch (NullPointerException e) {
276            // Expected
277        }
278
279        stringTokenizer = new StringTokenizer(new String(), (String) null);
280        try {
281            stringTokenizer.nextToken();
282            fail("should throw NullPointerException");
283        } catch (NullPointerException e) {
284            // Expected
285        }
286    }
287
288    public void test_nextTokenLjava_lang_String_NPE() {
289        StringTokenizer stringTokenizer = new StringTokenizer(new String());
290        try {
291            stringTokenizer.nextToken(null);
292            fail("should throw NullPointerException");
293        } catch (NullPointerException e) {
294            // Expected
295        }
296    }
297
298    /**
299     * Sets up the fixture, for example, open a network connection. This method
300     * is called before a test is executed.
301     */
302    protected void setUp() {
303    }
304
305    /**
306     * Tears down the fixture, for example, close a network connection. This
307     * method is called after a test is executed.
308     */
309    protected void tearDown() {
310    }
311}
312