File: Test.java

package info (click to toggle)
mauve 20080616-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 26,856 kB
  • ctags: 21,952
  • sloc: java: 234,107; sh: 2,834; xml: 208; makefile: 59
file content (449 lines) | stat: -rw-r--r-- 9,780 bytes parent folder | download | duplicates (5)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Tags: not-a-test

/* Test.java -- Classes used to test Object Input/Output

   Copyright (c) 1999, 2004 by Free Software Foundation, Inc.
   Written by Geoff Berry <gcb@gnu.org>, Guilhem Lavaux <guilhem@kaffe.org>.

   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 */

package gnu.testlet.java.io.ObjectInputOutput;

import java.io.*;
import java.lang.reflect.*;

public abstract class Test
{
  public static void main (String[] args) throws IOException
  {
    Test[] tests = Test.getValidTests ();
    for (int i = 0; i < tests.length; ++ i)
      writeRefData (tests[i], false);

    tests = Test.getErrorTests ();
    for (int i = 0; i < tests.length; ++ i)
      writeRefData (tests[i], true);
  }

  static void writeRefData (Test t, boolean throwsOSE) throws IOException
  {
    String file = t.getClass ().getName ();
    int idx = file.lastIndexOf ('.');
    if (idx != -1)
      file = file.substring (idx + 1);
    file += ".data";

    ObjectOutputStream oos
      = new ObjectOutputStream (new FileOutputStream (file));

    Object[] objs = t.getTestObjs ();
    for (int i = 0; i < objs.length; ++ i)
      writeData (oos, objs[i], throwsOSE);

    oos.close ();
  }
  
  static void writeData (ObjectOutputStream oos, Object obj, boolean throwsOSE)
    throws IOException
  {
    try
    {
      oos.writeObject (obj);
    }
    catch (ObjectStreamException nse)
    {
      if (!throwsOSE)
	throw nse;
    }
  }
  
  static Test[] getValidTests ()
  {
    return new Test[] {new CallDefault (),
			 new Extern (),
			 new NoCallDefault (),
			 new HairyGraph (),
			 new GetPutField (),
                         new FinalField ()};
  }

  static Test[] getErrorTests ()
  {
    return new Test[] {new NotSerial (), new BadField ()};
  }

  Test () {}
  
  abstract Object[] getTestObjs ();
  
  public String toString ()
  {
    try
    {
      Class clazz = getClass ();
      StringBuffer buf = new StringBuffer (clazz.getName ());
      buf.append (" (");
      Field[] fields = clazz.getDeclaredFields ();
      for (int i = 0; i < fields.length; ++ i)
      {
	Field f = fields[i];
	buf.append (f.getName ());
	buf.append (" = ");
	Class f_type = f.getType ();

	if (f_type == boolean.class)
	  buf.append (f.getBoolean (this));
	else if (f_type == byte.class)
	  buf.append (f.getByte (this));
	else if (f_type == char.class)
	  buf.append (f.getChar (this));
	else if (f_type == double.class)
	  buf.append (f.getDouble (this));
	else if (f_type == float.class)
	  buf.append (f.getFloat (this));
	else if (f_type == long.class)
	  buf.append (f.getLong (this));
	else if (f_type == short.class)
	  buf.append (f.getShort (this));
	else if (f_type == String.class)
	{
	  String s = (String)f.get (this);
	  if (s != null)
	    buf.append ('"');
	  buf.append (s);
	  if (s != null)
	    buf.append ('"');	  
	}
	else
	  buf.append (f.get (this));

	if (i != fields.length - 1)
	  buf.append (", ");
      }
      buf.append (')');
      return buf.toString ();
    }
    catch (IllegalAccessException iae)
    {
      return super.toString ();
    }
  }
  
  static class CallDefault extends Test implements Serializable
  {
    CallDefault () {}

    CallDefault (int X, double Y, String S)
    {
      x = X;
      y = Y;
      s = S;
    }
    
    public boolean equals (Object o)
    {
      CallDefault oo = (CallDefault)o;
      return oo.x == x
	&& oo.y == y
	&& oo.s.equals (s);
    }
    
    Object[] getTestObjs ()
    {
      return new Object[] {new CallDefault (1, 3.14, "test")};
    }

    private void writeObject (ObjectOutputStream oos) throws IOException
    {
      oos.defaultWriteObject ();
    }
    
    private void readObject (ObjectInputStream ois)
      throws ClassNotFoundException, IOException
    {
      ois.defaultReadObject ();
    }
    
    int x;
    double y;
    String s;
  }

  static class Extern extends NoCallDefault implements Externalizable
  {
    public Extern () {}
    
    Extern (int X, String S, boolean B)
    {
      super (X, S, B);
    }

    public void writeExternal (ObjectOutput oo) throws IOException
    {
      oo.writeInt (x);
      oo.writeObject (s);
      oo.writeBoolean (b);
    }
    
    public void readExternal (ObjectInput oi)
      throws ClassNotFoundException, IOException
    {
      x = oi.readInt ();
      s = (String)oi.readObject ();
      b = oi.readBoolean ();
    }
    
    public boolean equals (Object o)
    {
      Extern e = (Extern)o;
      return e.x == x
	&& e.s.equals (s)
	&& e.b == b;
    }  

