You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestDboTest.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package com.luticate.utils;
  2. import org.json.JSONObject;
  3. import org.junit.Test;
  4. import static org.junit.Assert.*;
  5. /**
  6. * Created by robin on 10/20/16.
  7. */
  8. public class TestDboTest {
  9. public static String TEST_DBO_JSON = "{" +
  10. "\"byte\": 42," +
  11. "\"char\": \"h\"," +
  12. "\"short\": 4200," +
  13. "\"int\": 420042," +
  14. "\"long\": 42004200," +
  15. "\"float\": 42.24," +
  16. "\"double\": 4200.0024," +
  17. "\"boolean\": true," +
  18. "\"byteObject\": 24," +
  19. "\"charObject\": \"w\"," +
  20. "\"shortObject\": 2400," +
  21. "\"intObject\": 240024," +
  22. "\"longObject\": 24002400," +
  23. "\"floatObject\": 24.42," +
  24. "\"doubleObject\": 2400.0042," +
  25. "\"booleanObject\": false," +
  26. "\"string\": \"Hello World!\"," +
  27. "\"test2Dbo\": {" +
  28. "\"aString\": \"a string\"," +
  29. "\"aInt\": 4242" +
  30. "}," +
  31. "\"integers\": [" +
  32. "0," +
  33. "42," +
  34. "24" +
  35. "]," +
  36. "ints: [" +
  37. "24," +
  38. "42," +
  39. "0" +
  40. "]," +
  41. "test2Dbos: [" +
  42. "{" +
  43. "\"aString\": \"42 42\"," +
  44. "\"aInt\": 4242" +
  45. "}" +
  46. "]," +
  47. "dbos: [" +
  48. "{" +
  49. "\"aString\": \"24 24\"," +
  50. "\"aInt\": 2424" +
  51. "}" +
  52. "]," +
  53. "\"hashMap\": {" +
  54. "\"forty-two\": 42," +
  55. "\"twenty-four\": 24" +
  56. "}," +
  57. "\"hashMap2\": {" +
  58. "\"an object\": {" +
  59. "\"aString\": \"42 24\"," +
  60. "\"aInt\": 4224" +
  61. "}" +
  62. "}," +
  63. "\"aNullString\": null," +
  64. "\"object\": {}," +
  65. "\"transientField\": \"a transient field\"" +
  66. "}";
  67. public void testTestDbo(TestDbo test)
  68. {
  69. assertEquals(42, test.getByte());
  70. assertEquals(24, test.getByteObject().byteValue());
  71. assertEquals('h', test.getChar());
  72. assertEquals('w', test.getCharObject().charValue());
  73. assertEquals(4200, test.getShort());
  74. assertEquals(2400, test.getShortObject().shortValue());
  75. assertEquals(420042, test.getInt());
  76. assertEquals(240024, test.getIntObject().intValue());
  77. assertEquals(42004200, test.getLong());
  78. assertEquals(24002400, test.getLongObject().longValue());
  79. assertEquals(42.24, test.getFloat(), 0.1);
  80. assertEquals(24.42, test.getFloatObject(), 0.1);
  81. assertEquals(4200.0024, test.getDouble(), 0.1);
  82. assertEquals(2400.0042, test.getDoubleObject(), 0.1);
  83. assertEquals(true, test.isBoolean());
  84. assertEquals(false, test.getBooleanObject());
  85. assertEquals("Hello World!", test.getString());
  86. assertNotNull(test.getTest2Dbo());
  87. assertEquals("a string", test.getTest2Dbo().getaString());
  88. assertEquals(4242, test.getTest2Dbo().getaInt());
  89. assertNull(test.getTest2Dbo2());
  90. assertNotNull(test.getIntegers());
  91. assertEquals(3, test.getIntegers().size());
  92. assertEquals(0, test.getIntegers().get(0).intValue());
  93. assertEquals(42, test.getIntegers().get(1).intValue());
  94. assertEquals(24, test.getIntegers().get(2).intValue());
  95. assertNotNull(test.getInts());
  96. assertEquals(3, test.getInts().length);
  97. assertEquals(24, test.getInts()[0]);
  98. assertEquals(42, test.getInts()[1]);
  99. assertEquals(0, test.getInts()[2]);
  100. assertNotNull(test.getTest2Dbos());
  101. assertEquals(1, test.getTest2Dbos().size());
  102. assertNotNull(test.getTest2Dbos().get(0));
  103. assertEquals("42 42", test.getTest2Dbos().get(0).getaString());
  104. assertEquals(4242, test.getTest2Dbos().get(0).getaInt());
  105. assertNotNull(test.getDbos());
  106. assertEquals(1, test.getDbos().length);
  107. assertNotNull(test.getDbos()[0]);
  108. assertEquals("24 24", test.getDbos()[0].getaString());
  109. assertEquals(2424, test.getDbos()[0].getaInt());
  110. assertNotNull(test.getHashMap());
  111. assertEquals(2, test.getHashMap().size());
  112. assertTrue(test.getHashMap().containsKey("forty-two"));
  113. assertEquals(42, (int)test.getHashMap().get("forty-two"));
  114. assertTrue(test.getHashMap().containsKey("twenty-four"));
  115. assertEquals(24, (int)test.getHashMap().get("twenty-four"));
  116. assertNotNull(test.getHashMap2());
  117. assertEquals(1, test.getHashMap2().size());
  118. assertTrue(test.getHashMap2().containsKey("an object"));
  119. assertNotNull(test.getHashMap2().get("an object"));
  120. assertEquals("42 24", test.getHashMap2().get("an object").getaString());
  121. assertEquals(4224, test.getHashMap2().get("an object").getaInt());
  122. assertNull(test.getaNullString());
  123. assertNull(test.getObject());
  124. assertNull(test.getTransientField());
  125. assertEquals(test, test);
  126. }
  127. @Test
  128. public void deserializeTest() throws Exception {
  129. TestDbo test = new TestDbo();
  130. test.fromJson(TEST_DBO_JSON);
  131. testTestDbo(test);
  132. }
  133. @Test
  134. public void serializeTest() throws Exception
  135. {
  136. TestDbo test = new TestDbo();
  137. test.fromJson(TEST_DBO_JSON);
  138. JSONObject json = test.toJson();
  139. assertFalse(json.has("transientField"));
  140. TestDbo test2 = new TestDbo();
  141. test2.fromJson(json);
  142. testTestDbo(test2);
  143. }
  144. @Test
  145. public void serializeStringTest() throws Exception
  146. {
  147. TestDbo test = new TestDbo();
  148. test.fromJson(TEST_DBO_JSON);
  149. TestDbo test2 = new TestDbo();
  150. test2.fromJson(test.toString());
  151. testTestDbo(test2);
  152. }
  153. }