1 /* 2 * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 @bug 4136620 4144590 26 @summary Make sure that Attribute & subclasses are serialized and deserialized correctly 27 @modules java.desktop 28 */ 29 30 package test.java.text.AttributedCharacterIterator.Attribute; 31 32 import java.text.AttributedCharacterIterator.Attribute; 33 import java.awt.font.TextAttribute; 34 import java.io.*; 35 36 public class ReadResolve { 37 main(String[] args)38 public static void main(String[] args) throws Exception { 39 testSerializationCycle(Attribute.LANGUAGE); 40 testSerializationCycle(TextAttribute.INPUT_METHOD_HIGHLIGHT); 41 42 boolean gotException = false; 43 Attribute result = null; 44 try { 45 result = doSerializationCycle(FakeAttribute.LANGUAGE); 46 } catch (Throwable e) { 47 gotException = true; 48 } 49 if (!gotException) { 50 throw new RuntimeException("Attribute should throw an exception when given a fake \"language\" attribute. Deserialized object: " + result); 51 } 52 } 53 doSerializationCycle(Attribute attribute)54 static Attribute doSerializationCycle(Attribute attribute) throws Exception { 55 56 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 57 ObjectOutputStream oos = new ObjectOutputStream(baos); 58 oos.writeObject(attribute); 59 oos.flush(); 60 61 byte[] data = baos.toByteArray(); 62 63 ByteArrayInputStream bais = new ByteArrayInputStream(data); 64 ObjectInputStream ois = new ObjectInputStream(bais); 65 Attribute result = (Attribute) ois.readObject(); 66 67 return result; 68 } 69 testSerializationCycle(Attribute attribute)70 static void testSerializationCycle(Attribute attribute) throws Exception { 71 Attribute result = doSerializationCycle(attribute); 72 if (result != attribute) { 73 throw new RuntimeException("attribute changed identity during serialization/deserialization"); 74 } 75 } 76 77 private static class FakeAttribute extends Attribute { 78 79 // This LANGUAGE attribute should never be confused with the 80 // Attribute.LANGUAGE attribute. However, we don't override 81 // readResolve here, so that deserialization goes 82 // to Attribute. Attribute has to catch this problem and reject 83 // the fake attribute. 84 static final FakeAttribute LANGUAGE = new FakeAttribute("language"); 85 FakeAttribute(String name)86 FakeAttribute(String name) { 87 super(name); 88 } 89 } 90 } 91