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) 2000, 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 |
/* @test |
|
25 |
* @summary Verifies basic correct functioning of proxy serialization. |
|
30046 | 26 |
* @key randomness |
2 | 27 |
*/ |
28 |
||
29 |
import java.io.*; |
|
30 |
import java.lang.reflect.*; |
|
31 |
import java.util.Random; |
|
32 |
||
33 |
// proxy interfaces |
|
34 |
interface Foo { int foo(); } |
|
35 |
interface Bar { float bar(); } |
|
36 |
||
37 |
// dummy invocation handler |
|
38 |
class Handler implements InvocationHandler, Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
39 |
private static final long serialVersionUID = 1L; |
2 | 40 |
|
41 |
static Method fooMethod, barMethod; |
|
42 |
static { |
|
43 |
try { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
44 |
fooMethod = Foo.class.getDeclaredMethod("foo", new Class<?>[0]); |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
45 |
barMethod = Bar.class.getDeclaredMethod("bar", new Class<?>[0]); |
2 | 46 |
} catch (NoSuchMethodException ex) { |
47 |
throw new Error(); |
|
48 |
} |
|
49 |
} |
|
50 |
||
51 |
int foo; |
|
52 |
float bar; |
|
53 |
||
54 |
Handler(int foo, float bar) { |
|
55 |
this.foo = foo; |
|
56 |
this.bar = bar; |
|
57 |
} |
|
58 |
||
59 |
public Object invoke(Object proxy, Method method, Object[] args) |
|
60 |
throws Throwable |
|
61 |
{ |
|
62 |
if (method.equals(fooMethod)) { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
63 |
return foo; |
2 | 64 |
} else if (method.equals(barMethod)) { |
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
65 |
return bar; |
2 | 66 |
} else { |
67 |
throw new UnsupportedOperationException(); |
|
68 |
} |
|
69 |
} |
|
70 |
} |
|
71 |
||
72 |
// ObjectInputStream incapable of resolving proxy classes |
|
73 |
class ProxyBlindInputStream extends ObjectInputStream { |
|
74 |
||
75 |
ProxyBlindInputStream(InputStream in) throws IOException { super(in); } |
|
76 |
||
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
77 |
protected Class<?> resolveProxyClass(String[] interfaces) |
2 | 78 |
throws IOException, ClassNotFoundException |
79 |
{ |
|
80 |
throw new ClassNotFoundException(); |
|
81 |
} |
|
82 |
} |
|
83 |
||
84 |
public class Basic { |
|
85 |
public static void main(String[] args) throws Exception { |
|
5809
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
86 |
ClassLoader loader = Basic.class.getClassLoader(); |
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
87 |
Class<?>[] interfaces = { Foo.class, Bar.class }; |
2 | 88 |
Random rand = new Random(); |
89 |
int foo = rand.nextInt(); |
|
90 |
float bar = rand.nextFloat(); |
|
91 |
ObjectOutputStream oout; |
|
92 |
ObjectInputStream oin; |
|
93 |
ByteArrayOutputStream bout; |
|
94 |
Object proxy; |
|
95 |
||
96 |
||
97 |
// test simple proxy write + read |
|
98 |
bout = new ByteArrayOutputStream(); |
|
99 |
oout = new ObjectOutputStream(bout); |
|
100 |
oout.writeObject(Proxy.newProxyInstance( |
|
101 |
loader, interfaces, new Handler(foo, bar))); |
|
102 |
oout.close(); |
|
103 |
||
104 |
oin = new ObjectInputStream( |
|
105 |
new ByteArrayInputStream(bout.toByteArray())); |
|
106 |
proxy = oin.readObject(); |
|
107 |
if ((((Foo) proxy).foo() != foo) || (((Bar) proxy).bar() != bar)) { |
|
108 |
throw new Error(); |
|
109 |
} |
|
110 |
||
111 |
||
112 |
// test missing proxy class ClassNotFoundException |
|
113 |
oin = new ProxyBlindInputStream( |
|
114 |
new ByteArrayInputStream(bout.toByteArray())); |
|
115 |
try { |
|
116 |
oin.readObject(); |
|
117 |
throw new Error(); |
|
118 |
} catch (ClassNotFoundException ex) { |
|
119 |
} |
|
120 |
} |
|
121 |
} |