0N/A/*
2362N/A * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Aimport java.beans.ExceptionListener;
0N/Aimport java.beans.XMLDecoder;
0N/Aimport java.beans.XMLEncoder;
0N/A
0N/Aimport java.io.ByteArrayInputStream;
0N/Aimport java.io.ByteArrayOutputStream;
0N/A
0N/Aimport java.nio.charset.Charset;
0N/A
6051N/Aimport java.lang.reflect.Field;
6051N/A
0N/Aabstract class AbstractTest<T> implements ExceptionListener {
6051N/A final BeanValidator validator = new BeanValidator();
0N/A
0N/A public final void exceptionThrown(Exception exception) {
0N/A throw new Error("unexpected exception", exception);
0N/A }
0N/A
0N/A /**
0N/A * Returns an object to test.
0N/A * This object will be encoded and decoded
0N/A * and the object creation will be tested.
0N/A *
0N/A * @return an object to test
0N/A */
0N/A protected abstract T getObject();
0N/A
0N/A /**
0N/A * Returns a different object to test.
0N/A * If this object is not {@code null}
0N/A * it will be encoded and decoded
0N/A * and the object updating will be tested.
0N/A *
0N/A * @return a different object to test
0N/A */
0N/A protected T getAnotherObject() {
0N/A return null;
0N/A }
0N/A
0N/A /**
6051N/A * This method should be overridden
0N/A * if specified encoder should be initialized.
0N/A *
0N/A * @param encoder the XML encoder to initialize
0N/A */
0N/A protected void initialize(XMLEncoder encoder) {
0N/A }
0N/A
0N/A /**
6051N/A * This method should be overridden
0N/A * if specified decoder should be initialized.
0N/A *
0N/A * @param decoder the XML decoder to initialize
0N/A */
0N/A protected void initialize(XMLDecoder decoder) {
0N/A }
0N/A
0N/A /**
6051N/A * This method should be overridden
0N/A * for test-specific comparison.
0N/A *
0N/A * @param before the object before encoding
0N/A * @param after the object after decoding
0N/A */
0N/A protected void validate(T before, T after) {
0N/A this.validator.validate(before, after);
0N/A }
0N/A
0N/A /**
0N/A * This is entry point to start testing.
0N/A *
0N/A * @param security use {@code true} to start
0N/A * second pass in secure context
0N/A */
0N/A final void test(boolean security) {
0N/A Bean.DEFAULT = null;
0N/A T object = getObject();
0N/A
0N/A System.out.println("Test object");
0N/A validate(object, testObject(object));
0N/A
0N/A System.out.println("Test object creating");
0N/A validate(object, testBean(object));
0N/A
0N/A Bean.DEFAULT = object;
0N/A object = getAnotherObject();
0N/A if (object != null) {
0N/A System.out.println("Test another object");
0N/A validate(object, testObject(object));
0N/A
0N/A System.out.println("Test object updating");
0N/A validate(object, testBean(object));
0N/A }
0N/A if (security) {
0N/A System.setSecurityManager(new SecurityManager());
0N/A test(false);
0N/A }
0N/A }
0N/A
0N/A private T testBean(T object) {
0N/A Bean bean = new Bean();
0N/A bean.setValue(object);
0N/A bean = testObject(bean);
0N/A return (T) bean.getValue();
0N/A }
0N/A
0N/A private <Z> Z testObject(Z object) {
0N/A byte[] array = writeObject(object);
0N/A System.out.print(new String(array, Charset.forName("UTF-8")));
0N/A return (Z) readObject(array);
0N/A }
0N/A
0N/A private byte[] writeObject(Object object) {
0N/A ByteArrayOutputStream output = new ByteArrayOutputStream();
0N/A XMLEncoder encoder = new XMLEncoder(output);
6051N/A encoder.setExceptionListener(this);
0N/A initialize(encoder);
0N/A encoder.writeObject(object);
0N/A encoder.close();
0N/A return output.toByteArray();
0N/A }
0N/A
0N/A private Object readObject(byte[] array) {
0N/A ByteArrayInputStream input = new ByteArrayInputStream(array);
0N/A XMLDecoder decoder = new XMLDecoder(input);
6051N/A decoder.setExceptionListener(this);
0N/A initialize(decoder);
0N/A Object object = decoder.readObject();
0N/A decoder.close();
0N/A return object;
0N/A }
6051N/A
6051N/A static Field getField(String name) {
6051N/A try {
6051N/A int index = name.lastIndexOf('.');
6051N/A String className = name.substring(0, index);
6051N/A String fieldName = name.substring(1 + index);
6051N/A Field field = Class.forName(className).getDeclaredField(fieldName);
6051N/A field.setAccessible(true);
6051N/A return field;
6051N/A }
6051N/A catch (Exception exception) {
6051N/A throw new Error(exception);
6051N/A }
6051N/A }
0N/A}