jdk/test/java/awt/image/GetSamplesTest.java
changeset 6631 8bb1126f01d3
child 8937 38ffd0c8fd2b
equal deleted inserted replaced
6605:f960f117f162 6631:8bb1126f01d3
       
     1 /*
       
     2  * Copyright (c) 2010, 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  * @bug     6735275
       
    27  * @summary Test verifies that SampleModel.getSamples() throws an appropriate
       
    28  *           exception if coordinates are not in bounds.
       
    29  *
       
    30  * @run     main GetSamplesTest
       
    31  */
       
    32 
       
    33 import java.awt.image.BandedSampleModel;
       
    34 import java.awt.image.ComponentSampleModel;
       
    35 import java.awt.image.DataBuffer;
       
    36 import java.awt.image.MultiPixelPackedSampleModel;
       
    37 import java.awt.image.PixelInterleavedSampleModel;
       
    38 import java.awt.image.SampleModel;
       
    39 import java.awt.image.SinglePixelPackedSampleModel;
       
    40 import java.util.Vector;
       
    41 
       
    42 public class GetSamplesTest {
       
    43 
       
    44     public static int width = 100;
       
    45     public static int height = 100;
       
    46     public static int dataType = DataBuffer.TYPE_BYTE;
       
    47     public static int numBands = 4;
       
    48 
       
    49     public static void main(String[] args) {
       
    50         Vector<Class<? extends SampleModel>> classes = new Vector<Class<? extends SampleModel>>();
       
    51 
       
    52         classes.add(ComponentSampleModel.class);
       
    53         classes.add(MultiPixelPackedSampleModel.class);
       
    54         classes.add(SinglePixelPackedSampleModel.class);
       
    55         classes.add(BandedSampleModel.class);
       
    56         classes.add(PixelInterleavedSampleModel.class);
       
    57 
       
    58         for (Class<? extends SampleModel> c : classes) {
       
    59             doTest(c);
       
    60         }
       
    61     }
       
    62     private static void doTest(Class<? extends SampleModel> c) {
       
    63         System.out.println("Test for: " + c.getName());
       
    64         SampleModel sm = createSampleModel(c);
       
    65 
       
    66         DataBuffer db = sm.createDataBuffer();
       
    67 
       
    68         int[] iArray = new int[ width * height + numBands];
       
    69         float[] fArray = new float[ width * height + numBands];
       
    70         double[] dArray = new double[ width * height + numBands];
       
    71 
       
    72         boolean iOk = false;
       
    73         boolean fOk = false;
       
    74         boolean dOk = false;
       
    75 
       
    76         try {
       
    77             sm.getSamples(Integer.MAX_VALUE, 0, 1, 1, 0, iArray, db);
       
    78         } catch (ArrayIndexOutOfBoundsException e) {
       
    79             System.out.println(e.getMessage());
       
    80             iOk = true;
       
    81         }
       
    82 
       
    83         try {
       
    84             sm.getSamples(Integer.MAX_VALUE, 0, 1, 1, 0, fArray, db);
       
    85         } catch (ArrayIndexOutOfBoundsException e) {
       
    86             System.out.println(e.getMessage());
       
    87             fOk = true;
       
    88         }
       
    89 
       
    90         try {
       
    91             sm.getSamples(0, Integer.MAX_VALUE, 1, 1, 0, dArray, db);
       
    92         } catch (ArrayIndexOutOfBoundsException e) {
       
    93             System.out.println(e.getMessage());
       
    94             dOk = true;
       
    95         }
       
    96         if (!iOk || !fOk || !dOk) {
       
    97             throw new RuntimeException("Test for " + c.getSimpleName() +
       
    98                     " failed: iOk=" + iOk + "; fOk=" + fOk + "; dOk=" + dOk);
       
    99         }
       
   100     }
       
   101 
       
   102     private static SampleModel createSampleModel(Class<? extends SampleModel> cls) {
       
   103         SampleModel res = null;
       
   104 
       
   105         if (cls == ComponentSampleModel.class) {
       
   106             res = new ComponentSampleModel(dataType, width, height, 4, width * 4, new int[] { 0, 1, 2, 3 } );
       
   107         } else if (cls == MultiPixelPackedSampleModel.class) {
       
   108             res = new MultiPixelPackedSampleModel(dataType, width, height, 4);
       
   109         } else if (cls == SinglePixelPackedSampleModel.class) {
       
   110             res = new SinglePixelPackedSampleModel(dataType, width, height,
       
   111                     new int[]{ 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff });
       
   112         } else if (cls == BandedSampleModel.class) {
       
   113             res = new BandedSampleModel(dataType, width, height, numBands);
       
   114         } else if (cls == PixelInterleavedSampleModel.class) {
       
   115             res = new PixelInterleavedSampleModel(dataType, width, height, 4, width * 4, new int[] { 0, 1, 2, 3 });
       
   116         } else {
       
   117             throw new RuntimeException("Unknown class " + cls);
       
   118         }
       
   119         return res;
       
   120     }
       
   121 }