OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [classpath/] [test/] [java.io/] [ObjectOutputStreamTest.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/*************************************************************************
2
/* ObjectOutputStreamTest.java -- Tests ObjectOutputStream class
3
/*
4
/* Copyright (c) 1998 by Free Software Foundation, Inc.
5
/*
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)
9
/*
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.
14
/*
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
/*************************************************************************/
19
 
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;
31
 
32
public class ObjectOutputStreamTest extends Test
33
{
34
  public static void testSerial( Object obj, String filename )
35
  {
36
    if( writeMode )
37
    {
38
      try
39
      {
40
        ObjectOutputStream oos =
41
          new ObjectOutputStream( new FileOutputStream( filename ) );
42
        oos.writeObject( obj );
43
        oos.close();
44
      }
45
      catch( ObjectStreamException e )
46
      {}
47
      catch( IOException ioe )
48
      {
49
        ioe.printStackTrace();
50
      }
51
    }
52
    else
53
    {
54
      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
55
 
56
      try
57
      {
58
        ObjectOutputStream oos = new ObjectOutputStream( bytes );
59
        oos.writeObject( obj );
60
        oos.close();
61
      }
62
      catch( ObjectStreamException e )
63
      {}
64
      catch( IOException ioe )
65
      {
66
        fail();
67
        return;
68
      }
69
 
70
      byte[] jcl_bytes = bytes.toByteArray();
71
      int data;
72
 
73
      FileInputStream jdk_file;
74
      try
75
      {
76
        jdk_file = new FileInputStream( filename );
77
 
78
        for( int i=0; i < jcl_bytes.length; i++ )
79
        {
80
          data = jdk_file.read();
81
 
82
          if( data == -1 )
83
          {
84
            fail();
85
            return;
86
          }
87
 
88
          if( (byte)data != jcl_bytes[i] )
89
          {
90
            fail();
91
            return;
92
          }
93
        }
94
 
95
        if( jdk_file.read() != -1 )
96
        {
97
          fail();
98
          return;
99
        }
100
      }
101
      catch( IOException e )
102
      {
103
        error();
104
        return;
105
      }
106
 
107
      pass();
108
    }
109
  }
110
 
111
 
112
  public static void main( String[] args )
113
  {
114
    writeMode = (args.length != 0);
115
 
116
    testSerial( new OOSNotSerial(), "notserial.data" );
117
    System.out.println( "Non-serializable class" );
118
 
119
    testSerial( new OOSBadField( 1, 2, new OOSNotSerial() ),
120
                "notserialfield.data" );
121
    System.out.println( "Object with non-serializable field" );
122
 
123
    testSerial( new OOSCallDefault( 1, 3.14, "test" ),
124
                "calldefault.data" );
125
    System.out.println( "Object calling defaultWriteObject()" );
126
 
127
    testSerial( new OOSNoCallDefault( 17, "no\ndefault", false ),
128
                "nocalldefault.data" );
129
    System.out.println( "Object not calling defaultWriteObject()" );
130
 
131
    testSerial( new OOSExtern( -1, "", true ), "external.data" );
132
    System.out.println( "Externalizable class" );
133
 
134
    testSerial( new HairyGraph(), "graph.data" );
135
    System.out.println( "Graph of objects with circular references" );
136
  }
137
 
138
 
139
  public static boolean writeMode;
140
}
141
 
142
 
143
class OOSNotSerial {}
144
 
145
class OOSBadField implements Serializable
146
{
147
  int x;
148
  int y;
149
  OOSNotSerial o;
150
 
151
  OOSBadField( int X, int Y, OOSNotSerial O )
152
  {
153
    x = X;
154
    y = Y;
155
    o = O;
156
  }
157
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.