/driver-legacy/src/test/unit/com/mongodb/util/JSONCallbackTest.java

https://github.com/mongodb/mongo-java-driver · Java · 146 lines · 101 code · 28 blank · 17 comment · 0 complexity · bc956fd8958fd28c781e93068072cd2d MD5 · raw file

  1. /*
  2. * Copyright 2008-present MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mongodb.util;
  17. import com.mongodb.DBObject;
  18. import com.mongodb.DBRef;
  19. import org.bson.BsonUndefined;
  20. import org.bson.Transformer;
  21. import org.bson.types.BSONTimestamp;
  22. import org.bson.types.Binary;
  23. import org.bson.types.Decimal128;
  24. import org.bson.types.ObjectId;
  25. import org.junit.Test;
  26. import java.text.ParsePosition;
  27. import java.text.SimpleDateFormat;
  28. import java.util.Date;
  29. import java.util.GregorianCalendar;
  30. import java.util.SimpleTimeZone;
  31. import java.util.regex.Pattern;
  32. import static org.junit.Assert.assertArrayEquals;
  33. import static org.junit.Assert.assertEquals;
  34. @SuppressWarnings("deprecation")
  35. public class JSONCallbackTest {
  36. @Test
  37. public void dateParsing() {
  38. SimpleDateFormat format = new SimpleDateFormat(JSONCallback._msDateFormat);
  39. format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
  40. Date rightNow = new Date();
  41. rightNow.setTime(System.currentTimeMillis());
  42. Date parsedDate = (Date) JSON.parse("{ \"$date\" : " + rightNow.getTime() + "}");
  43. assertEquals(0, rightNow.compareTo(parsedDate));
  44. // Test formatted dates with ms granularity
  45. parsedDate = (Date) JSON.parse("{ \"$date\" : \"" + format.format(rightNow) + "\"}");
  46. assertEquals(0, parsedDate.compareTo(format.parse(format.format(rightNow), new ParsePosition(0))));
  47. // Test formatted dates with sec granularity
  48. format = new SimpleDateFormat(JSONCallback._secDateFormat);
  49. format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
  50. parsedDate = (Date) JSON.parse("{ \"$date\" : \"" + format.format(rightNow) + "\"}");
  51. assertEquals(0, parsedDate.compareTo(format.parse(format.format(rightNow), new ParsePosition(0))));
  52. }
  53. @Test
  54. public void encodingHooks() {
  55. org.bson.BSON.addDecodingHook(Date.class, new Transformer() {
  56. @Override
  57. public Object transform(final Object o) {
  58. return ((Date) o).getTime();
  59. }
  60. });
  61. try {
  62. Date now = new Date();
  63. Object parsedDate = JSON.parse("{ \"$date\" : " + now.getTime() + "}");
  64. assertEquals(Long.class, parsedDate.getClass());
  65. DBObject doc = (DBObject) JSON.parse("{ date : { \"$date\" : " + now.getTime() + "} }");
  66. assertEquals(Long.class, doc.get("date").getClass());
  67. } finally {
  68. org.bson.BSON.removeDecodingHooks(Date.class);
  69. }
  70. }
  71. @Test
  72. public void binaryParsing() {
  73. Binary parsedBinary = (Binary) JSON.parse(("{ \"$binary\" : \"YWJjZA==\", \"$type\" : 0 }"));
  74. assertEquals(0, parsedBinary.getType());
  75. assertArrayEquals(new byte[]{97, 98, 99, 100}, parsedBinary.getData());
  76. Binary parsedBinaryWithHexType = (Binary) JSON.parse(("{ \"$binary\" : \"YWJjZA==\", \"$type\" : \"80\" }"));
  77. assertEquals((byte) 128, parsedBinaryWithHexType.getType());
  78. assertArrayEquals(new byte[]{97, 98, 99, 100}, parsedBinaryWithHexType.getData());
  79. }
  80. @Test
  81. public void timestampParsing() {
  82. BSONTimestamp timestamp = (BSONTimestamp) JSON.parse(("{ \"$timestamp\" : { \"t\": 123, \"i\": 456 } }"));
  83. assertEquals(456, timestamp.getInc());
  84. assertEquals(123, timestamp.getTime());
  85. }
  86. @Test
  87. public void regexParsing() {
  88. Pattern pattern = (Pattern) JSON.parse(("{ \"$regex\" : \".*\", \"$options\": \"i\" }"));
  89. assertEquals(".*", pattern.pattern());
  90. assertEquals(Pattern.CASE_INSENSITIVE, pattern.flags());
  91. }
  92. @Test
  93. public void oidParsing() {
  94. ObjectId id = (ObjectId) JSON.parse(("{ \"$oid\" : \"01234567890123456789abcd\" }"));
  95. assertEquals(new ObjectId("01234567890123456789abcd"), id);
  96. }
  97. @Test
  98. public void refParsing() {
  99. DBRef ref = (DBRef) JSON.parse(("{ \"$ref\" : \"friends\", \"$id\" : { \"$oid\" : \"01234567890123456789abcd\" } }"));
  100. assertEquals("friends", ref.getCollectionName());
  101. assertEquals(new ObjectId("01234567890123456789abcd"), ref.getId());
  102. }
  103. @Test
  104. public void undefinedParsing() {
  105. Object undefined = JSON.parse("{ \"$undefined\" : true }");
  106. assertEquals(new BsonUndefined(), undefined);
  107. }
  108. @Test
  109. public void numberLongParsing() {
  110. Long number = (Long) JSON.parse(("{ \"$numberLong\" : \"123456\" }"));
  111. assertEquals(number, Long.valueOf("123456"));
  112. }
  113. @Test
  114. public void numberDecimalParsing() {
  115. Decimal128 number = (Decimal128) JSON.parse(("{\"$numberDecimal\" : \"314E-2\"}"));
  116. assertEquals(number, Decimal128.parse("314E-2"));
  117. }
  118. }