@Override public int hashCode() { return getCode().hashCode() ^ scope.hashCode(); } }
@Override public void serialize(final Object obj, final StringBuilder buf) { CodeWScope c = (CodeWScope) obj; BasicDBObject temp = new BasicDBObject(); temp.put("$code", c.getCode()); temp.put("$scope", c.getScope()); serializer.serialize(temp, buf); }
@SuppressWarnings("unchecked") private void encodeCodeWScope(final BsonWriter bsonWriter, final CodeWScope value) { bsonWriter.writeJavaScriptWithScope(value.getCode()); encodeBsonObject(bsonWriter, value.getScope()); }
/** * Encodes a field to a {@link org.bson.BsonType#JAVASCRIPT_WITH_SCOPE} value. * * @param name the field name * @param codeWScope the value */ protected void putCodeWScope(final String name, final CodeWScope codeWScope) { putName(name); bsonWriter.writeJavaScriptWithScope(codeWScope.getCode()); putObject(codeWScope.getScope()); }
@Test public void testWhereWithInvalidStringQuery() { getDs().save(new PhotoWithKeywords()); final CodeWScope hasKeyword = new CodeWScope("keywords != null", new BasicDBObject()); try { // must fail assertNotNull(getDs().find(PhotoWithKeywords.class).where(hasKeyword.getCode()) .find(new FindOptions().limit(1)) .next()); fail("Invalid javascript magically isn't invalid anymore?"); } catch (MongoInternalException e) { // fine } catch (MongoException e) { // fine } }
@Override public void serialize(final Object obj, final StringBuilder buf) { CodeWScope c = (CodeWScope) obj; BasicDBObject temp = new BasicDBObject(); temp.put("$code", c.getCode()); temp.put("$scope", c.getScope()); serializer.serialize(temp, buf); }
@Override protected void serialize(Object obj, Writer writer, OutputStream out) throws IOException { CodeWScope c = (CodeWScope) obj; BasicDBObject temp = new BasicDBObject(); temp.put("$code", c.getCode()); temp.put("$scope", c.getScope()); JSON.serialize(serializer, temp, writer, out); } }
@SuppressWarnings("unchecked") private void encodeCodeWScope(final BsonWriter bsonWriter, final CodeWScope value) { bsonWriter.writeJavaScriptWithScope(value.getCode()); encodeBsonObject(bsonWriter, value.getScope()); }
public void serialize(Object obj, StringBuilder buf) { CodeWScope c = (CodeWScope) obj; BasicBSONObject temp = new BasicBSONObject(); temp.put("$code", c.getCode()); temp.put("$scope", c.getScope()); serializer.serialize(temp, buf); }
protected void putCodeWScope(String name, CodeWScope code) { _put(CODE_W_SCOPE, name); int temp = _buf.getPosition(); _buf.writeInt(0); _putValueString(code.getCode()); putObject(code.getScope()); _buf.writeInt(temp, _buf.getPosition() - temp); }
protected void putCodeWScope(String name, CodeWScope code) { this._put(15, name); int temp = this._buf.getPosition(); this._buf.writeInt(0); this._putValueString(code.getCode()); this.putObject(code.getScope()); this._buf.writeInt(temp, this._buf.getPosition() - temp); }
/** * Test if {@link JavaScript} objects with a scope can be serialized * @throws Exception if something goes wrong */ @Test public void javascriptWithScope() throws Exception { Map<String, Object> scope = new LinkedHashMap<String, Object>(); scope.put("a", 99); scope.put("b", 80); JavaScript javaScript = new JavaScript("a < 100", scope); Map<String, Object> data = new LinkedHashMap<String, Object>(); data.put("javaScript", javaScript); BSONObject obj = generateAndParse(data); CodeWScope result = (CodeWScope) obj.get("javaScript"); assertNotNull(result); assertEquals(javaScript.getCode(), result.getCode()); Map<?, ?> returnedScope = result.getScope().toMap(); assertEquals(returnedScope, scope); }
/** * Tests {@link BsonJavaScriptSerializer} * @throws Exception if something goes wrong */ @Test public void javascriptWithScope() throws Exception { Map<String, Object> scope = new HashMap<>(); scope.put("j", 5); JavaScript js = new JavaScript("code", scope); CodeWScope code = (CodeWScope)generateAndParse(js); assertEquals(js.getCode(), code.getCode()); assertEquals(js.getScope(), code.getScope()); }