StringTokenizerTest.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
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 tests.api.java.util;
19
20import dalvik.annotation.TestTargetNew;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetClass;
24
25import java.util.NoSuchElementException;
26import java.util.StringTokenizer;
27
28@TestTargetClass(StringTokenizer.class)
29public class StringTokenizerTest extends junit.framework.TestCase {
30
31    /**
32     * @tests java.util.StringTokenizer#StringTokenizer(java.lang.String)
33     */
34    @TestTargetNew(
35        level = TestLevel.COMPLETE,
36        notes = "",
37        method = "StringTokenizer",
38        args = {java.lang.String.class}
39    )
40    public void test_ConstructorLjava_lang_String() {
41        // Test for method java.util.StringTokenizer(java.lang.String)
42        try {
43            new StringTokenizer(null);
44            fail("NullPointerException is not thrown.");
45        } catch(NullPointerException npe) {
46          //expected
47        }
48    }
49
50    /**
51     * @tests java.util.StringTokenizer#StringTokenizer(java.lang.String,
52     *        java.lang.String)
53     */
54    @TestTargetNew(
55        level = TestLevel.COMPLETE,
56        notes = "",
57        method = "StringTokenizer",
58        args = {java.lang.String.class, java.lang.String.class}
59    )
60    public void test_ConstructorLjava_lang_StringLjava_lang_String() {
61        // Test for method java.util.StringTokenizer(java.lang.String,
62        // java.lang.String)
63        StringTokenizer st = new StringTokenizer("This:is:a:test:String", ":");
64        assertTrue("Created incorrect tokenizer", st.countTokens() == 5
65                && (st.nextElement().equals("This")));
66        st = new StringTokenizer("This:is:a:test:String", null);
67
68        try {
69            new StringTokenizer(null, ":");
70            fail("NullPointerException expected");
71        } catch (NullPointerException e) {
72            //expected
73        }
74    }
75
76    /**
77     * @tests java.util.StringTokenizer#StringTokenizer(java.lang.String,
78     *        java.lang.String, boolean)
79     */
80    @TestTargetNew(
81        level = TestLevel.COMPLETE,
82        notes = "",
83        method = "StringTokenizer",
84        args = {java.lang.String.class, java.lang.String.class, boolean.class}
85    )
86    public void test_ConstructorLjava_lang_StringLjava_lang_StringZ() {
87        // Test for method java.util.StringTokenizer(java.lang.String,
88        // java.lang.String, boolean)
89        StringTokenizer st = new StringTokenizer("This:is:a:test:String", ":",
90                true);
91        st.nextElement();
92        assertTrue("Created incorrect tokenizer", st.countTokens() == 8
93                && (st.nextElement().equals(":")));
94        st = new StringTokenizer("This:is:a:test:String", null, true);
95        st = new StringTokenizer("This:is:a:test:String", null, false);
96
97        try {
98            new StringTokenizer(null, ":", true);
99            fail("NullPointerException expected");
100        } catch (NullPointerException e) {
101            //expected
102        }
103    }
104
105    /**
106     * @tests java.util.StringTokenizer#countTokens()
107     */
108    @TestTargetNew(
109        level = TestLevel.COMPLETE,
110        notes = "",
111        method = "countTokens",
112        args = {}
113    )
114    public void test_countTokens() {
115        // Test for method int java.util.StringTokenizer.countTokens()
116        StringTokenizer st = new StringTokenizer("This is a test String");
117
118        assertEquals("Incorrect token count returned", 5, st.countTokens());
119    }
120
121    /**
122     * @tests java.util.StringTokenizer#hasMoreElements()
123     */
124    @TestTargetNew(
125        level = TestLevel.COMPLETE,
126        notes = "",
127        method = "hasMoreElements",
128        args = {}
129    )
130    public void test_hasMoreElements() {
131        // Test for method boolean java.util.StringTokenizer.hasMoreElements()
132
133        StringTokenizer st = new StringTokenizer("This is a test String");
134        st.nextElement();
135        assertTrue("hasMoreElements returned incorrect value", st
136                .hasMoreElements());
137        st.nextElement();
138        st.nextElement();
139        st.nextElement();
140        st.nextElement();
141        assertTrue("hasMoreElements returned incorrect value", !st
142                .hasMoreElements());
143    }
144
145    /**
146     * @tests java.util.StringTokenizer#hasMoreTokens()
147     */
148    @TestTargetNew(
149        level = TestLevel.COMPLETE,
150        notes = "",
151        method = "hasMoreTokens",
152        args = {}
153    )
154    public void test_hasMoreTokens() {
155        // Test for method boolean java.util.StringTokenizer.hasMoreTokens()
156        StringTokenizer st = new StringTokenizer("This is a test String");
157        for (int counter = 0; counter < 5; counter++) {
158            assertTrue(
159                    "StringTokenizer incorrectly reports it has no more tokens",
160                    st.hasMoreTokens());
161            st.nextToken();
162        }
163        assertTrue("StringTokenizer incorrectly reports it has more tokens",
164                !st.hasMoreTokens());
165    }
166
167    /**
168     * @tests java.util.StringTokenizer#nextElement()
169     */
170    @TestTargetNew(
171        level = TestLevel.COMPLETE,
172        notes = "",
173        method = "nextElement",
174        args = {}
175    )
176    public void test_nextElement() {
177        // Test for method java.lang.Object
178        // java.util.StringTokenizer.nextElement()
179        StringTokenizer st = new StringTokenizer("This is a test String");
180        assertEquals("nextElement returned incorrect value", "This", ((String) st
181                .nextElement()));
182        assertEquals("nextElement returned incorrect value", "is", ((String) st
183                .nextElement()));
184        assertEquals("nextElement returned incorrect value", "a", ((String) st
185                .nextElement()));
186        assertEquals("nextElement returned incorrect value", "test", ((String) st
187                .nextElement()));
188        assertEquals("nextElement returned incorrect value", "String", ((String) st
189                .nextElement()));
190        try {
191            st.nextElement();
192            fail(
193                    "nextElement failed to throw a NoSuchElementException when it should have been out of elements");
194        } catch (NoSuchElementException e) {
195            return;
196        }
197    }
198
199    /**
200     * @tests java.util.StringTokenizer#nextToken()
201     */
202    @TestTargetNew(
203        level = TestLevel.COMPLETE,
204        notes = "",
205        method = "nextToken",
206        args = {}
207    )
208    public void test_nextToken() {
209        // Test for method java.lang.String
210        // java.util.StringTokenizer.nextToken()
211        StringTokenizer st = new StringTokenizer("This is a test String");
212        assertEquals("nextToken returned incorrect value",
213                "This", st.nextToken());
214        assertEquals("nextToken returned incorrect value",
215                "is", st.nextToken());
216        assertEquals("nextToken returned incorrect value",
217                "a", st.nextToken());
218        assertEquals("nextToken returned incorrect value",
219                "test", st.nextToken());
220        assertEquals("nextToken returned incorrect value",
221                "String", st.nextToken());
222        try {
223            st.nextToken();
224            fail(
225                    "nextToken failed to throw a NoSuchElementException when it should have been out of elements");
226        } catch (NoSuchElementException e) {
227            return;
228        }
229    }
230
231    /**
232     * @tests java.util.StringTokenizer#nextToken(java.lang.String)
233     */
234    @TestTargetNew(
235        level = TestLevel.COMPLETE,
236        notes = "",
237        method = "nextToken",
238        args = {java.lang.String.class}
239    )
240    public void test_nextTokenLjava_lang_String() {
241        // Test for method java.lang.String
242        // java.util.StringTokenizer.nextToken(java.lang.String)
243        StringTokenizer st = new StringTokenizer("This is a test String");
244        assertEquals("nextToken(String) returned incorrect value with normal token String",
245                "This", st.nextToken(" "));
246        assertEquals("nextToken(String) returned incorrect value with custom token String",
247                " is a ", st.nextToken("tr"));
248        assertEquals("calling nextToken() did not use the new default delimiter list",
249                "es", st.nextToken());
250        st = new StringTokenizer("This:is:a:test:String", " ");
251        assertTrue(st.nextToken(":").equals("This"));
252        assertTrue(st.nextToken(":").equals("is"));
253        assertTrue(st.nextToken(":").equals("a"));
254        assertTrue(st.nextToken(":").equals("test"));
255        assertTrue(st.nextToken(":").equals("String"));
256
257        try {
258            st.nextToken(":");
259            fail("NoSuchElementException expected");
260        } catch (NoSuchElementException e) {
261            //expected
262        }
263
264        try {
265            st.nextToken(null);
266            fail("NullPointerException expected");
267        } catch (NullPointerException e) {
268            //expected
269        }
270    }
271
272    /**
273     * Sets up the fixture, for example, open a network connection. This method
274     * is called before a test is executed.
275     */
276    protected void setUp() {
277    }
278
279    /**
280     * Tears down the fixture, for example, close a network connection. This
281     * method is called after a test is executed.
282     */
283    protected void tearDown() {
284    }
285}
286