tests/libjava-mauve/src/gnu/testlet/java/io/PrintStream/encodings.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // Copyright (c) 2005  Red Hat, Inc.
       
     2 // Written by Ito Kazumitsu <kaz@maczuka.gcd.org>
       
     3 
       
     4 // This file is part of Mauve.
       
     5 
       
     6 // Mauve is free software; you can redistribute it and/or modify
       
     7 // it under the terms of the GNU General Public License as published by
       
     8 // the Free Software Foundation; either version 2, or (at your option)
       
     9 // any later version.
       
    10 
       
    11 // Mauve is distributed in the hope that it will be useful,
       
    12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14 // GNU General Public License for more details.
       
    15 
       
    16 // You should have received a copy of the GNU General Public License
       
    17 // along with Mauve; see the file COPYING.  If not, write to
       
    18 // the Free Software Foundation, 59 Temple Place - Suite 330,
       
    19 // Boston, MA 02111-1307, USA.
       
    20 
       
    21 // Tags: JDK1.1
       
    22 
       
    23 package gnu.testlet.java.io.PrintStream;
       
    24 
       
    25 import gnu.testlet.Testlet;
       
    26 import gnu.testlet.TestHarness;
       
    27 
       
    28 import java.io.*;
       
    29 
       
    30 public class encodings implements Testlet
       
    31 {
       
    32 
       
    33   private void test1 (TestHarness harness, String encoding,
       
    34 		      String input, byte[] expected)
       
    35   {
       
    36     byte[] output = null;
       
    37     try
       
    38       {
       
    39 	ByteArrayOutputStream b = new ByteArrayOutputStream ();
       
    40 	PrintStream ps = null;
       
    41 	if (encoding == null)
       
    42 	  {
       
    43 	    ps = new PrintStream(b, false);
       
    44 	  }
       
    45 	else
       
    46 	  {
       
    47 	    ps = new PrintStream(b, false, encoding);
       
    48 	  }
       
    49 	ps.print(input);
       
    50 	ps.flush();
       
    51 	output = b.toByteArray();
       
    52       }
       
    53     catch (UnsupportedEncodingException e)
       
    54       {
       
    55       }
       
    56     if (output == null && expected == null)
       
    57       {
       
    58 	harness.check(true);
       
    59 	return;
       
    60       }
       
    61     boolean result = (output != null && output.length == expected.length);
       
    62     if (result)
       
    63       {
       
    64 	for (int i = 0; i < output.length; i++)
       
    65 	  {
       
    66 	    if (output[i] != expected[i])
       
    67 	      {
       
    68 	        result = false;
       
    69 	        break;
       
    70 	      }
       
    71 	  }
       
    72       }
       
    73     harness.check(result);
       
    74   }
       
    75 
       
    76   public void test (TestHarness harness)
       
    77   {
       
    78     String input = "abc";
       
    79     byte[] expected = new byte[] {(byte)'a', (byte)'b', (byte)'c'};
       
    80 
       
    81     test1 (harness, "ISO-8859-1", input, expected);
       
    82     test1 (harness, "??UNSUPPORTED??", input, null);
       
    83    /*
       
    84       The result of setting the system property "file.encoding"
       
    85       is uncertain.
       
    86     String saved_encoding = System.getProperty("file.encoding");
       
    87     System.setProperty ("file.encoding", "ISO-8859-1");
       
    88     test1 (harness, null, input, expected);
       
    89     System.setProperty ("file.encoding", "??UNSUPPORTED??");
       
    90     test1 (harness, null, input, expected);
       
    91     System.setProperty("file.encoding", saved_encoding);
       
    92     */
       
    93   }
       
    94 
       
    95 }