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

http://github.com/mongodb/mongo-java-driver · Java · 137 lines · 88 code · 26 blank · 23 comment · 0 complexity · 44684920c1fb17e3b035a0a22fbf3850 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.Transformer;
  21. import org.bson.types.BSONTimestamp;
  22. import org.bson.types.Binary;
  23. import org.bson.types.ObjectId;
  24. import org.junit.Test;
  25. import java.text.ParsePosition;
  26. import java.text.SimpleDateFormat;
  27. import java.util.Date;
  28. import java.util.GregorianCalendar;
  29. import java.util.SimpleTimeZone;
  30. import java.util.regex.Pattern;
  31. import static org.junit.Assert.assertArrayEquals;
  32. import static org.junit.Assert.assertEquals;
  33. public class JSONCallbackTest extends com.mongodb.util.TestCase {
  34. @Test
  35. public void dateParsing() {
  36. SimpleDateFormat format = new SimpleDateFormat(JSONCallback._msDateFormat);
  37. format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
  38. Date rightNow = new Date();
  39. rightNow.setTime(System.currentTimeMillis());
  40. Date parsedDate = (Date) JSON.parse("{ \"$date\" : " + rightNow.getTime() + "}");
  41. assertEquals(rightNow.compareTo(parsedDate), 0);
  42. // Test formatted dates with ms granularity
  43. parsedDate = (Date) JSON.parse("{ \"$date\" : \"" + format.format(rightNow) + "\"}");
  44. assertEquals(
  45. parsedDate.compareTo(format.parse(format.format(rightNow), new ParsePosition(0))), 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(
  51. parsedDate.compareTo(format.parse(format.format(rightNow), new ParsePosition(0))), 0);
  52. }
  53. @Test
  54. public void encodingHooks() {
  55. 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. BSON.removeDecodingHooks(Date.class);
  69. }
  70. }
  71. @Test
  72. public void binaryParsing() {
  73. Binary parsedBinary = (Binary) JSON.parse(("{ \"$binary\" : \"YWJjZA==\", \"$type\" : 0 }"));
  74. assertEquals(parsedBinary.getType(), 0);
  75. assertArrayEquals(parsedBinary.getData(), new byte[]{97, 98, 99, 100});
  76. }
  77. @Test
  78. public void timestampParsing() {
  79. BSONTimestamp timestamp = (BSONTimestamp) JSON.parse(("{ \"$timestamp\" : { \"t\": 123, \"i\": 456 } }"));
  80. assertEquals(timestamp.getInc(), 456);
  81. assertEquals(timestamp.getTime(), 123);
  82. }
  83. @Test
  84. public void regexParsing() {
  85. Pattern pattern = (Pattern) JSON.parse(("{ \"$regex\" : \".*\", \"$options\": \"i\" }"));
  86. assertEquals(pattern.pattern(), ".*");
  87. assertEquals(pattern.flags(), Pattern.CASE_INSENSITIVE);
  88. }
  89. @Test
  90. public void oidParsing() {
  91. ObjectId id = (ObjectId) JSON.parse(("{ \"$oid\" : \"01234567890123456789abcd\" }"));
  92. assertEquals(id, new ObjectId("01234567890123456789abcd"));
  93. }
  94. @Test
  95. public void refParsing() {
  96. DBRef ref = (DBRef) JSON.parse(("{ \"$ref\" : \"friends\", \"$id\" : \"01234567890123456789abcd\" }"));
  97. assertEquals(ref.getRef(), "friends");
  98. assertEquals(ref.getId(), new ObjectId("01234567890123456789abcd").toHexString());
  99. }
  100. @Test
  101. public void numberLongParsing() {
  102. Long number = (Long) JSON.parse(("{ \"$numberLong\" : \"123456\" }"));
  103. assertEquals(number, Long.valueOf("123456"));
  104. }
  105. // No such concept in Java
  106. // @Test
  107. // public void undefinedParsing() {
  108. // BasicDBObject undef = (BasicDBObject) JSON.parse(("{ \"$undefined\" : true }"));
  109. // assertEquals(undef, 123);
  110. // }
  111. }