diff --git a/src/java.desktop/share/classes/java/awt/image/Kernel.java b/src/java.desktop/share/classes/java/awt/image/Kernel.java index d2b9760aba50d..67f62e79b9ed7 100644 --- a/src/java.desktop/share/classes/java/awt/image/Kernel.java +++ b/src/java.desktop/share/classes/java/awt/image/Kernel.java @@ -25,6 +25,7 @@ package java.awt.image; +import java.util.Objects; /** * The {@code Kernel} class defines a matrix that describes how a @@ -59,7 +60,7 @@ public class Kernel implements Cloneable { * @param width width of the kernel * @param height height of the kernel * @param data kernel data in row major order - * @throws IllegalArgumentException if {@code data} is null + * @throws NullPointerException if {@code data} is null * @throws IllegalArgumentException if {@code width} or {@code height} * is not positive * @throws IllegalArgumentException if product of {@code width} and @@ -69,9 +70,7 @@ public class Kernel implements Cloneable { * {@code height} */ public Kernel(int width, int height, float[] data) { - if (data == null) { - throw new IllegalArgumentException("Data must not be null"); - } + Objects.requireNonNull(data, "Data must not be null"); if ((width <= 0) || (height <= 0)) { throw new IllegalArgumentException("Invalid width or height"); } diff --git a/test/jdk/java/awt/image/ConvolveOp/KernelInitialisationTest.java b/test/jdk/java/awt/image/ConvolveOp/KernelInitialisationTest.java index 607f0a7a8dc66..0d58472385cc5 100644 --- a/test/jdk/java/awt/image/ConvolveOp/KernelInitialisationTest.java +++ b/test/jdk/java/awt/image/ConvolveOp/KernelInitialisationTest.java @@ -25,36 +25,49 @@ * @test * @bug 8368729 * @summary Tests that passing invalid values to Kernel constructor - * throws only IllegalArgumentException + * throws only IllegalArgumentException or NullPointerException */ import java.awt.image.Kernel; public class KernelInitialisationTest { - private static void expectIllegalArgumentException(Runnable code) { + + private static void test(int width, int height, float[] data, + Class expected) + { + System.out.printf("Testing for width: %d, height: %d, data: %s%n", + width, height, data == null ? "null" : "not null"); + Class actual = null; try { - code.run(); - throw new RuntimeException("Expected IllegalArgumentException" + - " but no exception was thrown"); - } catch (IllegalArgumentException e) { - // we expect IllegalArgumentException + new Kernel(width, height, data); + } catch (Exception e) { + actual = e.getClass(); + } + if (actual != expected) { + System.err.println("Expected: " + expected); + System.err.println("Actual: " + actual); + throw new RuntimeException("Test failed"); } } - private static void testKernel(int width, int height, float[] data) { - System.out.println("Testing for width: " + width + ", height: " - + height + ", data: " + (data == null ? "null" : "not null")); - expectIllegalArgumentException(() -> new Kernel(width, height, data)); + private static void testIAE(int width, int height, int len) { + test(width, height, new float[len], IllegalArgumentException.class); } - public static void main(String[] args) { - testKernel(-1, 1, new float[100]); - testKernel(1, -1, new float[100]); - testKernel(-1, -1, new float[100]); - testKernel(1, 1, null); + private static void testNPE(int width, int height) { + test(width, height, null, NullPointerException.class); + } - int width = 50; - int height = Integer.MAX_VALUE; - testKernel(width, height, new float[100]); + public static void main(String[] args) { + int[][] sizes = {{-1, 1}, {1, -1}, {-1, -1}, {50, Integer.MAX_VALUE}}; + int[] lens = {1, 100}; + for (int[] kernelSize : sizes) { + for (int len : lens) { + testIAE(kernelSize[0], kernelSize[1], len); + } + testNPE(kernelSize[0], kernelSize[1]); + } + testNPE(10, 10); // NPE on valid width and height + testIAE(10, 10, 10); // IAE on valid width and height but small data } }