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.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
31
32
33
34 class MathRuntimeExceptionTest {
35
36
37
38
39
40 @Test
41 void testGetMessageError() {
42
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
53 try {
54 throw new MathRuntimeException(
55 LocalizedCoreFormats.URL_CONTAINS_NO_DATA,
56 bad);
57 } catch (MathRuntimeException e) {
58
59 e.printStackTrace(writer);
60 String message = buffer.toString();
61
62 assertThat(message,
63 CoreMatchers.containsString("contains no data"));
64 assertThat(message,
65 CoreMatchers.containsString("MathRuntimeException"));
66
67 assertThat(message,
68 CoreMatchers.containsString("toString failed"));
69 }
70 }
71
72
73 @Test
74 void testGetMessageDecimalFormatUnsupportedLocale() {
75
76 double a = FastMath.nextUp(1.0), b = FastMath.nextDown(1.0);
77 double fa = -Double.MIN_NORMAL, fb = -12.345678901234567e-10;
78
79
80 String message = new MathRuntimeException(
81 LocalizedCoreFormats.NOT_BRACKETING_INTERVAL, a, b, fa, fb)
82 .getMessage(Locale.TRADITIONAL_CHINESE);
83
84
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
92 @Test
93 void testGetMessageDecimalFormatSupportedLocale() {
94
95 double a = FastMath.nextUp(1.0), b = FastMath.nextDown(1.0);
96 double fa = -Double.MIN_NORMAL, fb = -12.345678901234567e-10;
97
98
99 String message = new MathRuntimeException(
100 LocalizedCoreFormats.NOT_BRACKETING_INTERVAL, a, b, fa, fb)
101 .getMessage(Locale.FRENCH);
102
103
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 }