Skip to content

Commit 4522abf

Browse files
committed
addition of further unit tests
1 parent 82f9f03 commit 4522abf

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
<version>1.5.16</version>
6666
<scope>test</scope>
6767
</dependency>
68+
<dependency>
69+
<groupId>org.mockito</groupId>
70+
<artifactId>mockito-core</artifactId>
71+
<version>5.14.2</version>
72+
<scope>test</scope>
73+
</dependency>
6874
</dependencies>
6975

7076
<scm>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package de.taimos.gpsd4java.api;
2+
3+
import de.taimos.gpsd4java.types.TPVObject;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.mockito.Mockito;
7+
8+
public class DistanceListenerTest {
9+
10+
@Test
11+
public void testDistanceListener() {
12+
13+
final boolean[] called = new boolean[1];
14+
15+
final DistanceListener distanceListener =
16+
new DistanceListener(100.0) {
17+
@Override
18+
protected void handleLocation(final TPVObject tpv) {
19+
called[0] = true;
20+
}
21+
};
22+
23+
final TPVObject tpvObject1 = Mockito.mock(TPVObject.class);
24+
Mockito.when(tpvObject1.getLongitude()).thenReturn(1.0);
25+
Mockito.when(tpvObject1.getLatitude()).thenReturn(1.0);
26+
Mockito.when(tpvObject1.getLongitude()).thenReturn(1.0);
27+
Mockito.when(tpvObject1.getLongitude()).thenReturn(1.0);
28+
29+
final TPVObject tpvObject2 = Mockito.mock(TPVObject.class);
30+
Mockito.when(tpvObject1.getLongitude()).thenReturn(2.0);
31+
Mockito.when(tpvObject1.getLatitude()).thenReturn(2.0);
32+
Mockito.when(tpvObject1.getLongitude()).thenReturn(2.0);
33+
Mockito.when(tpvObject1.getLongitude()).thenReturn(2.0);
34+
35+
// first call, lastPosition is null, should call handleLocation
36+
called[0] = false;
37+
distanceListener.handleTPV(tpvObject1);
38+
Assert.assertTrue(called[0]);
39+
40+
// second call, lastPosition is set, same position so no call
41+
called[0] = false;
42+
distanceListener.handleTPV(tpvObject1);
43+
Assert.assertFalse(called[0]);
44+
45+
// third call, lastPosition is set, other position so should call
46+
called[0] = false;
47+
distanceListener.handleTPV(tpvObject2);
48+
Assert.assertTrue(called[0]);
49+
}
50+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package de.taimos.gpsd4java.backend;
2+
3+
import de.taimos.gpsd4java.types.TPVObject;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.mockito.Mockito;
7+
8+
public class GISToolTest {
9+
10+
@Test
11+
public void testDistance() {
12+
13+
// the distance between 1,1 and 2,2 is about 157 kilometers
14+
15+
final TPVObject one = Mockito.mock(TPVObject.class);
16+
Mockito.when(one.getLongitude()).thenReturn(1.0);
17+
Mockito.when(one.getLatitude()).thenReturn(1.0);
18+
19+
final TPVObject two = Mockito.mock(TPVObject.class);
20+
Mockito.when(two.getLongitude()).thenReturn(2.0);
21+
Mockito.when(two.getLatitude()).thenReturn(2.0);
22+
23+
Assert.assertEquals(157.2254320380729, GISTool.getDistance(one, two), 0.0);
24+
25+
Assert.assertEquals(0, GISTool.getDistance(one, one), 0.0);
26+
Assert.assertEquals(0, GISTool.getDistance(two, two), 0.0);
27+
}
28+
}

src/test/resources/logback.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE configuration>
3+
4+
<configuration>
5+
<import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/>
6+
<import class="ch.qos.logback.core.ConsoleAppender"/>
7+
8+
<appender name="STDOUT" class="ConsoleAppender">
9+
<encoder class="PatternLayoutEncoder">
10+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%kvp- %msg%n</pattern>
11+
</encoder>
12+
</appender>
13+
14+
<root level="INFO">
15+
<appender-ref ref="STDOUT"/>
16+
</root>
17+
</configuration>

0 commit comments

Comments
 (0)