1 /*
   2  * Copyright (c) 2016, 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 testColorMapEquality() {
  40         // test with different cmap values.
  41         IndexColorModel model1 = new IndexColorModel(8, 3, new int[] {1, 2, 3},
  42                 0, true, -1, DataBuffer.TYPE_BYTE);
  43         IndexColorModel model2 = new IndexColorModel(8, 3, new int[] {4, 5, 6},
  44                 0, true, -1, DataBuffer.TYPE_BYTE);
  45         if (model1.equals(model2)) {
  46             throw new RuntimeException("equals() method is determining"
  47                     + " ColorMap equality improperly");
  48         }
  49         if (model2.equals(model1)) {
  50             throw new RuntimeException("equals() method is determining"
  51                     + " ColorMap equality improperly");
  52         }
  53     }
  54 
  55     private static void testSizeEquality() {
  56         // test with different size for cmap.
  57         IndexColorModel model1 = new IndexColorModel(8, 4,
  58                 new int[] {1, 2, 3, 4},
  59                 0, true, -1, DataBuffer.TYPE_BYTE);
  60         IndexColorModel model2 = new IndexColorModel(8, 3,
  61                 new int[] {1, 2, 3},
  62                 0, true, -1, DataBuffer.TYPE_BYTE);
  63         if (model1.equals(model2)) {
  64             throw new RuntimeException("equals() method is determining"
  65                     + " Map size equality improperly");
  66         }
  67         if (model2.equals(model1)) {
  68             throw new RuntimeException("equals() method is determining"
  69                     + " Map size equality improperly");
  70         }
  71     }
  72 
  73     private static void testTransparentIndexEquality() {
  74         // test with different values for transparent_index.
  75         IndexColorModel model1 = new IndexColorModel(8, 3, new int[] {1, 2, 3},
  76                 0, true, 1, DataBuffer.TYPE_BYTE);
  77         IndexColorModel model2 = new IndexColorModel(8, 3, new int[] {1, 2, 3},
  78                 0, true, 2, DataBuffer.TYPE_BYTE);
  79         if (model1.equals(model2)) {
  80             throw new RuntimeException("equals() method is determining"
  81                     + " TransparentIndex equality improperly");
  82         }
  83         if (model2.equals(model1)) {
  84             throw new RuntimeException("equals() method is determining"
  85                     + " TransparentIndex equality improperly");
  86         }
  87     }
  88 
  89     private static void testValidPixelsEquality() {
  90         // test with different valid pixels.
  91         /*
  92          * In setRGBs() function of IndexColorModel we override
  93          * transparent_index value to map to pixel value if alpha is 0x00
  94          * so we should have atleast minimum alpha value to verify
  95          * equality of validBits thats why we have color value as
  96          * 16777216(2 ^ 24).
  97          */
  98         int color = 16777216;
  99         IndexColorModel model1 = new IndexColorModel(8, 3, new int[] {color,
 100                 color, color}, 0, DataBuffer.TYPE_BYTE, new BigInteger("1"));
 101         IndexColorModel model2 = new IndexColorModel(8, 3, new int[] {color,
 102                 color, color}, 0, DataBuffer.TYPE_BYTE, new BigInteger("2"));
 103         if (model1.equals(model2)) {
 104             throw new RuntimeException("equals() method is determining"
 105                     + " Valid pixels equality improperly");
 106         }
 107         if (model2.equals(model1)) {
 108             throw new RuntimeException("equals() method is determining"
 109                     + " Valid pixels equality improperly");
 110         }
 111     }
 112 
 113     private static void testConstructor1() {
 114         /*
 115          * verify equality with constructor
 116          * IndexColorModel(int bits, int size, byte[] r, byte[] g, byte[] b)
 117          */
 118         IndexColorModel model1 = new IndexColorModel(8, 2,
 119                 new byte[] {1, 2}, new byte[] {1, 2}, new byte[] {1, 2});
 120         IndexColorModel model2 = new IndexColorModel(8, 2,
 121                 new byte[] {1, 2}, new byte[] {1, 2}, new byte[] {1, 2});
 122         if (model1.equals(null)) {
 123             throw new RuntimeException("equals(null) returns true");
 124         }
 125         if (!(model1.equals(model2))) {
 126             throw new RuntimeException("equals() method is not working"
 127                     + " properly");
 128         }
 129         if (!(model2.equals(model1))) {
 130             throw new RuntimeException("equals() method is not working"
 131                     + " properly");
 132         }
 133         if (model1.hashCode() != model2.hashCode()) {
 134             throw new RuntimeException("HashCode is not same for same"
 135                     + " IndexColorModels");
 136         }
 137     }
 138 
 139     private static void testConstructor2() {
 140         /*
 141          * verify equality with constructor
 142          * IndexColorModel(int bits, int size, byte[] r, byte[] g, byte[] b,
 143          * byte[] a)
 144          */
 145         IndexColorModel model1 = new IndexColorModel(8, 2, new byte[] {1, 2},
 146                 new byte[] {1, 2}, new byte[] {1, 2}, new byte[] {1, 2});
 147         IndexColorModel model2 = new IndexColorModel(8, 2, new byte[] {1, 2},
 148                 new byte[] {1, 2}, new byte[] {1, 2}, new byte[] {1, 2});
 149         if (model1.equals(null)) {
 150             throw new RuntimeException("equals(null) returns true");
 151         }
 152         if (!(model1.equals(model2))) {
 153             throw new RuntimeException("equals() method is not working"
 154                     + " properly");
 155         }
 156         if (!(model2.equals(model1))) {
 157             throw new RuntimeException("equals() method is not working"
 158                     + " properly");
 159         }
 160         if (model1.hashCode() != model2.hashCode()) {
 161             throw new RuntimeException("HashCode is not same for same"
 162                     + " IndexColorModels");
 163         }
 164     }
 165 
 166     private static void testConstructor3() {
 167         /*
 168          * verify equality with constructor
 169          * IndexColorModel(int bits, int size, byte[] r, byte[] g, byte[] b,
 170          * int trans)
 171          */
 172         IndexColorModel model1 = new IndexColorModel(8, 2, new byte[] {1, 2},
 173                 new byte[] {1, 2}, new byte[] {1, 2}, 1);
 174         IndexColorModel model2 = new IndexColorModel(8, 2, new byte[] {1, 2},
 175                 new byte[] {1, 2}, new byte[] {1, 2}, 1);
 176         if (model1.equals(null)) {
 177             throw new RuntimeException("equals(null) returns true");
 178         }
 179         if (!(model1.equals(model2))) {
 180             throw new RuntimeException("equals() method is not working"
 181                     + " properly");
 182         }
 183         if (!(model2.equals(model1))) {
 184             throw new RuntimeException("equals() method is not working"
 185                     + " properly");
 186         }
 187         if (model1.hashCode() != model2.hashCode()) {
 188             throw new RuntimeException("HashCode is not same for same"
 189                     + " IndexColorModels");
 190         }
 191     }
 192 
 193     private static void testConstructor4() {
 194         /*
 195          * verify equality with constructor
 196          * IndexColorModel(int bits, int size, byte[] cmap, int start,
 197          * boolean hasalpha)
 198          */
 199         IndexColorModel model1 = new IndexColorModel(8, 1,
 200                 new byte[] {1, 2, 3, 4}, 0, true);
 201         IndexColorModel model2 = new IndexColorModel(8, 1,
 202                 new byte[] {1, 2, 3, 4}, 0, true);
 203         if (model1.equals(null)) {
 204             throw new RuntimeException("equals(null) returns true");
 205         }
 206         if (!(model1.equals(model2))) {
 207             throw new RuntimeException("equals() method is not working"
 208                     + " properly");
 209         }
 210         if (!(model2.equals(model1))) {
 211             throw new RuntimeException("equals() method is not working"
 212                     + " properly");
 213         }
 214         if (model1.hashCode() != model2.hashCode()) {
 215             throw new RuntimeException("HashCode is not same for same"
 216                     + " IndexColorModels");
 217         }
 218     }
 219 
 220     private static void testConstructor5() {
 221         /*
 222          * verify equality with constructor
 223          * IndexColorModel(int bits, int size, byte[] cmap, int start,
 224          * boolean hasalpha, int trans)
 225          */
 226         IndexColorModel model1 = new IndexColorModel(8, 1,
 227                 new byte[] {1, 2, 3, 4}, 0, true, 0);
 228         IndexColorModel model2 = new IndexColorModel(8, 1,
 229                 new byte[] {1, 2, 3, 4}, 0, true, 0);
 230         if (model1.equals(null)) {
 231             throw new RuntimeException("equals(null) returns true");
 232         }
 233         if (!(model1.equals(model2))) {
 234             throw new RuntimeException("equals() method is not working"
 235                     + " properly");
 236         }
 237         if (!(model2.equals(model1))) {
 238             throw new RuntimeException("equals() method is not working"
 239                     + " properly");
 240         }
 241         if (model1.hashCode() != model2.hashCode()) {
 242             throw new RuntimeException("HashCode is not same for same"
 243                     + " IndexColorModels");
 244         }
 245     }
 246 
 247     private static void testConstructor6() {
 248         /*
 249          * verify equality with constructor
 250          * IndexColorModel(int bits, int size, int[] cmap, int start,
 251          * boolean hasalpha, int trans, int transferType)
 252          */
 253         IndexColorModel model1 = new IndexColorModel(8, 3, new int[] {1, 2, 3},
 254                 0, true, -1, DataBuffer.TYPE_BYTE);
 255         IndexColorModel model2 = new IndexColorModel(8, 3, new int[] {1, 2, 3},
 256                 0, true, -1, DataBuffer.TYPE_BYTE);
 257         if (model1.equals(null)) {
 258             throw new RuntimeException("equals(null) returns true");
 259         }
 260         if (!(model1.equals(model2))) {
 261             throw new RuntimeException("equals() method is not working"
 262                     + " properly");
 263         }
 264         if (!(model2.equals(model1))) {
 265             throw new RuntimeException("equals() method is not working"
 266                     + " properly");
 267         }
 268         if (model1.hashCode() != model2.hashCode()) {
 269             throw new RuntimeException("HashCode is not same for same"
 270                     + " IndexColorModels");
 271         }
 272     }
 273 
 274     private static void testConstructor7() {
 275         /*
 276          * verify equality with constructor
 277          * IndexColorModel(int bits, int size, int[] cmap, int start,
 278          * int transferType, BigInteger validBits)
 279          */
 280         /*
 281          * In setRGBs() function of IndexColorModel we override
 282          * transparent_index value to map to pixel value if alpha is 0x00
 283          * so we should have atleast minimum alpha value to keep
 284          * both model1 and model2 same.
 285          */
 286         int color = 16777216;
 287         IndexColorModel model1 = new IndexColorModel(8, 3, new int[] {color,
 288                 color, color}, 0, DataBuffer.TYPE_BYTE, new BigInteger("1"));
 289         IndexColorModel model2 = new IndexColorModel(8, 3, new int[] {color,
 290                 color, color}, 0, DataBuffer.TYPE_BYTE, new BigInteger("1"));
 291         if (model1.equals(null)) {
 292             throw new RuntimeException("equals(null) returns true");
 293         }
 294         if (!(model1.equals(model2))) {
 295             throw new RuntimeException("equals() method is not working"
 296                     + " properly");
 297         }
 298         if (!(model2.equals(model1))) {
 299             throw new RuntimeException("equals() method is not working"
 300                     + " properly");
 301         }
 302         if (model1.hashCode() != model2.hashCode()) {
 303             throw new RuntimeException("HashCode is not same for same"
 304                     + " IndexColorModels");
 305         }
 306     }
 307 
 308     private static void testSameIndexColorModel() {
 309         testConstructor1();
 310         testConstructor2();
 311         testConstructor3();
 312         testConstructor4();
 313         testConstructor5();
 314         testConstructor6();
 315         testConstructor7();
 316     }
 317     public static void main(String[] args) {
 318         /* test whether equals() method works properly for parameters
 319          * unique to IndexColorModel.
 320          */
 321         testColorMapEquality();
 322         testSizeEquality();
 323         testTransparentIndexEquality();
 324         testValidPixelsEquality();
 325         // verify same IndexColorModel equality using different constructors.
 326         testSameIndexColorModel();
 327     }
 328 }