Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/java.desktop/share/classes/java/awt/image/Kernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package java.awt.image;

import java.util.Objects;

/**
* The {@code Kernel} class defines a matrix that describes how a
Expand Down Expand Up @@ -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
Expand All @@ -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");
}
Expand Down
51 changes: 32 additions & 19 deletions test/jdk/java/awt/image/ConvolveOp/KernelInitialisationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}