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 */ 16package libcore.java.time; 17 18import org.junit.Test; 19import java.time.OffsetTime; 20import java.time.ZoneOffset; 21import java.time.temporal.ChronoUnit; 22import java.time.temporal.TemporalUnit; 23import java.time.temporal.UnsupportedTemporalTypeException; 24import java.util.EnumSet; 25 26import static org.junit.Assert.assertEquals; 27import static org.junit.Assert.assertSame; 28import static org.junit.Assert.fail; 29 30/** 31 * Additional tests for {@link OffsetTime}. 32 * 33 * @see tck.java.time.TCKOffsetTime 34 * @see test.java.time.TestOffsetTime 35 */ 36public class OffsetTimeTest { 37 38 private static final OffsetTime NOON_UTC = OffsetTime 39 .of(/* hour */ 12, /* minute */ 0, /* second */ 0, /* nano */ 0, ZoneOffset.UTC); 40 41 @Test 42 public void test_plus() { 43 // Most of the logic is in LocalTime, to which OffsetTime#plus() delegates, verify only some 44 // simple cases here. In-depth tests for LocalTime#plus() can be found in TCKLocalTime. 45 assertEquals(OffsetTime.of(13, 0, 0, 0, ZoneOffset.UTC), 46 NOON_UTC.plus(1, ChronoUnit.HOURS)); 47 assertEquals(OffsetTime.of(11, 0, 0, 0, ZoneOffset.UTC), 48 NOON_UTC.plus(23, ChronoUnit.HOURS)); 49 assertEquals(OffsetTime.of(12, 1, 0, 0, ZoneOffset.UTC), 50 NOON_UTC.plus(1, ChronoUnit.MINUTES)); 51 assertEquals(OffsetTime.of(12, 1, 0, 0, ZoneOffset.UTC), 52 NOON_UTC.plus(60, ChronoUnit.SECONDS)); 53 assertEquals(OffsetTime.of(12, 0, 0, 1_000_000, ZoneOffset.UTC), 54 NOON_UTC.plus(1, ChronoUnit.MILLIS)); 55 assertEquals(OffsetTime.of(12, 0, 0, 1, ZoneOffset.UTC), 56 NOON_UTC.plus(1, ChronoUnit.NANOS)); 57 } 58 59 @Test 60 public void test_plus_noop() { 61 assertPlusIsNoop(0, ChronoUnit.MINUTES); 62 assertPlusIsNoop(2, ChronoUnit.HALF_DAYS); 63 assertPlusIsNoop(24, ChronoUnit.HOURS); 64 assertPlusIsNoop(24 * 60, ChronoUnit.MINUTES); 65 assertPlusIsNoop(24 * 60 * 60, ChronoUnit.SECONDS); 66 assertPlusIsNoop(24 * 60 * 60 * 1_000, ChronoUnit.MILLIS); 67 assertPlusIsNoop(24 * 60 * 60 * 1_000_000_000L, ChronoUnit.NANOS); 68 } 69 70 private static void assertPlusIsNoop(long amount, TemporalUnit unit) { 71 assertSame(NOON_UTC, NOON_UTC.plus(amount, unit)); 72 } 73 74 @Test 75 public void test_plus_minus_invalidUnits() { 76 for (ChronoUnit unit : EnumSet.range(ChronoUnit.DAYS, ChronoUnit.FOREVER)) { 77 try { 78 NOON_UTC.plus(1, unit); 79 fail("Adding 1 " + unit + " should have failed."); 80 } catch (UnsupportedTemporalTypeException expected) { 81 } 82 try { 83 NOON_UTC.minus(1, unit); 84 fail("Subtracting 1 " + unit + " should have failed."); 85 } catch (UnsupportedTemporalTypeException expected) { 86 } 87 } 88 } 89} 90