Path: blob/master/test/jdk/java/io/Serializable/oldTests/SerializeWithException.java
41153 views
/*1* Copyright (c) 2005, 2006, 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*/2223/* @test24* @summary it is a new version of an old test which was25* /src/share/test/serialization/piotest.java26* Test of serialization when there is27* exceptions on the I/O stream28*29* @build PrimitivesTest30* @run main SerializeWithException31*/3233import java.io.*;3435public class SerializeWithException {36public static void main (String argv[]) {37System.err.println("\nRegression test for testing of " +38"serialization when there is exceptions on the I/O stream \n");3940try {41int i = 123456;42byte b = 12;43short s = 45;44char c = 'A';45long l = 1234567890000L;46float f = 3.14159f;47double d = f*2;48boolean z = true;49String string = "The String";50PrimitivesTest prim = new PrimitivesTest();5152/* For each of the byte offsets from 0 to 100,53do the pickling but expect an exception */54for (int offset = 0; offset < 200; offset++) {55ExceptionOutputStream ostream;56boolean expect_exception = false;57IOException exception = null;5859try {60expect_exception = true;61exception = null;6263ostream = new ExceptionOutputStream();64ostream.setExceptionOffset(offset);65ObjectOutputStream p = new ObjectOutputStream(ostream);6667p.writeInt(i);68p.writeByte(b);69p.writeShort(s);70p.writeChar(c);71p.writeLong(l);72p.writeFloat(f);73p.writeDouble(d);74p.writeBoolean(z);75p.writeUTF(string);76p.writeObject(string);7778p.writeObject(prim);79p.flush();80expect_exception = false;81} catch (IOException ee) {82exception = ee;83}8485if (expect_exception && exception == null) {86System.err.println("\nIOException did not occur at " +87"offset " + offset);88throw new Error();89}90if (!expect_exception && exception != null) {91System.err.println("\n " + exception.toString() +92" not expected at offset " + offset);93throw new Error();94}95}96System.err.println("\nTEST PASSED");97} catch (Exception e) {98System.err.print("TEST FAILED: ");99e.printStackTrace();100throw new Error();101}102}103}104105class ExceptionOutputStream extends OutputStream {106private int exceptionOffset = 0;107private int currentOffset = 0;108109/**110* Writes a byte to the buffer.111* @param b the byte112*/113public void write(int b) throws IOException {114if (currentOffset >= exceptionOffset) {115throw new IOException("Debug exception");116}117currentOffset++;118}119120121public void setExceptionOffset(int offset) {122exceptionOffset = offset;123currentOffset = 0;124}125}126127128