GetSamplesTest.java revision 3811
6443N/A/*
6443N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
6443N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6443N/A *
6443N/A * This code is free software; you can redistribute it and/or modify it
6443N/A * under the terms of the GNU General Public License version 2 only, as
6443N/A * published by the Free Software Foundation.
6443N/A *
6443N/A * This code is distributed in the hope that it will be useful, but WITHOUT
6443N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6443N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6443N/A * version 2 for more details (a copy is included in the LICENSE file that
6443N/A * accompanied this code).
6443N/A *
6443N/A * You should have received a copy of the GNU General Public License version
6443N/A * 2 along with this work; if not, write to the Free Software Foundation,
6443N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6443N/A *
6443N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6443N/A * or visit www.oracle.com if you need additional information or have any
6443N/A * questions.
6443N/A */
7321N/A
6443N/A/*
6443N/A * @test
6443N/A * @bug 6735275 6993561
7255N/A * @summary Test verifies that SampleModel.getSamples() SampleModel.setSamples()
7255N/A * throw an appropriate exception if coordinates are not in bounds.
7255N/A *
7255N/A * @run main GetSamplesTest
7255N/A */
6443N/A
6443N/Aimport java.awt.image.BandedSampleModel;
6443N/Aimport java.awt.image.ComponentSampleModel;
6443N/Aimport java.awt.image.DataBuffer;
6443N/Aimport java.awt.image.MultiPixelPackedSampleModel;
6443N/Aimport java.awt.image.PixelInterleavedSampleModel;
6443N/Aimport java.awt.image.SampleModel;
6443N/Aimport java.awt.image.SinglePixelPackedSampleModel;
6443N/Aimport java.util.Vector;
6443N/A
6443N/Apublic class GetSamplesTest {
6466N/A
6443N/A public static int width = 100;
6443N/A public static int height = 100;
6443N/A public static int dataType = DataBuffer.TYPE_BYTE;
6443N/A public static int numBands = 4;
6443N/A
6443N/A public static void main(String[] args) {
6443N/A Vector<Class<? extends SampleModel>> classes = new Vector<Class<? extends SampleModel>>();
6443N/A
6443N/A classes.add(ComponentSampleModel.class);
6443N/A classes.add(MultiPixelPackedSampleModel.class);
6443N/A classes.add(SinglePixelPackedSampleModel.class);
6443N/A classes.add(BandedSampleModel.class);
6443N/A classes.add(PixelInterleavedSampleModel.class);
6443N/A
6443N/A for (Class<? extends SampleModel> c : classes) {
6443N/A doTest(c);
7255N/A }
7255N/A }
7255N/A private static void doTest(Class<? extends SampleModel> c) {
7255N/A System.out.println("Test for: " + c.getName());
7321N/A SampleModel sm = createSampleModel(c);
7255N/A
7255N/A DataBuffer db = sm.createDataBuffer();
7255N/A
7255N/A int[] iArray = new int[ width * height + numBands];
6443N/A float[] fArray = new float[ width * height + numBands];
6466N/A double[] dArray = new double[ width * height + numBands];
6443N/A
6443N/A boolean iOk = false;
6466N/A boolean fOk = false;
6443N/A boolean dOk = false;
6443N/A
6466N/A try {
6443N/A sm.getSamples(Integer.MAX_VALUE, 0, 1, 1, 0, iArray, db);
6443N/A sm.setSamples(Integer.MAX_VALUE, 0, 1, 1, 0, iArray, db);
6443N/A } catch (ArrayIndexOutOfBoundsException e) {
6443N/A System.out.println(e.getMessage());
6443N/A iOk = true;
6443N/A }
6443N/A
6443N/A try {
6443N/A sm.getSamples(Integer.MAX_VALUE, 0, 1, 1, 0, fArray, db);
6443N/A sm.setSamples(Integer.MAX_VALUE, 0, 1, 1, 0, fArray, db);
6443N/A } catch (ArrayIndexOutOfBoundsException e) {
6443N/A System.out.println(e.getMessage());
6443N/A fOk = true;
6443N/A }
6443N/A
6443N/A try {
6443N/A sm.getSamples(0, Integer.MAX_VALUE, 1, 1, 0, dArray, db);
6443N/A sm.setSamples(0, Integer.MAX_VALUE, 1, 1, 0, dArray, db);
6443N/A } catch (ArrayIndexOutOfBoundsException e) {
6443N/A System.out.println(e.getMessage());
6443N/A dOk = true;
6443N/A }
6443N/A if (!iOk || !fOk || !dOk) {
6443N/A throw new RuntimeException("Test for " + c.getSimpleName() +
6443N/A " failed: iOk=" + iOk + "; fOk=" + fOk + "; dOk=" + dOk);
6443N/A }
6443N/A }
6443N/A
6443N/A private static SampleModel createSampleModel(Class<? extends SampleModel> cls) {
6443N/A SampleModel res = null;
6443N/A
6443N/A if (cls == ComponentSampleModel.class) {
6443N/A res = new ComponentSampleModel(dataType, width, height, 4, width * 4, new int[] { 0, 1, 2, 3 } );
6443N/A } else if (cls == MultiPixelPackedSampleModel.class) {
6443N/A res = new MultiPixelPackedSampleModel(dataType, width, height, 4);
6443N/A } else if (cls == SinglePixelPackedSampleModel.class) {
6443N/A res = new SinglePixelPackedSampleModel(dataType, width, height,
6443N/A new int[]{ 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff });
6443N/A } else if (cls == BandedSampleModel.class) {
6443N/A res = new BandedSampleModel(dataType, width, height, numBands);
6443N/A } else if (cls == PixelInterleavedSampleModel.class) {
6443N/A res = new PixelInterleavedSampleModel(dataType, width, height, 4, width * 4, new int[] { 0, 1, 2, 3 });
6443N/A } else {
6443N/A throw new RuntimeException("Unknown class " + cls);
6443N/A }
6443N/A return res;
6443N/A }
6443N/A}
6443N/A