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