Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / test / java.io / ObjectOutputStreamTest.java
blob62f60dce1e08b2ea4e3f6cbfdf04ff76de50cbe1
1 /*************************************************************************
2 /* ObjectOutputStreamTest.java -- Tests ObjectOutputStream class
3 /*
4 /* Copyright (c) 1998 by Free Software Foundation, Inc.
5 /*
6 /* This program is free software; you can redistribute it and/or modify
7 /* it under the terms of the GNU General Public License as published
8 /* by the Free Software Foundation, version 2. (see COPYING)
9 /*
10 /* This program is distributed in the hope that it will be useful, but
11 /* WITHOUT ANY WARRANTY; without even the implied warranty of
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 /* GNU General Public License for more details.
15 /* You should have received a copy of the GNU General Public License
16 /* along with this program; if not, write to the Free Software Foundation
17 /* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 /*************************************************************************/
20 import java.io.ByteArrayOutputStream;
21 import java.io.Externalizable;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.ObjectInput;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutput;
28 import java.io.ObjectOutputStream;
29 import java.io.ObjectStreamException;
30 import java.io.Serializable;
32 public class ObjectOutputStreamTest extends Test
34 public static void testSerial( Object obj, String filename )
36 if( writeMode )
38 try
40 ObjectOutputStream oos =
41 new ObjectOutputStream( new FileOutputStream( filename ) );
42 oos.writeObject( obj );
43 oos.close();
45 catch( ObjectStreamException e )
47 catch( IOException ioe )
49 ioe.printStackTrace();
52 else
54 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
56 try
58 ObjectOutputStream oos = new ObjectOutputStream( bytes );
59 oos.writeObject( obj );
60 oos.close();
62 catch( ObjectStreamException e )
64 catch( IOException ioe )
66 fail();
67 return;
70 byte[] jcl_bytes = bytes.toByteArray();
71 int data;
73 FileInputStream jdk_file;
74 try
76 jdk_file = new FileInputStream( filename );
78 for( int i=0; i < jcl_bytes.length; i++ )
80 data = jdk_file.read();
82 if( data == -1 )
84 fail();
85 return;
88 if( (byte)data != jcl_bytes[i] )
90 fail();
91 return;
95 if( jdk_file.read() != -1 )
97 fail();
98 return;
101 catch( IOException e )
103 error();
104 return;
107 pass();
112 public static void main( String[] args )
114 writeMode = (args.length != 0);
116 testSerial( new OOSNotSerial(), "notserial.data" );
117 System.out.println( "Non-serializable class" );
119 testSerial( new OOSBadField( 1, 2, new OOSNotSerial() ),
120 "notserialfield.data" );
121 System.out.println( "Object with non-serializable field" );
123 testSerial( new OOSCallDefault( 1, 3.14, "test" ),
124 "calldefault.data" );
125 System.out.println( "Object calling defaultWriteObject()" );
127 testSerial( new OOSNoCallDefault( 17, "no\ndefault", false ),
128 "nocalldefault.data" );
129 System.out.println( "Object not calling defaultWriteObject()" );
131 testSerial( new OOSExtern( -1, "", true ), "external.data" );
132 System.out.println( "Externalizable class" );
134 testSerial( new HairyGraph(), "graph.data" );
135 System.out.println( "Graph of objects with circular references" );
139 public static boolean writeMode;
143 class OOSNotSerial {}
145 class OOSBadField implements Serializable
147 int x;
148 int y;
149 OOSNotSerial o;
151 OOSBadField( int X, int Y, OOSNotSerial O )
153 x = X;
154 y = Y;
155 o = O;