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.tests.java.util.zip; 18 19 import java.io.File; 20 import java.io.FileInputStream; 21 import java.io.IOException; 22 import java.io.InputStream; 23 import java.util.zip.CRC32; 24 import java.util.zip.CheckedInputStream; 25 import junit.framework.TestCase; 26 import tests.support.resource.Support_Resources; 27 28 public class CheckedInputStreamTest extends TestCase { 29 30 /** 31 * java.util.zip.CheckedInputStream#CheckedInputStream(java.io.InputStream, 32 *java.util.zip.Checksum) 33 */ test_ConstructorLjava_io_InputStreamLjava_util_zip_Checksum()34 public void test_ConstructorLjava_io_InputStreamLjava_util_zip_Checksum() throws Exception { 35 InputStream checkInput = Support_Resources.getStream("hyts_checkInput.txt"); 36 CheckedInputStream checkIn = new CheckedInputStream(checkInput, new CRC32()); 37 assertEquals("constructor of checkedInputStream has failed", 0, checkIn.getChecksum() 38 .getValue()); 39 checkInput.close(); 40 } 41 42 /** 43 * java.util.zip.CheckedInputStream#getChecksum() 44 */ test_getChecksum()45 public void test_getChecksum() throws Exception { 46 byte outBuf[] = new byte[100]; 47 File f = File.createTempFile("CheckedInputStreamTest", ".txt"); 48 InputStream inEmp = new FileInputStream(f); 49 CheckedInputStream checkEmpty = new CheckedInputStream(inEmp, new CRC32()); 50 while (checkEmpty.read() >= 0) { 51 } 52 assertEquals("the checkSum value of an empty file is not zero", 0, checkEmpty 53 .getChecksum().getValue()); 54 inEmp.close(); 55 56 // testing getChecksum for the file checkInput 57 InputStream checkInput = Support_Resources.getStream("hyts_checkInput.txt"); 58 CheckedInputStream checkIn = new CheckedInputStream(checkInput, new CRC32()); 59 while (checkIn.read() >= 0) { 60 } 61 // ran JDK and found that the checkSum value of this is 2036203193 62 // System.out.print(" " + checkIn.getChecksum().getValue()); 63 assertEquals("the checksum value is incorrect", 2036203193, checkIn.getChecksum() 64 .getValue()); 65 checkInput.close(); 66 // testing getChecksum for file checkInput 67 checkInput = Support_Resources.getStream("hyts_checkInput.txt"); 68 CheckedInputStream checkIn2 = new CheckedInputStream(checkInput, new CRC32()); 69 checkIn2.read(outBuf, 0, 10); 70 // ran JDK and found that the checkSum value of this is 2235765342 71 // System.out.print(" " + checkIn2.getChecksum().getValue()); 72 assertEquals("the checksum value is incorrect", 2235765342L, checkIn2.getChecksum() 73 .getValue()); 74 checkInput.close(); 75 } 76 77 /** 78 * java.util.zip.CheckedInputStream#skip(long) 79 */ test_skipJ()80 public void test_skipJ() throws Exception { 81 // testing that the return by skip is valid 82 InputStream checkInput = Support_Resources.getStream("hyts_checkInput.txt"); 83 CheckedInputStream checkIn = new CheckedInputStream(checkInput, new CRC32()); 84 long skipValue = 5; 85 assertEquals("the value returned by skip(n) is not the same as its parameter", 86 skipValue, checkIn.skip(skipValue)); 87 checkIn.skip(skipValue); 88 // ran JDK and found the checkSum value is 2235765342 89 // System.out.print(checkIn.getChecksum().getValue()); 90 assertEquals("checkSum value is not correct", 2235765342L, checkIn.getChecksum() 91 .getValue()); 92 assertEquals(0, checkIn.skip(0)); 93 checkInput.close(); 94 } 95 test_read()96 public void test_read() throws Exception { 97 // testing that the return by skip is valid 98 InputStream checkInput = Support_Resources 99 .getStream("hyts_checkInput.txt"); 100 CheckedInputStream checkIn = new CheckedInputStream(checkInput, 101 new CRC32()); 102 checkIn.read(); 103 checkIn.close(); 104 try { 105 checkIn.read(); 106 fail("IOException expected."); 107 } catch (IOException ee) { 108 // expected 109 } 110 checkInput.close(); 111 } 112 test_read$byteII()113 public void test_read$byteII() throws Exception { 114 // testing that the return by skip is valid 115 InputStream checkInput = Support_Resources 116 .getStream("hyts_checkInput.txt"); 117 CheckedInputStream checkIn = new CheckedInputStream(checkInput, 118 new CRC32()); 119 byte buff[] = new byte[50]; 120 checkIn.read(buff, 10, 5); 121 checkIn.close(); 122 try { 123 checkIn.read(buff, 10, 5); 124 fail("IOException expected."); 125 } catch (IOException ee) { 126 // expected 127 } 128 checkInput.close(); 129 } 130 } 131