1   /* Copyright 2002-2022 CS GROUP
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS 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.orekit.time;
18  
19  
20  
21  import org.junit.Assert;
22  import org.junit.Test;
23  import org.orekit.Utils;
24  import org.orekit.errors.OrekitException;
25  import org.orekit.errors.OrekitMessages;
26  
27  public class AGILeapSecondFilesLoaderTest {
28  
29      @Test
30      public void testRegularFile() {
31  
32          Utils.setDataRoot("AGI");
33  
34          // we arbitrary put UTC == TAI before 1961-01-01
35          checkOffset(1950,  1,  1,   0);
36  
37          // linear models between 1961 and 1972
38          checkOffset(1961,  1,  2,  -(1.422818 +   1 * 0.001296));  // MJD 37300 +   1
39          checkOffset(1961,  8,  2,  -(1.372818 + 213 * 0.001296));  // MJD 37300 + 213
40          checkOffset(1962,  1,  2,  -(1.845858 +   1 * 0.0011232)); // MJD 37665 +   1
41          checkOffset(1963, 11,  2,  -(1.945858 + 670 * 0.0011232)); // MJD 37665 + 670
42          checkOffset(1964,  1,  2,  -(3.240130 - 365 * 0.001296));  // MJD 38761 - 365
43          checkOffset(1964,  4,  2,  -(3.340130 - 274 * 0.001296));  // MJD 38761 - 274
44          checkOffset(1964,  9,  2,  -(3.440130 - 121 * 0.001296));  // MJD 38761 - 121
45          checkOffset(1965,  1,  2,  -(3.540130 +   1 * 0.001296));  // MJD 38761 +   1
46          checkOffset(1965,  3,  2,  -(3.640130 +  60 * 0.001296));  // MJD 38761 +  60
47          checkOffset(1965,  7,  2,  -(3.740130 + 182 * 0.001296));  // MJD 38761 + 182
48          checkOffset(1965,  9,  2,  -(3.840130 + 244 * 0.001296));  // MJD 38761 + 244
49          checkOffset(1966,  1,  2,  -(4.313170 +   1 * 0.002592));  // MJD 39126 +   1
50          checkOffset(1968,  2,  2,  -(4.213170 + 762 * 0.002592));  // MJD 39126 + 762
51  
52          // since 1972-01-01, offsets are only whole seconds
53          checkOffset(1972,  3,  5, -10);
54          checkOffset(1972,  7, 14, -11);
55          checkOffset(1979, 12, 31, -18);
56          checkOffset(1980,  1, 22, -19);
57          checkOffset(2006,  7,  7, -33);
58          checkOffset(2010,  7,  7, -34);
59          checkOffset(2012,  7,  7, -35);
60          checkOffset(2015,  7,  7, -36);
61  
62      }
63  
64      @Test
65      public void testInconsistentDate() {
66          checkException("LeapSecond-inconsistent-date.dat",
67                         OrekitMessages.INCONSISTENT_DATES_IN_IERS_FILE);
68      }
69  
70      @Test
71      public void testNonChronological() {
72          checkException("LeapSecond-non-chronological.dat",
73                         OrekitMessages.NON_CHRONOLOGICAL_DATES_IN_FILE);
74      }
75  
76      @Test
77      public void testFormatError() {
78          checkException("LeapSecond-format-error.dat",
79                         OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE);
80      }
81  
82      private void checkOffset(int year, int month, int day, double offset) {
83          TimeScale utc = TimeScalesFactory.getUTC();
84          AbsoluteDate date = new AbsoluteDate(year, month, day, utc);
85          Assert.assertEquals(offset, utc.offsetFromTAI(date), 1.0e-10);
86      }
87  
88      private void checkException(String name, OrekitMessages message) {
89          Utils.setDataRoot("AGI");
90          TimeScalesFactory.addUTCTAIOffsetsLoader(new AGILeapSecondFilesLoader(name));
91          try {
92              TimeScalesFactory.getUTC();
93              Assert.fail("an exception should have been thrown");
94          } catch (OrekitException oe) {
95              Assert.assertEquals(message, oe.getSpecifier());
96          }
97      }
98  
99  }