1   /* Copyright 2002-2025 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.errors;
18  
19  import org.hipparchus.exception.MathRuntimeException;
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.api.BeforeEach;
22  import org.junit.jupiter.api.Test;
23  import org.orekit.Utils;
24  import org.orekit.time.AbsoluteDate;
25  
26  import java.util.Locale;
27  
28  public class TimeStampedCacheExceptionTest {
29  
30      @Test
31      public void testMessage() {
32          TimeStampedCacheException e =
33                          new TimeStampedCacheException(OrekitMessages.UNABLE_TO_GENERATE_NEW_DATA_BEFORE,
34                                                        AbsoluteDate.MODIFIED_JULIAN_EPOCH,
35                                                        AbsoluteDate.MODIFIED_JULIAN_EPOCH.shiftedBy(-1e-16),
36                                                        1e-16);
37          Assertions.assertEquals(OrekitMessages.UNABLE_TO_GENERATE_NEW_DATA_BEFORE, e.getSpecifier());
38          Assertions.assertEquals(3, e.getParts().length);
39          Assertions.assertEquals(0, ((AbsoluteDate) e.getParts()[0]).durationFrom(AbsoluteDate.MODIFIED_JULIAN_EPOCH), 1.0e-10);
40          Assertions.assertEquals(e.getMessage(Locale.getDefault()), e.getLocalizedMessage());
41          Assertions.assertEquals("impossible de générer des données avant le 1858-11-16T23:59:27.816Z, données requises pour 1858-11-16T23:59:27.8159999999999999Z qui est 1,0E-16 s avant",
42                              e.getMessage(Locale.FRENCH));
43      }
44  
45      @Test
46      public void testCause() {
47          TimeStampedCacheException e =
48                          new TimeStampedCacheException(new ArrayIndexOutOfBoundsException(),
49                                                        OrekitMessages.UNABLE_TO_GENERATE_NEW_DATA_BEFORE,
50                                                        AbsoluteDate.MODIFIED_JULIAN_EPOCH);
51          Assertions.assertInstanceOf(ArrayIndexOutOfBoundsException.class, e.getCause());
52          Assertions.assertEquals(OrekitMessages.UNABLE_TO_GENERATE_NEW_DATA_BEFORE, e.getSpecifier());
53          Assertions.assertEquals(1, e.getParts().length);
54          Assertions.assertEquals(0, ((AbsoluteDate) e.getParts()[0]).durationFrom(AbsoluteDate.MODIFIED_JULIAN_EPOCH), 1.0e-10);
55          Assertions.assertEquals(e.getMessage(Locale.getDefault()), e.getLocalizedMessage());
56          Assertions.assertEquals("impossible de générer des données avant le 1858-11-16T23:59:27.816Z, données requises pour {1} qui est {2} s avant",
57                              e.getMessage(Locale.FRENCH));
58      }
59  
60      @Test
61      public void testUnwrapOrekitExceptionNeedsCreation() {
62          OrekitException base = new OrekitException(OrekitMessages.UNABLE_TO_GENERATE_NEW_DATA_BEFORE,
63                                                     AbsoluteDate.MODIFIED_JULIAN_EPOCH);
64          TimeStampedCacheException unwraped = TimeStampedCacheException.unwrap(base);
65          Assertions.assertSame(base, unwraped.getCause());
66      }
67  
68      @Test
69      public void testUnwrapOrekitExceptionSimpleExtraction() {
70          TimeStampedCacheException base = new TimeStampedCacheException(OrekitMessages.UNABLE_TO_GENERATE_NEW_DATA_BEFORE,
71                                                                         AbsoluteDate.MODIFIED_JULIAN_EPOCH);
72          OrekitException intermediate = new OrekitException(base);
73          TimeStampedCacheException unwraped = TimeStampedCacheException.unwrap(intermediate);
74          Assertions.assertNull(unwraped.getCause());
75          Assertions.assertSame(base, unwraped);
76      }
77  
78      @Test
79      public void testUnwrapMathRuntimeExceptionNeedsCreation() {
80          MathRuntimeException base = new MathRuntimeException(OrekitMessages.UNABLE_TO_GENERATE_NEW_DATA_BEFORE,
81                                                               AbsoluteDate.MODIFIED_JULIAN_EPOCH);
82          TimeStampedCacheException unwraped = TimeStampedCacheException.unwrap(base);
83          Assertions.assertSame(base, unwraped.getCause());
84      }
85  
86      @Test
87      public void testUnwrapMathRuntimeExceptionSimpleExtraction() {
88          TimeStampedCacheException base = new TimeStampedCacheException(OrekitMessages.UNABLE_TO_GENERATE_NEW_DATA_BEFORE,
89                                                                         AbsoluteDate.MODIFIED_JULIAN_EPOCH);
90          MathRuntimeException intermediate = new MathRuntimeException(base, base.getSpecifier(), base.getParts());
91          TimeStampedCacheException unwraped = TimeStampedCacheException.unwrap(intermediate);
92          Assertions.assertNull(unwraped.getCause());
93          Assertions.assertSame(base, unwraped);
94      }
95  
96      @BeforeEach
97      public void setUp() {
98          Utils.setDataRoot("regular-data");
99      }
100 
101 }