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 dalvik.annotation.TestTargets;
20import dalvik.annotation.TestLevel;
21import dalvik.annotation.TestTargetNew;
22import dalvik.annotation.TestTargetClass;
23
24import java.io.ByteArrayInputStream;
25import java.io.ByteArrayOutputStream;
26import java.io.IOException;
27import java.io.InvalidObjectException;
28import java.io.ObjectInputStream;
29import java.io.ObjectOutputStream;
30import java.text.AttributedCharacterIterator;
31
32
33@TestTargetClass(AttributedCharacterIterator.Attribute.class)
34public class AttributedCharacterIteratorAttributeTest extends
35        junit.framework.TestCase {
36
37    private class MockAttributedCharacterIteratorAttribute extends
38            AttributedCharacterIterator.Attribute {
39
40        private static final long serialVersionUID = 1L;
41
42        public MockAttributedCharacterIteratorAttribute(String name) {
43            super(name);
44        }
45
46        @Override
47        public String getName() {
48            return super.getName();
49        }
50
51        @Override
52        public Object readResolve() throws InvalidObjectException {
53            return super.readResolve();
54        }
55    }
56
57    private class TestAttributedCharacterIteratorAttribute extends
58            AttributedCharacterIterator.Attribute {
59        private static final long serialVersionUID = -2917613373935785179L;
60
61        public TestAttributedCharacterIteratorAttribute(String name) {
62            super(name);
63        }
64    }
65
66    /**
67     * @tests java.text.AttributedCharacterIterator.Attribute#AttributedCharacterIterator.Attribute(java.lang.String)
68     *        Test of method
69     *        java.text.AttributedCharacterIterator.Attribute#AttributedCharacterIterator.Attribute(java.lang.String).
70     */
71    @TestTargetNew(
72        level = TestLevel.COMPLETE,
73        notes = "",
74        method = "Attribute",
75        args = {java.lang.String.class}
76    )
77    public void test_Constructor() {
78        try {
79            new MockAttributedCharacterIteratorAttribute("test");
80        } catch (Exception e) {
81            fail("Unexpected exception " + e.toString());
82        }
83    }
84
85    /**
86     * @tests java.text.AttributedCharacterIterator.Attribute#equals(java.lang.Object)
87     *        Test of method
88     *        java.text.AttributedCharacterIterator.Attribute#equals(java.lang.Object).
89     */
90    @TestTargetNew(
91        level = TestLevel.COMPLETE,
92        notes = "",
93        method = "equals",
94        args = {java.lang.Object.class}
95    )
96    public void test_equalsLjava_lang_Object() {
97        try {
98            MockAttributedCharacterIteratorAttribute mac1 = new MockAttributedCharacterIteratorAttribute(
99                    "test1");
100            MockAttributedCharacterIteratorAttribute mac2 = new MockAttributedCharacterIteratorAttribute(
101                    "test2");
102
103            assertFalse("Attributes are equal", mac2.equals(mac1));
104
105            TestAttributedCharacterIteratorAttribute mac3 = new TestAttributedCharacterIteratorAttribute(
106                    "test1");
107
108            assertFalse("Attributes are equal", mac3.equals(mac1));
109
110            AttributedCharacterIterator.Attribute mac4 = mac1;
111
112            assertTrue("Attributes are non-equal", mac4.equals(mac1));
113        } catch (Exception e) {
114            fail("Unexpected exception " + e.toString());
115        }
116    }
117
118    /**
119     * @tests java.text.AttributedCharacterIterator.Attribute#getName() Test of
120     *        method java.text.AttributedCharacterIterator.Attribute#getName().
121     */
122    @TestTargetNew(
123        level = TestLevel.COMPLETE,
124        notes = "",
125        method = "getName",
126        args = {}
127    )
128    public void test_getName() {
129        try {
130            MockAttributedCharacterIteratorAttribute mac1 = new MockAttributedCharacterIteratorAttribute(
131                    "test1");
132            assertEquals("Incorrect attribute name", "test1", mac1.getName());
133        } catch (Exception e) {
134            fail("Unexpected exception " + e.toString());
135        }
136    }
137
138    /**
139     * @tests java.text.AttributedCharacterIterator.Attribute#hashCode()
140     */
141    @TestTargetNew(
142        level = TestLevel.COMPLETE,
143        notes = "",
144        method = "hashCode",
145        args = {}
146    )
147    public void test_hashCode() {
148        try {
149            MockAttributedCharacterIteratorAttribute mac1 = new MockAttributedCharacterIteratorAttribute(
150                    "test1");
151            TestAttributedCharacterIteratorAttribute mac2 = new TestAttributedCharacterIteratorAttribute(
152                    "test1");
153
154            assertTrue("The hash codes of same attributes are not equal", mac1
155                    .hashCode() != mac2.hashCode());
156
157            MockAttributedCharacterIteratorAttribute mac3 = new MockAttributedCharacterIteratorAttribute(
158                    "test2");
159
160            assertTrue("The hash codes of different attributes are equal", mac1
161                    .hashCode() != mac3.hashCode());
162
163            AttributedCharacterIterator.Attribute mac4 = mac1;
164
165            assertTrue("The hash codes of same attributes but different hierarchy classes are not equal",
166                    mac1.hashCode() == mac4.hashCode());
167        } catch (Exception e) {
168            fail("Unexpected exception " + e.toString());
169        }
170    }
171
172    /**
173     * @tests java.text.AttributedCharacterIterator.Attribute#readResolve() Test
174     *        of method
175     *        java.text.AttributedCharacterIterator.Attribute#readResolve().
176     */
177    @TestTargetNew(
178        level = TestLevel.COMPLETE,
179        notes = "",
180        method = "readResolve",
181        args = {}
182    )
183    public void test_readResolve() {
184        MockAttributedCharacterIteratorAttribute mac1 = new MockAttributedCharacterIteratorAttribute(
185                "test");
186        try {
187            mac1.readResolve();
188            fail("InvalidObjectException has not been thrown");
189        } catch (InvalidObjectException e) {
190            // expected
191        }
192
193        ObjectOutputStream out = null;
194        ObjectInputStream in = null;
195        try {
196            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
197            out = new ObjectOutputStream(bytes);
198
199            AttributedCharacterIterator.Attribute attr1, attr2;
200
201            attr1 = AttributedCharacterIterator.Attribute.LANGUAGE;
202
203            out.writeObject(attr1);
204
205            in = new ObjectInputStream(new ByteArrayInputStream(bytes
206                    .toByteArray()));
207
208            try {
209                attr2 = (AttributedCharacterIterator.Attribute) in.readObject();
210                assertSame("resolved incorrectly", attr1, attr2);
211            } catch (IllegalArgumentException e) {
212                fail("Unexpected IllegalArgumentException: " + e);
213            }
214
215        } catch (IOException e) {
216            fail("unexpected IOException" + e);
217        } catch (ClassNotFoundException e) {
218            fail("unexpected ClassNotFoundException" + e);
219        } finally {
220            try {
221                if (out != null)
222                    out.close();
223                if (in != null)
224                    in.close();
225            } catch (IOException e) {
226            }
227        }
228    }
229
230    /**
231     * @tests java.text.AttributedCharacterIterator.Attribute#toString() Test of
232     *        method java.text.AttributedCharacterIterator.Attribute#toString().
233     */
234    @TestTargetNew(
235        level = TestLevel.COMPLETE,
236        notes = "",
237        method = "toString",
238        args = {}
239    )
240    public void test_toString() {
241        MockAttributedCharacterIteratorAttribute mac1 = new MockAttributedCharacterIteratorAttribute(
242                null);
243        assertEquals("Unexpected class representation string", mac1.toString(),
244                getClass().getName()
245                        + "$MockAttributedCharacterIteratorAttribute(null)");
246        TestAttributedCharacterIteratorAttribute mac2 = new TestAttributedCharacterIteratorAttribute(
247                "test1");
248        assertEquals("Unexpected class representation string", mac2.toString(),
249                getClass().getName()
250                        + "$TestAttributedCharacterIteratorAttribute(test1)");
251    }
252}
253