summaryrefslogtreecommitdiff
path: root/test/java.io/ObjectStreamClassTest.java
blob: f43f2d9f91bfc1d87b6820e20ee5e3b016c6686a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*************************************************************************
/* ObjectStreamClassTest.java -- Tests ObjectStreamClass class
/*
/* Copyright (c) 1998 by Free Software Foundation, Inc.
/*
/* This program is free software; you can redistribute it and/or modify
/* it under the terms of the GNU General Public License as published 
/* by the Free Software Foundation, version 2. (see COPYING)
/*
/* This program is distributed in the hope that it will be useful, but
/* WITHOUT ANY WARRANTY; without even the implied warranty of
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/* GNU General Public License for more details.
/*
/* You should have received a copy of the GNU General Public License
/* along with this program; if not, write to the Free Software Foundation
/* Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
/*************************************************************************/

import java.io.Externalizable;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.Serializable;
import java.util.Hashtable;
import java.util.Vector;

public class ObjectStreamClassTest
{
  public static void pass()
  {
    System.out.print( "PASSED: " );
  }
  
  public static void fail()
  {
    System.out.print( "FAILED: " );
  }

  public static void pass( boolean exp_pass )
  {
    if( exp_pass )
      pass();
    else
      System.out.print( "XPASSED: " );
  }
  
  public static void fail( boolean exp_pass )
  {
    if( exp_pass )
      fail();
    else
      System.out.print( "XFAIL: " );
  }

  public static void testLookup( Class cl, boolean non_null )
  {
    if( non_null == (ObjectStreamClass.lookup( cl ) != null) )
      pass();
    else
      fail();

    System.out.println( "lookup() for " + cl );
  }

  public static void testGetName( Class cl, String name )
  {
    if( ObjectStreamClass.lookup( cl ).getName().equals( name ) )
      pass();
    else
      fail();
    
    System.out.println( "getName() for " + cl );
  }

  public static void testForClass( Class cl, Class clazz )
  {
    if( ObjectStreamClass.lookup( cl ).forClass() == clazz )
      pass();
    else
      fail();
    
    System.out.println( "forClass() for " + cl );
  }

  public static void testSUID( Class cl, long suid )
  {
    testSUID( cl, suid, true );
  }

  public static void testSUID( Class cl, long suid, boolean exp_pass )
  {
    if( ObjectStreamClass.lookup( cl ).getSerialVersionUID() == suid )
      pass( exp_pass );
    else
      fail( exp_pass );
    
    System.out.println( "getSerialVersionUID() for " + cl );
  }

  public static void testHasWrite( Class cl, boolean has_write )
  {
    if( ObjectStreamClass.lookup( cl ).hasWriteMethod() == has_write )
      pass();
    else
      fail();
    
    System.out.println( "hasWriteMethod() for " + cl );
  }

  public static void testIsSerial( Class cl, boolean is_serial )
  {
    if( ObjectStreamClass.lookup( cl ).isSerializable() == is_serial )
      pass();
    else
      fail();
    
    System.out.println( "isSerializable() for " + cl );
  }

  public static void testIsExtern( Class cl, boolean is_extern )
  {
    if( ObjectStreamClass.lookup( cl ).isExternalizable() == is_extern )
      pass();
    else
      fail();
    
    System.out.println( "isExternalizable() for " + cl );
  }

  public static void main( String[] args )
  {
    try
    {
      // lookup
      testLookup( Serial.class, true );
      testLookup( NotSerial.class, false );

      // getName
      testGetName( java.lang.String.class, "java.lang.String" );
      testGetName( java.util.Hashtable.class, "java.util.Hashtable" );
      
      // forClass
      testForClass( java.lang.String.class, java.lang.String.class );
      testForClass( java.util.Vector.class, (new Vector()).getClass() );      
      
      // getSerialVersionUID
      testSUID( A.class, 1577839372146469075L );
      testSUID( B.class, -7069956958769787679L );

      // NOTE: this fails for JDK 1.1.5v5 on linux because a non-null
      // jmethodID is returned from
      // GetStaticMethodID( env, C, "<clinit>", "()V" )
      // even though class C does not have a class initializer.
      // The JDK's serialver tool does not have this problem somehow.
      // I have not tested this on other platforms.
      testSUID( C.class, 7441756018870420732L, false );

      testSUID( Defined.class, 17 );
      testSUID( DefinedNotStatic.class, 8797806279193632512L );
      testSUID( DefinedNotFinal.class, -1014973327673071657L );

      // hasWriteMethod
      testHasWrite( Serial.class, false );
      testHasWrite( HasWrite.class, true );
      testHasWrite( InherWrite.class, false );
      testHasWrite( PubWrite.class, false );
      testHasWrite( StaticWrite.class, false );
      testHasWrite( ReturnWrite.class, false );

      // isSerializable
      testIsSerial( Serial.class, true );
      testIsSerial( Extern.class, false );
      testIsSerial( InherSerial.class, true );
      testIsSerial( InherExtern.class, false );

      // isExternalizable
      testIsExtern( Serial.class, false );
      testIsExtern( Extern.class, true );
      testIsExtern( InherSerial.class, false );
      testIsExtern( InherExtern.class, true );
    }
    catch( Exception e )
    {
      e.printStackTrace();
    }
  }
}

class NotSerial {}

class A implements Serializable
{
  int b;
  int a;

  public int f() { return 0; }
  float g() { return 3; }

  private float c;
}
  
abstract class B extends A
{
  private B( int[] ar ) {}
  public B() {}
  public static void foo() {}
  public abstract void absfoo();
  
  private static String s;
  public int[] a;

  static
  {
    s = "hello";
  }
}

class C extends B implements Cloneable, Externalizable
{
  public void absfoo() {}
  public void readExternal( ObjectInput i ) {}
  public void writeExternal( ObjectOutput o ) {}
}


class Defined implements Serializable
{
  static final long serialVersionUID = 17;
}

class DefinedNotStatic implements Serializable
{
  final long serialVersionUID = 17;
}

class DefinedNotFinal implements Serializable
{
  static long serialVersionUID = 17;
}

class HasWrite implements Serializable
{
  private void writeObject( ObjectOutputStream o ) {}
}

class InherWrite extends HasWrite {}

class PubWrite implements Serializable
{
  public void writeObject( ObjectOutputStream o ) {}
}

class StaticWrite implements Serializable
{
  private static void writeObject( ObjectOutputStream o ) {}
}

class ReturnWrite implements Serializable
{
  private int writeObject( ObjectOutputStream o ) 
  {
    return -1;
  }
}


class Serial implements Serializable {}

class Extern implements Externalizable
{
  public void readExternal( ObjectInput i )
  {}

  public void writeExternal( ObjectOutput o )
  {}
}

class InherExtern extends Extern implements Serializable {}

class InherSerial extends Serial {}