1 /*
   2  * Copyright (c) 2020, 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 package datatype;
  25 
  26 import javax.xml.datatype.DatatypeFactory;
  27 import javax.xml.datatype.XMLGregorianCalendar;
  28 import org.testng.annotations.Test;
  29 import org.testng.Assert;
  30 import org.testng.annotations.DataProvider;
  31 
  32 /*
  33  * @test
  34  * @bug 8246816
  35  * @run testng datatype.HashCodeTest
  36  * @summary Test hashCode generation.
  37  */
  38 public class HashCodeTest {
  39     /*
  40        DataProvider: for testHashCode
  41        Data: datetime1, datetime1, flag indicating if their hashCodes are equal
  42      */
  43     @DataProvider(name = "testHashCode")
  44     public Object[][] getData() {
  45 
  46         return new Object[][]{
  47             // the reported case: identical hash codes before the patch
  48             {"2020-04-24T12:53:00+02:00", "2020-06-04T06:58:17.727Z", false},
  49             {"1002000-01-15T12:00:00-05:00", "2002000-01-15T13:00:00-04:00", false},
  50             {"2000-01-15T12:00:59.99999-05:00", "2000-01-15T13:00:59-04:00", false},
  51             {"2000-01-15T12:00:00-05:00", "2000-01-15T13:00:00-04:00", true},
  52             {"2000-01-15T12:00:59.99999-05:00", "2000-01-15T13:00:59.99999-04:00", true},
  53             {"999999999-01-15T12:00:00-05:00", "999999999-01-15T13:00:00-04:00", true},
  54             {"999999999-01-15T12:00:59.99999-05:00", "999999999-01-15T13:00:59.99999-04:00", true},
  55         };
  56     }
  57 
  58     @Test(dataProvider = "testHashCode")
  59     public final void testHashCode(String dt1, String dt2, boolean equal) throws Exception {
  60         DatatypeFactory dataTypeFactory = DatatypeFactory.newInstance();
  61         XMLGregorianCalendar cal1 = dataTypeFactory.newXMLGregorianCalendar(dt1);
  62         XMLGregorianCalendar cal2 = dataTypeFactory.newXMLGregorianCalendar(dt2);
  63 
  64         // identical hash codes before the patch
  65         int hashCode1 = cal1.hashCode();
  66         int hashCode2 = cal2.hashCode();
  67 
  68         if (equal) {
  69             Assert.assertTrue(cal1.equals(cal2));
  70             Assert.assertEquals(hashCode1, hashCode2);
  71         } else {
  72             Assert.assertFalse(cal1.equals(cal2));
  73             Assert.assertNotEquals(hashCode1, hashCode2);
  74         }
  75     }
  76 }