OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / libcore / java / io / FilterInputStreamNullSourceTest.java
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package libcore.java.io;
18
19 import java.io.BufferedInputStream;
20 import java.io.DataInputStream;
21 import java.io.FilterInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.LineNumberInputStream;
25 import java.io.PushbackInputStream;
26 import java.security.DigestInputStream;
27 import java.security.MessageDigest;
28 import java.security.NoSuchAlgorithmException;
29 import java.util.zip.CRC32;
30 import java.util.zip.CheckedInputStream;
31 import java.util.zip.Inflater;
32 import java.util.zip.InflaterInputStream;
33 import javax.crypto.CipherInputStream;
34 import javax.crypto.NullCipher;
35 import junit.framework.TestCase;
36
37 public final class FilterInputStreamNullSourceTest extends TestCase {
38
39     public void testBufferedInputStream() throws IOException {
40         assertReadsFailWithIoException(new BufferedInputStream(null));
41         assertReadsFailWithIoException(new BufferedInputStream(null, 1024));
42     }
43
44     public void testCheckedInputStream() throws IOException {
45         assertReadsFailWithNullPointerException(new CheckedInputStream(null, new CRC32()));
46     }
47
48     public void testCipherInputStream() throws IOException {
49         InputStream in = new CipherInputStream(null, new NullCipher());
50         try {
51             in.read();
52             fail();
53         } catch (NullPointerException expected) {
54         }
55
56         assertEquals(0, in.available());
57
58         try {
59             in.close();
60             fail();
61         } catch (NullPointerException expected) {
62         }
63     }
64
65     public void testDataInputStream() throws IOException {
66         assertReadsFailWithNullPointerException(new DataInputStream(null));
67     }
68
69     public void testDigestInputStream() throws IOException, NoSuchAlgorithmException {
70         MessageDigest md5 = MessageDigest.getInstance("MD5");
71         assertReadsFailWithNullPointerException(new DigestInputStream(null, md5));
72     }
73
74     public void testFilterInputStream() throws IOException {
75         assertReadsFailWithNullPointerException(new FilterInputStream(null) {});
76     }
77
78     public void testInflaterInputStream() throws IOException {
79         try {
80             new InflaterInputStream(null);
81             fail();
82         } catch (NullPointerException expected) {
83         }
84         try {
85             new InflaterInputStream(null, new Inflater());
86             fail();
87         } catch (NullPointerException expected) {
88         }
89         try {
90             new InflaterInputStream(null, new Inflater(), 1024);
91             fail();
92         } catch (NullPointerException expected) {
93         }
94     }
95
96     public void testLineNumberInputStream() throws IOException {
97         assertReadsFailWithNullPointerException(new LineNumberInputStream(null));
98     }
99
100     public void testPushbackInputStream() throws IOException {
101         assertReadsFailWithIoException(new PushbackInputStream(null));
102         assertReadsFailWithIoException(new PushbackInputStream(null, 1024));
103     }
104
105     private void assertReadsFailWithIoException(InputStream in) throws IOException {
106         try {
107             in.read();
108             fail();
109         } catch (IOException expected) {
110         }
111
112         try {
113             in.available();
114             fail();
115         } catch (IOException expected) {
116         }
117
118         in.close();
119     }
120
121     private void assertReadsFailWithNullPointerException(InputStream in) throws IOException {
122         try {
123             in.read();
124             fail();
125         } catch (NullPointerException expected) {
126         }
127
128         try {
129             in.available();
130             fail();
131         } catch (NullPointerException expected) {
132         }
133
134         try {
135             in.close();
136             fail();
137         } catch (NullPointerException expected) {
138         }
139     }
140 }