View Javadoc
1   /*
2    * Copyright (C) 2021-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
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   * WGS84 position tests.
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     * Index out of bounds exception expected constant.
28     */
29    private static final String INDEX_OUT_OF_BOUNDS_EXPECTED = "Index out of bounds exception expected"; //$NON-NLS-1$
30  
31    /**
32     * Test equals constant.
33     */
34    private static final String TEST_EQUALS = "testEquals"; //$NON-NLS-1$
35  
36    /**
37     * Zero position constant.
38     */
39    private static final String ZERO = "0.0 0.0 0.0"; //$NON-NLS-1$
40  
41    /**
42     * Latitute error constant.
43     */
44    private static final String LATITUTE_ERROR = "Latitute error!"; //$NON-NLS-1$
45  
46    /**
47     * Longitute error constant.
48     */
49    private static final String LONGITUTE_ERROR = "Longitute error!"; //$NON-NLS-1$
50  
51    /**
52     * Altitude error constant.
53     */
54    private static final String ALTITUDE_ERROR = "Altitude error!"; //$NON-NLS-1$
55  
56    /**
57     * Suppress sonarqube warning.
58     */
59    private static final String JAVA_S5785 = "java:S5785"; //$NON-NLS-1$
60  
61  
62    /**
63     * Default constructor.
64     */
65    /* default */ WGS84PositionTests()
66     {
67      super();
68     }
69  
70  
71    /**
72     * Factory string test.
73     */
74    @Test
75    /* default */ void testFactory1()
76     {
77      final WGS84Position pos = WGS84Position.of(ZERO);
78      assertAll("factory", //$NON-NLS-1$
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     * Factory string test.
88     */
89    @Test
90    /* default */ void testFactory2()
91     {
92      assertThrows(IllegalArgumentException.class, () ->
93       {
94        /* final WGS84Position pos = */ WGS84Position.of("0.0");
95       }, "Illegal argument excption"
96      );
97     }
98  
99  
100   /**
101    * Is WGS84 position.
102    */
103   @Test
104   /* default */ void testIsWGS84Position()
105    {
106     final WGS84Position pos = WGS84Position.of(0.0, 0.0, 0.0);
107     assertAll("testIsPosition", //$NON-NLS-1$
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    * Is not a WGS84 position.
117    */
118   @Test
119   /* default */ void testIsNotWGS84Position1()
120    {
121     assertThrows(IndexOutOfBoundsException.class, () ->
122      {
123       /* final WGS84Position pos = */ WGS84Position.of(-90.1, 0.0, 0.0);
124      }, WGS84PositionTests.INDEX_OUT_OF_BOUNDS_EXPECTED
125     );
126    }
127 
128 
129   /**
130    * Is not a WGS84 position.
131    */
132   @Test
133   /* default */ void testIsNotWGS84Position2()
134    {
135     assertThrows(IndexOutOfBoundsException.class, () ->
136      {
137       /* final WGS84Position pos = */ WGS84Position.of(90.1, 0.0, 0.0);
138      }, WGS84PositionTests.INDEX_OUT_OF_BOUNDS_EXPECTED
139     );
140    }
141 
142 
143   /**
144    * Is not a WGS84 position.
145    */
146   @Test
147   /* default */ void testIsNotWGS84Position3()
148    {
149     assertThrows(IndexOutOfBoundsException.class, () ->
150      {
151       /* final WGS84Position pos = */ WGS84Position.of(0.0, -180.1, 0.0);
152      }, WGS84PositionTests.INDEX_OUT_OF_BOUNDS_EXPECTED
153     );
154    }
155 
156 
157   /**
158    * Is not a WGS84 position.
159    */
160   @Test
161   /* default */ void testIsNotWGS84Position4()
162    {
163     assertThrows(IndexOutOfBoundsException.class, () ->
164      {
165       /* final WGS84Position pos = */ WGS84Position.of(0.0, 180.1, 0.0);
166      }, WGS84PositionTests.INDEX_OUT_OF_BOUNDS_EXPECTED
167     );
168    }
169 
170 
171   /**
172    * Test stringValue.
173    */
174   @Test
175   /* default */ void testStringValue()
176    {
177     assertEquals(ZERO, WGS84Position.of(0.0, 0.0, 0.0).stringValue(), "Result not as expected");
178    }
179 
180 
181   /**
182    * Test hash code.
183    */
184   @Test
185   /* default */ 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", //$NON-NLS-1$
191       () -> assertEquals(pos1.hashCode(), pos2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
192       () -> assertNotEquals(pos1.hashCode(), pos3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
193     );
194    }
195 
196 
197   /**
198    * Test equals.
199    */
200   @Test
201   @SuppressWarnings(JAVA_S5785)
202   /* default */ 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"), //$NON-NLS-1$
210       () -> assertTrue(pos1.equals(pos2), "pos12 are not equal"), //$NON-NLS-1$
211       () -> assertTrue(pos2.equals(pos1), "pos21 are not equal"), //$NON-NLS-1$
212       () -> assertTrue(pos2.equals(pos4), "pos24 are not equal"), //$NON-NLS-1$
213       () -> assertTrue(pos1.equals(pos4), "pos14 are not equal"), //$NON-NLS-1$
214       () -> assertFalse(pos1.equals(pos3), "pos13 are equal"), //$NON-NLS-1$
215       () -> assertFalse(pos3.equals(pos1), "pos31 are equal"), //$NON-NLS-1$
216       () -> assertFalse(pos1.equals(null), "pos10 is equal") //$NON-NLS-1$
217     );
218    }
219 
220 
221   /**
222    * Test equals.
223    */
224   @Test
225   @SuppressWarnings(JAVA_S5785)
226   /* default */ 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") //$NON-NLS-1$
232     );
233    }
234 
235 
236   /**
237    * Test not equals.
238    */
239   @Test
240   @SuppressWarnings(JAVA_S5785)
241   /* default */ 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", //$NON-NLS-1$
247       () -> assertFalse(pos1.equals(pos2), "pos12 is equal"), //$NON-NLS-1$
248       () -> assertFalse(pos1.equals(pos3), "pos13 is equal") //$NON-NLS-1$
249     );
250    }
251 
252 
253   /**
254    * Test toString.
255    */
256   @Test
257   /* default */ 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"); //$NON-NLS-1$ //$NON-NLS-2$
261    }
262 
263 
264   /**
265    * Test compareTo.
266    */
267   @Test
268   @SuppressWarnings(JAVA_S5785)
269   /* default */ 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", //$NON-NLS-1$
277       () -> assertTrue(pos1.compareTo(pos2) == -pos2.compareTo(pos1), "reflexive1"), //$NON-NLS-1$
278       () -> assertTrue(pos1.compareTo(pos3) == -pos3.compareTo(pos1), "reflexive2"), //$NON-NLS-1$
279       () -> assertTrue((pos4.compareTo(pos3) > 0) && (pos3.compareTo(pos1) > 0) && (pos4.compareTo(pos1) > 0), "transitive1"), //$NON-NLS-1$
280       () -> assertTrue((pos1.compareTo(pos2) == 0) && (Math.abs(pos1.compareTo(pos5)) == Math.abs(pos2.compareTo(pos5))), "sgn1"), //$NON-NLS-1$
281       () -> assertTrue((pos1.compareTo(pos2) == 0) && pos1.equals(pos2), "equals") //$NON-NLS-1$
282     );
283    }
284 
285 
286   /**
287    * Test not compareTo.
288    */
289   @Test
290   @SuppressWarnings(JAVA_S5785)
291   /* default */ 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", //$NON-NLS-1$
296       () -> assertTrue(pos1.compareTo(pos2) != 0, "equal") //$NON-NLS-1$
297     );
298    }
299 
300  }