14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.InputStream;
26 import java.io.IOException;
27 import org.testng.annotations.AfterGroups;
28 import org.testng.annotations.BeforeGroups;
29 import org.testng.annotations.Test;
30 import static org.testng.Assert.*;
31
32 /*
33 * @test
34 * @bug 4358774
35 * @run testng NullInputStream
36 * @summary Check for expected behavior of InputStream.nullInputStream().
37 */
38 public class NullInputStream {
39 private static InputStream openStream;
40 private static InputStream closedStream;
41
42 @BeforeGroups(groups="open")
43 public static void openStream() {
44 openStream = InputStream.nullInputStream();
45 }
46
47 @BeforeGroups(groups="closed")
48 public static void openAndCloseStream() {
49 closedStream = InputStream.nullInputStream();
50 try {
51 closedStream.close();
52 } catch (IOException e) {
53 fail("Unexpected IOException");
54 }
90 public static void testReadBII() {
91 try {
92 assertEquals(-1, openStream.read(new byte[1], 0, 1),
93 "read(byte[],int,int) != -1");
94 } catch (IOException ioe) {
95 fail("Unexpected IOException");
96 }
97 }
98
99 @Test(groups = "open")
100 public static void testReadAllBytes() {
101 try {
102 assertEquals(0, openStream.readAllBytes().length,
103 "readAllBytes().length != 0");
104 } catch (IOException ioe) {
105 fail("Unexpected IOException");
106 }
107 }
108
109 @Test(groups = "open")
110 public static void testreadNBytes() {
111 try {
112 assertEquals(0, openStream.readNBytes(new byte[1], 0, 1),
113 "readNBytes(byte[],int,int) != 0");
114 } catch (IOException ioe) {
115 fail("Unexpected IOException");
116 }
117 }
118
119 @Test(groups = "open")
120 public static void testSkip() {
121 try {
122 assertEquals(0, openStream.skip(1), "skip() != 0");
123 } catch (IOException ioe) {
124 fail("Unexpected IOException");
125 }
126 }
127
128 @Test(groups = "open")
129 public static void testTransferTo() {
130 try {
131 assertEquals(0, openStream.transferTo(new ByteArrayOutputStream(7)),
132 "transferTo() != 0");
133 } catch (IOException ioe) {
134 fail("Unexpected IOException");
135 }
136 }
137
138 @Test(groups = "closed")
139 public static void testAvailableClosed() {
164
165 @Test(groups = "closed")
166 public static void testReadAllBytesClosed() {
167 try {
168 closedStream.readAllBytes();
169 fail("Expected IOException not thrown");
170 } catch (IOException e) {
171 }
172 }
173
174 @Test(groups = "closed")
175 public static void testReadNBytesClosed() {
176 try {
177 closedStream.readNBytes(new byte[1], 0, 1);
178 fail("Expected IOException not thrown");
179 } catch (IOException e) {
180 }
181 }
182
183 @Test(groups = "closed")
184 public static void testSkipClosed() {
185 try {
186 closedStream.skip(1);
187 fail("Expected IOException not thrown");
188 } catch (IOException e) {
189 }
190 }
191
192 @Test(groups = "closed")
193 public static void testTransferToClosed() {
194 try {
195 closedStream.transferTo(new ByteArrayOutputStream(7));
196 fail("Expected IOException not thrown");
197 } catch (IOException e) {
198 }
199 }
200 }
|
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.InputStream;
26 import java.io.IOException;
27 import org.testng.annotations.AfterGroups;
28 import org.testng.annotations.BeforeGroups;
29 import org.testng.annotations.Test;
30 import static org.testng.Assert.*;
31
32 /*
33 * @test
34 * @bug 4358774 8139206
35 * @run testng NullInputStream
36 * @summary Check for expected behavior of InputStream.nullInputStream().
37 */
38 public class NullInputStream {
39 private static InputStream openStream;
40 private static InputStream closedStream;
41
42 @BeforeGroups(groups="open")
43 public static void openStream() {
44 openStream = InputStream.nullInputStream();
45 }
46
47 @BeforeGroups(groups="closed")
48 public static void openAndCloseStream() {
49 closedStream = InputStream.nullInputStream();
50 try {
51 closedStream.close();
52 } catch (IOException e) {
53 fail("Unexpected IOException");
54 }
90 public static void testReadBII() {
91 try {
92 assertEquals(-1, openStream.read(new byte[1], 0, 1),
93 "read(byte[],int,int) != -1");
94 } catch (IOException ioe) {
95 fail("Unexpected IOException");
96 }
97 }
98
99 @Test(groups = "open")
100 public static void testReadAllBytes() {
101 try {
102 assertEquals(0, openStream.readAllBytes().length,
103 "readAllBytes().length != 0");
104 } catch (IOException ioe) {
105 fail("Unexpected IOException");
106 }
107 }
108
109 @Test(groups = "open")
110 public static void testReadNBytes() {
111 try {
112 assertEquals(0, openStream.readNBytes(new byte[1], 0, 1),
113 "readNBytes(byte[],int,int) != 0");
114 } catch (IOException ioe) {
115 fail("Unexpected IOException");
116 }
117 }
118
119 @Test(groups = "open")
120 public static void testReadNBytesWithLength() {
121 try {
122 assertEquals(0, openStream.readNBytes(-1).length,
123 "readNBytes(-1) != 0");
124 fail("Expected IllegalArgumentException not thrown");
125 } catch (IllegalArgumentException iae) {
126 } catch (IOException ioe) {
127 fail("Unexpected IOException");
128 }
129 try {
130 assertEquals(0, openStream.readNBytes(0).length,
131 "readNBytes(0, false) != 0");
132 assertEquals(0, openStream.readNBytes(1).length,
133 "readNBytes(1, false) != 0");
134 } catch (IOException ioe) {
135 fail("Unexpected IOException");
136 }
137 }
138
139 @Test(groups = "open")
140 public static void testSkip() {
141 try {
142 assertEquals(0, openStream.skip(1), "skip() != 0");
143 } catch (IOException ioe) {
144 fail("Unexpected IOException");
145 }
146 }
147
148 @Test(groups = "open")
149 public static void testTransferTo() {
150 try {
151 assertEquals(0, openStream.transferTo(new ByteArrayOutputStream(7)),
152 "transferTo() != 0");
153 } catch (IOException ioe) {
154 fail("Unexpected IOException");
155 }
156 }
157
158 @Test(groups = "closed")
159 public static void testAvailableClosed() {
184
185 @Test(groups = "closed")
186 public static void testReadAllBytesClosed() {
187 try {
188 closedStream.readAllBytes();
189 fail("Expected IOException not thrown");
190 } catch (IOException e) {
191 }
192 }
193
194 @Test(groups = "closed")
195 public static void testReadNBytesClosed() {
196 try {
197 closedStream.readNBytes(new byte[1], 0, 1);
198 fail("Expected IOException not thrown");
199 } catch (IOException e) {
200 }
201 }
202
203 @Test(groups = "closed")
204 public static void testReadNBytesWithLengthClosed() {
205 try {
206 closedStream.readNBytes(1);
207 fail("Expected IOException not thrown");
208 } catch (IOException e) {
209 }
210 }
211
212 @Test(groups = "closed")
213 public static void testSkipClosed() {
214 try {
215 closedStream.skip(1);
216 fail("Expected IOException not thrown");
217 } catch (IOException e) {
218 }
219 }
220
221 @Test(groups = "closed")
222 public static void testTransferToClosed() {
223 try {
224 closedStream.transferTo(new ByteArrayOutputStream(7));
225 fail("Expected IOException not thrown");
226 } catch (IOException e) {
227 }
228 }
229 }
|