123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- package com.luticate.utils;
-
- import org.json.JSONObject;
- import org.junit.Test;
-
- import static org.junit.Assert.*;
-
- /**
- * Created by robin on 10/20/16.
- */
-
- public class TestDboTest {
-
- public static String TEST_DBO_JSON = "{" +
- "\"byte\": 42," +
- "\"char\": \"h\"," +
- "\"short\": 4200," +
- "\"int\": 420042," +
- "\"long\": 42004200," +
- "\"float\": 42.24," +
- "\"double\": 4200.0024," +
- "\"boolean\": true," +
-
- "\"byteObject\": 24," +
- "\"charObject\": \"w\"," +
- "\"shortObject\": 2400," +
- "\"intObject\": 240024," +
- "\"longObject\": 24002400," +
- "\"floatObject\": 24.42," +
- "\"doubleObject\": 2400.0042," +
- "\"booleanObject\": false," +
-
- "\"string\": \"Hello World!\"," +
-
- "\"test2Dbo\": {" +
- "\"aString\": \"a string\"," +
- "\"aInt\": 4242" +
- "}," +
-
- "\"integers\": [" +
- "0," +
- "42," +
- "24" +
- "]," +
- "ints: [" +
- "24," +
- "42," +
- "0" +
- "]," +
-
- "test2Dbos: [" +
- "{" +
- "\"aString\": \"42 42\"," +
- "\"aInt\": 4242" +
- "}" +
- "]," +
- "dbos: [" +
- "{" +
- "\"aString\": \"24 24\"," +
- "\"aInt\": 2424" +
- "}" +
- "]," +
-
- "\"hashMap\": {" +
- "\"forty-two\": 42," +
- "\"twenty-four\": 24" +
- "}," +
-
- "\"hashMap2\": {" +
- "\"an object\": {" +
- "\"aString\": \"42 24\"," +
- "\"aInt\": 4224" +
- "}" +
- "}" +
-
- "}";
-
- public void testTestDbo(TestDbo test)
- {
- assertEquals(42, test.getByte());
- assertEquals(24, test.getByteObject().byteValue());
- assertEquals('h', test.getChar());
- assertEquals('w', test.getCharObject().charValue());
- assertEquals(4200, test.getShort());
- assertEquals(2400, test.getShortObject().shortValue());
- assertEquals(420042, test.getInt());
- assertEquals(240024, test.getIntObject().intValue());
- assertEquals(42004200, test.getLong());
- assertEquals(24002400, test.getLongObject().longValue());
- assertEquals(42.24, test.getFloat(), 0.1);
- assertEquals(24.42, test.getFloatObject(), 0.1);
- assertEquals(4200.0024, test.getDouble(), 0.1);
- assertEquals(2400.0042, test.getDoubleObject(), 0.1);
- assertEquals(true, test.isBoolean());
- assertEquals(false, test.getBooleanObject());
- assertEquals("Hello World!", test.getString());
-
- assertNotNull(test.getTest2Dbo());
- assertEquals("a string", test.getTest2Dbo().getaString());
- assertEquals(4242, test.getTest2Dbo().getaInt());
- assertNull(test.getTest2Dbo2());
-
- assertNotNull(test.getIntegers());
- assertEquals(3, test.getIntegers().size());
- assertEquals(0, test.getIntegers().get(0).intValue());
- assertEquals(42, test.getIntegers().get(1).intValue());
- assertEquals(24, test.getIntegers().get(2).intValue());
-
- assertNotNull(test.getInts());
- assertEquals(3, test.getInts().length);
- assertEquals(24, test.getInts()[0]);
- assertEquals(42, test.getInts()[1]);
- assertEquals(0, test.getInts()[2]);
-
- assertNotNull(test.getTest2Dbos());
- assertEquals(1, test.getTest2Dbos().size());
- assertNotNull(test.getTest2Dbos().get(0));
- assertEquals("42 42", test.getTest2Dbos().get(0).getaString());
- assertEquals(4242, test.getTest2Dbos().get(0).getaInt());
-
- assertNotNull(test.getDbos());
- assertEquals(1, test.getDbos().length);
- assertNotNull(test.getDbos()[0]);
- assertEquals("24 24", test.getDbos()[0].getaString());
- assertEquals(2424, test.getDbos()[0].getaInt());
-
- assertNotNull(test.getHashMap());
- assertEquals(2, test.getHashMap().size());
- assertTrue(test.getHashMap().containsKey("forty-two"));
- assertEquals(42, (int)test.getHashMap().get("forty-two"));
- assertTrue(test.getHashMap().containsKey("twenty-four"));
- assertEquals(24, (int)test.getHashMap().get("twenty-four"));
-
- assertNotNull(test.getHashMap2());
- assertEquals(1, test.getHashMap2().size());
- assertTrue(test.getHashMap2().containsKey("an object"));
- assertNotNull(test.getHashMap2().get("an object"));
- assertEquals("42 24", test.getHashMap2().get("an object").getaString());
- assertEquals(4224, test.getHashMap2().get("an object").getaInt());
-
- assertEquals(test, test);
- }
-
- @Test
- public void deserializeTest() throws Exception {
- TestDbo test = new TestDbo();
- test.fromString(TEST_DBO_JSON);
- testTestDbo(test);
- }
-
- @Test
- public void serializeTest() throws Exception
- {
- TestDbo test = new TestDbo();
- test.fromString(TEST_DBO_JSON);
- JSONObject json = test.toJson();
-
- TestDbo test2 = new TestDbo();
- test2.fromJson(json);
-
- testTestDbo(test2);
- }
-
- @Test
- public void serializeStringTest() throws Exception
- {
- TestDbo test = new TestDbo();
- test.fromString(TEST_DBO_JSON);
- JSONObject json = test.toJson();
-
- TestDbo test2 = new TestDbo();
- test2.fromString(json.toString());
-
- testTestDbo(test2);
- }
- }
|