Path: blob/master/test/jdk/java/beans/XMLDecoder/spec/AbstractTest.java
41155 views
/*1* Copyright (c) 2008, 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;2526import java.io.ByteArrayInputStream;2728abstract class AbstractTest implements ExceptionListener {29public void exceptionThrown(Exception exception) {30throw new Error("unexpected exception", exception);31}3233/**34* Validates the XML decoder for XML archive35* that defined in the public field of the subclass.36*37* @param decoder the initialized XML decoder38* @throws Error if validation failed39*/40protected abstract void validate(XMLDecoder decoder);4142/**43* This is entry point to start testing.44*45* @param security use {@code true} to start46* second pass in secure context47*/48final void test(boolean security) {49byte[] array = getFieldValue("XML").getBytes(); // NON-NLS: the field name50ByteArrayInputStream input = new ByteArrayInputStream(array);51XMLDecoder decoder = new XMLDecoder(input);52decoder.setExceptionListener(this);53validate(decoder);54try {55throw new Error("unexpected object" + decoder.readObject());56} catch (ArrayIndexOutOfBoundsException exception) {57// expected exception58}59decoder.close();60if (security) {61System.setSecurityManager(new SecurityManager());62test(false);63}64}6566private String getFieldValue(String field) {67try {68return getClass().getField(field).get(this).toString();69} catch (NoSuchFieldException exception) {70throw new Error("unexpected exception", exception);71} catch (IllegalAccessException exception) {72throw new Error("unexpected exception", exception);73}74}75}767778