|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 test.java.time.zone; |
| 25 | + |
| 26 | +import java.time.*; |
| 27 | +import java.time.zone.*; |
| 28 | +import java.util.*; |
| 29 | + |
| 30 | +import org.testng.annotations.Test; |
| 31 | +import static org.testng.Assert.assertEquals; |
| 32 | +import static org.testng.Assert.assertThrows; |
| 33 | + |
| 34 | +/** |
| 35 | + * @summary ZoneRules invariants can be broken. |
| 36 | + * |
| 37 | + * @bug 8246788 |
| 38 | + */ |
| 39 | +@Test |
| 40 | +public class TestMutableZoneRules { |
| 41 | + static final ZoneOffset offset = ZoneOffset.ofHoursMinutes(1, 30); |
| 42 | + |
| 43 | + static final ZoneOffsetTransitionRule rule1 = |
| 44 | + ZoneOffsetTransitionRule.of(Month.APRIL, 2, DayOfWeek.TUESDAY, LocalTime.MIN, true, |
| 45 | + ZoneOffsetTransitionRule.TimeDefinition.UTC, offset, offset, offset); |
| 46 | + |
| 47 | + static final ZoneOffsetTransitionRule rule2 = |
| 48 | + ZoneOffsetTransitionRule.of(Month.MARCH, 2, DayOfWeek.MONDAY, LocalTime.MIN, true, |
| 49 | + ZoneOffsetTransitionRule.TimeDefinition.UTC, offset, offset, offset); |
| 50 | + |
| 51 | + public void testMutation() { |
| 52 | + ZoneOffsetTransitionRule[] array = { rule1 }; |
| 53 | + ZoneRules zr1 = ZoneRules.of(offset, offset, List.of(), List.of(), List.of(rule1)); |
| 54 | + ZoneRules zr2 = ZoneRules.of(offset, offset, List.of(), List.of(), new TestList(array, array.length)); |
| 55 | + |
| 56 | + assertEquals(zr2, zr1); |
| 57 | + array[0] = rule2; |
| 58 | + assertEquals(zr2, zr1); |
| 59 | + } |
| 60 | + |
| 61 | + public void testLength() { |
| 62 | + ZoneOffsetTransitionRule[] array = new ZoneOffsetTransitionRule[17]; |
| 63 | + Arrays.setAll(array, i -> rule1); |
| 64 | + |
| 65 | + assertThrows(IllegalArgumentException.class, |
| 66 | + () -> ZoneRules.of(offset, offset, List.of(), List.of(), new TestList(array, 1))); |
| 67 | + } |
| 68 | + |
| 69 | + static class TestList extends AbstractList<ZoneOffsetTransitionRule> { |
| 70 | + final ZoneOffsetTransitionRule[] array; |
| 71 | + final int size; |
| 72 | + |
| 73 | + TestList(ZoneOffsetTransitionRule[] array, int size) { |
| 74 | + this.array = array; |
| 75 | + this.size = size; |
| 76 | + } |
| 77 | + |
| 78 | + public int size() { return size; } |
| 79 | + public ZoneOffsetTransitionRule get(int i) { return array[i]; } |
| 80 | + public Object[] toArray() { return array; } |
| 81 | + |
| 82 | + @SuppressWarnings("unchecked") |
| 83 | + public <T> T[] toArray(T[] a) { return (T[]) array; } |
| 84 | + } |
| 85 | +} |
0 commit comments