Skip to content

Commit

Permalink
addition of further unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0x08 committed Feb 12, 2025
1 parent 82f9f03 commit 4522abf
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
<version>1.5.16</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.14.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<scm>
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/de/taimos/gpsd4java/api/DistanceListenerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package de.taimos.gpsd4java.api;

import de.taimos.gpsd4java.types.TPVObject;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

public class DistanceListenerTest {

@Test
public void testDistanceListener() {

final boolean[] called = new boolean[1];

final DistanceListener distanceListener =
new DistanceListener(100.0) {
@Override
protected void handleLocation(final TPVObject tpv) {
called[0] = true;
}
};

final TPVObject tpvObject1 = Mockito.mock(TPVObject.class);
Mockito.when(tpvObject1.getLongitude()).thenReturn(1.0);
Mockito.when(tpvObject1.getLatitude()).thenReturn(1.0);
Mockito.when(tpvObject1.getLongitude()).thenReturn(1.0);
Mockito.when(tpvObject1.getLongitude()).thenReturn(1.0);

final TPVObject tpvObject2 = Mockito.mock(TPVObject.class);
Mockito.when(tpvObject1.getLongitude()).thenReturn(2.0);
Mockito.when(tpvObject1.getLatitude()).thenReturn(2.0);
Mockito.when(tpvObject1.getLongitude()).thenReturn(2.0);
Mockito.when(tpvObject1.getLongitude()).thenReturn(2.0);

// first call, lastPosition is null, should call handleLocation
called[0] = false;
distanceListener.handleTPV(tpvObject1);
Assert.assertTrue(called[0]);

// second call, lastPosition is set, same position so no call
called[0] = false;
distanceListener.handleTPV(tpvObject1);
Assert.assertFalse(called[0]);

// third call, lastPosition is set, other position so should call
called[0] = false;
distanceListener.handleTPV(tpvObject2);
Assert.assertTrue(called[0]);
}
}
28 changes: 28 additions & 0 deletions src/test/java/de/taimos/gpsd4java/backend/GISToolTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.taimos.gpsd4java.backend;

import de.taimos.gpsd4java.types.TPVObject;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

public class GISToolTest {

@Test
public void testDistance() {

// the distance between 1,1 and 2,2 is about 157 kilometers

final TPVObject one = Mockito.mock(TPVObject.class);
Mockito.when(one.getLongitude()).thenReturn(1.0);
Mockito.when(one.getLatitude()).thenReturn(1.0);

final TPVObject two = Mockito.mock(TPVObject.class);
Mockito.when(two.getLongitude()).thenReturn(2.0);
Mockito.when(two.getLatitude()).thenReturn(2.0);

Assert.assertEquals(157.2254320380729, GISTool.getDistance(one, two), 0.0);

Assert.assertEquals(0, GISTool.getDistance(one, one), 0.0);
Assert.assertEquals(0, GISTool.getDistance(two, two), 0.0);
}
}
17 changes: 17 additions & 0 deletions src/test/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration>

<configuration>
<import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/>
<import class="ch.qos.logback.core.ConsoleAppender"/>

<appender name="STDOUT" class="ConsoleAppender">
<encoder class="PatternLayoutEncoder">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%kvp- %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>

0 comments on commit 4522abf

Please sign in to comment.