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.tests.java.text; 19 20 import java.text.AttributedCharacterIterator; 21 import java.text.AttributedString; 22 import java.text.CharacterIterator; 23 24 public class AttributedCharacterIteratorTest extends junit.framework.TestCase { 25 26 /** 27 * @tests java.text.AttributedCharacterIterator#current() 28 */ 29 public void test_current() { 30 String test = "Test 23ring"; 31 AttributedString attrString = new AttributedString(test); 32 AttributedCharacterIterator it = attrString.getIterator(); 33 assertEquals("Wrong first", 'T', it.current()); 34 it.next(); 35 assertEquals("Wrong second", 'e', it.current()); 36 for (int i = 0; i < 9; i++) 37 it.next(); 38 assertEquals("Wrong last", 'g', it.current()); 39 it.next(); 40 assertTrue("Wrong final", it.current() == CharacterIterator.DONE); 41 42 it = attrString.getIterator(null, 2, 8); 43 assertEquals("Wrong first2", 's', it.current()); 44 } 45 46 /** 47 * @tests java.text.AttributedCharacterIterator#first() 48 */ 49 public void test_first() { 50 String test = "Test 23ring"; 51 AttributedString attrString = new AttributedString(test); 52 AttributedCharacterIterator it = attrString.getIterator(); 53 assertEquals("Wrong first1", 'T', it.first()); 54 it = attrString.getIterator(null, 0, 3); 55 assertEquals("Wrong first2", 'T', it.first()); 56 it = attrString.getIterator(null, 2, 8); 57 assertEquals("Wrong first3", 's', it.first()); 58 it = attrString.getIterator(null, 11, 11); 59 assertTrue("Wrong first4", it.first() == CharacterIterator.DONE); 60 } 61 62 /** 63 * @tests java.text.AttributedCharacterIterator#getBeginIndex() 64 */ 65 public void test_getBeginIndex() { 66 String test = "Test 23ring"; 67 AttributedString attrString = new AttributedString(test); 68 AttributedCharacterIterator it = attrString.getIterator(null, 2, 6); 69 assertEquals("Wrong begin index", 2, it.getBeginIndex()); 70 } 71 72 /** 73 * @tests java.text.AttributedCharacterIterator#getEndIndex() 74 */ 75 public void test_getEndIndex() { 76 String test = "Test 23ring"; 77 AttributedString attrString = new AttributedString(test); 78 AttributedCharacterIterator it = attrString.getIterator(null, 2, 6); 79 assertEquals("Wrong begin index", 6, it.getEndIndex()); 80 } 81 82 /** 83 * @tests java.text.AttributedCharacterIterator#getIndex() 84 */ 85 public void test_getIndex() { 86 String test = "Test 23ring"; 87 AttributedString attrString = new AttributedString(test); 88 AttributedCharacterIterator it = attrString.getIterator(); 89 assertEquals("Wrong first", 0, it.getIndex()); 90 it.next(); 91 assertEquals("Wrong second", 1, it.getIndex()); 92 for (int i = 0; i < 9; i++) 93 it.next(); 94 assertEquals("Wrong last", 10, it.getIndex()); 95 it.next(); 96 assertEquals("Wrong final", 11, it.getIndex()); 97 } 98 99 /** 100 * @tests java.text.AttributedCharacterIterator#last() 101 */ 102 public void test_last() { 103 String test = "Test 23ring"; 104 AttributedString attrString = new AttributedString(test); 105 AttributedCharacterIterator it = attrString.getIterator(); 106 assertEquals("Wrong last1", 'g', it.last()); 107 it = attrString.getIterator(null, 0, 3); 108 assertEquals("Wrong last2", 's', it.last()); 109 it = attrString.getIterator(null, 2, 8); 110 assertEquals("Wrong last3", 'r', it.last()); 111 it = attrString.getIterator(null, 0, 0); 112 assertTrue("Wrong last4", it.last() == CharacterIterator.DONE); 113 } 114 115 /** 116 * @tests java.text.AttributedCharacterIterator#next() 117 */ 118 public void test_next() { 119 String test = "Test 23ring"; 120 AttributedString attrString = new AttributedString(test); 121 AttributedCharacterIterator it = attrString.getIterator(); 122 assertEquals("Wrong first", 'e', it.next()); 123 for (int i = 0; i < 8; i++) 124 it.next(); 125 assertEquals("Wrong last", 'g', it.next()); 126 assertTrue("Wrong final", it.next() == CharacterIterator.DONE); 127 128 it = attrString.getIterator(null, 2, 8); 129 assertEquals("Wrong first2", 't', it.next()); 130 } 131 132 /** 133 * @tests java.text.AttributedCharacterIterator#previous() 134 */ 135 public void test_previous() { 136 String test = "Test 23ring"; 137 AttributedString attrString = new AttributedString(test); 138 AttributedCharacterIterator it = attrString.getIterator(); 139 it.setIndex(11); 140 assertEquals("Wrong first", 'g', it.previous()); 141 } 142 143 /** 144 * @tests java.text.AttributedCharacterIterator#setIndex(int) 145 */ 146 public void test_setIndexI() { 147 String test = "Test 23ring"; 148 AttributedString attrString = new AttributedString(test); 149 AttributedCharacterIterator it = attrString.getIterator(); 150 it.setIndex(5); 151 assertEquals("Wrong first", '2', it.current()); 152 } 153 154 /** 155 * @tests java.text.AttributedCharacterIterator#getRunLimit(java.text.AttributedCharacterIterator$Attribute) 156 */ 157 public void test_getRunLimitLjava_text_AttributedCharacterIterator$Attribute() { 158 AttributedString as = new AttributedString("test"); 159 as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, "a", 2, 160 3); 161 AttributedCharacterIterator it = as.getIterator(); 162 assertEquals("non-null value limit", 163 2, it.getRunLimit(AttributedCharacterIterator.Attribute.LANGUAGE)); 164 165 as = new AttributedString("test"); 166 as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, null, 167 2, 3); 168 it = as.getIterator(); 169 assertEquals("null value limit", 170 4, it.getRunLimit(AttributedCharacterIterator.Attribute.LANGUAGE)); 171 } 172 173 protected void setUp() { 174 } 175 176 protected void tearDown() { 177 } 178 } 179