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  *          ComponentColorModel works properly.
  29  * @run     main ComponentColorModelEqualsTest
  30  */
  31 
  32 import java.awt.Transparency;
  33 import java.awt.color.ColorSpace;
  34 import java.awt.image.ComponentColorModel;
  35 import java.awt.image.DataBuffer;
  36 
  37 public class ComponentColorModelEqualsTest {
  38 
  39     private static void compareComponentColorModels(ComponentColorModel m1,
  40                                                     ComponentColorModel 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                     + " ComponentColorModels");
  55         }
  56     }
  57 
  58     private static void testConstructor1() {
  59         /*
  60          * verify equality with constructor
  61          * ComponentColorModel(ColorSpace colorSpace,
  62          *                  int[] bits,
  63          *                  boolean hasAlpha,
  64          *                  boolean isAlphaPremultiplied,
  65          *                  int transparency,
  66          *                  int transferType)
  67          */
  68         ComponentColorModel model1 =
  69             new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
  70                                     new int[] {8, 8, 8},
  71                                     false,
  72                                     false,
  73                                     Transparency.OPAQUE,
  74                                     DataBuffer.TYPE_BYTE);
  75         ComponentColorModel model2 =
  76             new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
  77                                     new int[] {8, 8, 8},
  78                                     false,
  79                                     false,
  80                                     Transparency.OPAQUE,
  81                                     DataBuffer.TYPE_BYTE);
  82         compareComponentColorModels(model1, model2);
  83     }
  84 
  85     private static void testConstructor2() {
  86         /*
  87          * verify equality with constructor
  88          * ComponentColorModel(ColorSpace colorSpace,
  89          *                  boolean hasAlpha,
  90          *                  boolean isAlphaPremultiplied,
  91          *                  int transparency,
  92          *                  int transferType)
  93          */
  94         ComponentColorModel model1 =
  95             new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
  96                                     false,
  97                                     false,
  98                                     Transparency.OPAQUE,
  99                                     DataBuffer.TYPE_BYTE);
 100         ComponentColorModel model2 =
 101             new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
 102                                     false,
 103                                     false,
 104                                     Transparency.OPAQUE,
 105                                     DataBuffer.TYPE_BYTE);
 106         compareComponentColorModels(model1, model2);
 107     }
 108 
 109     private static void testSameComponentColorModel() {
 110         testConstructor1();
 111         testConstructor2();
 112     }
 113     public static void main(String[] args) {
 114         // verify ComponentColorModel equality using different constructors.
 115         testSameComponentColorModel();
 116     }
 117 }
 118