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 18 package org.apache.harmony.text.tests.java.text; 19 20 import java.text.AttributedCharacterIterator; 21 import java.text.AttributedCharacterIterator.Attribute; 22 import java.io.ByteArrayInputStream; 23 import java.io.ByteArrayOutputStream; 24 import java.io.NotSerializableException; 25 import java.io.IOException; 26 import java.io.ObjectInputStream; 27 import java.io.ObjectOutputStream; 28 import java.util.Locale; 29 import java.text.Annotation; 30 31 public class AttributedCharacterIteratorAttributeTest extends junit.framework.TestCase { 32 33 /** 34 * @tests java.text.AttributedCharacterIterator$Attribute() 35 */ test_constructor()36 public void test_constructor() { 37 MyAttribute attribute = new MyAttribute("attribute"); 38 39 assertEquals("Attribute has wrong name", "attribute", attribute.getExposedName()); 40 41 attribute = new MyAttribute(null); 42 assertEquals("Attribute has wrong name", null, attribute.getExposedName()); 43 } 44 45 /** 46 * @tests java.text.AttributedCharacterIterator.Attribute#equals(Object) 47 */ test_equals()48 public void test_equals() { 49 50 assertTrue(Attribute.LANGUAGE.equals(Attribute.LANGUAGE)); 51 52 assertFalse(Attribute.LANGUAGE.equals(Attribute.READING)); 53 54 MyAttribute attribute = new MyAttribute("test"); 55 56 assertTrue(attribute.equals(attribute)); 57 58 /* this implementation of equals should only return true 59 * if the same objects */ 60 assertFalse(attribute.equals(new MyAttribute("test"))); 61 62 attribute = new MyAttribute(null); 63 assertFalse(attribute.equals(new MyAttribute(null))); 64 } 65 66 /** 67 * @tests java.text.AttributedCharacterIterator$Attribute#readResolve() 68 */ test_readResolve()69 public void test_readResolve() { 70 // test for method java.lang.Object readResolve() 71 72 ObjectOutputStream out = null; 73 ObjectInputStream in = null; 74 try { 75 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 76 out = new ObjectOutputStream(bytes); 77 78 AttributedCharacterIterator.Attribute dattribute, dattribute2; 79 MyAttribute attribute; 80 81 // a regular instance of DateFormat.Field 82 dattribute = AttributedCharacterIterator.Attribute.LANGUAGE; 83 84 // a subclass instance with null name 85 attribute = new MyAttribute(null); 86 87 out.writeObject(dattribute); 88 try { 89 out.writeObject(attribute); 90 } catch (NotSerializableException e) { 91 } 92 93 in = new ObjectInputStream(new ByteArrayInputStream(bytes 94 .toByteArray())); 95 96 try { 97 dattribute2 = (AttributedCharacterIterator.Attribute) in.readObject(); 98 assertSame("resolved incorrectly", dattribute, dattribute2); 99 } catch (IllegalArgumentException e) { 100 fail("Unexpected IllegalArgumentException: " + e); 101 } 102 103 } catch (IOException e) { 104 fail("unexpected IOException" + e); 105 } catch (ClassNotFoundException e) { 106 fail("unexpected ClassNotFoundException" + e); 107 } finally { 108 try { 109 if (out != null) 110 out.close(); 111 if (in != null) 112 in.close(); 113 } catch (IOException e) { 114 } 115 } 116 } 117 118 /** 119 * @tests java.text.AttributedCharacterIterator$Attribute#LANGUAGE 120 * java.text.AttributedCharacterIterator$Attribute#READING 121 * java.text.AttributedCharacterIterator$Attribute#INPUT_METHOD_SEGMENT 122 */ test_fields()123 public void test_fields() { 124 125 // Just check that the fields are accessible as all 126 // methods are protected 127 Attribute language = Attribute.LANGUAGE; 128 Attribute reading = Attribute.READING; 129 Attribute inputMethodSegment = Attribute.INPUT_METHOD_SEGMENT; 130 } 131 setUp()132 protected void setUp() { 133 } 134 tearDown()135 protected void tearDown() { 136 } 137 138 class MyAttribute extends AttributedCharacterIterator.Attribute { MyAttribute(String name)139 protected MyAttribute(String name) { 140 super(name); 141 } 142 getExposedName()143 public Object getExposedName() { 144 return this.getName(); 145 } 146 } 147 }