diff options
Diffstat (limited to 'src/test/java/org')
6 files changed, 47 insertions, 102 deletions
diff --git a/src/test/java/org/traccar/BaseTest.java b/src/test/java/org/traccar/BaseTest.java index 2eedd4bd8..0f6c55857 100644 --- a/src/test/java/org/traccar/BaseTest.java +++ b/src/test/java/org/traccar/BaseTest.java @@ -1,7 +1,5 @@ package org.traccar; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import org.traccar.config.Config; import org.traccar.database.ConnectionManager; import org.traccar.database.IdentityManager; @@ -18,10 +16,6 @@ import static org.mockito.Mockito.when; public class BaseTest { - static { - Context.init(new TestIdentityManager()); - } - protected <T extends BaseProtocolDecoder> T inject(T decoder) throws Exception { decoder.setConfig(new Config()); var device = mock(Device.class); diff --git a/src/test/java/org/traccar/TestIdentityManager.java b/src/test/java/org/traccar/TestIdentityManager.java deleted file mode 100644 index 68d98db9a..000000000 --- a/src/test/java/org/traccar/TestIdentityManager.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.traccar; - -import org.traccar.database.IdentityManager; -import org.traccar.model.Device; -import org.traccar.model.Position; - -public final class TestIdentityManager implements IdentityManager { - - private static Device createDevice() { - Device device = new Device(); - device.setId(1); - device.setName("test"); - device.setUniqueId("123456789012345"); - return device; - } - - @Override - public long addUnknownDevice(String uniqueId) { - return 1; - } - - @Override - public Device getById(long id) { - return createDevice(); - } - - @Override - public Device getByUniqueId(String uniqueId) { - return createDevice(); - } - - @Override - public String getDevicePassword(long id, String protocol, String defaultPassword) { - return defaultPassword; - } - - @Override - public Position getLastPosition(long deviceId) { - return null; - } - - @Override - public boolean isLatestPosition(Position position) { - return true; - } - - @Override - public boolean lookupAttributeBoolean( - long deviceId, String attributeName, boolean defaultValue, boolean lookupServer, boolean lookupConfig) { - return defaultValue; - } - - @Override - public String lookupAttributeString( - long deviceId, String attributeName, String defaultValue, boolean lookupServer, boolean lookupConfig) { - return defaultValue; - } - - @Override - public int lookupAttributeInteger( - long deviceId, String attributeName, int defaultValue, boolean lookupServer, boolean lookupConfig) { - return defaultValue; - } - - @Override - public long lookupAttributeLong( - long deviceId, String attributeName, long defaultValue, boolean lookupServer, boolean lookupConfig) { - return defaultValue; - } - - @Override - public double lookupAttributeDouble( - long deviceId, String attributeName, double defaultValue, boolean lookupServer, boolean lookupConfig) { - return defaultValue; - } - -} diff --git a/src/test/java/org/traccar/WebDataHandlerTest.java b/src/test/java/org/traccar/WebDataHandlerTest.java index cfbd71f23..aaec9f530 100644 --- a/src/test/java/org/traccar/WebDataHandlerTest.java +++ b/src/test/java/org/traccar/WebDataHandlerTest.java @@ -3,9 +3,14 @@ package org.traccar; import org.junit.Test; import org.traccar.config.Config; import org.traccar.config.Keys; +import org.traccar.database.IdentityManager; +import org.traccar.model.Device; import org.traccar.model.Position; import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class WebDataHandlerTest extends ProtocolTest { @@ -17,7 +22,15 @@ public class WebDataHandlerTest extends ProtocolTest { Position position = position("2016-01-01 01:02:03.000", true, 20, 30); - WebDataHandler handler = new WebDataHandler(config, Context.getIdentityManager(), null, null); + var device = mock(Device.class); + when(device.getId()).thenReturn(1L); + when(device.getName()).thenReturn("test"); + when(device.getUniqueId()).thenReturn("123456789012345"); + when(device.getStatus()).thenReturn(Device.STATUS_ONLINE); + var identityManager = mock(IdentityManager.class); + when(identityManager.getById(anyLong())).thenReturn(device); + + WebDataHandler handler = new WebDataHandler(config, identityManager, null, null); assertEquals( "http://localhost/?fixTime=1451610123000&gprmc=$GPRMC,010203.000,A,2000.0000,N,03000.0000,E,0.00,0.00,010116,,*05&name=test", diff --git a/src/test/java/org/traccar/handler/events/AlertEventHandlerTest.java b/src/test/java/org/traccar/handler/events/AlertEventHandlerTest.java index 3f0823245..4934695ad 100644 --- a/src/test/java/org/traccar/handler/events/AlertEventHandlerTest.java +++ b/src/test/java/org/traccar/handler/events/AlertEventHandlerTest.java @@ -2,13 +2,14 @@ package org.traccar.handler.events; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.mock; import java.util.Map; import org.junit.Test; import org.traccar.BaseTest; -import org.traccar.TestIdentityManager; import org.traccar.config.Config; +import org.traccar.database.IdentityManager; import org.traccar.model.Event; import org.traccar.model.Position; @@ -17,7 +18,7 @@ public class AlertEventHandlerTest extends BaseTest { @Test public void testAlertEventHandler() { - AlertEventHandler alertEventHandler = new AlertEventHandler(new Config(), new TestIdentityManager()); + AlertEventHandler alertEventHandler = new AlertEventHandler(new Config(), mock(IdentityManager.class)); Position position = new Position(); position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); @@ -25,6 +26,7 @@ public class AlertEventHandlerTest extends BaseTest { assertNotNull(events); Event event = events.keySet().iterator().next(); assertEquals(Event.TYPE_ALARM, event.getType()); + } } diff --git a/src/test/java/org/traccar/handler/events/IgnitionEventHandlerTest.java b/src/test/java/org/traccar/handler/events/IgnitionEventHandlerTest.java index dade20fb8..f568b6a74 100644 --- a/src/test/java/org/traccar/handler/events/IgnitionEventHandlerTest.java +++ b/src/test/java/org/traccar/handler/events/IgnitionEventHandlerTest.java @@ -1,12 +1,13 @@ package org.traccar.handler.events; import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; import java.util.Map; import org.junit.Test; import org.traccar.BaseTest; -import org.traccar.TestIdentityManager; +import org.traccar.database.IdentityManager; import org.traccar.model.Event; import org.traccar.model.Position; @@ -15,7 +16,7 @@ public class IgnitionEventHandlerTest extends BaseTest { @Test public void testIgnitionEventHandler() { - IgnitionEventHandler ignitionEventHandler = new IgnitionEventHandler(new TestIdentityManager()); + IgnitionEventHandler ignitionEventHandler = new IgnitionEventHandler(mock(IdentityManager.class)); Position position = new Position(); position.set(Position.KEY_IGNITION, true); diff --git a/src/test/java/org/traccar/reports/ReportUtilsTest.java b/src/test/java/org/traccar/reports/ReportUtilsTest.java index deb17ddd2..b27104f76 100644 --- a/src/test/java/org/traccar/reports/ReportUtilsTest.java +++ b/src/test/java/org/traccar/reports/ReportUtilsTest.java @@ -4,6 +4,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.text.DateFormat; import java.text.ParseException; @@ -17,7 +20,8 @@ import java.util.TimeZone; import org.junit.Test; import org.traccar.BaseTest; -import org.traccar.TestIdentityManager; +import org.traccar.database.IdentityManager; +import org.traccar.model.Device; import org.traccar.model.Position; import org.traccar.reports.model.StopReport; import org.traccar.reports.model.TripReport; @@ -43,6 +47,14 @@ public class ReportUtilsTest extends BaseTest { return position; } + private IdentityManager mockIdentityManager() { + var device = mock(Device.class); + when(device.getName()).thenReturn("test"); + var identityManager = mock(IdentityManager.class); + when(identityManager.getById(anyLong())).thenReturn(device); + return identityManager; + } + @Test public void testCalculateDistance() { Position startPosition = new Position(); @@ -81,7 +93,7 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, 900000, false, false, 0.01); Collection<TripReport> trips = ReportUtils.detectTripsAndStops( - new TestIdentityManager(), null, data, tripsConfig, false, TripReport.class); + mockIdentityManager(), null, data, tripsConfig, false, TripReport.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -96,7 +108,7 @@ public class ReportUtilsTest extends BaseTest { assertEquals(3000, itemTrip.getDistance(), 0.01); Collection<StopReport> stops = ReportUtils.detectTripsAndStops( - new TestIdentityManager(), null, data, tripsConfig, false, StopReport.class); + mockIdentityManager(), null, data, tripsConfig, false, StopReport.class); assertNotNull(stops); assertFalse(stops.isEmpty()); @@ -135,7 +147,7 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, 900000, true, false, 0.01); Collection<TripReport> trips = ReportUtils.detectTripsAndStops( - new TestIdentityManager(), null, data, tripsConfig, false, TripReport.class); + mockIdentityManager(), null, data, tripsConfig, false, TripReport.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -150,7 +162,7 @@ public class ReportUtilsTest extends BaseTest { assertEquals(3000, itemTrip.getDistance(), 0.01); trips = ReportUtils.detectTripsAndStops( - new TestIdentityManager(), null, data, tripsConfig, false, TripReport.class); + mockIdentityManager(), null, data, tripsConfig, false, TripReport.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -165,7 +177,7 @@ public class ReportUtilsTest extends BaseTest { assertEquals(3000, itemTrip.getDistance(), 0.01); Collection<StopReport> stops = ReportUtils.detectTripsAndStops( - new TestIdentityManager(), null, data, tripsConfig, false, StopReport.class); + mockIdentityManager(), null, data, tripsConfig, false, StopReport.class); assertNotNull(stops); assertFalse(stops.isEmpty()); @@ -206,7 +218,7 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, 900000, false, false, 0.01); Collection<TripReport> trips = ReportUtils.detectTripsAndStops( - new TestIdentityManager(), null, data, tripsConfig, false, TripReport.class); + mockIdentityManager(), null, data, tripsConfig, false, TripReport.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -221,7 +233,7 @@ public class ReportUtilsTest extends BaseTest { assertEquals(7000, itemTrip.getDistance(), 0.01); Collection<StopReport> stops = ReportUtils.detectTripsAndStops( - new TestIdentityManager(), null, data, tripsConfig, false, StopReport.class); + mockIdentityManager(), null, data, tripsConfig, false, StopReport.class); assertNotNull(stops); assertFalse(stops.isEmpty()); @@ -256,7 +268,7 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01); Collection<StopReport> result = ReportUtils.detectTripsAndStops( - new TestIdentityManager(), null, data, tripsConfig, false, StopReport.class); + mockIdentityManager(), null, data, tripsConfig, false, StopReport.class); assertNotNull(result); assertFalse(result.isEmpty()); @@ -283,7 +295,7 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01); Collection<StopReport> result = ReportUtils.detectTripsAndStops( - new TestIdentityManager(), null, data, tripsConfig, false, StopReport.class); + mockIdentityManager(), null, data, tripsConfig, false, StopReport.class); assertNotNull(result); assertFalse(result.isEmpty()); @@ -310,7 +322,7 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01); Collection<StopReport> result = ReportUtils.detectTripsAndStops( - new TestIdentityManager(), null, data, tripsConfig, false, StopReport.class); + mockIdentityManager(), null, data, tripsConfig, false, StopReport.class); assertNotNull(result); assertFalse(result.isEmpty()); @@ -337,7 +349,7 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01); Collection<StopReport> result = ReportUtils.detectTripsAndStops( - new TestIdentityManager(), null, data, tripsConfig, false, StopReport.class); + mockIdentityManager(), null, data, tripsConfig, false, StopReport.class); assertNotNull(result); assertTrue(result.isEmpty()); @@ -360,7 +372,7 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 200000, 200000, 900000, false, false, 0.01); Collection<TripReport> trips = ReportUtils.detectTripsAndStops( - new TestIdentityManager(), null, data, tripsConfig, false, TripReport.class); + mockIdentityManager(), null, data, tripsConfig, false, TripReport.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -375,7 +387,7 @@ public class ReportUtilsTest extends BaseTest { assertEquals(600, itemTrip.getDistance(), 0.01); Collection<StopReport> stops = ReportUtils.detectTripsAndStops( - new TestIdentityManager(), null, data, tripsConfig, false, StopReport.class); + mockIdentityManager(), null, data, tripsConfig, false, StopReport.class); assertNotNull(stops); assertFalse(stops.isEmpty()); |