diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2019-03-31 22:35:39 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2019-03-31 22:35:39 -0700 |
commit | 59416923dcb3a756eaf532cc4259f2f6625c0762 (patch) | |
tree | 9082dae6616deac8fda432b7bfd80e4a52b6d9dc /src/test/java/org/traccar/helper | |
parent | 79a129dd6327d932133d6b9a50190d3f4927bff9 (diff) | |
download | traccar-server-59416923dcb3a756eaf532cc4259f2f6625c0762.tar.gz traccar-server-59416923dcb3a756eaf532cc4259f2f6625c0762.tar.bz2 traccar-server-59416923dcb3a756eaf532cc4259f2f6625c0762.zip |
Convert project to gradle
Diffstat (limited to 'src/test/java/org/traccar/helper')
12 files changed, 313 insertions, 0 deletions
diff --git a/src/test/java/org/traccar/helper/BcdUtilTest.java b/src/test/java/org/traccar/helper/BcdUtilTest.java new file mode 100644 index 000000000..86a32f725 --- /dev/null +++ b/src/test/java/org/traccar/helper/BcdUtilTest.java @@ -0,0 +1,24 @@ +package org.traccar.helper; + +import io.netty.buffer.Unpooled; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class BcdUtilTest { + + @Test + public void testReadInteger() { + byte[] buf = {0x01, (byte) 0x90, 0x34}; + int result = BcdUtil.readInteger(Unpooled.wrappedBuffer(buf), 5); + assertEquals(1903, result); + } + + @Test + public void testReadCoordinate() { + byte[] buf = {0x03, (byte) 0x85, 0x22, 0x59, 0x34}; + double result = BcdUtil.readCoordinate(Unpooled.wrappedBuffer(buf)); + assertEquals(38.870989, result, 0.00001); + } + +} diff --git a/src/test/java/org/traccar/helper/BitBufferTest.java b/src/test/java/org/traccar/helper/BitBufferTest.java new file mode 100644 index 000000000..c2abad36d --- /dev/null +++ b/src/test/java/org/traccar/helper/BitBufferTest.java @@ -0,0 +1,23 @@ +package org.traccar.helper; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class BitBufferTest { + + @Test + public void test() { + BitBuffer buffer = new BitBuffer(); + + buffer.write(0b100100); + buffer.write(0b110110); + buffer.write(0b111111); + buffer.write(0b111111); + + assertEquals(0b100, buffer.readUnsigned(3)); + assertEquals(-7, buffer.readSigned(4)); + assertEquals(0b10110, buffer.readUnsigned(5)); + } + +} diff --git a/src/test/java/org/traccar/helper/BitUtilTest.java b/src/test/java/org/traccar/helper/BitUtilTest.java new file mode 100644 index 000000000..90431bf55 --- /dev/null +++ b/src/test/java/org/traccar/helper/BitUtilTest.java @@ -0,0 +1,38 @@ +package org.traccar.helper; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class BitUtilTest { + + @Test + public void testCheck() { + assertFalse(BitUtil.check(0, 0)); + assertTrue(BitUtil.check(1, 0)); + assertFalse(BitUtil.check(2, 0)); + } + + @Test + public void testBetween() { + assertEquals(0, BitUtil.between(0, 0, 0)); + assertEquals(1, BitUtil.between(1, 0, 1)); + assertEquals(2, BitUtil.between(2, 0, 2)); + assertEquals(2, BitUtil.between(6, 0, 2)); + } + + @Test + public void testFrom() { + assertEquals(1, BitUtil.from(1, 0)); + assertEquals(0, BitUtil.from(1, 1)); + } + + @Test + public void testTo() { + assertEquals(2, BitUtil.to(2, 2)); + assertEquals(0, BitUtil.to(2, 1)); + } + +} diff --git a/src/test/java/org/traccar/helper/ChecksumTest.java b/src/test/java/org/traccar/helper/ChecksumTest.java new file mode 100644 index 000000000..5737b9ff5 --- /dev/null +++ b/src/test/java/org/traccar/helper/ChecksumTest.java @@ -0,0 +1,39 @@ +package org.traccar.helper; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import org.junit.Test; + +import java.nio.charset.StandardCharsets; + +import static org.junit.Assert.assertEquals; + +public class ChecksumTest { + + @Test + public void testCrc8() { + ByteBuf buf = Unpooled.copiedBuffer("123456789", StandardCharsets.US_ASCII); + + assertEquals(0xF7, Checksum.crc8(Checksum.CRC8_EGTS, buf.nioBuffer())); + assertEquals(0xD0, Checksum.crc8(Checksum.CRC8_ROHC, buf.nioBuffer())); + } + + @Test + public void testCrc16() { + ByteBuf buf = Unpooled.copiedBuffer("123456789", StandardCharsets.US_ASCII); + + assertEquals(0xBB3D, Checksum.crc16(Checksum.CRC16_IBM, buf.nioBuffer())); + assertEquals(0x4B37, Checksum.crc16(Checksum.CRC16_MODBUS, buf.nioBuffer())); + assertEquals(0x906e, Checksum.crc16(Checksum.CRC16_X25, buf.nioBuffer())); + assertEquals(0x29b1, Checksum.crc16(Checksum.CRC16_CCITT_FALSE, buf.nioBuffer())); + assertEquals(0x2189, Checksum.crc16(Checksum.CRC16_KERMIT, buf.nioBuffer())); + assertEquals(0x31c3, Checksum.crc16(Checksum.CRC16_XMODEM, buf.nioBuffer())); + } + + @Test + public void testLuhn() { + assertEquals(7, Checksum.luhn(12345678901234L)); + assertEquals(0, Checksum.luhn(63070019470771L)); + } + +} diff --git a/src/test/java/org/traccar/helper/DateBuilderTest.java b/src/test/java/org/traccar/helper/DateBuilderTest.java new file mode 100644 index 000000000..b6323cc1d --- /dev/null +++ b/src/test/java/org/traccar/helper/DateBuilderTest.java @@ -0,0 +1,27 @@ +package org.traccar.helper; + +import org.junit.Test; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.TimeZone; + +import static org.junit.Assert.assertEquals; + +public class DateBuilderTest { + + @Test + public void testDateBuilder() throws ParseException { + + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + + DateBuilder dateBuilder = new DateBuilder() + .setDate(2015, 10, 20).setTime(1, 21, 11); + + assertEquals(dateFormat.parse("2015-10-20 01:21:11"), dateBuilder.getDate()); + + } + +} diff --git a/src/test/java/org/traccar/helper/DateUtilTest.java b/src/test/java/org/traccar/helper/DateUtilTest.java new file mode 100644 index 000000000..ec42e71ae --- /dev/null +++ b/src/test/java/org/traccar/helper/DateUtilTest.java @@ -0,0 +1,30 @@ +package org.traccar.helper; + +import org.junit.Test; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; + +import static org.junit.Assert.assertEquals; + +public class DateUtilTest { + + @Test + public void testCorrectDate() throws ParseException { + + DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + assertEquals(f.parse("2015-12-31 23:59:59"), + DateUtil.correctDate(f.parse("2016-01-01 00:00:01"), f.parse("2016-01-01 23:59:59"), Calendar.DAY_OF_MONTH)); + + assertEquals(f.parse("2016-01-01 00:00:02"), + DateUtil.correctDate(f.parse("2016-01-01 00:00:01"), f.parse("2016-01-01 00:00:02"), Calendar.DAY_OF_MONTH)); + + assertEquals(f.parse("2016-01-01 00:00:02"), + DateUtil.correctDate(f.parse("2016-01-01 00:00:01"), f.parse("2015-12-31 00:00:02"), Calendar.DAY_OF_MONTH)); + + } + +} diff --git a/src/test/java/org/traccar/helper/DistanceCalculatorTest.java b/src/test/java/org/traccar/helper/DistanceCalculatorTest.java new file mode 100644 index 000000000..a7457b6c4 --- /dev/null +++ b/src/test/java/org/traccar/helper/DistanceCalculatorTest.java @@ -0,0 +1,24 @@ +package org.traccar.helper; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class DistanceCalculatorTest { + + @Test + public void testDistance() { + assertEquals( + DistanceCalculator.distance(0.0, 0.0, 0.05, 0.05), 7863.0, 10.0); + } + + @Test + public void testDistanceToLine() { + assertEquals(DistanceCalculator.distanceToLine( + 56.83801, 60.59748, 56.83777, 60.59833, 56.83766, 60.5968), 33.0, 5.0); + + assertEquals(DistanceCalculator.distanceToLine( + 56.83753, 60.59508, 56.83777, 60.59833, 56.83766, 60.5968), 105.0, 5.0); + } + +} diff --git a/src/test/java/org/traccar/helper/LocationTreeTest.java b/src/test/java/org/traccar/helper/LocationTreeTest.java new file mode 100644 index 000000000..21604144a --- /dev/null +++ b/src/test/java/org/traccar/helper/LocationTreeTest.java @@ -0,0 +1,30 @@ +package org.traccar.helper; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class LocationTreeTest { + + @Test + public void testLocationTree() { + + List<LocationTree.Item> items = new ArrayList<>(); + items.add(new LocationTree.Item(1, 1, "a")); + items.add(new LocationTree.Item(3, 2, "b")); + items.add(new LocationTree.Item(1, 3, "c")); + items.add(new LocationTree.Item(4, 3, "d")); + + LocationTree tree = new LocationTree(items); + + assertEquals("a", tree.findNearest(new LocationTree.Item(1f, 1f)).getData()); + assertEquals("d", tree.findNearest(new LocationTree.Item(10f, 10f)).getData()); + assertEquals("c", tree.findNearest(new LocationTree.Item(1f, 2.5f)).getData()); + assertEquals("a", tree.findNearest(new LocationTree.Item(1.5f, 1.5f)).getData()); + + } + +} diff --git a/src/test/java/org/traccar/helper/LogTest.java b/src/test/java/org/traccar/helper/LogTest.java new file mode 100644 index 000000000..853eb05c9 --- /dev/null +++ b/src/test/java/org/traccar/helper/LogTest.java @@ -0,0 +1,14 @@ +package org.traccar.helper; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class LogTest { + + @Test + public void testLog() { + assertEquals("test - Exception (LogTest:11 < ...)", Log.exceptionStack(new Exception("test"))); + } + +} diff --git a/src/test/java/org/traccar/helper/ObdDecoderTest.java b/src/test/java/org/traccar/helper/ObdDecoderTest.java new file mode 100644 index 000000000..1ffe68c8b --- /dev/null +++ b/src/test/java/org/traccar/helper/ObdDecoderTest.java @@ -0,0 +1,26 @@ +package org.traccar.helper; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class ObdDecoderTest { + + @Test + public void testDecode() { + + assertEquals(83, ObdDecoder.decode(0x01, "057b").getValue()); + assertEquals(1225, ObdDecoder.decode(0x01, "0C1324").getValue()); + assertEquals(20, ObdDecoder.decode(0x01, "0D14").getValue()); + assertEquals(64050, ObdDecoder.decode(0x01, "31fa32").getValue()); + assertEquals(25, ObdDecoder.decode(0x01, "2F41").getValue()); + + } + + @Test + public void testDecodeCodes() throws Exception { + assertEquals("P0D14", ObdDecoder.decodeCodes("0D14").getValue()); + assertEquals("dtcs", ObdDecoder.decodeCodes("0D14").getKey()); + } + +} diff --git a/src/test/java/org/traccar/helper/PatternBuilderTest.java b/src/test/java/org/traccar/helper/PatternBuilderTest.java new file mode 100644 index 000000000..4c76bc463 --- /dev/null +++ b/src/test/java/org/traccar/helper/PatternBuilderTest.java @@ -0,0 +1,20 @@ +package org.traccar.helper; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class PatternBuilderTest { + + @Test + public void testPatternBuilder() { + assertEquals("\\$GPRMC", new PatternBuilder().text("$GPRMC").toString()); + assertEquals("(\\d{2}\\.[0-9a-fA-F]+)", new PatternBuilder().number("(dd.x+)").toString()); + assertEquals("a(?:bc)?", new PatternBuilder().text("a").text("b").text("c").optional(2).toString()); + assertEquals("a|b", new PatternBuilder().expression("a|b").toString()); + assertEquals("ab\\|", new PatternBuilder().expression("ab|").toString()); + assertEquals("|", new PatternBuilder().or().toString()); + assertEquals("\\|\\d|\\d\\|", new PatternBuilder().number("|d|d|").toString()); + } + +} diff --git a/src/test/java/org/traccar/helper/PatternUtilTest.java b/src/test/java/org/traccar/helper/PatternUtilTest.java new file mode 100644 index 000000000..77660078a --- /dev/null +++ b/src/test/java/org/traccar/helper/PatternUtilTest.java @@ -0,0 +1,18 @@ +package org.traccar.helper; + +import org.junit.Ignore; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class PatternUtilTest { + + @Ignore + @Test + public void testCheckPattern() { + + assertEquals("ab", PatternUtil.checkPattern("abc", "abd").getPatternMatch()); + + } + +} |