1   /* Contributed in the public domain.
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.data;
18  
19  import org.hamcrest.MatcherAssert;
20  import org.hipparchus.ode.nonstiff.ClassicalRungeKuttaIntegrator;
21  import org.junit.jupiter.api.AfterAll;
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.Test;
24  import org.orekit.OrekitMatchers;
25  import org.orekit.Utils;
26  import org.orekit.attitudes.AttitudeProvider;
27  import org.orekit.attitudes.FrameAlignedProvider;
28  import org.orekit.errors.OrekitException;
29  import org.orekit.errors.OrekitMessages;
30  import org.orekit.frames.Frame;
31  import org.orekit.frames.LazyLoadedFrames;
32  import org.orekit.orbits.KeplerianOrbit;
33  import org.orekit.orbits.Orbit;
34  import org.orekit.orbits.PositionAngleType;
35  import org.orekit.propagation.Propagator;
36  import org.orekit.propagation.SpacecraftState;
37  import org.orekit.propagation.analytical.KeplerianPropagator;
38  import org.orekit.propagation.numerical.NumericalPropagator;
39  import org.orekit.time.AbsoluteDate;
40  import org.orekit.time.LazyLoadedTimeScales;
41  import org.orekit.utils.Constants;
42  import org.orekit.utils.IERSConventions;
43  
44  /**
45   * Test for {@link ExceptionalDataContext}.
46   *
47   * @author Evan Ward
48   */
49  public class ExceptionalDataContextTest {
50  
51      /** Check the methods throw exceptions. */
52      @Test
53      public void testThrows() {
54          // setup
55          ExceptionalDataContext context = new ExceptionalDataContext();
56  
57          // verify
58          try {
59              context.getCelestialBodies();
60              Assertions.fail("Expected Exception");
61          } catch (OrekitException e) {
62              Assertions.assertEquals(e.getSpecifier(), OrekitMessages.EXCEPTIONAL_DATA_CONTEXT);
63          }
64          try {
65              context.getFrames();
66              Assertions.fail("Expected Exception");
67          } catch (OrekitException e) {
68              Assertions.assertEquals(e.getSpecifier(), OrekitMessages.EXCEPTIONAL_DATA_CONTEXT);
69          }
70          try {
71              context.getGeoMagneticFields();
72              Assertions.fail("Expected Exception");
73          } catch (OrekitException e) {
74              Assertions.assertEquals(e.getSpecifier(), OrekitMessages.EXCEPTIONAL_DATA_CONTEXT);
75          }
76          try {
77              context.getGravityFields();
78              Assertions.fail("Expected Exception");
79          } catch (OrekitException e) {
80              Assertions.assertEquals(e.getSpecifier(), OrekitMessages.EXCEPTIONAL_DATA_CONTEXT);
81          }
82          try {
83              context.getTimeScales();
84              Assertions.fail("Expected Exception");
85          } catch (OrekitException e) {
86              Assertions.assertEquals(e.getSpecifier(), OrekitMessages.EXCEPTIONAL_DATA_CONTEXT);
87          }
88      }
89  
90      /** Check it can be set as the default data context with the right precautions. */
91      @Test
92      public void testDefault() {
93          // setup
94          Utils.setDataRoot("regular-data");
95          // hack to initialize static fields that need the default data context
96          hack();
97          LazyLoadedDataContext context = DataContext.getDefault();
98          DataContext.setDefault(new ExceptionalDataContext());
99  
100         // verify by running some code
101         LazyLoadedTimeScales timeScales = context.getTimeScales();
102         LazyLoadedFrames frames = context.getFrames();
103         Frame eci = frames.getEME2000();
104         Frame ecef = frames.getITRF(IERSConventions.IERS_2010, true);
105         AbsoluteDate date = new AbsoluteDate(2019, 12, 20, timeScales.getUTC());
106         double a = 6378e3 + 500e3;
107         Orbit orbit = new KeplerianOrbit(
108                 a, 0, 0, 0, 0, 0,
109                 PositionAngleType.TRUE, eci, date, Constants.EIGEN5C_EARTH_MU);
110         AttitudeProvider attitude = new FrameAlignedProvider(eci);
111         Propagator propagator = new KeplerianPropagator(orbit, attitude);
112         SpacecraftState state = propagator.propagate(date.shiftedBy(86400));
113         MatcherAssert.assertThat(
114                 state.getPosition(ecef).getNorm(),
115                 OrekitMatchers.relativelyCloseTo(a, 10));
116 
117         // verify using default data context throws an exception
118         try {
119             new NumericalPropagator(new ClassicalRungeKuttaIntegrator(60.0));
120             Assertions.fail("Expected Exception");
121         } catch (OrekitException e) {
122             Assertions.assertEquals(e.getSpecifier(), OrekitMessages.EXCEPTIONAL_DATA_CONTEXT);
123         }
124 
125     }
126 
127     /**
128      * Force initialization of classes that have static fields that use the default data
129      * context. See JLS 12.4.1 "When Initialization Occurs".
130      */
131     private void hack() {
132         Object o = AbsoluteDate.ARBITRARY_EPOCH;
133         Assertions.assertNotNull(o);
134     }
135 
136     @AfterAll
137     static void tearDown() {
138         DataContext.setDefault(new LazyLoadedDataContext());
139     }
140 
141 }