Path: blob/master/test/jdk/java/beans/XMLEncoder/AbstractTest.java
41149 views
/*1* Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.beans.ExceptionListener;24import java.beans.XMLDecoder;25import java.beans.XMLEncoder;2627import java.io.ByteArrayInputStream;28import java.io.ByteArrayOutputStream;2930import java.nio.charset.Charset;3132import java.lang.reflect.Field;3334abstract class AbstractTest<T> implements ExceptionListener {35final BeanValidator validator = new BeanValidator();3637public final void exceptionThrown(Exception exception) {38throw new Error("unexpected exception", exception);39}4041/**42* Returns an object to test.43* This object will be encoded and decoded44* and the object creation will be tested.45*46* @return an object to test47*/48protected abstract T getObject();4950/**51* Returns a different object to test.52* If this object is not {@code null}53* it will be encoded and decoded54* and the object updating will be tested.55*56* @return a different object to test57*/58protected T getAnotherObject() {59return null;60}6162/**63* This method should be overridden64* if specified encoder should be initialized.65*66* @param encoder the XML encoder to initialize67*/68protected void initialize(XMLEncoder encoder) {69}7071/**72* This method should be overridden73* if specified decoder should be initialized.74*75* @param decoder the XML decoder to initialize76*/77protected void initialize(XMLDecoder decoder) {78}7980/**81* This method should be overridden82* for test-specific comparison.83*84* @param before the object before encoding85* @param after the object after decoding86*/87protected void validate(T before, T after) {88this.validator.validate(before, after);89}9091/**92* This is entry point to start testing.93*94* @param security use {@code true} to start95* second pass in secure context96*/97final void test(boolean security) {98Bean.DEFAULT = null;99T object = getObject();100101System.out.println("Test object");102validate(object, testObject(object));103104System.out.println("Test object creating");105validate(object, testBean(object));106107Bean.DEFAULT = object;108object = getAnotherObject();109if (object != null) {110System.out.println("Test another object");111validate(object, testObject(object));112113System.out.println("Test object updating");114validate(object, testBean(object));115}116if (security) {117System.setSecurityManager(new SecurityManager());118test(false);119}120}121122private T testBean(T object) {123Bean bean = new Bean();124bean.setValue(object);125bean = testObject(bean);126return (T) bean.getValue();127}128129private <Z> Z testObject(Z object) {130byte[] array = writeObject(object);131System.out.print(new String(array, Charset.forName("UTF-8")));132return (Z) readObject(array);133}134135private byte[] writeObject(Object object) {136ByteArrayOutputStream output = new ByteArrayOutputStream();137XMLEncoder encoder = new XMLEncoder(output);138encoder.setExceptionListener(this);139initialize(encoder);140encoder.writeObject(object);141encoder.close();142return output.toByteArray();143}144145private Object readObject(byte[] array) {146ByteArrayInputStream input = new ByteArrayInputStream(array);147XMLDecoder decoder = new XMLDecoder(input);148decoder.setExceptionListener(this);149initialize(decoder);150Object object = decoder.readObject();151decoder.close();152return object;153}154155static Field getField(String name) {156try {157int index = name.lastIndexOf('.');158String className = name.substring(0, index);159String fieldName = name.substring(1 + index);160Field field = Class.forName(className).getDeclaredField(fieldName);161field.setAccessible(true);162return field;163}164catch (Exception exception) {165throw new Error(exception);166}167}168}169170171