/** * Returns {@link #fromString(String) parsable} representation of Id. * @return hexadecimal formatted string. */ @Override public String toString() { return ObjectId.createFromLegacyFormat(time, machine, inc).toString(); } }
public static ObjectId convert(de.undercouch.bson4jackson.types.ObjectId objectId) { return ObjectId.createFromLegacyFormat(objectId.getTime(), objectId.getMachine(), objectId.getInc()); }
/** * Tests if {@code ObjectId} objects can be deserialized * @throws Exception if something goes wrong */ @Test public void objectId() throws Exception { org.bson.types.ObjectId id = org.bson.types.ObjectId.createFromLegacyFormat(1, 2, 3); TC.O obj = generateAndParse(id, TC.O.class); assertEquals(id, org.bson.types.ObjectId.createFromLegacyFormat( obj.obj.getTime(), obj.obj.getMachine(), obj.obj.getInc())); }
/** * Test if {@link ObjectId}s can be serialized * @throws Exception if something goes wrong */ @Test public void objectIds() throws Exception { Map<String, Object> data = new LinkedHashMap<String, Object>(); ObjectId objectId = new ObjectId((int)(System.currentTimeMillis() / 1000), new Random().nextInt(), 100); data.put("_id", objectId); BSONObject obj = generateAndParse(data); org.bson.types.ObjectId result = (org.bson.types.ObjectId) obj.get("_id"); assertNotNull(result); assertEquals(org.bson.types.ObjectId.createFromLegacyFormat( objectId.getTime(), objectId.getMachine(), objectId.getInc()), result); }
/** * Tests {@link BsonObjectIdSerializer} * @throws Exception if something goes wrong */ @Test public void objectId() throws Exception { ObjectId id = new ObjectId(1, 2, 3); org.bson.types.ObjectId roid = (org.bson.types.ObjectId)generateAndParse(id); assertEquals(org.bson.types.ObjectId.createFromLegacyFormat( id.getTime(), id.getMachine(), id.getInc()), roid); }
/** * Test if a complex BSON object containing various values can be * deserialized * @throws Exception if something goes wrong */ @Test public void parseComplex() throws Exception { BSONObject o = new BasicBSONObject(); o.put("Timestamp", new BSONTimestamp(0xAABB, 0xCCDD)); o.put("Symbol", new Symbol("Test")); o.put("ObjectId", org.bson.types.ObjectId.createFromLegacyFormat( Integer.MAX_VALUE, -2, Integer.MIN_VALUE)); Pattern p = Pattern.compile(".*", Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE | Pattern.UNICODE_CASE); o.put("Regex", p); Map<?, ?> data = parseBsonObject(o); assertEquals(new Timestamp(0xAABB, 0xCCDD), data.get("Timestamp")); assertEquals(new de.undercouch.bson4jackson.types.Symbol("Test"), data.get("Symbol")); ObjectId oid = (ObjectId)data.get("ObjectId"); assertEquals(Integer.MAX_VALUE, oid.getTime()); assertEquals(-2, oid.getMachine()); assertEquals(Integer.MIN_VALUE, oid.getInc()); Pattern p2 = (Pattern)data.get("Regex"); assertEquals(p.flags(), p2.flags()); assertEquals(p.pattern(), p2.pattern()); }