1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.harmony.archive.tests.java.util.zip; 18 19 import java.io.File; 20 import java.io.FileInputStream; 21 import java.io.FileOutputStream; 22 import java.io.InputStream; 23 import java.io.IOException; 24 import java.util.zip.CRC32; 25 import java.util.zip.CheckedInputStream; 26 27 import junit.framework.TestCase; 28 import tests.support.resource.Support_Resources; 29 30 public class CheckedInputStreamTest extends TestCase { 31 32 @Override 33 protected void tearDown() { 34 try { 35 File deletedFile = new File("empty.txt"); 36 deletedFile.delete(); 37 } catch (SecurityException e) { 38 fail("Cannot delete file for security reasons"); 39 } 40 41 } 42 43 /** 44 * @tests java.util.zip.CheckedInputStream#CheckedInputStream(java.io.InputStream, 45 * java.util.zip.Checksum) 46 */ 47 public void test_ConstructorLjava_io_InputStreamLjava_util_zip_Checksum() throws Exception { 48 InputStream checkInput = Support_Resources.getStream("hyts_checkInput.txt"); 49 CheckedInputStream checkIn = new CheckedInputStream(checkInput, new CRC32()); 50 assertEquals("constructor of checkedInputStream has failed", 0, checkIn.getChecksum() 51 .getValue()); 52 checkInput.close(); 53 } 54 55 /** 56 * @tests java.util.zip.CheckedInputStream#getChecksum() 57 */ 58 public void test_getChecksum() throws Exception { 59 byte outBuf[] = new byte[100]; 60 // testing getChecksum for an empty file 61 FileOutputStream outEmp = new FileOutputStream("empty.txt"); 62 outEmp.close(); 63 InputStream inEmp = new FileInputStream("empty.txt"); 64 CheckedInputStream checkEmpty = new CheckedInputStream(inEmp, new CRC32()); 65 while (checkEmpty.read() >= 0) { 66 } 67 assertEquals("the checkSum value of an empty file is not zero", 0, checkEmpty 68 .getChecksum().getValue()); 69 inEmp.close(); 70 71 // testing getChecksum for the file checkInput 72 InputStream checkInput = Support_Resources.getStream("hyts_checkInput.txt"); 73 CheckedInputStream checkIn = new CheckedInputStream(checkInput, new CRC32()); 74 while (checkIn.read() >= 0) { 75 } 76 // ran JDK and found that the checkSum value of this is 2036203193 77 // System.out.print(" " + checkIn.getChecksum().getValue()); 78 assertEquals("the checksum value is incorrect", 2036203193, checkIn.getChecksum() 79 .getValue()); 80 checkInput.close(); 81 // testing getChecksum for file checkInput 82 checkInput = Support_Resources.getStream("hyts_checkInput.txt"); 83 CheckedInputStream checkIn2 = new CheckedInputStream(checkInput, new CRC32()); 84 checkIn2.read(outBuf, 0, 10); 85 // ran JDK and found that the checkSum value of this is 2235765342 86 // System.out.print(" " + checkIn2.getChecksum().getValue()); 87 assertEquals("the checksum value is incorrect", 2235765342L, checkIn2.getChecksum() 88 .getValue()); 89 checkInput.close(); 90 } 91 92 /** 93 * @tests java.util.zip.CheckedInputStream#skip(long) 94 */ 95 public void test_skipJ() throws Exception { 96 // testing that the return by skip is valid 97 InputStream checkInput = Support_Resources.getStream("hyts_checkInput.txt"); 98 CheckedInputStream checkIn = new CheckedInputStream(checkInput, new CRC32()); 99 long skipValue = 5; 100 assertEquals("the value returned by skip(n) is not the same as its parameter", 101 skipValue, checkIn.skip(skipValue)); 102 checkIn.skip(skipValue); 103 // ran JDK and found the checkSum value is 2235765342 104 // System.out.print(checkIn.getChecksum().getValue()); 105 assertEquals("checkSum value is not correct", 2235765342L, checkIn.getChecksum() 106 .getValue()); 107 assertEquals(0, checkIn.skip(0)); 108 checkInput.close(); 109 } 110 111 public void test_read() throws Exception { 112 // testing that the return by skip is valid 113 InputStream checkInput = Support_Resources 114 .getStream("hyts_checkInput.txt"); 115 CheckedInputStream checkIn = new CheckedInputStream(checkInput, 116 new CRC32()); 117 checkIn.read(); 118 checkIn.close(); 119 try { 120 checkIn.read(); 121 fail("IOException expected."); 122 } catch (IOException ee) { 123 // expected 124 } 125 checkInput.close(); 126 } 127 128 public void test_read$byteII() throws Exception { 129 // testing that the return by skip is valid 130 InputStream checkInput = Support_Resources 131 .getStream("hyts_checkInput.txt"); 132 CheckedInputStream checkIn = new CheckedInputStream(checkInput, 133 new CRC32()); 134 byte buff[] = new byte[50]; 135 checkIn.read(buff, 10, 5); 136 checkIn.close(); 137 try { 138 checkIn.read(buff, 10, 5); 139 fail("IOException expected."); 140 } catch (IOException ee) { 141 // expected 142 } 143 checkInput.close(); 144 } 145 } 146