1 /* 2 * Copyright (c) 2017, 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 * @run testng RequestProcessorExceptions 27 */ 28 29 import java.io.FileNotFoundException; 30 import java.nio.file.Files; 31 import java.nio.file.Path; 32 import java.nio.file.Paths; 33 import java.util.List; 34 import org.testng.annotations.DataProvider; 35 import org.testng.annotations.Test; 36 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromByteArray; 37 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromFile; 38 39 public class RequestProcessorExceptions { 40 41 @DataProvider(name = "byteArrayOOBs") 42 public Object[][] byteArrayOOBs() { 43 return new Object[][] { 44 { new byte[100], 1, 100 }, 45 { new byte[100], -1, 10 }, 46 { new byte[100], 99, 2 }, 47 { new byte[1], -100, 1 } }; 48 } 49 50 @Test(dataProvider = "byteArrayOOBs", expectedExceptions = IndexOutOfBoundsException.class) 51 public void fromByteArrayCheck(byte[] buf, int offset, int length) { 52 fromByteArray(buf, offset, length); 53 } 54 55 @DataProvider(name = "nonExistentFiles") 56 public Object[][] nonExistentFiles() { 57 List<Path> paths = List.of(Paths.get("doesNotExist"), 58 Paths.get("tsixEtoNseod"), 59 Paths.get("doesNotExist2")); 60 paths.forEach(p -> { 61 if (Files.exists(p)) 62 throw new AssertionError("Unexpected " + p); 63 }); 64 65 return paths.stream().map(p -> new Object[] { p }).toArray(Object[][]::new); 66 } 67 68 @Test(dataProvider = "nonExistentFiles", expectedExceptions = FileNotFoundException.class) 69 public void fromFileCheck(Path path) throws Exception { 70 fromFile(path); 71 } 72 73 // --- 74 75 /* Main entry point for standalone testing of the main functional test. */ 76 public static void main(String... args) throws Exception { 77 RequestProcessorExceptions t = new RequestProcessorExceptions(); 78 for (Object[] objs : t.byteArrayOOBs()) { 79 try { 80 t.fromByteArrayCheck((byte[]) objs[0], (int) objs[1], (int) objs[2]); 81 throw new RuntimeException("fromByteArrayCheck failed"); 82 } catch (IndexOutOfBoundsException expected) { /* Ok */ } 83 } 84 for (Object[] objs : t.nonExistentFiles()) { 85 try { 86 t.fromFileCheck((Path) objs[0]); 87 throw new RuntimeException("fromFileCheck failed"); 88 } catch (FileNotFoundException expected) { /* Ok */ } 89 } 90 } 91 }