1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.text.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertTrue; 23 import static org.junit.Assert.fail; 24 25 import android.support.test.filters.SmallTest; 26 import android.support.test.runner.AndroidJUnit4; 27 import android.text.TextUtils.SimpleStringSplitter; 28 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 32 import java.util.Iterator; 33 34 /** 35 * Test {@link SimpleStringSplitter}. 36 */ 37 @SmallTest 38 @RunWith(AndroidJUnit4.class) 39 public class TextUtils_SimpleStringSplitterTest { 40 @Test 41 public void testConstructor() { 42 new SimpleStringSplitter('|'); 43 44 new SimpleStringSplitter(Character.MAX_VALUE); 45 46 new SimpleStringSplitter(Character.MIN_VALUE); 47 } 48 49 @Test 50 public void testHasNext() { 51 SimpleStringSplitter simpleStringSplitter = new SimpleStringSplitter('|'); 52 assertFalse(simpleStringSplitter.hasNext()); 53 54 simpleStringSplitter.setString("first|second"); 55 assertTrue(simpleStringSplitter.hasNext()); 56 57 simpleStringSplitter.next(); 58 assertTrue(simpleStringSplitter.hasNext()); 59 60 simpleStringSplitter.next(); 61 assertFalse(simpleStringSplitter.hasNext()); 62 63 simpleStringSplitter.setString(""); 64 assertFalse(simpleStringSplitter.hasNext()); 65 } 66 67 @Test 68 public void testIterator() { 69 SimpleStringSplitter simpleStringSplitter = new SimpleStringSplitter('|'); 70 71 Iterator<String> iterator = simpleStringSplitter.iterator(); 72 assertNotNull(iterator); 73 assertFalse(iterator.hasNext()); 74 75 simpleStringSplitter.setString("hello|world"); 76 iterator = simpleStringSplitter.iterator(); 77 assertNotNull(iterator); 78 assertTrue(iterator.hasNext()); 79 assertEquals("hello", iterator.next()); 80 assertTrue(iterator.hasNext()); 81 assertEquals("world", iterator.next()); 82 assertFalse(iterator.hasNext()); 83 } 84 85 @Test 86 public void testNext1() { 87 SimpleStringSplitter simpleStringSplitter = new SimpleStringSplitter(','); 88 89 simpleStringSplitter.setString("first, second"); 90 assertEquals("first", simpleStringSplitter.next()); 91 assertEquals(" second", simpleStringSplitter.next()); 92 try { 93 simpleStringSplitter.next(); 94 fail("Should throw StringIndexOutOfBoundsException!"); 95 } catch (StringIndexOutOfBoundsException e) { 96 } 97 } 98 99 @Test 100 public void testNext2() { 101 SimpleStringSplitter simpleStringSplitter = new SimpleStringSplitter(','); 102 103 simpleStringSplitter.setString(" ,"); 104 assertEquals(" ", simpleStringSplitter.next()); 105 // unexpected empty string 106 assertEquals("", simpleStringSplitter.next()); 107 108 simpleStringSplitter.setString(",,,"); 109 assertEquals("", simpleStringSplitter.next()); 110 assertEquals("", simpleStringSplitter.next()); 111 assertEquals("", simpleStringSplitter.next()); 112 // unexpected empty string 113 assertEquals("", simpleStringSplitter.next()); 114 } 115 116 @Test 117 public void testRemove() { 118 SimpleStringSplitter simpleStringSplitter = new SimpleStringSplitter(','); 119 120 try { 121 simpleStringSplitter.remove(); 122 fail("Should throw UnsupportedOperationException!"); 123 } catch (UnsupportedOperationException e) { 124 } 125 } 126 127 @Test 128 public void testSetString() { 129 SimpleStringSplitter simpleStringSplitter = new SimpleStringSplitter(','); 130 131 assertFalse(simpleStringSplitter.hasNext()); 132 simpleStringSplitter.setString("text1"); 133 assertTrue(simpleStringSplitter.hasNext()); 134 assertEquals("text1", simpleStringSplitter.next()); 135 assertFalse(simpleStringSplitter.hasNext()); 136 137 simpleStringSplitter.setString("text2"); 138 assertTrue(simpleStringSplitter.hasNext()); 139 assertEquals("text2", simpleStringSplitter.next()); 140 141 try { 142 simpleStringSplitter.setString(null); 143 fail("Should throw NullPointerException!"); 144 } catch (NullPointerException e) { 145 } 146 } 147 } 148