View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.time;
18  
19  import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import org.apache.commons.lang3.AbstractLangTest;
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * Tests for GmtTimeZone
29   */
30  class GmtTimeZoneTest extends AbstractLangTest {
31  
32      @Test
33      void testGetID() {
34          assertEquals("GMT+00:00", new GmtTimeZone(false, 0, 0).getID());
35          assertEquals("GMT+01:02", new GmtTimeZone(false, 1, 2).getID());
36          assertEquals("GMT+11:22", new GmtTimeZone(false, 11, 22).getID());
37          assertEquals("GMT-01:02", new GmtTimeZone(true, 1, 2).getID());
38          assertEquals("GMT-11:22", new GmtTimeZone(true, 11, 22).getID());
39      }
40  
41      @Test
42      void testGetOffset() {
43          assertEquals(0, new GmtTimeZone(false, 0, 0).getOffset(234304));
44          assertEquals(-(6 * 60 + 30) * 60 * 1000,
45                  new GmtTimeZone(true, 6, 30).getOffset(1, 1, 1, 1, 1, 1));
46      }
47  
48      @Test
49      void testGetRawOffset() {
50          assertEquals(0, new GmtTimeZone(false, 0, 0).getRawOffset());
51      }
52  
53      @Test
54      void testHoursInRange() {
55          assertEquals(23 * 60 * 60 * 1000, new GmtTimeZone(false, 23, 0).getRawOffset());
56      }
57  
58      @Test
59      void testHoursOutOfRange() {
60          assertIllegalArgumentException(() -> new GmtTimeZone(false, 24, 0));
61      }
62  
63      @Test
64      void testInDaylightTime() {
65          assertFalse(new GmtTimeZone(false, 0, 0).useDaylightTime());
66      }
67  
68      @Test
69      void testMinutesInRange() {
70          assertEquals(59 * 60 * 1000, new GmtTimeZone(false, 0, 59).getRawOffset());
71      }
72  
73      @Test
74      void testMinutesOutOfRange() {
75          assertIllegalArgumentException(() -> new GmtTimeZone(false, 0, 60));
76      }
77  
78      @Test
79      void testSetRawOffset() {
80          assertThrows(UnsupportedOperationException.class, () -> new GmtTimeZone(false, 0, 0).setRawOffset(0));
81      }
82  
83      @Test
84      void testToString() {
85          assertEquals("[GmtTimeZone id=\"GMT-12:00\",offset=-43200000]",
86              new GmtTimeZone(true, 12, 0).toString());
87      }
88  
89      @Test
90      void testUseDaylightTime() {
91          assertFalse(new GmtTimeZone(false, 0, 0).useDaylightTime());
92      }
93  }