1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.core;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.ObjectInputStream;
22 import java.io.ObjectOutputStream;
23 import java.util.regex.Pattern;
24
25 import org.junit.Assert;
26
27
28
29 public final class GeometryTestUtils {
30
31 private GeometryTestUtils() {}
32
33
34
35
36 public static void assertPositiveInfinity(final double value) {
37 final String msg = "Expected value to be positive infinity but was " + value;
38 Assert.assertTrue(msg, Double.isInfinite(value));
39 Assert.assertTrue(msg, value > 0);
40 }
41
42
43
44
45 public static void assertNegativeInfinity(final double value) {
46 final String msg = "Expected value to be negative infinity but was " + value;
47 Assert.assertTrue(msg, Double.isInfinite(value));
48 Assert.assertTrue(msg, value < 0);
49 }
50
51
52
53
54
55 public static void assertThrows(final Runnable r, final Class<?> exceptionType) {
56 assertThrows(r, exceptionType, (String) null);
57 }
58
59
60
61
62
63
64
65
66 public static void assertThrows(final Runnable r, final Class<?> exceptionType, final String message) {
67 try {
68 r.run();
69 Assert.fail("Operation should have thrown an exception");
70 } catch (final Exception exc) {
71 final Class<?> actualType = exc.getClass();
72
73 Assert.assertTrue("Expected exception of type " + exceptionType.getName() + " but was " + actualType.getName(),
74 exceptionType.isAssignableFrom(actualType));
75
76 if (message != null) {
77 Assert.assertEquals(message, exc.getMessage());
78 }
79 }
80 }
81
82
83
84
85
86
87
88
89 public static void assertThrows(final Runnable r, final Class<?> exceptionType, final Pattern pattern) {
90 try {
91 r.run();
92 Assert.fail("Operation should have thrown an exception");
93 } catch (final Exception exc) {
94 final Class<?> actualType = exc.getClass();
95
96 Assert.assertTrue("Expected exception of type " + exceptionType.getName() + " but was " + actualType.getName(),
97 exceptionType.isAssignableFrom(actualType));
98
99 if (pattern != null) {
100 final String message = exc.getMessage();
101
102 final String err = "Expected exception message to match /" + pattern + "/ but was [" + message + "]";
103 Assert.assertTrue(err, pattern.matcher(message).matches());
104 }
105 }
106 }
107
108
109
110
111
112 public static void assertContains(final String substr, final String actual) {
113 final String msg = "Expected string to contain [" + substr + "] but was [" + actual + "]";
114 Assert.assertTrue(msg, actual.contains(substr));
115 }
116
117
118
119
120
121
122
123 public static Object serializeAndRecover(final Object obj) {
124 try {
125
126 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
127 final ObjectOutputStream so = new ObjectOutputStream(bos);
128 so.writeObject(obj);
129
130
131 final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
132 final ObjectInputStream si = new ObjectInputStream(bis);
133 return si.readObject();
134 } catch (final Exception e) {
135 throw new RuntimeException(e);
136 }
137 }
138 }