1
2
3
4 package de.powerstat.validation.values.test;
5
6
7 import static org.junit.jupiter.api.Assertions.assertAll;
8 import static org.junit.jupiter.api.Assertions.assertEquals;
9 import static org.junit.jupiter.api.Assertions.assertFalse;
10 import static org.junit.jupiter.api.Assertions.assertNotEquals;
11 import static org.junit.jupiter.api.Assertions.assertThrows;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
13
14 import org.junit.jupiter.api.Test;
15
16 import de.powerstat.validation.values.WGS84Position;
17 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18
19
20
21
22
23 @SuppressFBWarnings({"EC_NULL_ARG", "RV_NEGATING_RESULT_OF_COMPARETO", "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", "SPP_USE_ZERO_WITH_COMPARATOR", "PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS"})
24 final class WGS84PositionTests
25 {
26
27
28
29 private static final String INDEX_OUT_OF_BOUNDS_EXPECTED = "Index out of bounds exception expected";
30
31
32
33
34 private static final String TEST_EQUALS = "testEquals";
35
36
37
38
39 private static final String ZERO = "0.0 0.0 0.0";
40
41
42
43
44 private static final String LATITUTE_ERROR = "Latitute error!";
45
46
47
48
49 private static final String LONGITUTE_ERROR = "Longitute error!";
50
51
52
53
54 private static final String ALTITUDE_ERROR = "Altitude error!";
55
56
57
58
59 private static final String JAVA_S5785 = "java:S5785";
60
61
62
63
64
65 WGS84PositionTests()
66 {
67 super();
68 }
69
70
71
72
73
74 @Test
75 void testFactory1()
76 {
77 final WGS84Position pos = WGS84Position.of(ZERO);
78 assertAll("factory",
79 () -> assertEquals(0.0, pos.getLatitude(), LATITUTE_ERROR),
80 () -> assertEquals(0.0, pos.getLongitude(), LONGITUTE_ERROR),
81 () -> assertEquals(0.0, pos.getAltitude(), ALTITUDE_ERROR)
82 );
83 }
84
85
86
87
88
89 @Test
90 void testFactory2()
91 {
92 assertThrows(IllegalArgumentException.class, () ->
93 {
94 WGS84Position.of("0.0");
95 }, "Illegal argument excption"
96 );
97 }
98
99
100
101
102
103 @Test
104 void testIsWGS84Position()
105 {
106 final WGS84Position pos = WGS84Position.of(0.0, 0.0, 0.0);
107 assertAll("testIsPosition",
108 () -> assertEquals(0.0, pos.getLatitude(), LATITUTE_ERROR),
109 () -> assertEquals(0.0, pos.getLongitude(), LONGITUTE_ERROR),
110 () -> assertEquals(0.0, pos.getAltitude(), ALTITUDE_ERROR)
111 );
112 }
113
114
115
116
117
118 @Test
119 void testIsNotWGS84Position1()
120 {
121 assertThrows(IndexOutOfBoundsException.class, () ->
122 {
123 WGS84Position.of(-90.1, 0.0, 0.0);
124 }, WGS84PositionTests.INDEX_OUT_OF_BOUNDS_EXPECTED
125 );
126 }
127
128
129
130
131
132 @Test
133 void testIsNotWGS84Position2()
134 {
135 assertThrows(IndexOutOfBoundsException.class, () ->
136 {
137 WGS84Position.of(90.1, 0.0, 0.0);
138 }, WGS84PositionTests.INDEX_OUT_OF_BOUNDS_EXPECTED
139 );
140 }
141
142
143
144
145
146 @Test
147 void testIsNotWGS84Position3()
148 {
149 assertThrows(IndexOutOfBoundsException.class, () ->
150 {
151 WGS84Position.of(0.0, -180.1, 0.0);
152 }, WGS84PositionTests.INDEX_OUT_OF_BOUNDS_EXPECTED
153 );
154 }
155
156
157
158
159
160 @Test
161 void testIsNotWGS84Position4()
162 {
163 assertThrows(IndexOutOfBoundsException.class, () ->
164 {
165 WGS84Position.of(0.0, 180.1, 0.0);
166 }, WGS84PositionTests.INDEX_OUT_OF_BOUNDS_EXPECTED
167 );
168 }
169
170
171
172
173
174 @Test
175 void testStringValue()
176 {
177 assertEquals(ZERO, WGS84Position.of(0.0, 0.0, 0.0).stringValue(), "Result not as expected");
178 }
179
180
181
182
183
184 @Test
185 void testHashCode()
186 {
187 final WGS84Position pos1 = WGS84Position.of(0.0, 0.0, 0.0);
188 final WGS84Position pos2 = WGS84Position.of(0.0, 0.0, 0.0);
189 final WGS84Position pos3 = WGS84Position.of(10.0, 10.0, 0.0);
190 assertAll("testHashCode",
191 () -> assertEquals(pos1.hashCode(), pos2.hashCode(), "hashCodes are not equal"),
192 () -> assertNotEquals(pos1.hashCode(), pos3.hashCode(), "hashCodes are equal")
193 );
194 }
195
196
197
198
199
200 @Test
201 @SuppressWarnings(JAVA_S5785)
202 void testEquals()
203 {
204 final WGS84Position pos1 = WGS84Position.of(0.0, 0.0, 0.0);
205 final WGS84Position pos2 = WGS84Position.of(0.0, 0.0, 0.0);
206 final WGS84Position pos3 = WGS84Position.of(10.0, 10.0, 0.0);
207 final WGS84Position pos4 = WGS84Position.of(0.0, 0.0, 0.0);
208 assertAll(WGS84PositionTests.TEST_EQUALS,
209 () -> assertTrue(pos1.equals(pos1), "pos11 is not equal"),
210 () -> assertTrue(pos1.equals(pos2), "pos12 are not equal"),
211 () -> assertTrue(pos2.equals(pos1), "pos21 are not equal"),
212 () -> assertTrue(pos2.equals(pos4), "pos24 are not equal"),
213 () -> assertTrue(pos1.equals(pos4), "pos14 are not equal"),
214 () -> assertFalse(pos1.equals(pos3), "pos13 are equal"),
215 () -> assertFalse(pos3.equals(pos1), "pos31 are equal"),
216 () -> assertFalse(pos1.equals(null), "pos10 is equal")
217 );
218 }
219
220
221
222
223
224 @Test
225 @SuppressWarnings(JAVA_S5785)
226 void testEquals2()
227 {
228 final WGS84Position pos1 = WGS84Position.of(0.0, 0.0, 0.0);
229 final WGS84Position pos2 = WGS84Position.of(0.0, 0.0, 0.0000001D);
230 assertAll(WGS84PositionTests.TEST_EQUALS,
231 () -> assertTrue(pos1.equals(pos2), "pos12 is not equal")
232 );
233 }
234
235
236
237
238
239 @Test
240 @SuppressWarnings(JAVA_S5785)
241 void testNotEquals()
242 {
243 final WGS84Position pos1 = WGS84Position.of(0.0, 0.0, 0.0);
244 final WGS84Position pos2 = WGS84Position.of(0.0, 0.0, 0.1);
245 final WGS84Position pos3 = WGS84Position.of(0.0, 0.1, 0.0);
246 assertAll("testNotEquals",
247 () -> assertFalse(pos1.equals(pos2), "pos12 is equal"),
248 () -> assertFalse(pos1.equals(pos3), "pos13 is equal")
249 );
250 }
251
252
253
254
255
256 @Test
257 void testToString()
258 {
259 final WGS84Position pos = WGS84Position.of(0.0, 0.0, 0.0);
260 assertEquals("WGS84Position[latitude=0.0, longitude=0.0, altitude=0.0]", pos.toString(), "toString not equal");
261 }
262
263
264
265
266
267 @Test
268 @SuppressWarnings(JAVA_S5785)
269 void testCompareTo()
270 {
271 final WGS84Position pos1 = WGS84Position.of(0.0, 0.0, 0.0);
272 final WGS84Position pos2 = WGS84Position.of(0.0, 0.0, 0.0);
273 final WGS84Position pos3 = WGS84Position.of(10.0, 10.0, 0.0);
274 final WGS84Position pos4 = WGS84Position.of(20.0, 20.0, 0.0);
275 final WGS84Position pos5 = WGS84Position.of(0.0, 0.0, 0.0);
276 assertAll("testCompareTo",
277 () -> assertTrue(pos1.compareTo(pos2) == -pos2.compareTo(pos1), "reflexive1"),
278 () -> assertTrue(pos1.compareTo(pos3) == -pos3.compareTo(pos1), "reflexive2"),
279 () -> assertTrue((pos4.compareTo(pos3) > 0) && (pos3.compareTo(pos1) > 0) && (pos4.compareTo(pos1) > 0), "transitive1"),
280 () -> assertTrue((pos1.compareTo(pos2) == 0) && (Math.abs(pos1.compareTo(pos5)) == Math.abs(pos2.compareTo(pos5))), "sgn1"),
281 () -> assertTrue((pos1.compareTo(pos2) == 0) && pos1.equals(pos2), "equals")
282 );
283 }
284
285
286
287
288
289 @Test
290 @SuppressWarnings(JAVA_S5785)
291 void testNotCompareTo()
292 {
293 final WGS84Position pos1 = WGS84Position.of(0.0, 0.0, 0.0);
294 final WGS84Position pos2 = WGS84Position.of(0.0, 150.0, 0.0);
295 assertAll("testNotCompareTo",
296 () -> assertTrue(pos1.compareTo(pos2) != 0, "equal")
297 );
298 }
299
300 }