author | rriggs |
Fri, 11 Oct 2019 13:11:56 -0400 | |
changeset 58565 | baa5969ecf34 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
@test |
|
26 |
@bug 4094892 |
|
27 |
@summary Verify that an object is not validated more than once during deserialization. |
|
28 |
||
29 |
*/ |
|
30 |
||
31 |
import java.io.*; |
|
32 |
import java.util.Date; |
|
33 |
||
34 |
public class Validate { |
|
35 |
public static void main(String[] args) throws Exception { |
|
36 |
try { |
|
37 |
// Write class out |
|
38 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
39 |
ObjectOutput out = new ObjectOutputStream(baos); |
|
40 |
||
41 |
Class1 c1 = new Class1(11, 22); |
|
42 |
out.writeObject(c1); |
|
43 |
out.writeObject(new Class1(22,33)); |
|
44 |
out.writeObject(new Date()); |
|
45 |
out.writeObject(new Date()); |
|
46 |
out.writeObject(new Date()); |
|
47 |
out.writeObject(new Date()); |
|
48 |
out.flush(); |
|
49 |
out.close(); |
|
50 |
||
51 |
// Read it back |
|
52 |
ByteArrayInputStream bais = |
|
53 |
new ByteArrayInputStream(baos.toByteArray()); |
|
54 |
ObjectInputStream in = new ObjectInputStream(bais); |
|
55 |
Class1 cc1 = (Class1) in.readObject(); |
|
56 |
Class1 cc2 = (Class1) in.readObject(); |
|
57 |
System.out.println("date: " + in.readObject()); |
|
58 |
System.out.println("date: " + in.readObject()); |
|
59 |
System.out.println("date: " + in.readObject()); |
|
60 |
System.out.println("date: " + in.readObject()); |
|
61 |
in.close(); |
|
62 |
||
63 |
System.out.println(cc1.a + " " + cc1.b); |
|
64 |
System.out.println(cc2.a + " " + cc2.b); |
|
65 |
} catch (IOException e) { |
|
66 |
e.printStackTrace(); |
|
67 |
throw e; |
|
68 |
} catch (ClassNotFoundException e) { |
|
69 |
e.printStackTrace(); |
|
70 |
throw e; |
|
71 |
} |
|
72 |
} |
|
73 |
} |
|
74 |
||
75 |
class Class1 implements Serializable, ObjectInputValidation { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
76 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
77 |
|
2 | 78 |
int a, b; |
79 |
transient int validates; |
|
80 |
||
81 |
public Class1(int aa, int bb) { |
|
82 |
a = aa; |
|
83 |
b = bb; |
|
84 |
} |
|
85 |
public void validateObject() throws InvalidObjectException { |
|
86 |
if (validates > 0) |
|
87 |
throw new Error("Implementation error: Re-validating object " + this.toString()); |
|
88 |
validates++; |
|
89 |
||
90 |
System.out.println("Validating " + this.toString()); |
|
91 |
if (a > b) { |
|
92 |
throw new InvalidObjectException("Fields cannot be negative"); |
|
93 |
} |
|
94 |
} |
|
95 |
private void readObject(ObjectInputStream in) |
|
96 |
throws IOException, ClassNotFoundException { |
|
97 |
in.registerValidation(this, 1); |
|
98 |
in.defaultReadObject(); |
|
99 |
} |
|
100 |
} |