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