    Object[] getTestObjs ()
    {
      return new Object[] {new Extern (-1, "", true)};
    }
  }
  
  static class NoCallDefault extends Test implements Serializable
  {
    NoCallDefault () {}
    
    NoCallDefault (int X, String S, boolean B)
    {
      x = X;
      s = S;
      b = B;
    }
    
    public boolean equals (Object o)
    {
      NoCallDefault oo = (NoCallDefault)o;
      return oo.x == x
	&& oo.b == b
	&& oo.s.equals (s);
    }
    
    Object[] getTestObjs ()
    {
      return new Object[] {new NoCallDefault (17, "no\ncalldefaults", false)};
    }

    private void writeObject (ObjectOutputStream oos) throws IOException
    {
      oos.writeInt (x);
      oos.writeObject (s);
      oos.writeBoolean (b);
    }
    
    private void readObject (ObjectInputStream ois)
      throws ClassNotFoundException, IOException
    {
      x = ois.readInt ();
      s = (String)ois.readObject ();
      b = ois.readBoolean ();
    }
    
    int x;
    String s;
    boolean b;    
  }
  
  static class GraphNode implements Serializable
  {
    GraphNode (String s)
    {
      this.s = s;
    }
    
    public String toString ()
    {
      return this.s;
    }
    
    String s;
    GraphNode a;
    GraphNode b;
    GraphNode c;
    GraphNode d;
  }
  
  static class HairyGraph extends Test implements Serializable
  {
    HairyGraph ()
    {
      A = new GraphNode ("A");
      B = new GraphNode ("B");
      C = new GraphNode ("C");
      D = new GraphNode ("D");
      
      A.a = B;
      A.b = C;
      A.c = D;
      A.d = A;
      
      B.a = C;
      B.b = D;
      B.c = A;
      B.d = B;
      
      C.a = D;
      C.b = A;
      C.c = B;
      C.d = C;
      
      D.a = A;
      D.b = B;
      D.c = C;
      D.d = D;
    }
    
    public boolean equals (Object o)
    {
      HairyGraph hg = (HairyGraph)o;
      
      return (A.a == B.d) && (A.a == C.c) && (A.a == D.b)
	&& (A.b == B.a) && (A.b == C.d) && (A.b == D.c)
	&& (A.c == B.b) && (A.c == C.a) && (A.c == D.d)
	&& (A.d == B.c) && (A.d == C.b) && (A.d == D.a);
    }
    
    Object[] getTestObjs ()
    {
      return new Object[] {new HairyGraph ()};
    }

    void printOneLevel (GraphNode gn)
    {
      System.out.println ("GraphNode( " + gn + ": " + gn.a + ", " + gn.b
			  + ", " + gn.c + ", " + gn.d + " )");
    }

    GraphNode A;
    GraphNode B;
    GraphNode C;
    GraphNode D;
  }

  static class GetPutField extends Test implements Serializable
  {
    Object[] getTestObjs ()
    {
      // Don't make a test with WRONG_STR_VAL or WRONG_X_VAL
      return new Object[] {new GetPutField ("test123", 123),
			   new GetPutField ("", 0),
			   new GetPutField (null, -1)};
    }

    GetPutField () {}
    
    GetPutField (String str, int x)
    {
      this.str = str;
      this.x = x;
    }
    
    public boolean equals (Object o)
    {
      if (!(o instanceof GetPutField))
	return false;

      GetPutField other = (GetPutField)o;
      
      return (other.str == str || other.str.equals (str))
	&& other.x == x;
    }

    public String toString ()
    {
      return "test(str=" + str + ", x=" + x + ")";
    }
    
    private void writeObject (ObjectOutputStream oo) throws IOException
    {
      ObjectOutputStream.PutField pf = oo.putFields ();
      pf.put ("str", str);
      pf.put ("x", x);
      oo.writeFields ();
    }

    private void readObject (ObjectInputStream oi)
      throws ClassNotFoundException, IOException
    {
      ObjectInputStream.GetField gf = oi.readFields ();
      str = (String)gf.get ("str", WRONG_STR_VAL);
      x = gf.get ("x", WRONG_X_VAL);
    }

    private static final String WRONG_STR_VAL = "wrong-o";
    private static final int WRONG_X_VAL = -17;
    private String str;
    private int x;
  }
  
  static class NotSerial extends Test
  {
    Object[] getTestObjs ()
    {
      return new Object[] {new NotSerial ()};
    }
  }

  static class BadField extends Test implements Serializable
  {
    BadField (int X, int Y, NotSerial O)
    {
      x = X;
      y = Y;
      o = O;
    }

    BadField () {}
    
    Object[] getTestObjs ()
    {
      return new Object[] {new BadField (1, 2, new NotSerial ())};
    }

    int x;
    int y;
    NotSerial o;    
  }

  static class FinalField extends Test implements Serializable
  {
    final int a;
    final String s;

    FinalField() 
    {
      s = "C";
      a = 2;
    }
    
    Object[] getTestObjs ()
    {
      return new Object[] { new FinalField () };
    }
    
    public boolean equals (Object o)
    {
      FinalField oo = (FinalField)o;
      return oo.a == a
	&& oo.s.equals (s);
    }
  }
}