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.FileOutputStream; 21 import java.io.IOException; 22 import java.io.OutputStream; 23 import java.io.PipedInputStream; 24 import java.io.PipedOutputStream; 25 import java.util.zip.Checksum; 26 import java.util.zip.GZIPInputStream; 27 import java.util.zip.GZIPOutputStream; 28 import libcore.junit.junit3.TestCaseWithRules; 29 import libcore.junit.util.ResourceLeakageDetector; 30 import org.junit.Rule; 31 import org.junit.rules.TestRule; 32 33 public class GZIPOutputStreamTest extends TestCaseWithRules { 34 @Rule 35 public TestRule guardRule = ResourceLeakageDetector.getRule(); 36 37 class TestGZIPOutputStream extends GZIPOutputStream { 38 TestGZIPOutputStream(OutputStream out) throws IOException { 39 super(out); 40 } 41 42 TestGZIPOutputStream(OutputStream out, int size) throws IOException { 43 super(out, size); 44 } 45 46 Checksum getChecksum() { 47 return crc; 48 } 49 } 50 51 /** 52 * java.util.zip.GZIPOutputStream#GZIPOutputStream(java.io.OutputStream) 53 */ 54 public void test_ConstructorLjava_io_OutputStream() { 55 try { 56 FileOutputStream outFile = new FileOutputStream( 57 File.createTempFile("GZIPCon", ".txt")); 58 TestGZIPOutputStream outGZIP = new TestGZIPOutputStream(outFile); 59 assertNotNull("the constructor for GZIPOutputStream is null", 60 outGZIP); 61 assertEquals("the CRC value of the outputStream is not zero", 0, outGZIP 62 .getChecksum().getValue()); 63 outGZIP.close(); 64 } catch (IOException e) { 65 fail( 66 "an IO error occured while trying to find the output file or creating GZIP constructor"); 67 } 68 } 69 70 /** 71 * java.util.zip.GZIPOutputStream#GZIPOutputStream(java.io.OutputStream, 72 *int) 73 */ 74 public void test_ConstructorLjava_io_OutputStreamI() { 75 try { 76 FileOutputStream outFile = new FileOutputStream( 77 File.createTempFile("GZIPOutCon", ".txt")); 78 TestGZIPOutputStream outGZIP = new TestGZIPOutputStream(outFile, 79 100); 80 assertNotNull("the constructor for GZIPOutputStream is null", 81 outGZIP); 82 assertEquals("the CRC value of the outputStream is not zero", 0, outGZIP 83 .getChecksum().getValue()); 84 outGZIP.close(); 85 } catch (IOException e) { 86 fail( 87 "an IO error occured while trying to find the output file or creating GZIP constructor"); 88 } 89 } 90 91 /** 92 * java.util.zip.GZIPOutputStream#finish() 93 */ 94 public void test_finish() { 95 // test method java.util.zip.GZIPOutputStream.finish() 96 byte byteArray[] = { 3, 5, 2, 'r', 'g', 'e', 'f', 'd', 'e', 'w' }; 97 try { 98 FileOutputStream outFile = new FileOutputStream( 99 File.createTempFile("GZIPOutFinish", ".txt")); 100 TestGZIPOutputStream outGZIP = new TestGZIPOutputStream(outFile); 101 102 outGZIP.finish(); 103 int r = 0; 104 try { 105 outGZIP.write(byteArray, 0, 1); 106 } catch (IOException e) { 107 r = 1; 108 } 109 110 assertEquals("GZIP instance can still be used after finish is called", 111 1, r); 112 outGZIP.close(); 113 } catch (IOException e) { 114 fail( 115 "an IO error occured while trying to find the output file or creating GZIP constructor"); 116 } 117 } 118 119 /** 120 * java.util.zip.GZIPOutputStream#close() 121 */ 122 public void test_close() { 123 // test method java.util.zip.GZIPOutputStream.close() 124 byte byteArray[] = { 3, 5, 2, 'r', 'g', 'e', 'f', 'd', 'e', 'w' }; 125 try { 126 FileOutputStream outFile = new FileOutputStream( 127 File.createTempFile("GZIPOutClose2", ".txt")); 128 TestGZIPOutputStream outGZIP = new TestGZIPOutputStream(outFile); 129 outGZIP.close(); 130 int r = 0; 131 try { 132 outGZIP.write(byteArray, 0, 1); 133 } catch (IOException e) { 134 r = 1; 135 } 136 assertEquals("GZIP instance can still be used after close is called", 137 1, r); 138 } catch (IOException e) { 139 fail( 140 "an IO error occured while trying to find the output file or creating GZIP constructor"); 141 } 142 } 143 144 /** 145 * java.util.zip.GZIPOutputStream#write(byte[], int, int) 146 */ 147 public void test_write$BII() { 148 // test method java.util.zip.GZIPOutputStream.writeBII 149 byte byteArray[] = { 3, 5, 2, 'r', 'g', 'e', 'f', 'd', 'e', 'w' }; 150 try { 151 FileOutputStream outFile = new FileOutputStream( 152 File.createTempFile("GZIPOutWrite", ".txt")); 153 TestGZIPOutputStream outGZIP = new TestGZIPOutputStream(outFile); 154 outGZIP.write(byteArray, 0, 10); 155 // ran JDK and found this CRC32 value is 3097700292 156 // System.out.print(outGZIP.getChecksum().getValue()); 157 assertEquals("the checksum value was incorrect result of write from GZIP", 158 3097700292L, outGZIP.getChecksum().getValue()); 159 160 // test for boundary check 161 int r = 0; 162 try { 163 outGZIP.write(byteArray, 0, 11); 164 } catch (IndexOutOfBoundsException e) { 165 r = 1; 166 } 167 assertEquals("out of bounds exception is not present", 1, r); 168 outGZIP.close(); 169 } catch (IOException e) { 170 fail( 171 "an IO error occured while trying to find the output file or creating GZIP constructor"); 172 } 173 } 174 175 public void testSyncFlush() throws IOException { 176 PipedOutputStream pout = new PipedOutputStream(); 177 PipedInputStream pin = new PipedInputStream(pout); 178 // Must create in this order so that GZIPOutputStream writes the header before 179 // GZIPInputStream tries to read it otherwise it will deadlock with GZIPInputStream waiting 180 // for the header to be written but it cannot be written until after GZIPInputStream has 181 // read it. 182 GZIPOutputStream out = new GZIPOutputStream(pout, true /* syncFlush */); 183 GZIPInputStream in = new GZIPInputStream(pin); 184 out.write(1); 185 out.write(2); 186 out.write(3); 187 out.flush(); 188 // flush() is guaranteed to flush data only if syncFlush is true. 189 // The default flush param is NO_FLUSH so it's up to the deflater to 190 // decide how much input it wants to read before generating a compressed 191 // block. 192 assertEquals(1, in.read()); 193 assertEquals(2, in.read()); 194 assertEquals(3, in.read()); 195 out.close(); 196 in.close(); 197 } 198 } 199