Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/beans/XMLEncoder/AbstractTest.java
41149 views
1
/*
2
* Copyright (c) 2006, 2013, 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
import java.beans.ExceptionListener;
25
import java.beans.XMLDecoder;
26
import java.beans.XMLEncoder;
27
28
import java.io.ByteArrayInputStream;
29
import java.io.ByteArrayOutputStream;
30
31
import java.nio.charset.Charset;
32
33
import java.lang.reflect.Field;
34
35
abstract class AbstractTest<T> implements ExceptionListener {
36
final BeanValidator validator = new BeanValidator();
37
38
public final void exceptionThrown(Exception exception) {
39
throw new Error("unexpected exception", exception);
40
}
41
42
/**
43
* Returns an object to test.
44
* This object will be encoded and decoded
45
* and the object creation will be tested.
46
*
47
* @return an object to test
48
*/
49
protected abstract T getObject();
50
51
/**
52
* Returns a different object to test.
53
* If this object is not {@code null}
54
* it will be encoded and decoded
55
* and the object updating will be tested.
56
*
57
* @return a different object to test
58
*/
59
protected T getAnotherObject() {
60
return null;
61
}
62
63
/**
64
* This method should be overridden
65
* if specified encoder should be initialized.
66
*
67
* @param encoder the XML encoder to initialize
68
*/
69
protected void initialize(XMLEncoder encoder) {
70
}
71
72
/**
73
* This method should be overridden
74
* if specified decoder should be initialized.
75
*
76
* @param decoder the XML decoder to initialize
77
*/
78
protected void initialize(XMLDecoder decoder) {
79
}
80
81
/**
82
* This method should be overridden
83
* for test-specific comparison.
84
*
85
* @param before the object before encoding
86
* @param after the object after decoding
87
*/
88
protected void validate(T before, T after) {
89
this.validator.validate(before, after);
90
}
91
92
/**
93
* This is entry point to start testing.
94
*
95
* @param security use {@code true} to start
96
* second pass in secure context
97
*/
98
final void test(boolean security) {
99
Bean.DEFAULT = null;
100
T object = getObject();
101
102
System.out.println("Test object");
103
validate(object, testObject(object));
104
105
System.out.println("Test object creating");
106
validate(object, testBean(object));
107
108
Bean.DEFAULT = object;
109
object = getAnotherObject();
110
if (object != null) {
111
System.out.println("Test another object");
112
validate(object, testObject(object));
113
114
System.out.println("Test object updating");
115
validate(object, testBean(object));
116
}
117
if (security) {
118
System.setSecurityManager(new SecurityManager());
119
test(false);
120
}
121
}
122
123
private T testBean(T object) {
124
Bean bean = new Bean();
125
bean.setValue(object);
126
bean = testObject(bean);
127
return (T) bean.getValue();
128
}
129
130
private <Z> Z testObject(Z object) {
131
byte[] array = writeObject(object);
132
System.out.print(new String(array, Charset.forName("UTF-8")));
133
return (Z) readObject(array);
134
}
135
136
private byte[] writeObject(Object object) {
137
ByteArrayOutputStream output = new ByteArrayOutputStream();
138
XMLEncoder encoder = new XMLEncoder(output);
139
encoder.setExceptionListener(this);
140
initialize(encoder);
141
encoder.writeObject(object);
142
encoder.close();
143
return output.toByteArray();
144
}
145
146
private Object readObject(byte[] array) {
147
ByteArrayInputStream input = new ByteArrayInputStream(array);
148
XMLDecoder decoder = new XMLDecoder(input);
149
decoder.setExceptionListener(this);
150
initialize(decoder);
151
Object object = decoder.readObject();
152
decoder.close();
153
return object;
154
}
155
156
static Field getField(String name) {
157
try {
158
int index = name.lastIndexOf('.');
159
String className = name.substring(0, index);
160
String fieldName = name.substring(1 + index);
161
Field field = Class.forName(className).getDeclaredField(fieldName);
162
field.setAccessible(true);
163
return field;
164
}
165
catch (Exception exception) {
166
throw new Error(exception);
167
}
168
}
169
}
170
171