diff options
-rw-r--r-- | test/org/traccar/BaseTest.java | 60 | ||||
-rw-r--r-- | test/org/traccar/FilterHandlerTest.java | 56 | ||||
-rw-r--r-- | test/org/traccar/TestIdentityManager.java | 122 | ||||
-rw-r--r-- | test/org/traccar/protocol/Tk103ProtocolEncoderTest.java | 65 |
4 files changed, 182 insertions, 121 deletions
diff --git a/test/org/traccar/BaseTest.java b/test/org/traccar/BaseTest.java index 4b9ee5451..fb1b5de8f 100644 --- a/test/org/traccar/BaseTest.java +++ b/test/org/traccar/BaseTest.java @@ -1,67 +1,9 @@ package org.traccar; -import org.traccar.database.IdentityManager; -import org.traccar.model.Device; -import org.traccar.model.Position; - public class BaseTest { static { - Context.init(new IdentityManager() { - - private Device createDevice() { - Device device = new Device(); - device.setId(1); - device.setName("test"); - device.setUniqueId("123456789012345"); - return device; - } - - @Override - public Device getById(long id) { - return createDevice(); - } - - @Override - public Device getByUniqueId(String uniqueId) { - return createDevice(); - } - - @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 lookupConfig) { - return defaultValue; - } - - @Override - public String lookupAttributeString( - long deviceId, String attributeName, String defaultValue, boolean lookupConfig) { - return defaultValue; - } - - @Override - public int lookupAttributeInteger( - long deviceId, String attributeName, int defaultValue, boolean lookupConfig) { - return defaultValue; - } - - @Override - public long lookupAttributeLong( - long deviceId, String attributeName, long defaultValue, boolean lookupConfig) { - return defaultValue; - } - - }); + Context.init(new TestIdentityManager()); } } diff --git a/test/org/traccar/FilterHandlerTest.java b/test/org/traccar/FilterHandlerTest.java index 7ebab3af5..0d488a7fb 100644 --- a/test/org/traccar/FilterHandlerTest.java +++ b/test/org/traccar/FilterHandlerTest.java @@ -15,61 +15,7 @@ import static org.junit.Assert.assertNull; public class FilterHandlerTest { static { - Context.init(new IdentityManager() { - - private Device createDevice() { - Device device = new Device(); - device.setId(1); - device.setName("test"); - device.setUniqueId("123456789012345"); - return device; - } - - @Override - public Device getById(long id) { - return createDevice(); - } - - @Override - public Device getByUniqueId(String uniqueId) { - return createDevice(); - } - - @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 lookupConfig) { - return defaultValue; - } - - @Override - public String lookupAttributeString( - long deviceId, String attributeName, String defaultValue, boolean lookupConfig) { - return "alarm,result"; - } - - @Override - public int lookupAttributeInteger( - long deviceId, String attributeName, int defaultValue, boolean lookupConfig) { - return defaultValue; - } - - @Override - public long lookupAttributeLong( - long deviceId, String attributeName, long defaultValue, boolean lookupConfig) { - return defaultValue; - } - - }); + Context.init(new TestIdentityManager()); } private FilterHandler filtingHandler; diff --git a/test/org/traccar/TestIdentityManager.java b/test/org/traccar/TestIdentityManager.java new file mode 100644 index 000000000..82bcf5c63 --- /dev/null +++ b/test/org/traccar/TestIdentityManager.java @@ -0,0 +1,122 @@ +package org.traccar; + +import org.traccar.database.*; +import org.traccar.model.*; +import java.util.concurrent.ConcurrentHashMap; + +import java.util.Map; + +public final class TestIdentityManager implements IdentityManager { + + private static Map<Long, Device> devicesById = new ConcurrentHashMap<>(); + + private static final long TEST_DEVICE_ID_START = 100; + private static final String TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX = "555"; + private static long newDeviceId = TEST_DEVICE_ID_START; + + public TestIdentityManager() { + } + + public static Device createTestDevice() { + Device device = new Device(); + device.setId(newDeviceId); + device.setName("TestDevice" + newDeviceId); + device.setUniqueId(TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX + newDeviceId); + devicesById.put(newDeviceId, device); + newDeviceId++; + return device; + } + + private Device createDefaultTestDevice() { + Device device = new Device(); + device.setId(1); + device.setName("test"); + device.setUniqueId("123456789012345"); + return device; + } + + @Override + public Device getById(long id) { + return id < TEST_DEVICE_ID_START ? createDefaultTestDevice() : devicesById.get(id); + } + + @Override + public Device getByUniqueId(String uniqueId) { + if (uniqueId.length() > TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX.length() + && uniqueId.substring(0, TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX.length()) + .equals(TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX)) { + try { + long id = Long.parseLong( + uniqueId.substring(TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX.length(), uniqueId.length())); + if (id >= TEST_DEVICE_ID_START) { + return devicesById.get(id); + } + } catch (NumberFormatException e) {} + } + return createDefaultTestDevice(); + } + + @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 lookupConfig) { + if (deviceId < TEST_DEVICE_ID_START) { + return defaultValue; + } + + Object result = devicesById.get(deviceId).getAttributes().get(attributeName); + if (result != null) { + return result instanceof String ? Boolean.parseBoolean((String) result) : (Boolean) result; + } + return defaultValue; + } + + @Override + public String lookupAttributeString( + long deviceId, String attributeName, String defaultValue, boolean lookupConfig) { + if (deviceId < TEST_DEVICE_ID_START) { + return "alarm,result"; + } + + Object result = devicesById.get(deviceId).getAttributes().get(attributeName); + return result != null ? (String) result : defaultValue; + } + + @Override + public int lookupAttributeInteger( + long deviceId, String attributeName, int defaultValue, boolean lookupConfig) { + if (deviceId < TEST_DEVICE_ID_START) { + return defaultValue; + } + + Object result = devicesById.get(deviceId).getAttributes().get(attributeName); + if (result != null) { + return result instanceof String ? Integer.parseInt((String) result) : ((Number) result).intValue(); + } + return defaultValue; + } + + @Override + public long lookupAttributeLong( + long deviceId, String attributeName, long defaultValue, boolean lookupConfig) { + if (deviceId < TEST_DEVICE_ID_START) { + return defaultValue; + } + + Object result = devicesById.get(deviceId).getAttributes().get(attributeName); + if (result != null) { + return result instanceof String ? Long.parseLong((String) result) : ((Number) result).longValue(); + } + return defaultValue; + } + +} diff --git a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java index 0c16ccee6..a58cd9ee1 100644 --- a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java @@ -3,7 +3,9 @@ package org.traccar.protocol; import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; +import org.traccar.TestIdentityManager; import org.traccar.model.Command; +import org.traccar.model.Device; public class Tk103ProtocolEncoderTest extends ProtocolTest { @@ -31,6 +33,12 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AP00)", encoder.encodeCommand(command)); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + command.setDeviceId(device.getId()); + + Assert.assertEquals("[begin]sms2,*getposl*,[end]", encoder.encodeCommand(command)); + } @Test @@ -45,6 +53,12 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AR00003C0000)", encoder.encodeCommand(command)); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + command.setDeviceId(device.getId()); + + Assert.assertEquals("[begin]sms2,*routetrack*99*,[end]", encoder.encodeCommand(command)); + } @Test @@ -58,6 +72,12 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AR0000000000)", encoder.encodeCommand(command)); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + command.setDeviceId(device.getId()); + + Assert.assertEquals("[begin]sms2,*routetrackoff*,[end]", encoder.encodeCommand(command)); + } @Test @@ -71,6 +91,12 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AP07)", encoder.encodeCommand(command)); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + command.setDeviceId(device.getId()); + + Assert.assertEquals("[begin]sms2,*about*,[end]", encoder.encodeCommand(command)); + } @Test @@ -84,6 +110,12 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AT00)", encoder.encodeCommand(command)); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + command.setDeviceId(device.getId()); + + Assert.assertEquals("[begin]sms2,88888888,[end]", encoder.encodeCommand(command)); + } @Test @@ -104,8 +136,11 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(device.getId()); command.setType(Command.TYPE_IDENTIFICATION); Assert.assertEquals("[begin]sms2,999999,[end]", encoder.encodeCommand(command)); @@ -117,8 +152,11 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(device.getId()); command.setType(Command.TYPE_ALARM_SOS); command.set(Command.KEY_ENABLE, true); @@ -131,8 +169,11 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(device.getId()); command.setType(Command.TYPE_ALARM_SOS); command.set(Command.KEY_ENABLE, false); @@ -145,8 +186,11 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(device.getId()); command.setType(Command.TYPE_CUSTOM); command.set(Command.KEY_DATA, "any text is ok"); @@ -159,8 +203,11 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(device.getId()); command.setType(Command.TYPE_SET_CONNECTION); command.set(Command.KEY_SERVER, "1.2.3.4"); command.set(Command.KEY_PORT, "5555"); @@ -174,13 +221,17 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + device.set(Command.KEY_DEVICE_PASSWORD, "232323"); + Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(device.getId()); command.setType(Command.TYPE_SOS_NUMBER); command.set(Command.KEY_INDEX, "0"); command.set(Command.KEY_PHONE, "+55555555555"); - Assert.assertEquals("[begin]sms2,*master*123456*+55555555555*,[end]", encoder.encodeCommand(command)); + Assert.assertEquals("[begin]sms2,*master*232323*+55555555555*,[end]", encoder.encodeCommand(command)); } |