1 /*
   2  * Copyright (c) 2017, 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     8152561
  27  * @summary Test verifies whether all boundary conditions are checked
  28  *          properly in TIFFField.createArrayForType().
  29  * @run     main TIFFCreateArrayForTypeTest
  30  */
  31 
  32 import javax.imageio.plugins.tiff.TIFFField;
  33 import javax.imageio.plugins.tiff.TIFFTag;
  34 
  35 public class TIFFCreateArrayForTypeTest {
  36 
  37     static int count = 0;
  38     static boolean unknownDataType, negativeCount, zeroCount, countNotOne;
  39     static String errorMsg = "";
  40 
  41     private static void testCase1() {
  42         // check passing unknown data type to createArrayForType()
  43         count = 2;
  44         int dataType = 15;
  45         try {
  46             TIFFField.createArrayForType(dataType, count);
  47         } catch (IllegalArgumentException e) {
  48             unknownDataType = true;
  49         } catch (Exception e) {
  50             // just consume if it throws any other exception.
  51         }
  52         if (!unknownDataType) {
  53             errorMsg = errorMsg + "testCase1 ";
  54         }
  55     }
  56 
  57     private static void testCase2() {
  58         // check passing negative count value for createArrayForType()
  59         count = -1;
  60         try {
  61             TIFFField.createArrayForType(TIFFTag.TIFF_LONG, count);
  62         } catch (IllegalArgumentException e) {
  63             negativeCount = true;
  64         } catch (Exception e) {
  65             // just consume if it throws any other exception.
  66         }
  67         if (!negativeCount) {
  68             errorMsg = errorMsg + "testCase2 ";
  69         }
  70     }
  71 
  72     private static void testCase3() {
  73         /*
  74          * check passing zero count value for createArrayForType() with
  75          * TIFFTag.TIFF_RATIONAL or TIFFTag.TIFF_SRATIONAL data type.
  76          */
  77         count = 0;
  78         try {
  79             TIFFField.createArrayForType(TIFFTag.TIFF_RATIONAL, count);
  80         } catch (IllegalArgumentException e) {
  81             zeroCount = true;
  82         } catch (Exception e) {
  83             // just consume if it throws any other exception.
  84         }
  85         if (!zeroCount) {
  86             errorMsg = errorMsg + "testCase3 ";
  87         }
  88     }
  89 
  90     private static void testCase4() {
  91         /*
  92          * check passing count value other than 1 for createArrayForType() with
  93          * TIFFTag.TIFF_IFD_POINTER data type.
  94          */
  95         count = 2;
  96         try {
  97             TIFFField.createArrayForType(TIFFTag.TIFF_IFD_POINTER, count);
  98         } catch (IllegalArgumentException e) {
  99             countNotOne = true;
 100         } catch (Exception e) {
 101             // just consume if it throws any other exception.
 102         }
 103         if (!countNotOne) {
 104             errorMsg = errorMsg + "testCase4 ";
 105         }
 106     }
 107 
 108     public static void main(String[] args) {
 109         /*
 110          * test different scenarios where TIFFField.createArrayForType()
 111          * is required to throw IllegalArgumentException.
 112          */
 113         testCase1();
 114         testCase2();
 115         testCase3();
 116         testCase4();
 117         if ((!unknownDataType) ||
 118             (!negativeCount) ||
 119             (!zeroCount) ||
 120             (!countNotOne))
 121         {
 122             throw new RuntimeException(errorMsg + "is/are not throwing"
 123                     + " required IllegalArgumentException");
 124         }
 125     }
 126 }
 127