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 testConstructor1() {
  40         /*
  41          * verify equality with constructor
  42          * ComponentColorModel(ColorSpace colorSpace,
  43          *                  int[] bits,
  44          *                  boolean hasAlpha,
  45          *                  boolean isAlphaPremultiplied,
  46          *                  int transparency,
  47          *                  int transferType)
  48          */
  49         ComponentColorModel model1 =
  50             new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
  51                                     new int[] {8, 8, 8},
  52                                     false,
  53                                     false,
  54                                     Transparency.OPAQUE,
  55                                     DataBuffer.TYPE_BYTE);
  56         ComponentColorModel model2 =
  57             new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
  58                                     new int[] {8, 8, 8},
  59                                     false,
  60                                     false,
  61                                     Transparency.OPAQUE,
  62                                     DataBuffer.TYPE_BYTE);
  63         if (model1.equals(null)) {
  64             throw new RuntimeException("equals(null) returns true");
  65         }
  66         if (!(model1.equals(model2))) {
  67             throw new RuntimeException("equals() method is not working"
  68                     + " properly");
  69         }
  70         if (!(model2.equals(model1))) {
  71             throw new RuntimeException("equals() method is not working"
  72                     + " properly");
  73         }
  74         if (model1.hashCode() != model2.hashCode()) {
  75             throw new RuntimeException("HashCode is not same for same"
  76                     + " ComponentColorModels");
  77         }
  78     }
  79 
  80     private static void testConstructor2() {
  81         /*
  82          * verify equality with constructor
  83          * ComponentColorModel(ColorSpace colorSpace,
  84          *                  boolean hasAlpha,
  85          *                  boolean isAlphaPremultiplied,
  86          *                  int transparency,
  87          *                  int transferType)
  88          */
  89         ComponentColorModel model1 =
  90             new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
  91                                     false,
  92                                     false,
  93                                     Transparency.OPAQUE,
  94                                     DataBuffer.TYPE_BYTE);
  95         ComponentColorModel model2 =
  96             new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
  97                                     false,
  98                                     false,
  99                                     Transparency.OPAQUE,
 100                                     DataBuffer.TYPE_BYTE);
 101         if (model1.equals(null)) {
 102             throw new RuntimeException("equals(null) returns true");
 103         }
 104         if (!(model1.equals(model2))) {
 105             throw new RuntimeException("equals() method is not working"
 106                     + " properly");
 107         }
 108         if (!(model2.equals(model1))) {
 109             throw new RuntimeException("equals() method is not working"
 110                     + " properly");
 111         }
 112         if (model1.hashCode() != model2.hashCode()) {
 113             throw new RuntimeException("HashCode is not same for same"
 114                     + " ComponentColorModels");
 115         }
 116     }
 117 
 118     private static void testSameComponentColorModel() {
 119         testConstructor1();
 120         testConstructor2();
 121     }
 122     public static void main(String[] args) {
 123         // verify ComponentColorModel equality using different constructors.
 124         testSameComponentColorModel();
 125     }
 126 }
 127