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 libcore.java.text;
18
19import java.text.AttributedCharacterIterator;
20import java.text.AttributedString;
21import java.text.CharacterIterator;
22import java.util.HashMap;
23import java.util.Map;
24import java.util.Set;
25import java.util.TreeSet;
26import java.util.WeakHashMap;
27
28public class OldAttributedStringTest extends junit.framework.TestCase {
29
30    static void assertEqualString (String msg, String expected, AttributedString attrString) {
31        AttributedCharacterIterator it = attrString.getIterator();
32        StringBuffer buf = new StringBuffer();
33        buf.append(it.first());
34        char ch;
35        while ((ch = it.next()) != CharacterIterator.DONE)
36            buf.append(ch);
37        assertEquals(msg, expected, buf.toString());
38    }
39
40    public void test_ConstructorLAttributedCharacterIterator_1() {
41        String testString = "Test string";
42        AttributedString attrString = new AttributedString(testString);
43        AttributedCharacterIterator iter = attrString.getIterator();
44        AttributedString attrString2 = new AttributedString(iter);
45        assertEqualString("String must match!", testString, attrString2);
46    }
47
48    public void test_ConstructorLAttributedCharacterIterator_2() {
49        String testString = "Test string";
50        AttributedString attrString = new AttributedString(testString);
51        AttributedCharacterIterator iter = attrString.getIterator();
52        AttributedString attrString2 = new AttributedString(iter, 2, 7);
53        assertEqualString("String must match!", "st st", attrString2);
54    }
55
56    /**
57     * java.text.AttributedString#AttributedString(AttributedCharacterIterator,
58     *        int, int) Test of method
59     *        java.text.AttributedString#AttributedString(AttributedCharacterIterator,
60     *        int, int). Case 1: Try to consruct AttributedString. Case 2: Try
61     *        to consruct AttributedString using incorrect beginIndex. Case 3:
62     *        Try to consruct AttributedString using incorrect endIndex.
63     */
64    public void test_ConstructorLAttributedCharacterIteratorII() {
65        // Regression for HARMONY-1355
66
67        // case 1: Try to consruct AttributedString.
68        try {
69            new AttributedString(new testAttributedCharacterIterator(), 0, 0);
70        } catch (Exception e) {
71            fail("Unexpected exception " + e.toString());
72        }
73
74        // case 2: Try to consruct AttributedString using incorrect beginIndex.
75        try {
76            new AttributedString(new testAttributedCharacterIterator(), -1, 0);
77            fail("Expected IllegalArgumentException was not thrown");
78        } catch (IllegalArgumentException e) {
79            // expected
80        }
81
82        // case 3: Try to consruct AttributedString using incorrect endIndex.
83        try {
84            new AttributedString(new testAttributedCharacterIterator(), 0, -1);
85            fail("Expected IllegalArgumentException was not thrown");
86        } catch (IllegalArgumentException e) {
87            // expected
88        }
89    }
90
91    public void test_ConstructorLAttributedCharacterIterator_3() {
92        String testString = "Test string";
93        AttributedString attrString = new AttributedString(testString);
94        AttributedCharacterIterator iter = attrString.getIterator();
95        AttributedString attrString2;
96
97        attrString2 = new AttributedString(iter, 2, 7, new AttributedCharacterIterator.Attribute[] {});
98        assertEqualString("String must match!", "st st", attrString2);
99
100        attrString2 = new AttributedString(iter, 2, 7, null);
101        assertEqualString("String must match!", "st st", attrString2);
102    }
103
104    /**
105     * java.text.AttributedString#AttributedString(AttributedCharacterIterator,
106     *        int, int, AttributedCharacterIterator.Attribute[]) Test of method
107     *        java.text.AttributedString#AttributedString(AttributedCharacterIterator,
108     *        int, int, AttributedCharacterIterator.Attribute[]). Case 1: Try to
109     *        consruct AttributedString. Case 2: Try to consruct
110     *        AttributedString using incorrect beginIndex. Case 3: Try to
111     *        consruct AttributedString using incorrect endIndex. Case 4: Try to
112     *        consruct AttributedString using specified attributes.
113     */
114    public void test_ConstructorLAttributedCharacterIteratorII$Ljava_text_AttributedCharacterIterator$Attribute() {
115        // case 1: Try to consruct AttributedString.
116        try {
117            new AttributedString(new testAttributedCharacterIterator(), 0, 0,
118                    null);
119        } catch (Exception e) {
120            fail("Unexpected exception " + e.toString());
121        }
122
123        // case 2: Try to consruct AttributedString using incorrect beginIndex.
124        try {
125            new AttributedString(new testAttributedCharacterIterator(), -1, 0,
126                    null);
127            fail("Expected IllegalArgumentException was not thrown");
128        } catch (IllegalArgumentException e) {
129            // expected
130        }
131
132        // case 3: Try to consruct AttributedString using incorrect endIndex.
133        try {
134            new AttributedString(new testAttributedCharacterIterator(), 0, -1,
135                    null);
136            fail("Expected IllegalArgumentException was not thrown");
137        } catch (IllegalArgumentException e) {
138            // expected
139        }
140
141        // case 4: Try to consruct AttributedString using specified attributes.
142        try {
143            AttributedCharacterIterator.Attribute[] attributes = new AttributedCharacterIterator.Attribute[1];
144            attributes[0] = new TestAttributedCharacterIteratorAttribute("test");
145            new AttributedString(new testAttributedCharacterIterator(), 0, 0,
146                    attributes);
147        } catch (IllegalArgumentException e) {
148            fail("Unexpected expected " + e.toString());
149        }
150    }
151
152    /**
153     * java.text.AttributedString#AttributedString(AttributedCharacterIterator,
154     *        int, int, Map<? extends AttributedCharacterIterator.Attribute,?>)
155     *        Test of method
156     *        java.text.AttributedString#AttributedString(AttributedCharacterIterator,
157     *        int, int, Map<? extends
158     *        AttributedCharacterIterator.Attribute,?>). Case 1: Try to
159     *        construct AttributedString. Case 2: Try to construct
160     *        AttributedString using 0-length text and not an empty Map
161     *        attributes.
162     */
163    public void test_ConstructorLjava_lang_StringLjava_util_Map() {
164        String test = "Test string";
165
166        // case 1: Try to construct AttributedString
167        try {
168            AttributedString attrString = new AttributedString(
169                    test,
170                    new WeakHashMap<AttributedCharacterIterator.Attribute, String>());
171            AttributedCharacterIterator it = attrString.getIterator();
172            StringBuffer buf = new StringBuffer();
173            buf.append(it.first());
174            char ch;
175            while ((ch = it.next()) != CharacterIterator.DONE)
176                buf.append(ch);
177            assertTrue("Wrong string: " + buf, buf.toString().equals(test));
178        } catch (Exception e) {
179            fail("Unexpected exception " + e.toString());
180        }
181
182        // case 2: Try to construct AttributedString using 0-length text and
183        // not an empty Map attributes.
184        try {
185            Map<AttributedCharacterIterator.Attribute, String> whm = new WeakHashMap<AttributedCharacterIterator.Attribute, String>();
186            whm.put(new TestAttributedCharacterIteratorAttribute("test"),
187                    "value");
188            new AttributedString("", whm);
189            fail("Expected IllegalArgumentException was not thrown");
190        } catch (Exception e) {
191            // expected
192        }
193    }
194
195    private class TestAttributedCharacterIteratorAttribute extends
196            AttributedCharacterIterator.Attribute {
197        private static final long serialVersionUID = -2917613373935785179L;
198
199        public TestAttributedCharacterIteratorAttribute(String name) {
200            super(name);
201        }
202    }
203
204    private class testAttributedCharacterIterator implements
205            AttributedCharacterIterator {
206        public Set getAllAttributeKeys() {
207            return null;
208        }
209
210        public Object getAttribute(AttributedCharacterIterator.Attribute p) {
211            return null;
212        }
213
214        public Map getAttributes() {
215            return null;
216        }
217
218        public int getRunLimit(Set p) {
219            return 0;
220        }
221
222        public int getRunLimit(AttributedCharacterIterator.Attribute p) {
223            return 0;
224        }
225
226        public int getRunLimit() {
227            return 0;
228        }
229
230        public int getRunStart(Set p) {
231            return 0;
232        }
233
234        public int getRunStart(AttributedCharacterIterator.Attribute p) {
235            return 0;
236        }
237
238        public int getRunStart() {
239            return 0;
240        }
241
242        public Object clone() {
243            return null;
244        }
245
246        public int getIndex() {
247            return 0;
248        }
249
250        public int getEndIndex() {
251            return 0;
252        }
253
254        public int getBeginIndex() {
255            return 0;
256        }
257
258        public char setIndex(int p) {
259            return 'a';
260        }
261
262        public char previous() {
263            return 'a';
264        }
265
266        public char next() {
267            return 'a';
268        }
269
270        public char current() {
271            return 'a';
272        }
273
274        public char last() {
275            return 'a';
276        }
277
278        public char first() {
279            return 'a';
280        }
281    }
282    public void test_addAttributeLjava_text_AttributedCharacterIterator$AttributeLjava_lang_ObjectII() {
283        AttributedString as = new AttributedString("test");
284        as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, "a", 2,
285                3);
286        AttributedCharacterIterator it = as.getIterator();
287        assertEquals("non-null value limit", 2, it
288                .getRunLimit(AttributedCharacterIterator.Attribute.LANGUAGE));
289
290        as = new AttributedString("test");
291        as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, null,
292                2, 3);
293        it = as.getIterator();
294        assertEquals("null value limit", 4, it
295                .getRunLimit(AttributedCharacterIterator.Attribute.LANGUAGE));
296
297        try {
298            as = new AttributedString("test");
299            as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE,
300                    null, -1, 3);
301            fail("Expected IllegalArgumentException");
302        } catch (IllegalArgumentException e) {
303            // Expected
304        }
305
306        // regression for Harmony-1244
307        as = new AttributedString("123", new WeakHashMap());
308        try {
309            as.addAttribute(null, new TreeSet(), 0, 1);
310            fail("should throw NullPointerException");
311        } catch (NullPointerException e) {
312            // expected
313        }
314
315        try {
316            as.addAttribute(null, new TreeSet(), -1, 1);
317            fail("should throw NullPointerException");
318        } catch (NullPointerException e) {
319            // expected
320        }
321    }
322
323    /**
324     * java.text.AttributedString.addAttribute(AttributedCharacterIterator,
325     *        Object)
326     */
327    public void test_addAttributeLjava_text_AttributedCharacterIterator$AttributeLjava_lang_Object() {
328        // regression for Harmony-1244
329        AttributedString as = new AttributedString("123", new WeakHashMap());
330
331        as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, "english");
332        as.addAttribute(AttributedCharacterIterator.Attribute.INPUT_METHOD_SEGMENT,
333                                                                "input method");
334        as.addAttribute(AttributedCharacterIterator.Attribute.READING, "reading");
335
336        try {
337            as.addAttribute(null, new TreeSet());
338            fail("should throw NullPointerException");
339        } catch (NullPointerException e) {
340            // expected
341        }
342        try {
343            as.addAttribute(null, null);
344            fail("should throw NullPointerException");
345        } catch (NullPointerException e) {
346            // expected
347        }
348    }
349
350    /**
351     * java.text.AttributedString#addAttributes(Map<? extends
352     *        AttributedCharacterIterator.Attribute,?>, int, int) Tests of
353     *        method java.text.AttributedString#addAttributes(Map<? extends
354     *        AttributedCharacterIterator.Attribute,?>, int, int). Case 1: Try
355     *        to add attributes to AttributesString. Case 2: Try to add
356     *        null-attributes to AttributesString. Case 3: Try to add attributes
357     *        to AttributesString using incorrect index.
358     */
359    public void test_addAttributesLjava_util_MapII() {
360        AttributedString as = new AttributedString("test");
361        Map<AttributedCharacterIterator.Attribute, String> whm = new WeakHashMap<AttributedCharacterIterator.Attribute, String>();
362
363        // case 1: Try to add attributes to AttributesString.
364        try {
365            whm.put(new TestAttributedCharacterIteratorAttribute("test1"),
366                    "value1");
367            whm.put(new TestAttributedCharacterIteratorAttribute("test2"),
368                    "value2");
369            whm.put(new TestAttributedCharacterIteratorAttribute("test3"),
370                    "value3");
371            as.addAttributes(whm, 0, 3);
372        } catch (Exception e) {
373            fail("Unexpected exception " + e.toString());
374        }
375
376        // case 2: Try to add null-attributes to AttributesString.
377        try {
378            as.addAttributes(null, 0, 3);
379            fail("Expected NullPointerException was not thrown");
380        } catch (NullPointerException e) {
381            // expected
382        }
383
384        // case 3: Try to add attributes to AttributesString using incorrect
385        // index.
386        try {
387            as.addAttributes(whm, 0, 0);
388            fail("Expected IllegalArgumentException was not thrown");
389        } catch (IllegalArgumentException e) {
390            // expected
391        }
392    }
393
394    /**
395     * java.text.AttributedString#getIterator() Test of method
396     *        java.text.AttributedString#getIterator().
397     */
398    public void test_getIterator() {
399        String test = "Test string";
400        try {
401            AttributedString attrString = new AttributedString(test);
402            AttributedCharacterIterator it = attrString.getIterator();
403            assertEquals("Incorrect iteration on AttributedString", it.first(),
404                    test.charAt(0));
405        } catch (Exception e) {
406            fail("Unexpected exceptiption " + e.toString());
407        }
408    }
409
410    /**
411     * java.text.AttributedString#getIterator(AttributedCharacterIterator.Attribute[])
412     *        Test of method
413     *        java.text.AttributedString#getIterator(AttributedCharacterIterator.Attribute[]).
414     */
415    public void test_getIterator$Ljava_text_AttributedCharacterIterator$Attribute() {
416        String test = "Test string";
417        try {
418            Map<AttributedCharacterIterator.Attribute, String> hm = new HashMap<AttributedCharacterIterator.Attribute, String>();
419            AttributedCharacterIterator.Attribute[] aci = new AttributedCharacterIterator.Attribute[3];
420            aci[0] = new TestAttributedCharacterIteratorAttribute("att1");
421            aci[1] = new TestAttributedCharacterIteratorAttribute("att2");
422            aci[2] = new TestAttributedCharacterIteratorAttribute("att3");
423            hm.put(aci[0], "value1");
424            hm.put(aci[1], "value2");
425
426            AttributedString attrString = new AttributedString(test, hm);
427            AttributedCharacterIterator it = attrString.getIterator(aci);
428
429            assertTrue("Incorrect iteration on AttributedString", it
430                    .getAttribute(aci[0]).equals("value1"));
431            assertTrue("Incorrect iteration on AttributedString", it
432                    .getAttribute(aci[1]).equals("value2"));
433            assertTrue("Incorrect iteration on AttributedString", it
434                    .getAttribute(aci[2]) == null);
435        } catch (Exception e) {
436            fail("Unexpected exceptiption " + e.toString());
437        }
438    }
439
440    /**
441     * java.text.AttributedString#getIterator(AttributedCharacterIterator.Attribute[],
442     *        int, int) Test of method
443     *        java.text.AttributedString#getIterator(AttributedCharacterIterator.Attribute[],
444     *        int, int).
445     */
446    public void test_getIterator$Ljava_text_AttributedCharacterIterator$AttributeII() {
447        String test = "Test string";
448        try {
449            Map<AttributedCharacterIterator.Attribute, String> hm = new HashMap<AttributedCharacterIterator.Attribute, String>();
450            AttributedCharacterIterator.Attribute[] aci = new AttributedCharacterIterator.Attribute[3];
451            aci[0] = new TestAttributedCharacterIteratorAttribute("att1");
452            aci[1] = new TestAttributedCharacterIteratorAttribute("att2");
453            aci[2] = new TestAttributedCharacterIteratorAttribute("att3");
454            hm.put(aci[0], "value1");
455            hm.put(aci[1], "value2");
456
457            AttributedString attrString = new AttributedString(test);
458            attrString.addAttributes(hm, 2, 4);
459            AttributedCharacterIterator it = attrString.getIterator(aci, 1, 5);
460
461            assertTrue("Incorrect iteration on AttributedString", it
462                    .getAttribute(aci[0]) == null);
463            assertTrue("Incorrect iteration on AttributedString", it
464                    .getAttribute(aci[1]) == null);
465            assertTrue("Incorrect iteration on AttributedString", it
466                    .getAttribute(aci[2]) == null);
467
468            it.next();
469
470            assertTrue("Incorrect iteration on AttributedString", it
471                    .getAttribute(aci[0]).equals("value1"));
472            assertTrue("Incorrect iteration on AttributedString", it
473                    .getAttribute(aci[1]).equals("value2"));
474            assertTrue("Incorrect iteration on AttributedString", it
475                    .getAttribute(aci[2]) == null);
476
477            try {
478                attrString.getIterator(aci, -1, 5);
479                fail("IllegalArgumentException is not thrown.");
480            } catch(IllegalArgumentException iae) {
481                //expected
482            }
483
484            try {
485                attrString.getIterator(aci, 6, 5);
486                fail("IllegalArgumentException is not thrown.");
487            } catch(IllegalArgumentException iae) {
488                //expected
489            }
490
491            try {
492                attrString.getIterator(aci, 3, 2);
493                fail("IllegalArgumentException is not thrown.");
494            } catch(IllegalArgumentException iae) {
495                //expected
496            }
497        } catch (Exception e) {
498            fail("Unexpected exceptiption " + e.toString());
499        }
500
501    }
502}
503