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