diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2019-02-23 15:23:35 -0800 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2019-02-23 15:23:35 -0800 |
commit | 563243a8da888244e910a4a7a10fb86ad525fdd4 (patch) | |
tree | 0a50db4a2ca280d3cd3b2b78065d31ef96e5fef5 /test/org/traccar/handler/ComputedAttributesTest.java | |
parent | 7aa3760f02361a04e2c3b73ba5b9cc41bcb8ec3d (diff) | |
download | trackermap-server-563243a8da888244e910a4a7a10fb86ad525fdd4.tar.gz trackermap-server-563243a8da888244e910a4a7a10fb86ad525fdd4.tar.bz2 trackermap-server-563243a8da888244e910a4a7a10fb86ad525fdd4.zip |
Handler refactoring
Diffstat (limited to 'test/org/traccar/handler/ComputedAttributesTest.java')
-rw-r--r-- | test/org/traccar/handler/ComputedAttributesTest.java | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/test/org/traccar/handler/ComputedAttributesTest.java b/test/org/traccar/handler/ComputedAttributesTest.java new file mode 100644 index 000000000..8f4f69bcd --- /dev/null +++ b/test/org/traccar/handler/ComputedAttributesTest.java @@ -0,0 +1,68 @@ +package org.traccar.handler; + +import java.util.Date; + +import org.junit.Test; +import org.traccar.model.Attribute; +import org.traccar.model.Position; + +import static org.junit.Assert.assertEquals; + +public class ComputedAttributesTest { + + @Test + public void testComputedAttributes() { + Position position = new Position(); + ComputedAttributesHandler computedAttributesHandler = new ComputedAttributesHandler(); + Date date = new Date(); + position.setTime(date); + position.setSpeed(42); + position.setValid(false); + position.set("adc1", 128); + position.set("booleanFlag", true); + position.set("adc2", 100); + position.set("bitFlag", 7); + position.set("event", 42); + position.set("result", "success"); + Attribute attribute = new Attribute(); + + attribute.setExpression("adc1"); + assertEquals(128, computedAttributesHandler.computeAttribute(attribute, position)); + + attribute.setExpression("!booleanFlag"); + assertEquals(false, computedAttributesHandler.computeAttribute(attribute, position)); + + attribute.setExpression("adc2 * 2 + 50"); + assertEquals(250, computedAttributesHandler.computeAttribute(attribute, position)); + + attribute.setExpression("(bitFlag & 4) != 0"); + assertEquals(true, computedAttributesHandler.computeAttribute(attribute, position)); + + attribute.setExpression("if (event == 42) \"lowBattery\""); + assertEquals("lowBattery", computedAttributesHandler.computeAttribute(attribute, position)); + + attribute.setExpression("speed > 5 && valid"); + assertEquals(false, computedAttributesHandler.computeAttribute(attribute, position)); + + attribute.setExpression("fixTime"); + assertEquals(date, computedAttributesHandler.computeAttribute(attribute, position)); + + attribute.setExpression("math:pow(adc1, 2)"); + assertEquals(16384.0, computedAttributesHandler.computeAttribute(attribute, position)); + + // modification tests + attribute.setExpression("adc1 = 256"); + computedAttributesHandler.computeAttribute(attribute, position); + assertEquals(128, position.getInteger("adc1")); + + attribute.setExpression("result = \"fail\""); + computedAttributesHandler.computeAttribute(attribute, position); + assertEquals("success", position.getString("result")); + + attribute.setExpression("fixTime = \"2017-10-18 10:00:01\""); + computedAttributesHandler.computeAttribute(attribute, position); + assertEquals(date, position.getFixTime()); + + } + +} |