1 /*************************************************************************
2 /* ObjectOutputStreamTest.java -- Tests ObjectOutputStream class
4 /* Copyright (c) 1998 by Free Software Foundation, Inc.
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)
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
)
40 ObjectOutputStream oos
=
41 new ObjectOutputStream( new FileOutputStream( filename
) );
42 oos
.writeObject( obj
);
45 catch( ObjectStreamException e
)
47 catch( IOException ioe
)
49 ioe
.printStackTrace();
54 ByteArrayOutputStream bytes
= new ByteArrayOutputStream();
58 ObjectOutputStream oos
= new ObjectOutputStream( bytes
);
59 oos
.writeObject( obj
);
62 catch( ObjectStreamException e
)
64 catch( IOException ioe
)
70 byte[] jcl_bytes
= bytes
.toByteArray();
73 FileInputStream jdk_file
;
76 jdk_file
= new FileInputStream( filename
);
78 for( int i
=0; i
< jcl_bytes
.length
; i
++ )
80 data
= jdk_file
.read();
88 if( (byte)data
!= jcl_bytes
[i
] )
95 if( jdk_file
.read() != -1 )
101 catch( IOException e
)
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
151 OOSBadField( int X
, int Y
, OOSNotSerial O
)