1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
29
30
31
32 public class MathRuntimeExceptionTest {
33
34
35
36
37
38 @Test
39 public void testGetMessageError() {
40
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
51 try {
52 throw new MathRuntimeException(
53 LocalizedCoreFormats.URL_CONTAINS_NO_DATA,
54 bad);
55 } catch (MathRuntimeException e) {
56
57 e.printStackTrace(writer);
58 String message = buffer.toString();
59
60 MatcherAssert.assertThat(message,
61 CoreMatchers.containsString("contains no data"));
62 MatcherAssert.assertThat(message,
63 CoreMatchers.containsString("MathRuntimeException"));
64
65 MatcherAssert.assertThat(message,
66 CoreMatchers.containsString("toString failed"));
67 }
68 }
69
70
71 @Test
72 public void testGetMessageDecimalFormat() {
73
74 double a = FastMath.nextUp(1.0), b = FastMath.nextDown(1.0);
75 double fa = -Double.MIN_NORMAL, fb = -12.345678901234567e-10;
76
77
78 String message = new MathRuntimeException(
79 LocalizedCoreFormats.NOT_BRACKETING_INTERVAL, a, b, fa, fb)
80 .getMessage();
81
82
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 }