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