aboutsummaryrefslogtreecommitdiff
path: root/test/org
diff options
context:
space:
mode:
Diffstat (limited to 'test/org')
-rw-r--r--test/org/traccar/TestIdentityManager.java76
1 files changed, 16 insertions, 60 deletions
diff --git a/test/org/traccar/TestIdentityManager.java b/test/org/traccar/TestIdentityManager.java
index 82bcf5c63..c613dcb54 100644
--- a/test/org/traccar/TestIdentityManager.java
+++ b/test/org/traccar/TestIdentityManager.java
@@ -1,7 +1,8 @@
package org.traccar;
-import org.traccar.database.*;
-import org.traccar.model.*;
+import org.traccar.database.IdentityManager;
+import org.traccar.model.Device;
+import org.traccar.model.Position;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;
@@ -9,51 +10,34 @@ 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;
+ private static long newDeviceId = 100;
public TestIdentityManager() {
}
- public static Device createTestDevice() {
+ private static Device createDevice(long id) {
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++;
+ device.setId(id);
+ device.setName("test");
+ device.setUniqueId("123456789012345");
return device;
}
- private Device createDefaultTestDevice() {
- Device device = new Device();
- device.setId(1);
- device.setName("test");
- device.setUniqueId("123456789012345");
+ public static Device createTestDevice() {
+ Device device = createDevice(newDeviceId);
+ devicesById.put(newDeviceId++, device);
return device;
}
@Override
public Device getById(long id) {
- return id < TEST_DEVICE_ID_START ? createDefaultTestDevice() : devicesById.get(id);
+ Device device = devicesById.get(id);
+ return device != null ? device : createDevice(1);
}
@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();
+ return createDevice(1);
}
@Override
@@ -69,54 +53,26 @@ public final class TestIdentityManager implements IdentityManager {
@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;
+ Device device = devicesById.get(deviceId);
+ return device != null ? device.getBoolean(attributeName) : 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;
}
}