|
1 /* |
|
2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
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 /* |
|
25 * @test |
|
26 * @bug 4017158 8180410 |
|
27 * @library /test/lib |
|
28 * @build jdk.test.lib.RandomFactory |
|
29 * @run testng Write |
|
30 * @summary Check for correct implementation of ByteArrayInputStream.write |
|
31 * @key randomness |
|
32 */ |
|
33 |
|
34 import java.io.ByteArrayOutputStream; |
|
35 import java.util.Arrays; |
|
36 import java.util.Random; |
|
37 import jdk.test.lib.RandomFactory; |
|
38 import org.testng.annotations.Test; |
|
39 import static org.testng.Assert.*; |
|
40 |
|
41 public class Write { |
|
42 private static void doBoundsTest(byte[] b, int off, int len, |
|
43 ByteArrayOutputStream baos) |
|
44 throws Exception { |
|
45 if (b != null) { |
|
46 System.out.println("ByteArrayOutStream.write: b.length = " + |
|
47 b.length + " off = " + off + " len = " + len); |
|
48 } else{ |
|
49 System.out.println("ByteArrayOutStream.write: b is null off = " + |
|
50 off + " len = " + len); |
|
51 } |
|
52 |
|
53 try { |
|
54 baos.write(b, off, len); |
|
55 } catch (IndexOutOfBoundsException e) { |
|
56 System.out.println("IndexOutOfBoundsException is thrown: OKAY"); |
|
57 } catch (NullPointerException e) { |
|
58 System.out.println("NullPointerException is thrown: OKAY"); |
|
59 } catch (Throwable e){ |
|
60 throw new RuntimeException("Unexpected Exception is thrown", e); |
|
61 } |
|
62 |
|
63 if (b != null) { |
|
64 System.out.println("ByteArrayOutStream.writeBytes: b.length = " + |
|
65 b.length); |
|
66 } else{ |
|
67 System.out.println("ByteArrayOutStream.writeBytes: b is null"); |
|
68 } |
|
69 |
|
70 try { |
|
71 baos.writeBytes(b); |
|
72 } catch (NullPointerException e) { |
|
73 System.out.println("NullPointerException is thrown: OKAY"); |
|
74 } catch (Throwable e){ |
|
75 throw new RuntimeException("Unexpected Exception is thrown", e); |
|
76 } |
|
77 } |
|
78 |
|
79 @Test |
|
80 public static void boundsTest() throws Exception { |
|
81 byte array1[] = {1 , 2 , 3 , 4 , 5}; // Simple array |
|
82 |
|
83 //Create new ByteArrayOutputStream object |
|
84 ByteArrayOutputStream y1 = new ByteArrayOutputStream(5); |
|
85 |
|
86 doBoundsTest(array1, 0, Integer.MAX_VALUE , y1); |
|
87 doBoundsTest(array1, 0, array1.length+100, y1); |
|
88 doBoundsTest(array1, -1, 2, y1); |
|
89 doBoundsTest(array1, 0, -1, y1); |
|
90 doBoundsTest(null, 0, 2, y1); |
|
91 } |
|
92 |
|
93 @Test |
|
94 public static void writeTest() throws Exception { |
|
95 ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
96 Random rnd = RandomFactory.getRandom(); |
|
97 final int size = 17 + rnd.nextInt(128); |
|
98 |
|
99 byte[] b = new byte[size]; |
|
100 rnd.nextBytes(b); |
|
101 |
|
102 int off1 = rnd.nextInt(size / 4) + 1; |
|
103 int len1 = Math.min(rnd.nextInt(size / 4) + 1, size - off1); |
|
104 int off2 = rnd.nextInt(size / 2) + 1; |
|
105 int len2 = Math.min(rnd.nextInt(size / 2) + 1, size - off2); |
|
106 |
|
107 System.out.format("size: %d, off1: %d, len1: %d, off2: %d, len2: %d%n", |
|
108 size, off1, len1, off2, len2); |
|
109 |
|
110 baos.write(b, off1, len1); |
|
111 byte[] b1 = baos.toByteArray(); |
|
112 assertEquals(b1.length, len1, "Array length test 1 failed."); |
|
113 assertEquals(b1, Arrays.copyOfRange(b, off1, off1 + len1), |
|
114 "Array equality test 1 failed."); |
|
115 |
|
116 baos.write(b, off2, len2); |
|
117 byte[] b2 = baos.toByteArray(); |
|
118 assertEquals(b2.length, len1 + len2, "Array length test 2 failed."); |
|
119 assertEquals(Arrays.copyOfRange(b2, 0, len1), |
|
120 Arrays.copyOfRange(b, off1, off1 + len1), |
|
121 "Array equality test 2A failed."); |
|
122 assertEquals(Arrays.copyOfRange(b2, len1, len1 + len2), |
|
123 Arrays.copyOfRange(b, off2, off2 + len2), |
|
124 "Array equality test 2B failed."); |
|
125 |
|
126 baos.writeBytes(b); |
|
127 byte[] b3 = baos.toByteArray(); |
|
128 int len3 = len1 + len2 + b.length; |
|
129 if (b3.length != len1 + len2 + b.length) { |
|
130 throw new RuntimeException("Array length test 3 failed."); |
|
131 } |
|
132 assertEquals(b3.length, len3, "Array length test 3 failed."); |
|
133 assertEquals(Arrays.copyOfRange(b3, 0, len1), |
|
134 Arrays.copyOfRange(b, off1, off1 + len1), |
|
135 "Array equality test 3A failed."); |
|
136 assertEquals(Arrays.copyOfRange(b3, len1, len1 + len2), |
|
137 Arrays.copyOfRange(b, off2, off2 + len2), |
|
138 "Array equality test 3B failed."); |
|
139 assertEquals(Arrays.copyOfRange(b3, len1 + len2, len3), b, |
|
140 "Array equality test 3C failed."); |
|
141 } |
|
142 } |