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