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    *      http://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.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import org.apache.commons.lang3.AbstractLangTest;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Tests for GmtTimeZone
28   */
29  public class GmtTimeZoneTest extends AbstractLangTest {
30  
31      @Test
32      public void testGetID() {
33          assertEquals("GMT+00:00", new GmtTimeZone(false, 0, 0).getID());
34          assertEquals("GMT+01:02", new GmtTimeZone(false, 1, 2).getID());
35          assertEquals("GMT+11:22", new GmtTimeZone(false, 11, 22).getID());
36          assertEquals("GMT-01:02", new GmtTimeZone(true, 1, 2).getID());
37          assertEquals("GMT-11:22", new GmtTimeZone(true, 11, 22).getID());
38      }
39  
40      @Test
41      public void testGetOffset() {
42          assertEquals(0, new GmtTimeZone(false, 0, 0).getOffset(234304));
43          assertEquals(-(6 * 60 + 30) * 60 * 1000,
44                  new GmtTimeZone(true, 6, 30).getOffset(1, 1, 1, 1, 1, 1));
45      }
46  
47      @Test
48      public void testGetRawOffset() {
49          assertEquals(0, new GmtTimeZone(false, 0, 0).getRawOffset());
50      }
51  
52      @Test
53      public void testHoursInRange() {
54          assertEquals(23 * 60 * 60 * 1000, new GmtTimeZone(false, 23, 0).getRawOffset());
55      }
56  
57      @Test
58      public void testHoursOutOfRange() {
59          assertThrows(IllegalArgumentException.class, () -> new GmtTimeZone(false, 24, 0));
60      }
61  
62      @Test
63      public void testInDaylightTime() {
64          assertFalse(new GmtTimeZone(false, 0, 0).useDaylightTime());
65      }
66  
67      @Test
68      public void testMinutesInRange() {
69          assertEquals(59 * 60 * 1000, new GmtTimeZone(false, 0, 59).getRawOffset());
70      }
71  
72      @Test
73      public void testMinutesOutOfRange() {
74          assertThrows(IllegalArgumentException.class, () -> new GmtTimeZone(false, 0, 60));
75      }
76  
77      @Test
78      public void testSetRawOffset() {
79          assertThrows(UnsupportedOperationException.class, () -> new GmtTimeZone(false, 0, 0).setRawOffset(0));
80      }
81  
82      @Test
83      public void testToString() {
84          assertEquals("[GmtTimeZone id=\"GMT-12:00\",offset=-43200000]",
85              new GmtTimeZone(true, 12, 0).toString());
86      }
87  
88      @Test
89      public void testUseDaylightTime() {
90          assertFalse(new GmtTimeZone(false, 0, 0).useDaylightTime());
91      }
92  }