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.hamcrest.MatcherAssert;
21  import org.hipparchus.util.FastMath;
22  import org.junit.Test;
23  
24  import java.io.PrintWriter;
25  import java.io.StringWriter;
26  
27  /**
28   * Unit tests for {@link MathRuntimeException}.
29   *
30   * @author Evan Ward
31   */
32  public class MathRuntimeExceptionTest {
33  
34      /**
35       * Check that a helpful message and stack trace is still generated even when there is
36       * an error while building the exception message.
37       */
38      @Test
39      public void testGetMessageError() {
40          // setup
41          Object bad = new Object() {
42              @Override
43              public String toString() {
44                  throw new RuntimeException("toString failed");
45              }
46          };
47          StringWriter buffer = new StringWriter();
48          PrintWriter writer = new PrintWriter(buffer);
49  
50          // action
51          try {
52              throw new MathRuntimeException(
53                      LocalizedCoreFormats.URL_CONTAINS_NO_DATA,
54                      bad);
55          } catch (MathRuntimeException e) {
56              // verify
57              e.printStackTrace(writer);
58              String message = buffer.toString();
59              // check original reason is preserved
60              MatcherAssert.assertThat(message,
61                      CoreMatchers.containsString("contains no data"));
62              MatcherAssert.assertThat(message,
63                      CoreMatchers.containsString("MathRuntimeException"));
64              // check exception during formatting is preserved
65              MatcherAssert.assertThat(message,
66                      CoreMatchers.containsString("toString failed"));
67          }
68      }
69  
70      /** Check the bracketing exception message uses full precision. */
71      @Test
72      public void testGetMessageDecimalFormat() {
73          // setup
74          double a = FastMath.nextUp(1.0), b = FastMath.nextDown(1.0);
75          double fa = -Double.MIN_NORMAL, fb = -12.345678901234567e-10;
76  
77          // action
78          String message = new MathRuntimeException(
79                  LocalizedCoreFormats.NOT_BRACKETING_INTERVAL, a, b, fa, fb)
80                  .getMessage();
81  
82          // verify
83          String expected = "interval does not bracket a root: " +
84                  "f(1.0000000000000002E0) = -22.250738585072014E-309, " +
85                  "f(999.9999999999999E-3) = -1.2345678901234566E-9";
86          MatcherAssert.assertThat( message, CoreMatchers.is( expected) );
87      }
88  
89  }