author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 14342 | jdk/test/java/io/Serializable/expectedStackTrace/ExpectedStackTrace.java@8435a30053c1 |
child 58565 | baa5969ecf34 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
11895
diff
changeset
|
2 |
* Copyright (c) 2005, 2011, 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 |
/* @test |
|
11895 | 25 |
* @bug 6317435 7110700 |
2 | 26 |
* @summary Verify that stack trace contains a proper cause of |
27 |
* InvalidClassException (methods: checkSerialize, |
|
28 |
* checkDeserialize or checkDefaultSerialize) |
|
29 |
* |
|
30 |
* @author Andrey Ozerov |
|
31 |
* |
|
32 |
*/ |
|
33 |
import java.io.*; |
|
34 |
||
35 |
class NotSerializableObject { |
|
36 |
private String m_str; |
|
37 |
private Integer m_int; |
|
38 |
||
39 |
public NotSerializableObject(String m_str, Integer m_int) { |
|
40 |
this.m_str = m_str; |
|
41 |
this.m_int = m_int; |
|
42 |
} |
|
43 |
} |
|
44 |
||
45 |
class SerializableObject extends NotSerializableObject |
|
46 |
implements Serializable |
|
47 |
{ |
|
48 |
||
49 |
public SerializableObject(String m_str, Integer m_int) { |
|
50 |
super(m_str, m_int); |
|
51 |
} |
|
52 |
||
53 |
public SerializableObject() { |
|
54 |
super("test", 10); |
|
55 |
} |
|
56 |
} |
|
57 |
||
58 |
public class ExpectedStackTrace { |
|
59 |
private static final String SER_METHOD_NAME = "checkSerializable"; |
|
60 |
||
61 |
public static final void main(String[] args) throws Exception { |
|
11895 | 62 |
System.err.println("\nRegression test for CRs 6317435, 7110700"); |
2 | 63 |
checkSerializable(getObject()); |
64 |
} |
|
65 |
||
66 |
private static Object getObject() throws Exception { |
|
67 |
ObjectStreamClass osc = |
|
68 |
ObjectStreamClass.lookup(SerializableObject.class); |
|
69 |
SerializableObject initObj = |
|
70 |
(SerializableObject) osc.forClass().newInstance(); |
|
71 |
return initObj; |
|
72 |
} |
|
73 |
||
74 |
private static void checkSerializable(Object initObj) throws Exception { |
|
75 |
try { |
|
76 |
// Serialize to a byte array |
|
77 |
ByteArrayOutputStream bos = new ByteArrayOutputStream() ; |
|
78 |
ObjectOutputStream out = new ObjectOutputStream(bos) ; |
|
79 |
out.writeObject(initObj); |
|
80 |
out.close(); |
|
81 |
||
82 |
// Get the bytes of the serialized object |
|
83 |
byte[] buf = bos.toByteArray(); |
|
84 |
||
85 |
// Deserialize from a byte array |
|
86 |
ByteArrayInputStream bais = new ByteArrayInputStream(buf); |
|
87 |
ObjectInputStream in = new ObjectInputStream(bais); |
|
88 |
SerializableObject finObj = (SerializableObject) in.readObject(); |
|
89 |
in.close(); |
|
90 |
throw new Error(); |
|
91 |
} catch(ObjectStreamException ex) { |
|
92 |
StackTraceElement[] stes = ex.getStackTrace(); |
|
93 |
boolean found = false; |
|
94 |
for (int i = 0; i<stes.length-1; i++) { |
|
95 |
StackTraceElement ste = stes[i]; |
|
96 |
String nme = ste.getMethodName(); |
|
97 |
if (nme.equals(SER_METHOD_NAME)) { |
|
98 |
found = true; |
|
99 |
} |
|
100 |
} |
|
101 |
if (found) { |
|
11894
c7af6deeffd8
7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents:
5506
diff
changeset
|
102 |
if (ex.getCause() != null) { |
11895 | 103 |
throw new Error("\nTest for CR 7110700 FAILED"); |
11894
c7af6deeffd8
7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents:
5506
diff
changeset
|
104 |
} |
11895 | 105 |
System.err.println("\nTEST PASSED"); |
2 | 106 |
} else { |
11894
c7af6deeffd8
7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents:
5506
diff
changeset
|
107 |
throw new Error("\nTest for CR 6317435 FAILED"); |
2 | 108 |
} |
109 |
} |
|
110 |
} |
|
111 |
} |