0N/A/*
2362N/A * Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A@test
0N/A@bug 4094892
0N/A@summary Verify that an object is not validated more than once during deserialization.
0N/A
0N/A*/
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.Date;
0N/A
0N/Apublic class Validate {
0N/A public static void main(String[] args) throws Exception {
0N/A try {
0N/A // Write class out
0N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
0N/A ObjectOutput out = new ObjectOutputStream(baos);
0N/A
0N/A Class1 c1 = new Class1(11, 22);
0N/A out.writeObject(c1);
0N/A out.writeObject(new Class1(22,33));
0N/A out.writeObject(new Date());
0N/A out.writeObject(new Date());
0N/A out.writeObject(new Date());
0N/A out.writeObject(new Date());
0N/A out.flush();
0N/A out.close();
0N/A
0N/A // Read it back
0N/A ByteArrayInputStream bais =
0N/A new ByteArrayInputStream(baos.toByteArray());
0N/A ObjectInputStream in = new ObjectInputStream(bais);
0N/A Class1 cc1 = (Class1) in.readObject();
0N/A Class1 cc2 = (Class1) in.readObject();
0N/A System.out.println("date: " + in.readObject());
0N/A System.out.println("date: " + in.readObject());
0N/A System.out.println("date: " + in.readObject());
0N/A System.out.println("date: " + in.readObject());
0N/A in.close();
0N/A
0N/A System.out.println(cc1.a + " " + cc1.b);
0N/A System.out.println(cc2.a + " " + cc2.b);
0N/A } catch (IOException e) {
0N/A e.printStackTrace();
0N/A throw e;
0N/A } catch (ClassNotFoundException e) {
0N/A e.printStackTrace();
0N/A throw e;
0N/A }
0N/A }
0N/A}
0N/A
0N/Aclass Class1 implements Serializable, ObjectInputValidation {
0N/A int a, b;
0N/A transient int validates;
0N/A
0N/A public Class1(int aa, int bb) {
0N/A a = aa;
0N/A b = bb;
0N/A }
0N/A public void validateObject() throws InvalidObjectException {
0N/A if (validates > 0)
0N/A throw new Error("Implementation error: Re-validating object " + this.toString());
0N/A validates++;
0N/A
0N/A System.out.println("Validating " + this.toString());
0N/A if (a > b) {
0N/A throw new InvalidObjectException("Fields cannot be negative");
0N/A }
0N/A }
0N/A private void readObject(ObjectInputStream in)
0N/A throws IOException, ClassNotFoundException {
0N/A in.registerValidation(this, 1);
0N/A in.defaultReadObject();
0N/A }
0N/A}