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
18 package org.orekit.errors;
19
20 import org.hamcrest.CoreMatchers;
21 import org.hamcrest.MatcherAssert;
22 import org.hipparchus.exception.DummyLocalizable;
23 import org.junit.jupiter.api.Assertions;
24 import org.junit.jupiter.api.Test;
25
26 import java.io.PrintWriter;
27 import java.io.StringWriter;
28
29 /**
30 * Unit tests for {@link OrekitException}.
31 *
32 * @author Evan Ward
33 */
34 public class OrekitExceptionTest {
35
36 /** Check {@link OrekitException#getMessage()} does not throw a NPE. */
37 @Test
38 public void testNullString() {
39 // action
40 OrekitException exception = new OrekitException(new DummyLocalizable(null));
41
42 // verify
43 Assertions.assertEquals(exception.getMessage(), "");
44 }
45
46 /**
47 * Check that if the message formatting in {@link OrekitException#getLocalizedMessage()}
48 * throws an exception a useful message and stack trace is still printed.
49 */
50 @Test
51 public void testBadMessage() {
52 // setup
53 StringWriter writer = new StringWriter();
54 PrintWriter printer = new PrintWriter(writer);
55 OrekitMessages message = OrekitMessages.CORRUPTED_FILE;
56 Object part = new Object() {
57 @Override
58 public String toString() {
59 throw new IllegalStateException("bad message");
60 }
61 };
62
63 // action, message expects a parameter, but none is given
64 new OrekitException(message, part).printStackTrace(printer);
65
66 // verify
67 String actual = writer.toString();
68 MatcherAssert.assertThat(actual,
69 CoreMatchers.containsString(message.getSourceString()));
70 MatcherAssert.assertThat(actual,
71 CoreMatchers.containsString("IllegalStateException: bad message"));
72 }
73
74 }