File: Compat2.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 (148 lines) | stat: -rw-r--r-- 4,162 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
// Tags: JDK1.2
// Uses: SerBase

/* Compat2.java -- Test for Put/GetField.

   Copyright (c) 2003 by Free Software Foundation, Inc.
   Written by Guilhem Lavaux (guilhem@kaffe.org).
   Based on a test by Pat Tullmann <pat_kaffe@tullmann.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 gnu.testlet.Testlet;
import gnu.testlet.TestHarness;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;

public class Compat2 implements Testlet
{
  static String SERIAL_REFERENCE = "serial.bin";
  static String SERIAL_SCRATCH_FILENAME = "Compat2.tmp";
  static int SERIAL_REF_ID = 0;

  private static class GetTypeMismatch // object typemismatch in get
    implements Serializable
  {
    // Explicitly set serialVersionUID for different compilers handling
    // of inner classes.
    private static final long serialVersionUID = -2330048339523627109L;

    private Integer x = new Integer(17);
    private Integer y = new Integer(27);
    
    public String toString()
    {
      return (this.getClass().getName() + ": " +x+ "," +y);
    }
    
    private void writeObject(ObjectOutputStream stream) 
      throws IOException, ClassNotFoundException
    {
      ObjectOutputStream.PutField pf1 = stream.putFields();
      pf1.put("x", this.x);
      pf1.put("y", this.y);
      stream.writeFields();
    }
    
    private void readObject(ObjectInputStream stream) 
      throws IOException, ClassNotFoundException
    {
      ObjectInputStream.GetField gf1 = stream.readFields();
      this.x = (Integer)gf1.get("x", new String("Missed X?"));
      this.y = (Integer)gf1.get("y", new String("Missed Y?"));
    }
  }
  
  void generate(String fname) throws IOException
  { 
    FileOutputStream of = new FileOutputStream (fname);
    ObjectOutputStream oos = new ObjectOutputStream (of);
  
    oos.writeObject (new GetTypeMismatch());
  }

  GetTypeMismatch readSerial(String fname) throws IOException, ClassNotFoundException
  {
    FileInputStream ifs = new FileInputStream (fname);
    ObjectInputStream ios = new ObjectInputStream (ifs);
    
    return (GetTypeMismatch)ios.readObject();
  }

  public void test(TestHarness t)
  {
    int rand_id = 0;

    t.checkPoint ("Compatibility test for type mismatch when calling get methods");

    try
      {
	generate (SERIAL_SCRATCH_FILENAME);
	t.check (true);

	try
	  {
	    readSerial (SERIAL_SCRATCH_FILENAME);
	    t.check (false);
	    t.debug ("This should have triggered IllegalArgumentException");
	  }
	catch (Exception e)
	  {
	    if (e instanceof IllegalArgumentException)
	      t.check(true);
	    else
	      {
		t.check(false);
		t.debug("Expected IllegalArgumentException, not: " + e);
	      }
	  }
      }
    catch (Exception e)
      {
	t.check (false);
	t.debug (e);
      }
    
    try
      {
	ObjectInputStream ois = new ObjectInputStream (t.getResourceStream 
				  (getClass().getName().replace ('.', '#') + "." + SERIAL_REFERENCE));

	ois.readObject();
	t.check (false);
	t.debug ("This should have triggered IllegalArgumentException");
      }
    catch (Exception e)
      {
	if (e instanceof IllegalArgumentException)
	  t.check(true);
	else
	  {
	    t.check(false);
	    t.debug("Expected IllegalArgumentException, not: " + e);
	  }
      }
  }

  static public void main(String args[]) throws IOException
  {
    new Compat2().generate (SERIAL_REFERENCE);
  }
}