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.Assert;
22  import org.junit.Test;
23  import org.orekit.OrekitMatchers;
24  import org.orekit.Utils;
25  import org.orekit.attitudes.AttitudeProvider;
26  import org.orekit.attitudes.InertialProvider;
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.PositionAngle;
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  import org.orekit.utils.TimeStampedPVCoordinates;
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              Assert.fail("Expected Exception");
61          } catch (OrekitException e) {
62              Assert.assertEquals(e.getSpecifier(), OrekitMessages.EXCEPTIONAL_DATA_CONTEXT);
63          }
64          try {
65              context.getFrames();
66              Assert.fail("Expected Exception");
67          } catch (OrekitException e) {
68              Assert.assertEquals(e.getSpecifier(), OrekitMessages.EXCEPTIONAL_DATA_CONTEXT);
69          }
70          try {
71              context.getGeoMagneticFields();
72              Assert.fail("Expected Exception");
73          } catch (OrekitException e) {
74              Assert.assertEquals(e.getSpecifier(), OrekitMessages.EXCEPTIONAL_DATA_CONTEXT);
75          }
76          try {
77              context.getGravityFields();
78              Assert.fail("Expected Exception");
79          } catch (OrekitException e) {
80              Assert.assertEquals(e.getSpecifier(), OrekitMessages.EXCEPTIONAL_DATA_CONTEXT);
81          }
82          try {
83              context.getTimeScales();
84              Assert.fail("Expected Exception");
85          } catch (OrekitException e) {
86              Assert.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                 PositionAngle.TRUE, eci, date, Constants.EIGEN5C_EARTH_MU);
110         AttitudeProvider attitude = new InertialProvider(eci);
111         Propagator propagator = new KeplerianPropagator(orbit, attitude);
112         SpacecraftState state = propagator.propagate(date.shiftedBy(86400));
113         TimeStampedPVCoordinates pv = state.getPVCoordinates(ecef);
114         MatcherAssert.assertThat(
115                 pv.getPosition().getNorm(),
116                 OrekitMatchers.relativelyCloseTo(a, 10));
117 
118         // verify using default data context throws an exception
119         try {
120             new NumericalPropagator(new ClassicalRungeKuttaIntegrator(60.0));
121             Assert.fail("Expected Exception");
122         } catch (OrekitException e) {
123             Assert.assertEquals(e.getSpecifier(), OrekitMessages.EXCEPTIONAL_DATA_CONTEXT);
124         }
125 
126     }
127 
128     /**
129      * Force initialization of classes that have static fields that use the default data
130      * context. See JLS 12.4.1 "When Initialization Occurs".
131      */
132     private void hack() {
133         Object o = AbsoluteDate.ARBITRARY_EPOCH;
134         Assert.assertNotNull(o);
135     }
136 
137 }