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