Path: blob/master/test/jdk/java/io/ByteArrayOutputStream/Write.java
41149 views
/*1* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4017158 818041026* @library /test/lib27* @build jdk.test.lib.RandomFactory28* @run testng Write29* @summary Check for correct implementation of ByteArrayInputStream.write30* @key randomness31*/3233import java.io.ByteArrayOutputStream;34import java.util.Arrays;35import java.util.Random;36import jdk.test.lib.RandomFactory;37import org.testng.annotations.Test;38import static org.testng.Assert.*;3940public class Write {41private static void doBoundsTest(byte[] b, int off, int len,42ByteArrayOutputStream baos)43throws Exception {44if (b != null) {45System.out.println("ByteArrayOutStream.write: b.length = " +46b.length + " off = " + off + " len = " + len);47} else{48System.out.println("ByteArrayOutStream.write: b is null off = " +49off + " len = " + len);50}5152try {53baos.write(b, off, len);54} catch (IndexOutOfBoundsException e) {55System.out.println("IndexOutOfBoundsException is thrown: OKAY");56} catch (NullPointerException e) {57System.out.println("NullPointerException is thrown: OKAY");58} catch (Throwable e){59throw new RuntimeException("Unexpected Exception is thrown", e);60}6162if (b != null) {63System.out.println("ByteArrayOutStream.writeBytes: b.length = " +64b.length);65} else{66System.out.println("ByteArrayOutStream.writeBytes: b is null");67}6869try {70baos.writeBytes(b);71} catch (NullPointerException e) {72System.out.println("NullPointerException is thrown: OKAY");73} catch (Throwable e){74throw new RuntimeException("Unexpected Exception is thrown", e);75}76}7778@Test79public static void boundsTest() throws Exception {80byte array1[] = {1 , 2 , 3 , 4 , 5}; // Simple array8182//Create new ByteArrayOutputStream object83ByteArrayOutputStream y1 = new ByteArrayOutputStream(5);8485doBoundsTest(array1, 0, Integer.MAX_VALUE , y1);86doBoundsTest(array1, 0, array1.length+100, y1);87doBoundsTest(array1, -1, 2, y1);88doBoundsTest(array1, 0, -1, y1);89doBoundsTest(null, 0, 2, y1);90}9192@Test93public static void writeTest() throws Exception {94ByteArrayOutputStream baos = new ByteArrayOutputStream();95Random rnd = RandomFactory.getRandom();96final int size = 17 + rnd.nextInt(128);9798byte[] b = new byte[size];99rnd.nextBytes(b);100101int off1 = rnd.nextInt(size / 4) + 1;102int len1 = Math.min(rnd.nextInt(size / 4) + 1, size - off1);103int off2 = rnd.nextInt(size / 2) + 1;104int len2 = Math.min(rnd.nextInt(size / 2) + 1, size - off2);105106System.out.format("size: %d, off1: %d, len1: %d, off2: %d, len2: %d%n",107size, off1, len1, off2, len2);108109baos.write(b, off1, len1);110byte[] b1 = baos.toByteArray();111assertEquals(b1.length, len1, "Array length test 1 failed.");112assertEquals(b1, Arrays.copyOfRange(b, off1, off1 + len1),113"Array equality test 1 failed.");114115baos.write(b, off2, len2);116byte[] b2 = baos.toByteArray();117assertEquals(b2.length, len1 + len2, "Array length test 2 failed.");118assertEquals(Arrays.copyOfRange(b2, 0, len1),119Arrays.copyOfRange(b, off1, off1 + len1),120"Array equality test 2A failed.");121assertEquals(Arrays.copyOfRange(b2, len1, len1 + len2),122Arrays.copyOfRange(b, off2, off2 + len2),123"Array equality test 2B failed.");124125baos.writeBytes(b);126byte[] b3 = baos.toByteArray();127int len3 = len1 + len2 + b.length;128if (b3.length != len1 + len2 + b.length) {129throw new RuntimeException("Array length test 3 failed.");130}131assertEquals(b3.length, len3, "Array length test 3 failed.");132assertEquals(Arrays.copyOfRange(b3, 0, len1),133Arrays.copyOfRange(b, off1, off1 + len1),134"Array equality test 3A failed.");135assertEquals(Arrays.copyOfRange(b3, len1, len1 + len2),136Arrays.copyOfRange(b, off2, off2 + len2),137"Array equality test 3B failed.");138assertEquals(Arrays.copyOfRange(b3, len1 + len2, len3), b,139"Array equality test 3C failed.");140}141}142143144