1 /*
2  * Copyright (c) 2005, 2006, 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 
24 /* @test
25  * @summary it is a new version of an old test which was
26  *          /src/share/test/serialization/piotest.java
27  *          Test of serialization when there is
28  *          exceptions on the I/O stream
29  *
30  * @build PrimitivesTest
31  * @run main SerializeWithException
32  */
33 
34 import java.io.*;
35 
36 public class SerializeWithException {
main(String argv[])37    public static void main (String argv[]) {
38        System.err.println("\nRegression test for testing of " +
39             "serialization when there is exceptions on the I/O stream \n");
40 
41        try {
42            int i = 123456;
43            byte b = 12;
44            short s = 45;
45            char c = 'A';
46            long l = 1234567890000L;
47            float f = 3.14159f;
48            double d = f*2;
49            boolean z = true;
50            String string = "The String";
51            PrimitivesTest prim = new PrimitivesTest();
52 
53            /* For each of the byte offsets from 0 to 100,
54               do the pickling but expect an exception */
55            for (int offset = 0; offset < 200; offset++) {
56                ExceptionOutputStream ostream;
57                boolean expect_exception = false;
58                IOException exception = null;
59 
60                try {
61                    expect_exception = true;
62                    exception = null;
63 
64                    ostream = new ExceptionOutputStream();
65                    ostream.setExceptionOffset(offset);
66                    ObjectOutputStream p = new ObjectOutputStream(ostream);
67 
68                    p.writeInt(i);
69                    p.writeByte(b);
70                    p.writeShort(s);
71                    p.writeChar(c);
72                    p.writeLong(l);
73                    p.writeFloat(f);
74                    p.writeDouble(d);
75                    p.writeBoolean(z);
76                    p.writeUTF(string);
77                    p.writeObject(string);
78 
79                    p.writeObject(prim);
80                    p.flush();
81                    expect_exception = false;
82                } catch (IOException ee) {
83                    exception = ee;
84                }
85 
86                if (expect_exception && exception == null) {
87                    System.err.println("\nIOException did not occur at " +
88                         "offset " + offset);
89                    throw new Error();
90                }
91                if (!expect_exception && exception != null) {
92                    System.err.println("\n " + exception.toString() +
93                        " not expected at offset " + offset);
94                    throw new Error();
95                }
96            }
97            System.err.println("\nTEST PASSED");
98        } catch (Exception e) {
99            System.err.print("TEST FAILED: ");
100            e.printStackTrace();
101            throw new Error();
102        }
103     }
104 }
105 
106 class ExceptionOutputStream extends OutputStream {
107     private int exceptionOffset = 0;
108     private int currentOffset = 0;
109 
110     /**
111      * Writes a byte to the buffer.
112      * @param b the byte
113      */
write(int b)114     public void write(int b) throws IOException {
115         if (currentOffset >= exceptionOffset) {
116             throw new IOException("Debug exception");
117         }
118         currentOffset++;
119     }
120 
121 
setExceptionOffset(int offset)122     public void setExceptionOffset(int offset) {
123         exceptionOffset = offset;
124         currentOffset = 0;
125     }
126 }
127