tests/libjava-mauve/src/gnu/testlet/java/io/ObjectOutputStream/useProtocolVersion.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 //Tags: JDK1.2
       
     2 
       
     3 //Copyright (C) 2006 Free Software Foundation, Inc.
       
     4 //Written by Wolfgang Baer (WBaer@gmx.de)
       
     5 
       
     6 //This file is part of Mauve.
       
     7 
       
     8 //Mauve is free software; you can redistribute it and/or modify
       
     9 //it under the terms of the GNU General Public License as published by
       
    10 //the Free Software Foundation; either version 2, or (at your option)
       
    11 //any later version.
       
    12 
       
    13 //Mauve is distributed in the hope that it will be useful,
       
    14 //but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    16 //GNU General Public License for more details.
       
    17 
       
    18 //You should have received a copy of the GNU General Public License
       
    19 //along with Mauve; see the file COPYING.  If not, write to
       
    20 //the Free Software Foundation, 51 Franklin Street, Fifth Floor,
       
    21 //Boston, MA, 02110-1301 USA.
       
    22 
       
    23 package gnu.testlet.java.io.ObjectOutputStream;
       
    24 
       
    25 import java.io.ByteArrayOutputStream;
       
    26 import java.io.IOException;
       
    27 import java.io.ObjectOutputStream;
       
    28 import java.io.ObjectStreamConstants;
       
    29 
       
    30 import gnu.testlet.TestHarness;
       
    31 import gnu.testlet.Testlet;
       
    32 
       
    33 /**
       
    34  * Tests the correct behaviour of useProtocolVersion
       
    35  */
       
    36 public class useProtocolVersion implements Testlet
       
    37 {
       
    38   public void test(TestHarness harness)
       
    39   {
       
    40     try
       
    41       {
       
    42         String toSerialize = "Hello";
       
    43 
       
    44         ByteArrayOutputStream bytes = new ByteArrayOutputStream();
       
    45         ObjectOutputStream out = new ObjectOutputStream(bytes);
       
    46 
       
    47         try
       
    48           {
       
    49             // setting must be allowed
       
    50             out.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_1);
       
    51             harness.check(true);
       
    52           }
       
    53         catch (RuntimeException e)
       
    54           {
       
    55             harness.check(false);
       
    56           }
       
    57 
       
    58         // if only normal data is written
       
    59         // a subsequent call to useProtocolVersion must succeed also
       
    60         out.writeInt(4);
       
    61 
       
    62         try
       
    63           {
       
    64             out.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_1);
       
    65             harness.check(true);
       
    66           }
       
    67         catch (IllegalStateException e)
       
    68           {
       
    69             harness.check(false);
       
    70           }
       
    71 
       
    72         // as soon as the first object is serialized
       
    73         // subsequent calls must throw an exception
       
    74         out.writeObject(toSerialize);
       
    75 
       
    76         try
       
    77           {
       
    78             out.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_1);
       
    79             harness.check(false);
       
    80           }
       
    81         catch (IllegalStateException e)
       
    82           {
       
    83             harness.check(true);
       
    84           }
       
    85 
       
    86         // use new stream
       
    87         out = new ObjectOutputStream(bytes);
       
    88 
       
    89         // wrong versions must throw IllegalArgumentException
       
    90         try
       
    91           {
       
    92             out.useProtocolVersion(4);
       
    93             harness.check(false);
       
    94           }
       
    95         catch (IllegalArgumentException e)
       
    96           {
       
    97             harness.check(true);
       
    98           }
       
    99       }
       
   100     catch (IOException e)
       
   101       {
       
   102         harness.fail("Unexpected exception occured.");
       
   103         harness.debug(e);
       
   104       }
       
   105   }
       
   106 }