CheckedOutputStreamTest.java revision d567f9025c4b94fc5e9b47f5702c1b48c2a45c96
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 */ 17package org.apache.harmony.tests.java.util.zip; 18 19import java.io.File; 20import java.io.FileOutputStream; 21import java.io.IOException; 22import java.util.zip.Adler32; 23import java.util.zip.CRC32; 24import java.util.zip.CheckedOutputStream; 25 26public class CheckedOutputStreamTest extends junit.framework.TestCase { 27 28 /** 29 * java.util.zip.CheckedOutputStream#CheckedOutputStream(java.io.OutputStream, 30 *java.util.zip.Checksum) 31 */ 32 public void test_ConstructorLjava_io_OutputStreamLjava_util_zip_Checksum() { 33 // test method java.util.zip.checkedOutputStream.constructor 34 try { 35 FileOutputStream outFile = new FileOutputStream("chkOut.txt"); 36 CheckedOutputStream chkOut = new CheckedOutputStream(outFile, 37 new CRC32()); 38 assertEquals("the checkSum value of the constructor is not 0", 0, chkOut 39 .getChecksum().getValue()); 40 outFile.close(); 41 } catch (IOException e) { 42 fail("Unable to find file"); 43 } catch (SecurityException e) { 44 fail( 45 "file cannot be opened for writing due to security reasons"); 46 } 47 } 48 49 /** 50 * java.util.zip.CheckedOutputStream#getChecksum() 51 */ 52 public void test_getChecksum() { 53 // test method java.util.zip.checkedOutputStream.getChecksum() 54 byte byteArray[] = { 1, 2, 3, 'e', 'r', 't', 'g', 3, 6 }; 55 try { 56 FileOutputStream outFile = new FileOutputStream("chkOut.txt"); 57 CheckedOutputStream chkOut = new CheckedOutputStream(outFile, 58 new Adler32()); 59 chkOut.write(byteArray[4]); 60 // ran JDK and found that checkSum value is 7536755 61 // System.out.print(chkOut.getChecksum().getValue()); 62 63 assertEquals("the checkSum value for writeI is incorrect", 7536755, chkOut 64 .getChecksum().getValue()); 65 chkOut.getChecksum().reset(); 66 chkOut.write(byteArray, 5, 4); 67 // ran JDK and found that checkSum value is 51708133 68 // System.out.print(" " +chkOut.getChecksum().getValue()); 69 70 assertEquals("the checkSum value for writeBII is incorrect ", 51708133, chkOut 71 .getChecksum().getValue()); 72 outFile.close(); 73 } catch (IOException e) { 74 fail("Unable to find file"); 75 } catch (SecurityException e) { 76 fail( 77 "file cannot be opened for writing due to security reasons"); 78 } 79 } 80 81 /** 82 * java.util.zip.CheckedOutputStream#write(int) 83 */ 84 public void test_writeI() { 85 // test method java.util.zip.checkedOutputStream.writeI() 86 byte byteArray[] = { 1, 2, 3, 'e', 'r', 't', 'g', 3, 6 }; 87 try { 88 FileOutputStream outFile = new FileOutputStream("chkOut.txt"); 89 CheckedOutputStream chkOut = new CheckedOutputStream(outFile, 90 new CRC32()); 91 for (byte element : byteArray) { 92 chkOut.write(element); 93 } 94 assertTrue( 95 "the checkSum value is zero, no bytes are written to the output file", 96 chkOut.getChecksum().getValue() != 0); 97 outFile.close(); 98 } catch (IOException e) { 99 fail("Unable to find file"); 100 } catch (SecurityException e) { 101 fail("File cannot be opened for writing due to security reasons"); 102 } 103 } 104 105 /** 106 * java.util.zip.CheckedOutputStream#write(byte[], int, int) 107 */ 108 public void test_write$BII() { 109 // test method java.util.zip.checkOutputStream.writeBII() 110 byte byteArray[] = { 1, 2, 3, 'e', 'r', 't', 'g', 3, 6 }; 111 try { 112 FileOutputStream outFile = new FileOutputStream("chkOut.txt"); 113 CheckedOutputStream chkOut = new CheckedOutputStream(outFile, 114 new CRC32()); 115 chkOut.write(byteArray, 4, 5); 116 assertTrue( 117 "the checkSum value is zero, no bytes are written to the output file", 118 chkOut.getChecksum().getValue() != 0); 119 int r = 0; 120 try { 121 chkOut.write(byteArray, 4, 6); 122 } catch (IndexOutOfBoundsException e) { 123 r = 1; 124 } 125 assertEquals("boundary check is not performed", 1, r); 126 outFile.close(); 127 } catch (IOException e) { 128 fail("Unable to find file"); 129 } catch (SecurityException e) { 130 fail( 131 "file cannot be opened for writing due to security reasons"); 132 } catch (IndexOutOfBoundsException e) { 133 fail("Index for write is out of bounds"); 134 } 135 } 136 137 @Override 138 protected void setUp() { 139 } 140 141 @Override 142 protected void tearDown() { 143 try { 144 File deletedFile = new File("chkOut.txt"); 145 deletedFile.delete(); 146 } catch (SecurityException e) { 147 fail("Cannot delete file for security reasons"); 148 } 149 } 150 151} 152