tests/libjava-mauve/src/gnu/testlet/java/util/zip/ZipInputStream/basic.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // Tags: JDK1.1
       
     2 
       
     3 // Copyright (C) 1999 Cygnus Solutions
       
     4 
       
     5 // This file is part of Mauve.
       
     6 
       
     7 // Mauve is free software; you can redistribute it and/or modify
       
     8 // it under the terms of the GNU General Public License as published by
       
     9 // the Free Software Foundation; either version 2, or (at your option)
       
    10 // any later version.
       
    11 
       
    12 // Mauve is distributed in the hope that it will be useful,
       
    13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    15 // GNU General Public License for more details.
       
    16 
       
    17 // You should have received a copy of the GNU General Public License
       
    18 // along with Mauve; see the file COPYING.  If not, write to
       
    19 // the Free Software Foundation, 59 Temple Place - Suite 330,
       
    20 // Boston, MA 02111-1307, USA.  */
       
    21 
       
    22 package gnu.testlet.java.util.zip.ZipInputStream;
       
    23 
       
    24 import gnu.testlet.Testlet;
       
    25 import gnu.testlet.TestHarness;
       
    26 import java.util.zip.*;
       
    27 import java.io.*;
       
    28 
       
    29 public class basic implements Testlet
       
    30 {
       
    31   private String readall (InputStream in)
       
    32   {
       
    33     StringBuffer sb = new StringBuffer ();
       
    34     byte[] buf = new byte[512];
       
    35     int n;
       
    36     try
       
    37       {
       
    38 	while ((n = in.read(buf)) > 0)
       
    39 	  sb.append(new String (buf, 0, n, "8859_1"));
       
    40       }
       
    41     catch (IOException _)
       
    42       {
       
    43       }
       
    44     return sb.toString ();
       
    45   }
       
    46 
       
    47   public String read_a_file (ZipInputStream zis)
       
    48   {
       
    49     try
       
    50       {
       
    51 	ZipEntry ze = zis.getNextEntry();
       
    52 	if (ze == null)
       
    53 	  return "done";
       
    54 	return readall (zis);
       
    55       }
       
    56     catch (IOException _)
       
    57       {
       
    58 	return "";
       
    59       }
       
    60   }
       
    61 
       
    62   public void read_contents (TestHarness harness, ZipInputStream zis)
       
    63   {
       
    64     String s = read_a_file (zis);
       
    65     harness.check (s, "Contents of file 1\n");
       
    66     s = read_a_file (zis);
       
    67     harness.check (s, "Contents of file 2\n");
       
    68     s = read_a_file (zis);
       
    69     harness.check (s, "done");
       
    70   }
       
    71 
       
    72     public void read_from_end(TestHarness harness, ZipInputStream zis)
       
    73     {
       
    74 	try {
       
    75 	ZipEntry ze = zis.getNextEntry();
       
    76 	ze = zis.getNextEntry();
       
    77 	byte[] b = new byte["Contents of file 2\n".length()];
       
    78 	int count = zis.read(b, 0, b.length);
       
    79 	harness.check (new String(b), "Contents of file 2\n");
       
    80 	harness.check(count, b.length);
       
    81 
       
    82 	//read 0 bytes
       
    83 	count = zis.read(b, 0, 0);
       
    84 	harness.check(count, 0);
       
    85 
       
    86 	//read 1 byte past the end
       
    87 	count = zis.read(b,0,1);
       
    88 	harness.check(count, -1);
       
    89 	zis.close();
       
    90 	} catch(IOException e)
       
    91 	    {
       
    92 		harness.check(false, "failed all read_from_end tests");
       
    93 	    }
       
    94     }
       
    95 
       
    96   public void test (TestHarness harness)
       
    97   {
       
    98     harness.checkPoint ("reading zip file");
       
    99     try
       
   100       {
       
   101 	read_contents (harness,
       
   102 		       new ZipInputStream (harness.getResourceStream ("gnu#testlet#java#util#zip#ZipInputStream#reference.zip")));
       
   103       }
       
   104     catch (gnu.testlet.ResourceNotFoundException _)
       
   105       {
       
   106 	  // FIXME: all tests should fail.
       
   107 	  harness.check(false, "all basic tests failed");
       
   108       }
       
   109 
       
   110     harness.checkPoint ("checking 0 byte read");
       
   111 
       
   112     try
       
   113       {
       
   114 	read_from_end (harness,
       
   115 		       new ZipInputStream (harness.getResourceStream ("gnu#testlet#java#util#zip#ZipInputStream#reference.zip")));
       
   116       }
       
   117     catch (gnu.testlet.ResourceNotFoundException _)
       
   118       {
       
   119 	  // FIXME: all tests should fail.
       
   120 	  harness.check(false, "all read tests failed");
       
   121       }
       
   122 
       
   123     harness.checkPoint ("writing and re-reading");
       
   124     
       
   125   }
       
   126 }