View Javadoc
1   /*
2    * Licensed to the Hipparchus project under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The Hipparchus project 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    *      https://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.hipparchus.exception;
18  
19  import org.hamcrest.CoreMatchers;
20  import org.hipparchus.util.FastMath;
21  import org.junit.jupiter.api.Test;
22  
23  import java.io.PrintWriter;
24  import java.io.StringWriter;
25  import java.util.Locale;
26  
27  import static org.hamcrest.MatcherAssert.assertThat;
28  
29  /**
30   * Unit tests for {@link MathRuntimeException}.
31   *
32   * @author Evan Ward
33   */
34  class MathRuntimeExceptionTest {
35  
36      /**
37       * Check that a helpful message and stack trace is still generated even when there is
38       * an error while building the exception message.
39       */
40      @Test
41      void testGetMessageError() {
42          // setup
43          Object bad = new Object() {
44              @Override
45              public String toString() {
46                  throw new RuntimeException("toString failed");
47              }
48          };
49          StringWriter buffer = new StringWriter();
50          PrintWriter writer = new PrintWriter(buffer);
51  
52          // action
53          try {
54              throw new MathRuntimeException(
55                      LocalizedCoreFormats.URL_CONTAINS_NO_DATA,
56                      bad);
57          } catch (MathRuntimeException e) {
58              // verify
59              e.printStackTrace(writer);
60              String message = buffer.toString();
61              // check original reason is preserved
62              assertThat(message,
63                      CoreMatchers.containsString("contains no data"));
64              assertThat(message,
65                      CoreMatchers.containsString("MathRuntimeException"));
66              // check exception during formatting is preserved
67              assertThat(message,
68                      CoreMatchers.containsString("toString failed"));
69          }
70      }
71  
72      /** Check the bracketing exception message uses full precision. */
73      @Test
74      void testGetMessageDecimalFormatUnsupportedLocale() {
75          // setup
76          double a = FastMath.nextUp(1.0), b = FastMath.nextDown(1.0);
77          double fa = -Double.MIN_NORMAL, fb = -12.345678901234567e-10;
78  
79          // action
80          String message = new MathRuntimeException(
81                  LocalizedCoreFormats.NOT_BRACKETING_INTERVAL, a, b, fa, fb)
82                  .getMessage(Locale.TRADITIONAL_CHINESE);
83  
84          // verify
85          String expected = "interval does not bracket a root: " +
86                  "f(1.0000000000000002E0) = -22.250738585072014E-309, " +
87                  "f(999.9999999999999E-3) = -1.2345678901234566E-9";
88          assertThat( message, CoreMatchers.is( expected) );
89      }
90  
91      /** Check the bracketing exception message uses full precision. */
92      @Test
93      void testGetMessageDecimalFormatSupportedLocale() {
94          // setup
95          double a = FastMath.nextUp(1.0), b = FastMath.nextDown(1.0);
96          double fa = -Double.MIN_NORMAL, fb = -12.345678901234567e-10;
97  
98          // action
99          String message = new MathRuntimeException(
100                 LocalizedCoreFormats.NOT_BRACKETING_INTERVAL, a, b, fa, fb)
101                 .getMessage(Locale.FRENCH);
102 
103         // verify
104         String expected = "l'intervalle n'encadre pas une racine : " +
105                           "f(1,0000000000000002E0) = -22,250738585072014E-309, " +
106                           "f(999,9999999999999E-3) = -1,2345678901234566E-9";
107         assertThat( message, CoreMatchers.is( expected) );
108     }
109 
110 }