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

https://github.com/foursquare/mongo-java-driver · Java · 136 lines · 92 code · 27 blank · 17 comment · 0 complexity · 61837af7eaa72e396e1ab343de9be9e9 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2014 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.BSON;
  20. import org.bson.BsonUndefined;
  21. import org.bson.Transformer;
  22. import org.bson.types.BSONTimestamp;
  23. import org.bson.types.Binary;
  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. public class JSONCallbackTest {
  35. @Test
  36. public void dateParsing() {
  37. SimpleDateFormat format = new SimpleDateFormat(JSONCallback._msDateFormat);
  38. format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
  39. Date rightNow = new Date();
  40. rightNow.setTime(System.currentTimeMillis());
  41. Date parsedDate = (Date) JSON.parse("{ \"$date\" : " + rightNow.getTime() + "}");
  42. assertEquals(0, rightNow.compareTo(parsedDate));
  43. // Test formatted dates with ms granularity
  44. parsedDate = (Date) JSON.parse("{ \"$date\" : \"" + format.format(rightNow) + "\"}");
  45. assertEquals(0, parsedDate.compareTo(format.parse(format.format(rightNow), new ParsePosition(0))));
  46. // Test formatted dates with sec granularity
  47. format = new SimpleDateFormat(JSONCallback._secDateFormat);
  48. format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
  49. parsedDate = (Date) JSON.parse("{ \"$date\" : \"" + format.format(rightNow) + "\"}");
  50. assertEquals(0, parsedDate.compareTo(format.parse(format.format(rightNow), new ParsePosition(0))));
  51. }
  52. @Test
  53. public void encodingHooks() {
  54. BSON.addDecodingHook(Date.class, new Transformer() {
  55. @Override
  56. public Object transform(final Object o) {
  57. return ((Date) o).getTime();
  58. }
  59. });
  60. try {
  61. Date now = new Date();
  62. Object parsedDate = JSON.parse("{ \"$date\" : " + now.getTime() + "}");
  63. assertEquals(Long.class, parsedDate.getClass());
  64. DBObject doc = (DBObject) JSON.parse("{ date : { \"$date\" : " + now.getTime() + "} }");
  65. assertEquals(Long.class, doc.get("date").getClass());
  66. } finally {
  67. BSON.removeDecodingHooks(Date.class);
  68. }
  69. }
  70. @Test
  71. public void binaryParsing() {
  72. Binary parsedBinary = (Binary) JSON.parse(("{ \"$binary\" : \"YWJjZA==\", \"$type\" : 0 }"));
  73. assertEquals(0, parsedBinary.getType());
  74. assertArrayEquals(new byte[]{97, 98, 99, 100}, parsedBinary.getData());
  75. }
  76. @Test
  77. public void timestampParsing() {
  78. BSONTimestamp timestamp = (BSONTimestamp) JSON.parse(("{ \"$timestamp\" : { \"t\": 123, \"i\": 456 } }"));
  79. assertEquals(456, timestamp.getInc());
  80. assertEquals(123, timestamp.getTime());
  81. }
  82. @Test
  83. public void regexParsing() {
  84. Pattern pattern = (Pattern) JSON.parse(("{ \"$regex\" : \".*\", \"$options\": \"i\" }"));
  85. assertEquals(".*", pattern.pattern());
  86. assertEquals(Pattern.CASE_INSENSITIVE, pattern.flags());
  87. }
  88. @Test
  89. public void oidParsing() {
  90. ObjectId id = (ObjectId) JSON.parse(("{ \"$oid\" : \"01234567890123456789abcd\" }"));
  91. assertEquals(new ObjectId("01234567890123456789abcd"), id);
  92. }
  93. @Test
  94. public void refParsing() {
  95. DBRef ref = (DBRef) JSON.parse(("{ \"$ref\" : \"friends\", \"$id\" : { \"$oid\" : \"01234567890123456789abcd\" } }"));
  96. assertEquals("friends", ref.getCollectionName());
  97. assertEquals(new ObjectId("01234567890123456789abcd"), ref.getId());
  98. }
  99. @Test
  100. public void undefinedParsing() {
  101. Object undefined = JSON.parse("{ \"$undefined\" : true }");
  102. assertEquals(new BsonUndefined(), undefined);
  103. }
  104. @Test
  105. public void numberLongParsing() {
  106. Long number = (Long) JSON.parse(("{ \"$numberLong\" : \"123456\" }"));
  107. assertEquals(number, Long.valueOf("123456"));
  108. }
  109. }