1 /*************************************************************************
2 /* ObjectStreamClassTest.java -- Tests ObjectStreamClass 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
.Externalizable
;
21 import java
.io
.ObjectInput
;
22 import java
.io
.ObjectOutput
;
23 import java
.io
.ObjectOutputStream
;
24 import java
.io
.ObjectStreamClass
;
25 import java
.io
.Serializable
;
26 import java
.util
.Hashtable
;
27 import java
.util
.Vector
;
29 public class ObjectStreamClassTest
31 public static void pass()
33 System
.out
.print( "PASSED: " );
36 public static void fail()
38 System
.out
.print( "FAILED: " );
41 public static void pass( boolean exp_pass
)
46 System
.out
.print( "XPASSED: " );
49 public static void fail( boolean exp_pass
)
54 System
.out
.print( "XFAIL: " );
57 public static void testLookup( Class cl
, boolean non_null
)
59 if( non_null
== (ObjectStreamClass
.lookup( cl
) != null) )
64 System
.out
.println( "lookup() for " + cl
);
67 public static void testGetName( Class cl
, String name
)
69 if( ObjectStreamClass
.lookup( cl
).getName().equals( name
) )
74 System
.out
.println( "getName() for " + cl
);
77 public static void testForClass( Class cl
, Class clazz
)
79 if( ObjectStreamClass
.lookup( cl
).forClass() == clazz
)
84 System
.out
.println( "forClass() for " + cl
);
87 public static void testSUID( Class cl
, long suid
)
89 testSUID( cl
, suid
, true );
92 public static void testSUID( Class cl
, long suid
, boolean exp_pass
)
94 if( ObjectStreamClass
.lookup( cl
).getSerialVersionUID() == suid
)
99 System
.out
.println( "getSerialVersionUID() for " + cl
);
102 public static void testHasWrite( Class cl
, boolean has_write
)
104 if( ObjectStreamClass
.lookup( cl
).hasWriteMethod() == has_write
)
109 System
.out
.println( "hasWriteMethod() for " + cl
);
112 public static void testIsSerial( Class cl
, boolean is_serial
)
114 if( ObjectStreamClass
.lookup( cl
).isSerializable() == is_serial
)
119 System
.out
.println( "isSerializable() for " + cl
);
122 public static void testIsExtern( Class cl
, boolean is_extern
)
124 if( ObjectStreamClass
.lookup( cl
).isExternalizable() == is_extern
)
129 System
.out
.println( "isExternalizable() for " + cl
);
132 public static void main( String
[] args
)
137 testLookup( Serial
.class, true );
138 testLookup( NotSerial
.class, false );
141 testGetName( java
.lang
.String
.class, "java.lang.String" );
142 testGetName( java
.util
.Hashtable
.class, "java.util.Hashtable" );
145 testForClass( java
.lang
.String
.class, java
.lang
.String
.class );
146 testForClass( java
.util
.Vector
.class, (new Vector()).getClass() );
148 // getSerialVersionUID
149 testSUID( A
.class, 1577839372146469075L );
150 testSUID( B
.class, -7069956958769787679L );
152 // NOTE: this fails for JDK 1.1.5v5 on linux because a non-null
153 // jmethodID is returned from
154 // GetStaticMethodID( env, C, "<clinit>", "()V" )
155 // even though class C does not have a class initializer.
156 // The JDK's serialver tool does not have this problem somehow.
157 // I have not tested this on other platforms.
158 testSUID( C
.class, 7441756018870420732L, false );
160 testSUID( Defined
.class, 17 );
161 testSUID( DefinedNotStatic
.class, 8797806279193632512L );
162 testSUID( DefinedNotFinal
.class, -1014973327673071657L );
165 testHasWrite( Serial
.class, false );
166 testHasWrite( HasWrite
.class, true );
167 testHasWrite( InherWrite
.class, false );
168 testHasWrite( PubWrite
.class, false );
169 testHasWrite( StaticWrite
.class, false );
170 testHasWrite( ReturnWrite
.class, false );
173 testIsSerial( Serial
.class, true );
174 testIsSerial( Extern
.class, false );
175 testIsSerial( InherSerial
.class, true );
176 testIsSerial( InherExtern
.class, false );
179 testIsExtern( Serial
.class, false );
180 testIsExtern( Extern
.class, true );
181 testIsExtern( InherSerial
.class, false );
182 testIsExtern( InherExtern
.class, true );
193 class A
implements Serializable
198 public int f() { return 0; }
199 float g() { return 3; }
204 abstract class B
extends A
206 private B( int[] ar
) {}
208 public static void foo() {}
209 public abstract void absfoo();
211 private static String s
;
220 class C
extends B
implements Cloneable
, Externalizable
222 public void absfoo() {}
223 public void readExternal( ObjectInput i
) {}
224 public void writeExternal( ObjectOutput o
) {}
228 class Defined
implements Serializable
230 static final long serialVersionUID
= 17;
233 class DefinedNotStatic
implements Serializable
235 final long serialVersionUID
= 17;
238 class DefinedNotFinal
implements Serializable
240 static long serialVersionUID
= 17;
243 class HasWrite
implements Serializable
245 private void writeObject( ObjectOutputStream o
) {}
248 class InherWrite
extends HasWrite
{}
250 class PubWrite
implements Serializable
252 public void writeObject( ObjectOutputStream o
) {}
255 class StaticWrite
implements Serializable
257 private static void writeObject( ObjectOutputStream o
) {}
260 class ReturnWrite
implements Serializable
262 private int writeObject( ObjectOutputStream o
)
269 class Serial
implements Serializable
{}
271 class Extern
implements Externalizable
273 public void readExternal( ObjectInput i
)
276 public void writeExternal( ObjectOutput o
)
280 class InherExtern
extends Extern
implements Serializable
{}
282 class InherSerial
extends Serial
{}