|
| 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, datetime2, 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 | + // a case mentioned in the dev note of hashCode() implementation |
| 50 | + {"2000-01-15T12:00:00-05:00", "2000-01-15T13:00:00-04:00", true}, |
| 51 | + /** |
| 52 | + * Comparing with a datetime that needs to be normalized. |
| 53 | + * Before the patch, XMLGregorianCalendarImpl called the normalizeToTimezone |
| 54 | + * method that will set UNDEFINED fractional second to zero. |
| 55 | + */ |
| 56 | + {"2000-01-01T03:19:04Z", "1999-12-31T23:49:04-03:30", true}, |
| 57 | + // another case mentioned in the javadoc of XMLGregorianCalendar::normalize() |
| 58 | + {"2000-03-04T23:00:00+03:00", "2000-03-04T20:00:00Z", true}, |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + @Test(dataProvider = "testHashCode") |
| 63 | + public final void testHashCode(String dt1, String dt2, boolean equal) throws Exception { |
| 64 | + DatatypeFactory dataTypeFactory = DatatypeFactory.newInstance(); |
| 65 | + XMLGregorianCalendar cal1 = dataTypeFactory.newXMLGregorianCalendar(dt1); |
| 66 | + XMLGregorianCalendar cal2 = dataTypeFactory.newXMLGregorianCalendar(dt2); |
| 67 | + |
| 68 | + // identical hash codes before the patch |
| 69 | + int hashCode1 = cal1.hashCode(); |
| 70 | + int hashCode2 = cal2.hashCode(); |
| 71 | + |
| 72 | + if (equal) { |
| 73 | + Assert.assertTrue(cal1.equals(cal2)); |
| 74 | + Assert.assertEquals(hashCode1, hashCode2); |
| 75 | + } else { |
| 76 | + Assert.assertFalse(cal1.equals(cal2)); |
| 77 | + Assert.assertNotEquals(hashCode1, hashCode2); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments