1/* 2 * Copyright (c) 2008, 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 24import java.beans.ExceptionListener; 25import java.beans.XMLDecoder; 26 27import java.io.ByteArrayInputStream; 28 29abstract class AbstractTest implements ExceptionListener { 30 public void exceptionThrown(Exception exception) { 31 throw new Error("unexpected exception", exception); 32 } 33 34 /** 35 * Validates the XML decoder for XML archive 36 * that defined in the public field of the subclass. 37 * 38 * @param decoder the initialized XML decoder 39 * @throws Error if validation failed 40 */ 41 protected abstract void validate(XMLDecoder decoder); 42 43 /** 44 * This is entry point to start testing. 45 * 46 * @param security use {@code true} to start 47 * second pass in secure context 48 */ 49 final void test(boolean security) { 50 byte[] array = getFieldValue("XML").getBytes(); // NON-NLS: the field name 51 ByteArrayInputStream input = new ByteArrayInputStream(array); 52 XMLDecoder decoder = new XMLDecoder(input); 53 decoder.setExceptionListener(this); 54 validate(decoder); 55 try { 56 throw new Error("unexpected object" + decoder.readObject()); 57 } catch (ArrayIndexOutOfBoundsException exception) { 58 // expected exception 59 } 60 decoder.close(); 61 if (security) { 62 System.setSecurityManager(new SecurityManager()); 63 test(false); 64 } 65 } 66 67 private String getFieldValue(String field) { 68 try { 69 return getClass().getField(field).get(this).toString(); 70 } catch (NoSuchFieldException exception) { 71 throw new Error("unexpected exception", exception); 72 } catch (IllegalAccessException exception) { 73 throw new Error("unexpected exception", exception); 74 } 75 } 76} 77