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     8153943
  27  * @summary Test verifies whether equals() and hashCode() methods in
  28  *          PackedColorModel works properly.
  29  * @run     main PackedColorModelEqualsTest
  30  */
  31 
  32 import java.awt.color.ColorSpace;
  33 import java.awt.image.DataBuffer;
  34 import java.awt.image.DirectColorModel;
  35 
  36 public class PackedColorModelEqualsTest {
  37 
  38     private static void testMaskArrayEquality() {
  39         /*
  40          * Test with different maskArray values, since PackedColorModel
  41          * is abstract we use subclass DirectColorModel.
  42          */
  43         DirectColorModel model1 =
  44             new DirectColorModel(24, 0x00FF0000, 0x0000FF00, 0x000000FF);
  45         DirectColorModel model2 =
  46             new DirectColorModel(24, 0x000000FF, 0x0000FF00, 0x00FF0000);
  47         if (model1.equals(model2)) {
  48             throw new RuntimeException("equals() method is determining"
  49                     + " ColorMap equality improperly");
  50         }
  51         if (model2.equals(model1)) {
  52             throw new RuntimeException("equals() method is determining"
  53                     + " ColorMap equality improperly");
  54         }
  55     }
  56 
  57     private static void testConstructor1() {
  58         /*
  59          * verify equality with constructor
  60          * DirectColorModel(int bits, int rmask, int gmask, int bmask,
  61          *    int amask)
  62          */
  63         DirectColorModel model1 =
  64             new DirectColorModel(32, 0xFF000000, 0x00FF0000,
  65                     0x0000FF00, 0x000000FF);
  66         DirectColorModel model2 =
  67             new DirectColorModel(32, 0xFF000000, 0x00FF0000,
  68                     0x0000FF00, 0x000000FF);
  69         if (model1.equals(null)) {
  70             throw new RuntimeException("equals(null) returns true");
  71         }
  72         if (!(model1.equals(model2))) {
  73             throw new RuntimeException("equals() method is not working"
  74                     + " properly");
  75         }
  76         if (!(model2.equals(model1))) {
  77             throw new RuntimeException("equals() method is not working"
  78                     + " properly");
  79         }
  80         if (model1.hashCode() != model2.hashCode()) {
  81             throw new RuntimeException("HashCode is not same for same"
  82                     + " PackedColorModels");
  83         }
  84     }
  85 
  86     private static void testConstructor2() {
  87         /*
  88          * verify equality with constructor
  89          * DirectColorModel(ColorSpace space, int bits, int rmask, int gmask,
  90          * int bmask, int amask, boolean isAlphaPremultiplied, int transferType)
  91          */
  92         DirectColorModel model1 =
  93             new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
  94                     32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF,
  95                     false, DataBuffer.TYPE_BYTE);
  96         DirectColorModel model2 =
  97             new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
  98                     32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF,
  99                     false, DataBuffer.TYPE_BYTE);
 100         if (model1.equals(null)) {
 101             throw new RuntimeException("equals(null) returns true");
 102         }
 103         if (!(model1.equals(model2))) {
 104             throw new RuntimeException("equals() method is not working"
 105                     + " properly");
 106         }
 107         if (!(model2.equals(model1))) {
 108             throw new RuntimeException("equals() method is not working"
 109                     + " properly");
 110         }
 111         if (model1.hashCode() != model2.hashCode()) {
 112             throw new RuntimeException("HashCode is not same for same"
 113                     + " PackedColorModels");
 114         }
 115     }
 116 
 117     private static void testSamePackedColorModel() {
 118         testConstructor1();
 119         testConstructor2();
 120     }
 121     public static void main(String[] args) {
 122         // test with different mask array.
 123         testMaskArrayEquality();
 124         // verify PackedColorModel equality using different constructors.
 125         testSamePackedColorModel();
 126     }
 127 }