From a4eaeb47c9c42d8da4e3814c80247f40236a03a2 Mon Sep 17 00:00:00 2001 From: Phil Race Date: Fri, 24 Oct 2025 22:24:28 +0000 Subject: [PATCH] 6453640: BandedSampleModel.createCompatibleSampleModel() API docs are wrong Reviewed-by: azvegint, serb --- .../java/awt/image/BandedSampleModel.java | 13 +-- .../BSMCreateCompatibleSMTest.java | 100 ++++++++++++++++++ 2 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 test/jdk/java/awt/image/BandedSampleModel/BSMCreateCompatibleSMTest.java diff --git a/src/java.desktop/share/classes/java/awt/image/BandedSampleModel.java b/src/java.desktop/share/classes/java/awt/image/BandedSampleModel.java index bd955e35870..bad9abc6130 100644 --- a/src/java.desktop/share/classes/java/awt/image/BandedSampleModel.java +++ b/src/java.desktop/share/classes/java/awt/image/BandedSampleModel.java @@ -141,12 +141,9 @@ public final class BandedSampleModel extends ComponentSampleModel * @param h the height of the resulting {@code BandedSampleModel} * @return a new {@code BandedSampleModel} with the specified * width and height. - * @throws IllegalArgumentException if {@code w} or - * {@code h} equals either - * {@code Integer.MAX_VALUE} or - * {@code Integer.MIN_VALUE} - * @throws IllegalArgumentException if {@code dataType} is not - * one of the supported data types + * @throws IllegalArgumentException if the product of {@code w} + * and {@code h} is greater than {@code Integer.MAX_VALUE} + * or {@code w} or {@code h} is not greater than 0. */ public SampleModel createCompatibleSampleModel(int w, int h) { int[] bandOffs; @@ -172,8 +169,8 @@ public final class BandedSampleModel extends ComponentSampleModel * of the original BandedSampleModel/DataBuffer combination. * @throws RasterFormatException if the number of bands is greater than * the number of banks in this sample model. - * @throws IllegalArgumentException if {@code dataType} is not - * one of the supported data types + * @throws IllegalArgumentException if the number of bands is not greater than 0 + * @throws ArrayIndexOutOfBoundsException if any of the bank indices is out of bounds */ public SampleModel createSubsetSampleModel(int[] bands) { if (bands.length > bankIndices.length) diff --git a/test/jdk/java/awt/image/BandedSampleModel/BSMCreateCompatibleSMTest.java b/test/jdk/java/awt/image/BandedSampleModel/BSMCreateCompatibleSMTest.java new file mode 100644 index 00000000000..a47659300e8 --- /dev/null +++ b/test/jdk/java/awt/image/BandedSampleModel/BSMCreateCompatibleSMTest.java @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 6453640 + * @summary Verify BandedSampleModel.createCompatibleSampleModel + * and createSubsetSampleModel behaviour + * @run main BSMCreateCompatibleSMTest + */ + +import java.awt.image.BandedSampleModel; +import java.awt.image.DataBuffer; +import java.awt.image.RasterFormatException; + +public class BSMCreateCompatibleSMTest { + + public static void main(String[] args) { + + // These should all be OK + BandedSampleModel bsm = new BandedSampleModel(DataBuffer.TYPE_BYTE, 1, 1, 1); + bsm.createCompatibleSampleModel(20_000, 20_000); + int[] bands = { 0 } ; + bsm.createSubsetSampleModel(bands); + + // These should all throw an exception + try { + bsm.createCompatibleSampleModel(-1, 1); + throw new RuntimeException("No exception for illegal w"); + } catch (IllegalArgumentException e) { + System.out.println(e); + } + + try { + bsm.createCompatibleSampleModel(1, 0); + throw new RuntimeException("No exception for illegal h"); + } catch (IllegalArgumentException e) { + System.out.println(e); + } + + try { + bsm.createCompatibleSampleModel(-1, -1); + throw new RuntimeException("No exception for illegal w+h"); + } catch (IllegalArgumentException e) { + System.out.println(e); + } + + try { + bsm.createCompatibleSampleModel(50_000, 50_000); + throw new RuntimeException("No exception for too large dims"); + } catch (IllegalArgumentException e) { + System.out.println(e); + } + + try { + int[] bands0 = { } ; + bsm.createSubsetSampleModel(bands0); + throw new RuntimeException("No exception for empty bands[]"); + } catch (IllegalArgumentException e) { + System.out.println(e); + } + + try { + int[] bands1 = { 1 } ; + bsm.createSubsetSampleModel(bands1); + throw new RuntimeException("No exception for out of bounds band"); + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println(e); + } + + try { + int[] bands2 = { 0, 0 } ; + bsm.createSubsetSampleModel(bands2); + throw new RuntimeException("No exception for too many bands"); + } catch (RasterFormatException e) { + System.out.println(e); + } + } + +}