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 import java.text.AttributedCharacterIterator.Attribute;
  31 import java.awt.font.TextAttribute;
  32 import java.io.*;
  33 
  34 public class ReadResolve {
  35 
  36     public static void main(String[] args) throws Exception {
  37         testSerializationCycle(Attribute.LANGUAGE);
  38         testSerializationCycle(TextAttribute.INPUT_METHOD_HIGHLIGHT);
  39 
  40         boolean gotException = false;
  41         Attribute result = null;
  42         try {
  43             result = doSerializationCycle(FakeAttribute.LANGUAGE);
  44         } catch (Throwable e) {
  45             gotException = true;
  46         }
  47         if (!gotException) {
  48             throw new RuntimeException("Attribute should throw an exception when given a fake \"language\" attribute. Deserialized object: " + result);
  49         }
  50     }
  51 
  52     static Attribute doSerializationCycle(Attribute attribute) throws Exception {
  53 
  54         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  55         ObjectOutputStream  oos  =  new  ObjectOutputStream(baos);
  56         oos.writeObject(attribute);
  57         oos.flush();
  58 
  59         byte[] data = baos.toByteArray();
  60 
  61         ByteArrayInputStream bais = new ByteArrayInputStream(data);
  62         ObjectInputStream ois = new ObjectInputStream(bais);
  63         Attribute result = (Attribute) ois.readObject();
  64 
  65         return result;
  66     }
  67 
  68     static void testSerializationCycle(Attribute attribute) throws Exception {
  69         Attribute result = doSerializationCycle(attribute);
  70         if (result != attribute) {
  71             throw new RuntimeException("attribute changed identity during serialization/deserialization");
  72         }
  73     }
  74 
  75     private static class FakeAttribute extends Attribute {
  76 
  77         // This LANGUAGE attribute should never be confused with the
  78         // Attribute.LANGUAGE attribute. However, we don't override
  79         // readResolve here, so that deserialization goes
  80         // to Attribute. Attribute has to catch this problem and reject
  81         // the fake attribute.
  82         static final FakeAttribute LANGUAGE = new FakeAttribute("language");
  83 
  84         FakeAttribute(String name) {
  85             super(name);
  86         }
  87     }
  88 }