From 3af6a0d0bfe3be35a3bd4a6360edf93d6c25dad2 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 29 Jun 2015 11:59:59 +1200 Subject: Some cleanup and license note --- test/org/traccar/http/JsonConverterTest.java | 171 +++++++++++++++++++++ .../traccar/http/json/JsonConverterTestCase.java | 171 --------------------- 2 files changed, 171 insertions(+), 171 deletions(-) create mode 100644 test/org/traccar/http/JsonConverterTest.java delete mode 100644 test/org/traccar/http/json/JsonConverterTestCase.java (limited to 'test/org/traccar') diff --git a/test/org/traccar/http/JsonConverterTest.java b/test/org/traccar/http/JsonConverterTest.java new file mode 100644 index 000000000..b318420e0 --- /dev/null +++ b/test/org/traccar/http/JsonConverterTest.java @@ -0,0 +1,171 @@ +package org.traccar.http; + +import org.junit.Test; +import org.traccar.http.JsonConverter; +import org.traccar.model.Factory; + +import java.io.Reader; +import java.io.StringReader; +import java.text.ParseException; +import java.util.Calendar; +import java.util.Date; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class JsonConverterTest { + + private T convert(String jsonString, T prototype) throws ParseException { + Reader r = new StringReader( + jsonString); + + return JsonConverter.objectFromJson(r, prototype); + } + + @Test + public void primitiveConversion() throws ParseException { + + AllPrimitives o = convert("{" + + "\"aBoolean\": true, " + + "\"anInt\": 42, " + + "\"aDouble\": 41.99, " + + "\"aString\": \"discworld\", " + + "\"aDate\":\"2015-07-09T19:02:17\"" + + "}", + new AllPrimitives()); + + assertEquals(true, o.getaBoolean()); + assertEquals(42, o.getAnInt()); + assertEquals(41.99, o.getaDouble(), 0.001); + assertEquals("discworld", o.getaString()); + + Calendar c = Calendar.getInstance(); + c.setTime(o.getaDate()); + assertEquals(2015, c.get(Calendar.YEAR)); + assertEquals(Calendar.JULY, c.get(Calendar.MONTH)); + assertEquals(9, c.get(Calendar.DAY_OF_MONTH)); + assertEquals(19, c.get(Calendar.HOUR_OF_DAY)); + assertEquals(2, c.get(Calendar.MINUTE)); + assertEquals(17, c.get(Calendar.SECOND)); + } + + public static class AllPrimitives implements Factory { + + private boolean aBoolean; + private int anInt; + private double aDouble; + private String aString; + private Date aDate; + + + @Override + public Object create() { + return new AllPrimitives(); + } + + public boolean getaBoolean() { + return aBoolean; + } + + public void setaBoolean(boolean aBoolean) { + this.aBoolean = aBoolean; + } + + public int getAnInt() { + return anInt; + } + + public void setAnInt(int anInt) { + this.anInt = anInt; + } + + public double getaDouble() { + return aDouble; + } + + public void setaDouble(double aDouble) { + this.aDouble = aDouble; + } + + public String getaString() { + return aString; + } + + public void setaString(String aString) { + this.aString = aString; + } + + public Date getaDate() { + return aDate; + } + + public void setaDate(Date aDate) { + this.aDate = aDate; + } + } + + + @Test + public void enumConversion() throws ParseException { + ObjectWithEnum o = convert("{\"anEnum\": \"VALUE2\"}", new ObjectWithEnum()); + assertEquals(TestEnum.VALUE2, o.getAnEnum()); + } + + + public enum TestEnum { + VALUE1, VALUE2 + } + + public static class ObjectWithEnum implements Factory { + private TestEnum anEnum; + + public TestEnum getAnEnum() { + return anEnum; + } + + public void setAnEnum(TestEnum anEnum) { + this.anEnum = anEnum; + } + + @Override + public Object create() { + return new ObjectWithEnum(); + } + } + + + @Test + public void nestedObjectsConversion() throws ParseException { + NestedObjects o = convert("{\"name\": \"Rincewind\", \"nestedObject\": {\"anEnum\":\"VALUE1\"}}", new NestedObjects()); + assertEquals("Rincewind", o.getName()); + assertNotNull("The nested object should be populated", o.getNestedObject()); + assertEquals(TestEnum.VALUE1, o.getNestedObject().getAnEnum()); + } + + public static class NestedObjects implements Factory { + + private String name; + private ObjectWithEnum nestedObject; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public ObjectWithEnum getNestedObject() { + return nestedObject; + } + + public void setNestedObject(ObjectWithEnum nestedObject) { + this.nestedObject = nestedObject; + } + + @Override + public Object create() { + return new NestedObjects(); + } + } +} diff --git a/test/org/traccar/http/json/JsonConverterTestCase.java b/test/org/traccar/http/json/JsonConverterTestCase.java deleted file mode 100644 index d81ca3bc9..000000000 --- a/test/org/traccar/http/json/JsonConverterTestCase.java +++ /dev/null @@ -1,171 +0,0 @@ -package org.traccar.http.json; - -import org.junit.Test; -import org.traccar.http.JsonConverter; -import org.traccar.model.Factory; - -import java.io.Reader; -import java.io.StringReader; -import java.text.ParseException; -import java.util.Calendar; -import java.util.Date; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -public class JsonConverterTestCase { - - private T convert(String jsonString, T prototype) throws ParseException { - Reader r = new StringReader( - jsonString); - - return JsonConverter.objectFromJson(r, prototype); - } - - @Test - public void primitiveConversion() throws ParseException { - - AllPrimitives o = convert("{" + - "\"aBoolean\": true, " + - "\"anInt\": 42, " + - "\"aDouble\": 41.99, " + - "\"aString\": \"discworld\", " + - "\"aDate\":\"2015-07-09T19:02:17\"" + - "}", - new AllPrimitives()); - - assertEquals(true, o.getaBoolean()); - assertEquals(42, o.getAnInt()); - assertEquals(41.99, o.getaDouble(), 0.001); - assertEquals("discworld", o.getaString()); - - Calendar c = Calendar.getInstance(); - c.setTime(o.getaDate()); - assertEquals(2015, c.get(Calendar.YEAR)); - assertEquals(Calendar.JULY, c.get(Calendar.MONTH)); - assertEquals(9, c.get(Calendar.DAY_OF_MONTH)); - assertEquals(19, c.get(Calendar.HOUR_OF_DAY)); - assertEquals(2, c.get(Calendar.MINUTE)); - assertEquals(17, c.get(Calendar.SECOND)); - } - - public static class AllPrimitives implements Factory { - - private boolean aBoolean; - private int anInt; - private double aDouble; - private String aString; - private Date aDate; - - - @Override - public Object create() { - return new AllPrimitives(); - } - - public boolean getaBoolean() { - return aBoolean; - } - - public void setaBoolean(boolean aBoolean) { - this.aBoolean = aBoolean; - } - - public int getAnInt() { - return anInt; - } - - public void setAnInt(int anInt) { - this.anInt = anInt; - } - - public double getaDouble() { - return aDouble; - } - - public void setaDouble(double aDouble) { - this.aDouble = aDouble; - } - - public String getaString() { - return aString; - } - - public void setaString(String aString) { - this.aString = aString; - } - - public Date getaDate() { - return aDate; - } - - public void setaDate(Date aDate) { - this.aDate = aDate; - } - } - - - @Test - public void enumConversion() throws ParseException { - ObjectWithEnum o = convert("{\"anEnum\": \"VALUE2\"}", new ObjectWithEnum()); - assertEquals(TestEnum.VALUE2, o.getAnEnum()); - } - - - public enum TestEnum { - VALUE1, VALUE2 - } - - public static class ObjectWithEnum implements Factory { - private TestEnum anEnum; - - public TestEnum getAnEnum() { - return anEnum; - } - - public void setAnEnum(TestEnum anEnum) { - this.anEnum = anEnum; - } - - @Override - public Object create() { - return new ObjectWithEnum(); - } - } - - - @Test - public void nestedObjectsConversion() throws ParseException { - NestedObjects o = convert("{\"name\": \"Rincewind\", \"nestedObject\": {\"anEnum\":\"VALUE1\"}}", new NestedObjects()); - assertEquals("Rincewind", o.getName()); - assertNotNull("The nested object should be populated", o.getNestedObject()); - assertEquals(TestEnum.VALUE1, o.getNestedObject().getAnEnum()); - } - - public static class NestedObjects implements Factory { - - private String name; - private ObjectWithEnum nestedObject; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public ObjectWithEnum getNestedObject() { - return nestedObject; - } - - public void setNestedObject(ObjectWithEnum nestedObject) { - this.nestedObject = nestedObject; - } - - @Override - public Object create() { - return new NestedObjects(); - } - } -} -- cgit v1.2.3