Path: blob/master/test/jdk/java/text/AttributedCharacterIterator/Attribute/ReadResolve.java
41152 views
/*1* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 4136620 414459025@summary Make sure that Attribute & subclasses are serialized and deserialized correctly26@modules java.desktop27*/2829import java.text.AttributedCharacterIterator.Attribute;30import java.awt.font.TextAttribute;31import java.io.*;3233public class ReadResolve {3435public static void main(String[] args) throws Exception {36testSerializationCycle(Attribute.LANGUAGE);37testSerializationCycle(TextAttribute.INPUT_METHOD_HIGHLIGHT);3839boolean gotException = false;40Attribute result = null;41try {42result = doSerializationCycle(FakeAttribute.LANGUAGE);43} catch (Throwable e) {44gotException = true;45}46if (!gotException) {47throw new RuntimeException("Attribute should throw an exception when given a fake \"language\" attribute. Deserialized object: " + result);48}49}5051static Attribute doSerializationCycle(Attribute attribute) throws Exception {5253ByteArrayOutputStream baos = new ByteArrayOutputStream();54ObjectOutputStream oos = new ObjectOutputStream(baos);55oos.writeObject(attribute);56oos.flush();5758byte[] data = baos.toByteArray();5960ByteArrayInputStream bais = new ByteArrayInputStream(data);61ObjectInputStream ois = new ObjectInputStream(bais);62Attribute result = (Attribute) ois.readObject();6364return result;65}6667static void testSerializationCycle(Attribute attribute) throws Exception {68Attribute result = doSerializationCycle(attribute);69if (result != attribute) {70throw new RuntimeException("attribute changed identity during serialization/deserialization");71}72}7374private static class FakeAttribute extends Attribute {7576// This LANGUAGE attribute should never be confused with the77// Attribute.LANGUAGE attribute. However, we don't override78// readResolve here, so that deserialization goes79// to Attribute. Attribute has to catch this problem and reject80// the fake attribute.81static final FakeAttribute LANGUAGE = new FakeAttribute("language");8283FakeAttribute(String name) {84super(name);85}86}87}888990