1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package libcore.java.time.zone; 17 18 import org.junit.Test; 19 import java.time.Duration; 20 import java.time.Instant; 21 import java.time.LocalDateTime; 22 import java.time.Month; 23 import java.time.ZoneOffset; 24 import java.time.zone.ZoneRules; 25 import java.util.Collections; 26 27 import static org.junit.Assert.assertEquals; 28 import static org.junit.Assert.assertNull; 29 30 /** 31 * Additional tests for {@link ZoneRules}. 32 * 33 * @see tck.java.time.zone.TCKZoneRules 34 */ 35 public class ZoneRulesTest { 36 37 @Test test_of_ZoneOffset()38 public void test_of_ZoneOffset() { 39 ZoneOffset offset = ZoneOffset.MIN; 40 ZoneRules zoneRules = ZoneRules.of(offset); 41 42 assertEquals(Collections.emptyList(), zoneRules.getTransitionRules()); 43 assertEquals(Collections.emptyList(), zoneRules.getTransitions()); 44 assertNull(zoneRules.nextTransition(Instant.MIN)); 45 46 // Check various offsets at a bunch of instants, as they should be constant. 47 Instant[] instants = new Instant[] { 48 LocalDateTime.MIN.toInstant(offset), 49 Instant.EPOCH, 50 LocalDateTime.of(2000, Month.JANUARY, 1, 1, 1).toInstant(ZoneOffset.UTC), 51 Instant.now(), 52 LocalDateTime.MAX.toInstant(offset), 53 }; 54 55 for (Instant instant : instants) { 56 assertEquals(Duration.ZERO, zoneRules.getDaylightSavings(instant)); 57 assertEquals(offset, zoneRules.getOffset(instant)); 58 assertEquals(offset, zoneRules.getStandardOffset(instant)); 59 LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, offset); 60 assertNull(zoneRules.getTransition(localDateTime)); 61 assertEquals(Collections.singletonList(offset), 62 zoneRules.getValidOffsets(localDateTime)); 63 } 64 } 65 66 @Test(expected = NullPointerException.class) test_of_ZoneOffset_null()67 public void test_of_ZoneOffset_null() { 68 ZoneRules.of(null); 69 } 70 } 71