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     7107905
  27  * @summary Test verifies whether equals() and hashCode() methods in
  28  *          IndexColorModel works properly for IndexColorModel unique
  29  *          properties.
  30  * @run     main IndexColorModelEqualsTest
  31  */
  32 
  33 import java.awt.image.DataBuffer;
  34 import java.awt.image.IndexColorModel;
  35 import java.math.BigInteger;
  36 
  37 public class IndexColorModelEqualsTest {
  38 
  39     private static void compareIndexColorModels(IndexColorModel m1,
  40                                                 IndexColorModel m2) {
  41         if (m1.equals(null)) {
  42             throw new RuntimeException("equals(null) returns true");
  43         }
  44         if (!(m1.equals(m2))) {
  45             throw new RuntimeException("equals() method is not working"
  46                     + " properly");
  47         }
  48         if (!(m2.equals(m1))) {
  49             throw new RuntimeException("equals() method is not working"
  50                     + " properly");
  51         }
  52         if (m1.hashCode() != m2.hashCode()) {
  53             throw new RuntimeException("HashCode is not same for same"
  54                     + " IndexColorModels");
  55         }
  56     }
  57 
  58     private static void testColorMapEquality() {
  59         // test with different cmap values.
  60         IndexColorModel model1 = new IndexColorModel(8, 3, new int[] {1, 2, 3},
  61                 0, true, -1, DataBuffer.TYPE_BYTE);
  62         IndexColorModel model2 = new IndexColorModel(8, 3, new int[] {4, 5, 6},
  63                 0, true, -1, DataBuffer.TYPE_BYTE);
  64         if (model1.equals(model2)) {
  65             throw new RuntimeException("equals() method is determining"
  66                     + " ColorMap equality improperly");
  67         }
  68         if (model2.equals(model1)) {
  69             throw new RuntimeException("equals() method is determining"
  70                     + " ColorMap equality improperly");
  71         }
  72     }
  73 
  74     private static void testSizeEquality() {
  75         // test with different size for cmap.
  76         IndexColorModel model1 = new IndexColorModel(8, 4,
  77                 new int[] {1, 2, 3, 4},
  78                 0, true, -1, DataBuffer.TYPE_BYTE);
  79         IndexColorModel model2 = new IndexColorModel(8, 3,
  80                 new int[] {1, 2, 3},
  81                 0, true, -1, DataBuffer.TYPE_BYTE);
  82         if (model1.equals(model2)) {
  83             throw new RuntimeException("equals() method is determining"
  84                     + " Map size equality improperly");
  85         }
  86         if (model2.equals(model1)) {
  87             throw new RuntimeException("equals() method is determining"
  88                     + " Map size equality improperly");
  89         }
  90     }
  91 
  92     private static void testTransparentIndexEquality() {
  93         // test with different values for transparent_index.
  94         IndexColorModel model1 = new IndexColorModel(8, 3, new int[] {1, 2, 3},
  95                 0, true, 1, DataBuffer.TYPE_BYTE);
  96         IndexColorModel model2 = new IndexColorModel(8, 3, new int[] {1, 2, 3},
  97                 0, true, 2, DataBuffer.TYPE_BYTE);
  98         if (model1.equals(model2)) {
  99             throw new RuntimeException("equals() method is determining"
 100                     + " TransparentIndex equality improperly");
 101         }
 102         if (model2.equals(model1)) {
 103             throw new RuntimeException("equals() method is determining"
 104                     + " TransparentIndex equality improperly");
 105         }
 106     }
 107 
 108     private static void testValidPixelsEquality() {
 109         // test with different valid pixels.
 110         /*
 111          * In setRGBs() function of IndexColorModel we override
 112          * transparent_index value to map to pixel value if alpha is 0x00
 113          * so we should have atleast minimum alpha value to verify
 114          * equality of validBits thats why we have color value as
 115          * 16777216(2 ^ 24).
 116          */
 117         int color = 16777216;
 118         IndexColorModel model1 = new IndexColorModel(8, 3, new int[] {color,
 119                 color, color}, 0, DataBuffer.TYPE_BYTE, new BigInteger("1"));
 120         IndexColorModel model2 = new IndexColorModel(8, 3, new int[] {color,
 121                 color, color}, 0, DataBuffer.TYPE_BYTE, new BigInteger("2"));
 122         if (model1.equals(model2)) {
 123             throw new RuntimeException("equals() method is determining"
 124                     + " Valid pixels equality improperly");
 125         }
 126         if (model2.equals(model1)) {
 127             throw new RuntimeException("equals() method is determining"
 128                     + " Valid pixels equality improperly");
 129         }
 130     }
 131 
 132     private static void testConstructor1() {
 133         /*
 134          * verify equality with constructor
 135          * IndexColorModel(int bits, int size, byte[] r, byte[] g, byte[] b)
 136          */
 137         IndexColorModel model1 = new IndexColorModel(8, 2,
 138                 new byte[] {1, 2}, new byte[] {1, 2}, new byte[] {1, 2});
 139         IndexColorModel model2 = new IndexColorModel(8, 2,
 140                 new byte[] {1, 2}, new byte[] {1, 2}, new byte[] {1, 2});
 141         compareIndexColorModels(model1, model2);
 142     }
 143 
 144     private static void testConstructor2() {
 145         /*
 146          * verify equality with constructor
 147          * IndexColorModel(int bits, int size, byte[] r, byte[] g, byte[] b,
 148          * byte[] a)
 149          */
 150         IndexColorModel model1 = new IndexColorModel(8, 2, new byte[] {1, 2},
 151                 new byte[] {1, 2}, new byte[] {1, 2}, new byte[] {1, 2});
 152         IndexColorModel model2 = new IndexColorModel(8, 2, new byte[] {1, 2},
 153                 new byte[] {1, 2}, new byte[] {1, 2}, new byte[] {1, 2});
 154         compareIndexColorModels(model1, model2);
 155     }
 156 
 157     private static void testConstructor3() {
 158         /*
 159          * verify equality with constructor
 160          * IndexColorModel(int bits, int size, byte[] r, byte[] g, byte[] b,
 161          * int trans)
 162          */
 163         IndexColorModel model1 = new IndexColorModel(8, 2, new byte[] {1, 2},
 164                 new byte[] {1, 2}, new byte[] {1, 2}, 1);
 165         IndexColorModel model2 = new IndexColorModel(8, 2, new byte[] {1, 2},
 166                 new byte[] {1, 2}, new byte[] {1, 2}, 1);
 167         compareIndexColorModels(model1, model2);
 168     }
 169 
 170     private static void testConstructor4() {
 171         /*
 172          * verify equality with constructor
 173          * IndexColorModel(int bits, int size, byte[] cmap, int start,
 174          * boolean hasalpha)
 175          */
 176         IndexColorModel model1 = new IndexColorModel(8, 1,
 177                 new byte[] {1, 2, 3, 4}, 0, true);
 178         IndexColorModel model2 = new IndexColorModel(8, 1,
 179                 new byte[] {1, 2, 3, 4}, 0, true);
 180         compareIndexColorModels(model1, model2);
 181     }
 182 
 183     private static void testConstructor5() {
 184         /*
 185          * verify equality with constructor
 186          * IndexColorModel(int bits, int size, byte[] cmap, int start,
 187          * boolean hasalpha, int trans)
 188          */
 189         IndexColorModel model1 = new IndexColorModel(8, 1,
 190                 new byte[] {1, 2, 3, 4}, 0, true, 0);
 191         IndexColorModel model2 = new IndexColorModel(8, 1,
 192                 new byte[] {1, 2, 3, 4}, 0, true, 0);
 193         compareIndexColorModels(model1, model2);
 194     }
 195 
 196     private static void testConstructor6() {
 197         /*
 198          * verify equality with constructor
 199          * IndexColorModel(int bits, int size, int[] cmap, int start,
 200          * boolean hasalpha, int trans, int transferType)
 201          */
 202         IndexColorModel model1 = new IndexColorModel(8, 3, new int[] {1, 2, 3},
 203                 0, true, -1, DataBuffer.TYPE_BYTE);
 204         IndexColorModel model2 = new IndexColorModel(8, 3, new int[] {1, 2, 3},
 205                 0, true, -1, DataBuffer.TYPE_BYTE);
 206         compareIndexColorModels(model1, model2);
 207     }
 208 
 209     private static void testConstructor7() {
 210         /*
 211          * verify equality with constructor
 212          * IndexColorModel(int bits, int size, int[] cmap, int start,
 213          * int transferType, BigInteger validBits)
 214          */
 215         /*
 216          * In setRGBs() function of IndexColorModel we override
 217          * transparent_index value to map to pixel value if alpha is 0x00
 218          * so we should have atleast minimum alpha value to keep
 219          * both model1 and model2 same.
 220          */
 221         int color = 16777216;
 222         IndexColorModel model1 = new IndexColorModel(8, 3, new int[] {color,
 223                 color, color}, 0, DataBuffer.TYPE_BYTE, new BigInteger("1"));
 224         IndexColorModel model2 = new IndexColorModel(8, 3, new int[] {color,
 225                 color, color}, 0, DataBuffer.TYPE_BYTE, new BigInteger("1"));
 226         compareIndexColorModels(model1, model2);
 227     }
 228 
 229     private static void testSameIndexColorModel() {
 230         testConstructor1();
 231         testConstructor2();
 232         testConstructor3();
 233         testConstructor4();
 234         testConstructor5();
 235         testConstructor6();
 236         testConstructor7();
 237     }
 238     public static void main(String[] args) {
 239         /* test whether equals() method works properly for parameters
 240          * unique to IndexColorModel.
 241          */
 242         testColorMapEquality();
 243         testSizeEquality();
 244         testTransparentIndexEquality();
 245         testValidPixelsEquality();
 246         // verify same IndexColorModel equality using different constructors.
 247         testSameIndexColorModel();
 248     }
 249 }
 250