1/* 2 * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* @test 25 * @bug 4139771 26 * @summary test all aspects of AttributedString class 27 */ 28 29import java.text.Annotation; 30import java.text.AttributedCharacterIterator; 31import java.text.AttributedCharacterIterator.Attribute; 32import java.text.AttributedString; 33import java.text.CharacterIterator; 34import java.util.HashSet; 35import java.util.Iterator; 36import java.util.Locale; 37import java.util.Map; 38import java.util.Map.Entry; 39import java.util.Set; 40 41 42public class AttributedStringTest { 43 44 private static final String text = "Hello, world!"; 45 private static final Annotation hi = new Annotation("hi"); 46 private static final int[] array5_13 = {5, 13}; 47 private static final int[] array3_9_13 = {3, 9, 13}; 48 private static final int[] array5_9_13 = {5, 9, 13}; 49 private static final int[] array3_5_9_13 = {3, 5, 9, 13}; 50 private static final Attribute[] arrayLanguage = {Attribute.LANGUAGE}; 51 private static final Attribute[] arrayLanguageReading = {Attribute.LANGUAGE, Attribute.READING}; 52 private static final Set setLanguageReading = new HashSet(); 53 static { 54 setLanguageReading.add(Attribute.LANGUAGE); 55 setLanguageReading.add(Attribute.READING); 56 } 57 58 59 public static final void main(String argv[]) throws Exception { 60 61 62 AttributedString string; 63 AttributedCharacterIterator iterator; 64 65 // create a string with text, but no attributes 66 string = new AttributedString(text); 67 iterator = string.getIterator(); 68 69 // make sure the text is there and attributes aren't 70 checkIteratorText(iterator, text); 71 if (!iterator.getAllAttributeKeys().isEmpty()) { 72 throwException(iterator, "iterator provides attributes where none are defined"); 73 } 74 75 // add an attribute to a subrange 76 string.addAttribute(Attribute.LANGUAGE, Locale.ENGLISH, 3, 9); 77 iterator = string.getIterator(); 78 79 // make sure the attribute is defined, and it's on the correct subrange 80 checkIteratorAttributeKeys(iterator, arrayLanguage); 81 checkIteratorSubranges(iterator, array3_9_13); 82 checkIteratorAttribute(iterator, 0, Attribute.LANGUAGE, null); 83 checkIteratorAttribute(iterator, 3, Attribute.LANGUAGE, Locale.ENGLISH); 84 checkIteratorAttribute(iterator, 9, Attribute.LANGUAGE, null); 85 86 // add an attribute to a subrange 87 string.addAttribute(Attribute.READING, hi, 0, 5); 88 iterator = string.getIterator(); 89 90 // make sure the attribute is defined, and it's on the correct subrange 91 checkIteratorAttributeKeys(iterator, arrayLanguageReading); 92 checkIteratorSubranges(iterator, array3_5_9_13); 93 checkIteratorAttribute(iterator, 0, Attribute.READING, hi); 94 checkIteratorAttribute(iterator, 3, Attribute.READING, hi); 95 checkIteratorAttribute(iterator, 5, Attribute.READING, null); 96 checkIteratorAttribute(iterator, 9, Attribute.READING, null); 97 98 // make sure the first attribute wasn't adversely affected 99 // in particular, we shouldn't see separate subranges (3,5) and (5,9). 100 checkIteratorSubranges(iterator, Attribute.LANGUAGE, array3_9_13); 101 checkIteratorAttribute(iterator, 0, Attribute.LANGUAGE, null); 102 checkIteratorAttribute(iterator, 3, Attribute.LANGUAGE, Locale.ENGLISH); 103 checkIteratorAttribute(iterator, 5, Attribute.LANGUAGE, Locale.ENGLISH); 104 checkIteratorAttribute(iterator, 9, Attribute.LANGUAGE, null); 105 106 // for the entire set of attributes, we expect four subranges 107 checkIteratorSubranges(iterator, setLanguageReading, array3_5_9_13); 108 109 // redefine the language attribute so that both language and reading are continuous from 0 to 5 110 string.addAttribute(Attribute.LANGUAGE, Locale.US, 0, 5); 111 iterator = string.getIterator(); 112 113 // make sure attributes got changed and merged correctly 114 checkIteratorAttributeKeys(iterator, arrayLanguageReading); 115 checkIteratorSubranges(iterator, array3_5_9_13); 116 checkIteratorSubranges(iterator, Attribute.LANGUAGE, array5_9_13); 117 checkIteratorSubranges(iterator, Attribute.READING, array5_13); 118 checkIteratorSubranges(iterator, setLanguageReading, array5_9_13); 119 checkIteratorAttribute(iterator, 0, Attribute.LANGUAGE, Locale.US); 120 checkIteratorAttribute(iterator, 3, Attribute.LANGUAGE, Locale.US); 121 checkIteratorAttribute(iterator, 5, Attribute.LANGUAGE, Locale.ENGLISH); 122 checkIteratorAttribute(iterator, 9, Attribute.LANGUAGE, null); 123 124 // make sure an annotation is only returned if its range is contained in the iterator's range 125 iterator = string.getIterator(null, 3, 5); 126 checkIteratorAttribute(iterator, 3, Attribute.READING, null); 127 checkIteratorAttribute(iterator, 5, Attribute.READING, null); 128 iterator = string.getIterator(null, 0, 4); 129 checkIteratorAttribute(iterator, 0, Attribute.READING, null); 130 checkIteratorAttribute(iterator, 3, Attribute.READING, null); 131 iterator = string.getIterator(null, 0, 5); 132 checkIteratorAttribute(iterator, 0, Attribute.READING, hi); 133 checkIteratorAttribute(iterator, 4, Attribute.READING, hi); 134 checkIteratorAttribute(iterator, 5, Attribute.READING, null); 135 136 } 137 138 private static final void checkIteratorText(AttributedCharacterIterator iterator, String expectedText) throws Exception { 139 if (iterator.getEndIndex() - iterator.getBeginIndex() != expectedText.length()) { 140 throwException(iterator, "text length doesn't match between original text and iterator"); 141 } 142 143 char c = iterator.first(); 144 for (int i = 0; i < expectedText.length(); i++) { 145 if (c != expectedText.charAt(i)) { 146 throwException(iterator, "text content doesn't match between original text and iterator"); 147 } 148 c = iterator.next(); 149 } 150 if (c != CharacterIterator.DONE) { 151 throwException(iterator, "iterator text doesn't end with DONE"); 152 } 153 } 154 155 private static final void checkIteratorAttributeKeys(AttributedCharacterIterator iterator, Attribute[] expectedKeys) throws Exception { 156 Set iteratorKeys = iterator.getAllAttributeKeys(); 157 if (iteratorKeys.size() != expectedKeys.length) { 158 throwException(iterator, "number of keys returned by iterator doesn't match expectation"); 159 } 160 for (int i = 0; i < expectedKeys.length; i++) { 161 if (!iteratorKeys.contains(expectedKeys[i])) { 162 throwException(iterator, "expected key wasn't found in iterator's key set"); 163 } 164 } 165 } 166 167 private static final void checkIteratorSubranges(AttributedCharacterIterator iterator, int[] expectedLimits) throws Exception { 168 int previous = 0; 169 char c = iterator.first(); 170 for (int i = 0; i < expectedLimits.length; i++) { 171 if (iterator.getRunStart() != previous || iterator.getRunLimit() != expectedLimits[i]) { 172 throwException(iterator, "run boundaries are not as expected: " + iterator.getRunStart() + ", " + iterator.getRunLimit()); 173 } 174 previous = expectedLimits[i]; 175 c = iterator.setIndex(previous); 176 } 177 if (c != CharacterIterator.DONE) { 178 throwException(iterator, "iterator's run sequence doesn't end with DONE"); 179 } 180 } 181 182 private static final void checkIteratorSubranges(AttributedCharacterIterator iterator, Attribute key, int[] expectedLimits) throws Exception { 183 int previous = 0; 184 char c = iterator.first(); 185 for (int i = 0; i < expectedLimits.length; i++) { 186 if (iterator.getRunStart(key) != previous || iterator.getRunLimit(key) != expectedLimits[i]) { 187 throwException(iterator, "run boundaries are not as expected: " + iterator.getRunStart(key) + ", " + iterator.getRunLimit(key) + " for key " + key); 188 } 189 previous = expectedLimits[i]; 190 c = iterator.setIndex(previous); 191 } 192 if (c != CharacterIterator.DONE) { 193 throwException(iterator, "iterator's run sequence doesn't end with DONE"); 194 } 195 } 196 197 private static final void checkIteratorSubranges(AttributedCharacterIterator iterator, Set keys, int[] expectedLimits) throws Exception { 198 int previous = 0; 199 char c = iterator.first(); 200 for (int i = 0; i < expectedLimits.length; i++) { 201 if (iterator.getRunStart(keys) != previous || iterator.getRunLimit(keys) != expectedLimits[i]) { 202 throwException(iterator, "run boundaries are not as expected: " + iterator.getRunStart(keys) + ", " + iterator.getRunLimit(keys) + " for keys " + keys); 203 } 204 previous = expectedLimits[i]; 205 c = iterator.setIndex(previous); 206 } 207 if (c != CharacterIterator.DONE) { 208 throwException(iterator, "iterator's run sequence doesn't end with DONE"); 209 } 210 } 211 212 private static final void checkIteratorAttribute(AttributedCharacterIterator iterator, int index, Attribute key, Object expectedValue) throws Exception { 213 iterator.setIndex(index); 214 Object value = iterator.getAttribute(key); 215 if (!((expectedValue == null && value == null) || (expectedValue != null && expectedValue.equals(value)))) { 216 throwException(iterator, "iterator returns wrong attribute value - " + value + " instead of " + expectedValue); 217 } 218 value = iterator.getAttributes().get(key); 219 if (!((expectedValue == null && value == null) || (expectedValue != null && expectedValue.equals(value)))) { 220 throwException(iterator, "iterator's map returns wrong attribute value - " + value + " instead of " + expectedValue); 221 } 222 } 223 224 private static final void throwException(AttributedCharacterIterator iterator, String details) throws Exception { 225 dumpIterator(iterator); 226 throw new Exception(details); 227 } 228 229 private static final void dumpIterator(AttributedCharacterIterator iterator) { 230 Set attributeKeys = iterator.getAllAttributeKeys(); 231 System.out.print("All attributes: "); 232 Iterator keyIterator = attributeKeys.iterator(); 233 while (keyIterator.hasNext()) { 234 Attribute key = (Attribute) keyIterator.next(); 235 System.out.print(key); 236 } 237 for(char c = iterator.first(); c != CharacterIterator.DONE; c = iterator.next()) { 238 if (iterator.getIndex() == iterator.getBeginIndex() || 239 iterator.getIndex() == iterator.getRunStart()) { 240 System.out.println(); 241 Map attributes = iterator.getAttributes(); 242 Set entries = attributes.entrySet(); 243 Iterator attributeIterator = entries.iterator(); 244 while (attributeIterator.hasNext()) { 245 Map.Entry entry = (Map.Entry) attributeIterator.next(); 246 System.out.print("<" + entry.getKey() + ": " 247 + entry.getValue() + ">"); 248 } 249 } 250 System.out.print(" "); 251 System.out.print(c); 252 } 253 System.out.println(); 254 System.out.println("done"); 255 System.out.println(); 256 } 257 258} 259