Path: blob/master/test/jdk/java/text/AttributedString/AttributedStringTest.java
41149 views
/*1* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 413977125* @summary test all aspects of AttributedString class26*/2728import java.text.Annotation;29import java.text.AttributedCharacterIterator;30import java.text.AttributedCharacterIterator.Attribute;31import java.text.AttributedString;32import java.text.CharacterIterator;33import java.util.HashSet;34import java.util.Iterator;35import java.util.Locale;36import java.util.Map;37import java.util.Map.Entry;38import java.util.Set;394041public class AttributedStringTest {4243private static final String text = "Hello, world!";44private static final Annotation hi = new Annotation("hi");45private static final int[] array5_13 = {5, 13};46private static final int[] array3_9_13 = {3, 9, 13};47private static final int[] array5_9_13 = {5, 9, 13};48private static final int[] array3_5_9_13 = {3, 5, 9, 13};49private static final Attribute[] arrayLanguage = {Attribute.LANGUAGE};50private static final Attribute[] arrayLanguageReading = {Attribute.LANGUAGE, Attribute.READING};51private static final Set setLanguageReading = new HashSet();52static {53setLanguageReading.add(Attribute.LANGUAGE);54setLanguageReading.add(Attribute.READING);55}565758public static final void main(String argv[]) throws Exception {596061AttributedString string;62AttributedCharacterIterator iterator;6364// create a string with text, but no attributes65string = new AttributedString(text);66iterator = string.getIterator();6768// make sure the text is there and attributes aren't69checkIteratorText(iterator, text);70if (!iterator.getAllAttributeKeys().isEmpty()) {71throwException(iterator, "iterator provides attributes where none are defined");72}7374// add an attribute to a subrange75string.addAttribute(Attribute.LANGUAGE, Locale.ENGLISH, 3, 9);76iterator = string.getIterator();7778// make sure the attribute is defined, and it's on the correct subrange79checkIteratorAttributeKeys(iterator, arrayLanguage);80checkIteratorSubranges(iterator, array3_9_13);81checkIteratorAttribute(iterator, 0, Attribute.LANGUAGE, null);82checkIteratorAttribute(iterator, 3, Attribute.LANGUAGE, Locale.ENGLISH);83checkIteratorAttribute(iterator, 9, Attribute.LANGUAGE, null);8485// add an attribute to a subrange86string.addAttribute(Attribute.READING, hi, 0, 5);87iterator = string.getIterator();8889// make sure the attribute is defined, and it's on the correct subrange90checkIteratorAttributeKeys(iterator, arrayLanguageReading);91checkIteratorSubranges(iterator, array3_5_9_13);92checkIteratorAttribute(iterator, 0, Attribute.READING, hi);93checkIteratorAttribute(iterator, 3, Attribute.READING, hi);94checkIteratorAttribute(iterator, 5, Attribute.READING, null);95checkIteratorAttribute(iterator, 9, Attribute.READING, null);9697// make sure the first attribute wasn't adversely affected98// in particular, we shouldn't see separate subranges (3,5) and (5,9).99checkIteratorSubranges(iterator, Attribute.LANGUAGE, array3_9_13);100checkIteratorAttribute(iterator, 0, Attribute.LANGUAGE, null);101checkIteratorAttribute(iterator, 3, Attribute.LANGUAGE, Locale.ENGLISH);102checkIteratorAttribute(iterator, 5, Attribute.LANGUAGE, Locale.ENGLISH);103checkIteratorAttribute(iterator, 9, Attribute.LANGUAGE, null);104105// for the entire set of attributes, we expect four subranges106checkIteratorSubranges(iterator, setLanguageReading, array3_5_9_13);107108// redefine the language attribute so that both language and reading are continuous from 0 to 5109string.addAttribute(Attribute.LANGUAGE, Locale.US, 0, 5);110iterator = string.getIterator();111112// make sure attributes got changed and merged correctly113checkIteratorAttributeKeys(iterator, arrayLanguageReading);114checkIteratorSubranges(iterator, array3_5_9_13);115checkIteratorSubranges(iterator, Attribute.LANGUAGE, array5_9_13);116checkIteratorSubranges(iterator, Attribute.READING, array5_13);117checkIteratorSubranges(iterator, setLanguageReading, array5_9_13);118checkIteratorAttribute(iterator, 0, Attribute.LANGUAGE, Locale.US);119checkIteratorAttribute(iterator, 3, Attribute.LANGUAGE, Locale.US);120checkIteratorAttribute(iterator, 5, Attribute.LANGUAGE, Locale.ENGLISH);121checkIteratorAttribute(iterator, 9, Attribute.LANGUAGE, null);122123// make sure an annotation is only returned if its range is contained in the iterator's range124iterator = string.getIterator(null, 3, 5);125checkIteratorAttribute(iterator, 3, Attribute.READING, null);126checkIteratorAttribute(iterator, 5, Attribute.READING, null);127iterator = string.getIterator(null, 0, 4);128checkIteratorAttribute(iterator, 0, Attribute.READING, null);129checkIteratorAttribute(iterator, 3, Attribute.READING, null);130iterator = string.getIterator(null, 0, 5);131checkIteratorAttribute(iterator, 0, Attribute.READING, hi);132checkIteratorAttribute(iterator, 4, Attribute.READING, hi);133checkIteratorAttribute(iterator, 5, Attribute.READING, null);134135}136137private static final void checkIteratorText(AttributedCharacterIterator iterator, String expectedText) throws Exception {138if (iterator.getEndIndex() - iterator.getBeginIndex() != expectedText.length()) {139throwException(iterator, "text length doesn't match between original text and iterator");140}141142char c = iterator.first();143for (int i = 0; i < expectedText.length(); i++) {144if (c != expectedText.charAt(i)) {145throwException(iterator, "text content doesn't match between original text and iterator");146}147c = iterator.next();148}149if (c != CharacterIterator.DONE) {150throwException(iterator, "iterator text doesn't end with DONE");151}152}153154private static final void checkIteratorAttributeKeys(AttributedCharacterIterator iterator, Attribute[] expectedKeys) throws Exception {155Set iteratorKeys = iterator.getAllAttributeKeys();156if (iteratorKeys.size() != expectedKeys.length) {157throwException(iterator, "number of keys returned by iterator doesn't match expectation");158}159for (int i = 0; i < expectedKeys.length; i++) {160if (!iteratorKeys.contains(expectedKeys[i])) {161throwException(iterator, "expected key wasn't found in iterator's key set");162}163}164}165166private static final void checkIteratorSubranges(AttributedCharacterIterator iterator, int[] expectedLimits) throws Exception {167int previous = 0;168char c = iterator.first();169for (int i = 0; i < expectedLimits.length; i++) {170if (iterator.getRunStart() != previous || iterator.getRunLimit() != expectedLimits[i]) {171throwException(iterator, "run boundaries are not as expected: " + iterator.getRunStart() + ", " + iterator.getRunLimit());172}173previous = expectedLimits[i];174c = iterator.setIndex(previous);175}176if (c != CharacterIterator.DONE) {177throwException(iterator, "iterator's run sequence doesn't end with DONE");178}179}180181private static final void checkIteratorSubranges(AttributedCharacterIterator iterator, Attribute key, int[] expectedLimits) throws Exception {182int previous = 0;183char c = iterator.first();184for (int i = 0; i < expectedLimits.length; i++) {185if (iterator.getRunStart(key) != previous || iterator.getRunLimit(key) != expectedLimits[i]) {186throwException(iterator, "run boundaries are not as expected: " + iterator.getRunStart(key) + ", " + iterator.getRunLimit(key) + " for key " + key);187}188previous = expectedLimits[i];189c = iterator.setIndex(previous);190}191if (c != CharacterIterator.DONE) {192throwException(iterator, "iterator's run sequence doesn't end with DONE");193}194}195196private static final void checkIteratorSubranges(AttributedCharacterIterator iterator, Set keys, int[] expectedLimits) throws Exception {197int previous = 0;198char c = iterator.first();199for (int i = 0; i < expectedLimits.length; i++) {200if (iterator.getRunStart(keys) != previous || iterator.getRunLimit(keys) != expectedLimits[i]) {201throwException(iterator, "run boundaries are not as expected: " + iterator.getRunStart(keys) + ", " + iterator.getRunLimit(keys) + " for keys " + keys);202}203previous = expectedLimits[i];204c = iterator.setIndex(previous);205}206if (c != CharacterIterator.DONE) {207throwException(iterator, "iterator's run sequence doesn't end with DONE");208}209}210211private static final void checkIteratorAttribute(AttributedCharacterIterator iterator, int index, Attribute key, Object expectedValue) throws Exception {212iterator.setIndex(index);213Object value = iterator.getAttribute(key);214if (!((expectedValue == null && value == null) || (expectedValue != null && expectedValue.equals(value)))) {215throwException(iterator, "iterator returns wrong attribute value - " + value + " instead of " + expectedValue);216}217value = iterator.getAttributes().get(key);218if (!((expectedValue == null && value == null) || (expectedValue != null && expectedValue.equals(value)))) {219throwException(iterator, "iterator's map returns wrong attribute value - " + value + " instead of " + expectedValue);220}221}222223private static final void throwException(AttributedCharacterIterator iterator, String details) throws Exception {224dumpIterator(iterator);225throw new Exception(details);226}227228private static final void dumpIterator(AttributedCharacterIterator iterator) {229Set attributeKeys = iterator.getAllAttributeKeys();230System.out.print("All attributes: ");231Iterator keyIterator = attributeKeys.iterator();232while (keyIterator.hasNext()) {233Attribute key = (Attribute) keyIterator.next();234System.out.print(key);235}236for(char c = iterator.first(); c != CharacterIterator.DONE; c = iterator.next()) {237if (iterator.getIndex() == iterator.getBeginIndex() ||238iterator.getIndex() == iterator.getRunStart()) {239System.out.println();240Map attributes = iterator.getAttributes();241Set entries = attributes.entrySet();242Iterator attributeIterator = entries.iterator();243while (attributeIterator.hasNext()) {244Map.Entry entry = (Map.Entry) attributeIterator.next();245System.out.print("<" + entry.getKey() + ": "246+ entry.getValue() + ">");247}248}249System.out.print(" ");250System.out.print(c);251}252System.out.println();253System.out.println("done");254System.out.println();255}256257}258259260