|
26 | 26 | import org.bson.types.ObjectId;
|
27 | 27 | import org.junit.Test;
|
28 | 28 |
|
| 29 | +import java.text.ParsePosition; |
| 30 | +import java.text.SimpleDateFormat; |
29 | 31 | import java.util.Date;
|
| 32 | +import java.util.Locale; |
30 | 33 |
|
31 | 34 | import static org.junit.Assert.assertArrayEquals;
|
32 | 35 | import static org.junit.Assert.assertEquals;
|
@@ -716,12 +719,22 @@ public void testEmptyDateTimeConstructorWithNew() {
|
716 | 719 | }
|
717 | 720 |
|
718 | 721 | @Test
|
719 |
| - public void testEmptyDateTimeConstructor() { |
720 |
| - long currentTime = new Date().getTime(); |
| 722 | + public void testDateTimeWithOutNew() { |
| 723 | + long currentTime = currentTimeWithoutMillis(); |
721 | 724 | String json = "Date()";
|
722 | 725 | bsonReader = new JsonReader(json);
|
723 |
| - assertEquals(BsonType.DATE_TIME, bsonReader.readBsonType()); |
724 |
| - assertTrue(bsonReader.readDateTime() >= currentTime); |
| 726 | + assertEquals(BsonType.STRING, bsonReader.readBsonType()); |
| 727 | + assertTrue(dateStringToTime(bsonReader.readString()) >= currentTime); |
| 728 | + assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState()); |
| 729 | + } |
| 730 | + |
| 731 | + @Test |
| 732 | + public void testDateTimeWithOutNewContainingJunk() { |
| 733 | + long currentTime = currentTimeWithoutMillis(); |
| 734 | + String json = "Date({ok: 1}, 1234)"; |
| 735 | + bsonReader = new JsonReader(json); |
| 736 | + assertEquals(BsonType.STRING, bsonReader.readBsonType()); |
| 737 | + assertTrue(dateStringToTime(bsonReader.readString()) >= currentTime); |
725 | 738 | assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
|
726 | 739 | }
|
727 | 740 |
|
@@ -798,4 +811,14 @@ public void testDBPointerWithNew() {
|
798 | 811 | assertEquals(new ObjectId("5209296cd6c4e38cf96fffdc"), dbPointer.getId());
|
799 | 812 | }
|
800 | 813 |
|
| 814 | + private long dateStringToTime(final String date) { |
| 815 | + SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z", Locale.ENGLISH); |
| 816 | + return df.parse(date, new ParsePosition(0)).getTime(); |
| 817 | + } |
| 818 | + |
| 819 | + private long currentTimeWithoutMillis() { |
| 820 | + long currentTime = new Date().getTime(); |
| 821 | + return currentTime - (currentTime % 1000); |
| 822 | + } |
| 823 | + |
801 | 824 | }
|
0 commit comments