blob: a01deb6832e831663f25990a61958e35fe9a63c6 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2006-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24import java.beans.ExceptionListener;
25import java.beans.XMLDecoder;
26import java.beans.XMLEncoder;
27
28import java.io.ByteArrayInputStream;
29import java.io.ByteArrayOutputStream;
30
31import java.nio.charset.Charset;
32
33abstract class AbstractTest<T> implements ExceptionListener {
34 private final BeanValidator validator = new BeanValidator();
35
36 public final void exceptionThrown(Exception exception) {
37 throw new Error("unexpected exception", exception);
38 }
39
40 /**
41 * Returns an object to test.
42 * This object will be encoded and decoded
43 * and the object creation will be tested.
44 *
45 * @return an object to test
46 */
47 protected abstract T getObject();
48
49 /**
50 * Returns a different object to test.
51 * If this object is not {@code null}
52 * it will be encoded and decoded
53 * and the object updating will be tested.
54 *
55 * @return a different object to test
56 */
57 protected T getAnotherObject() {
58 return null;
59 }
60
61 /**
62 * This method should be overriden
63 * if specified encoder should be initialized.
64 *
65 * @param encoder the XML encoder to initialize
66 */
67 protected void initialize(XMLEncoder encoder) {
68 }
69
70 /**
71 * This method should be overriden
72 * if specified decoder should be initialized.
73 *
74 * @param decoder the XML decoder to initialize
75 */
76 protected void initialize(XMLDecoder decoder) {
77 }
78
79 /**
80 * This method should be overriden
81 * for test-specific comparison.
82 *
83 * @param before the object before encoding
84 * @param after the object after decoding
85 */
86 protected void validate(T before, T after) {
87 this.validator.validate(before, after);
88 }
89
90 /**
91 * This is entry point to start testing.
92 *
93 * @param security use {@code true} to start
94 * second pass in secure context
95 */
96 final void test(boolean security) {
97 Bean.DEFAULT = null;
98 T object = getObject();
99
100 System.out.println("Test object");
101 validate(object, testObject(object));
102
103 System.out.println("Test object creating");
104 validate(object, testBean(object));
105
106 Bean.DEFAULT = object;
107 object = getAnotherObject();
108 if (object != null) {
109 System.out.println("Test another object");
110 validate(object, testObject(object));
111
112 System.out.println("Test object updating");
113 validate(object, testBean(object));
114 }
115 if (security) {
116 System.setSecurityManager(new SecurityManager());
117 test(false);
118 }
119 }
120
121 private T testBean(T object) {
122 Bean bean = new Bean();
123 bean.setValue(object);
124 bean = testObject(bean);
125 return (T) bean.getValue();
126 }
127
128 private <Z> Z testObject(Z object) {
129 byte[] array = writeObject(object);
130 System.out.print(new String(array, Charset.forName("UTF-8")));
131 return (Z) readObject(array);
132 }
133
134 private byte[] writeObject(Object object) {
135 ByteArrayOutputStream output = new ByteArrayOutputStream();
136 XMLEncoder encoder = new XMLEncoder(output);
137 initialize(encoder);
138 encoder.writeObject(object);
139 encoder.close();
140 return output.toByteArray();
141 }
142
143 private Object readObject(byte[] array) {
144 ByteArrayInputStream input = new ByteArrayInputStream(array);
145 XMLDecoder decoder = new XMLDecoder(input);
146 initialize(decoder);
147 Object object = decoder.readObject();
148 decoder.close();
149 return object;
150 }
151}