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