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 */
17package org.apache.harmony.text.tests.java.text;
18
19import java.text.AttributedCharacterIterator;
20import java.text.AttributedString;
21import java.text.CharacterIterator;
22import java.util.Map;
23import java.util.Set;
24import java.util.TreeSet;
25import java.util.WeakHashMap;
26
27public class AttributedStringTest extends junit.framework.TestCase {
28
29	/**
30	 * @tests java.text.AttributedString#AttributedString(java.lang.String)
31	 */
32	public void test_ConstructorLjava_lang_String() {
33		String test = "Test string";
34		AttributedString attrString = new AttributedString(test);
35		AttributedCharacterIterator it = attrString.getIterator();
36		StringBuffer buf = new StringBuffer();
37		buf.append(it.first());
38		char ch;
39		while ((ch = it.next()) != CharacterIterator.DONE)
40			buf.append(ch);
41		assertTrue("Wrong string: " + buf, buf.toString().equals(test));
42	}
43
44	/**
45	 * @tests java.text.AttributedString#AttributedString(AttributedCharacterIterator)
46	 */
47	public void test_ConstructorLAttributedCharacterIterator() {
48		//Regression for HARMONY-1354
49		assertNotNull(new AttributedString(new testAttributedCharacterIterator()));
50	}
51	/**
52	 * @tests java.text.AttributedString#AttributedString(AttributedCharacterIterator, int, int)
53	 */
54	public void test_ConstructorLAttributedCharacterIteratorII() {
55		//Regression for HARMONY-1355
56		assertNotNull(new AttributedString(new testAttributedCharacterIterator(), 0, 0));
57	}
58
59	private class testAttributedCharacterIterator implements AttributedCharacterIterator {
60	    public Set getAllAttributeKeys() {
61	        return null;
62	    }
63	    public Object getAttribute(AttributedCharacterIterator.Attribute p) {
64	        return null;
65	    }
66	    public Map getAttributes() {
67	        return null;
68	    }
69	    public int getRunLimit(Set p) {
70	        return 0;
71	    }
72	    public int getRunLimit(AttributedCharacterIterator.Attribute p) {
73	        return 0;
74	    }
75	    public int getRunLimit() {
76	        return 0;
77	    }
78	    public int getRunStart(Set p) {
79	        return 0;
80	    }
81	    public int getRunStart(AttributedCharacterIterator.Attribute p) {
82	        return 0;
83	    }
84	    public int getRunStart() {
85	        return 0;
86	    }
87	    public Object clone() {
88	        return null;
89	    }
90	    public int getIndex() {
91	        return 0;
92	    }
93	    public int getEndIndex() {
94	        return 0;
95	    }
96	    public int getBeginIndex() {
97	        return 0;
98	    }
99	    public char setIndex(int p) {
100	        return 'a';
101	    }
102	    public char previous() {
103	        return 'a';
104	    }
105	    public char next() {
106	        return 'a';
107	    }
108	    public char current() {
109	        return 'a';
110	    }
111	    public char last() {
112	        return 'a';
113	    }
114	    public char first() {
115	        return 'a';
116	    }
117	}
118
119    public void test_addAttributeLjava_text_AttributedCharacterIterator$AttributeLjava_lang_ObjectII() {
120        AttributedString as = new AttributedString("test");
121        as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, "a", 2,
122                3);
123        AttributedCharacterIterator it = as.getIterator();
124        assertEquals("non-null value limit", 2, it
125                .getRunLimit(AttributedCharacterIterator.Attribute.LANGUAGE));
126
127        as = new AttributedString("test");
128        as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, null,
129                2, 3);
130        it = as.getIterator();
131        assertEquals("null value limit", 4, it
132                .getRunLimit(AttributedCharacterIterator.Attribute.LANGUAGE));
133
134        try {
135            as = new AttributedString("test");
136            as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE,
137                    null, -1, 3);
138            fail("Expected IllegalArgumentException");
139        } catch (IllegalArgumentException e) {
140            // Expected
141        }
142
143        // regression for Harmony-1244
144        as = new AttributedString("123", new WeakHashMap());
145        try {
146            as.addAttribute(null, new TreeSet(), 0, 1);
147            fail("should throw NullPointerException");
148        } catch (NullPointerException e) {
149            // expected
150        }
151
152        try {
153            as.addAttribute(null, new TreeSet(), -1, 1);
154            fail("should throw NullPointerException");
155        } catch (NullPointerException e) {
156            // expected
157        }
158    }
159
160    /**
161     * @tests java.text.AttributedString.addAttribute(AttributedCharacterIterator, Object)
162     */
163    public void test_addAttributeLjava_text_AttributedCharacterIterator$AttributeLjava_lang_Object() {
164        //regression for Harmony-1244
165        AttributedString as = new AttributedString("123", new WeakHashMap());
166        try {
167            as.addAttribute(null, new TreeSet());
168            fail("should throw NullPointerException");
169        } catch (NullPointerException e) {
170            // expected
171        }
172        try {
173            as.addAttribute(null, null);
174            fail("should throw NullPointerException");
175        } catch (NullPointerException e) {
176            // expected
177        }
178    }
179}
180