Search in sources :

Example 1 with BsonRegularExpression

use of org.bson.BsonRegularExpression in project mongo-java-driver by mongodb.

the class JsonReaderTest method testRegularExpressionStrict.

@Test
public void testRegularExpressionStrict() {
    String json = "{ \"$regex\" : \"pattern\", \"$options\" : \"imxs\" }";
    bsonReader = new JsonReader(json);
    assertEquals(BsonType.REGULAR_EXPRESSION, bsonReader.readBsonType());
    BsonRegularExpression regex = bsonReader.readRegularExpression();
    assertEquals("pattern", regex.getPattern());
    assertEquals("imsx", regex.getOptions());
    assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
}
Also used : BsonRegularExpression(org.bson.BsonRegularExpression) Test(org.junit.Test)

Example 2 with BsonRegularExpression

use of org.bson.BsonRegularExpression in project mongo-java-driver by mongodb.

the class JsonScannerTest method testRegularExpressionPatternAndOptions.

@Test
public void testRegularExpressionPatternAndOptions() {
    String json = "\t /pattern/im,";
    JsonBuffer buffer = new JsonBuffer(json);
    JsonScanner scanner = new JsonScanner(buffer);
    JsonToken token = scanner.nextToken();
    assertEquals(JsonTokenType.REGULAR_EXPRESSION, token.getType());
    BsonRegularExpression regularExpression = token.getValue(BsonRegularExpression.class);
    assertEquals("pattern", regularExpression.getPattern());
    assertEquals("im", regularExpression.getOptions());
    assertEquals(',', buffer.read());
}
Also used : BsonRegularExpression(org.bson.BsonRegularExpression) Test(org.junit.Test)

Example 3 with BsonRegularExpression

use of org.bson.BsonRegularExpression in project mongo-java-driver by mongodb.

the class JsonWriterTest method testRegularExpressionStrict.

@Test
@SuppressWarnings("deprecation")
public void testRegularExpressionStrict() {
    List<TestData<BsonRegularExpression>> tests;
    tests = asList(new TestData<BsonRegularExpression>(new BsonRegularExpression(""), "{ \"$regex\" : \"\", " + "\"$options\" : \"\" " + "}"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a"), "{ \"$regex\" : \"a\"," + " \"$options\" : \"\" " + "}"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a/b"), "{ \"$regex\" : " + "\"a/b\", " + "\"$options\" : \"\" " + "}"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a\\b"), "{ \"$regex\" : " + "\"a\\\\b\", " + "\"$options\" : \"\" " + "}"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "i"), "{ \"$regex\" : \"a\"," + " \"$options\" : \"i\"" + " }"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "m"), "{ \"$regex\" : \"a\"," + " \"$options\" : \"m\"" + " }"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "x"), "{ \"$regex\" : \"a\"," + " \"$options\" : \"x\"" + " }"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "s"), "{ \"$regex\" : \"a\"," + " \"$options\" : \"s\"" + " }"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "imxs"), "{ \"$regex\" : \"a\"," + " \"$options\" : \"imsx\" }"));
    for (final TestData<BsonRegularExpression> cur : tests) {
        stringWriter = new StringWriter();
        writer = new JsonWriter(stringWriter, JsonWriterSettings.builder().outputMode(JsonMode.STRICT).build());
        writer.writeStartDocument();
        writer.writeRegularExpression("regex", cur.value);
        writer.writeEndDocument();
        String expected = "{ \"regex\" : " + cur.expected + " }";
        assertEquals(expected, stringWriter.toString());
    }
}
Also used : StringWriter(java.io.StringWriter) BsonRegularExpression(org.bson.BsonRegularExpression) Test(org.junit.Test)

Example 4 with BsonRegularExpression

use of org.bson.BsonRegularExpression in project mongo-java-driver by mongodb.

the class JsonReaderTest method testRegExpWithNew.

@Test
public void testRegExpWithNew() {
    String json = "new RegExp(\"abc\",\"im\")";
    bsonReader = new JsonReader(json);
    assertEquals(BsonType.REGULAR_EXPRESSION, bsonReader.readBsonType());
    BsonRegularExpression regularExpression = bsonReader.readRegularExpression();
    assertEquals("abc", regularExpression.getPattern());
    assertEquals("im", regularExpression.getOptions());
}
Also used : BsonRegularExpression(org.bson.BsonRegularExpression) Test(org.junit.Test)

Example 5 with BsonRegularExpression

use of org.bson.BsonRegularExpression in project mongo-java-driver by mongodb.

the class ListCollectionsOperation method asQueryDocument.

private BsonDocument asQueryDocument(final ConnectionDescription connectionDescription, final ReadPreference readPreference) {
    BsonDocument document = new BsonDocument();
    BsonDocument transformedFilter = null;
    if (filter != null) {
        if (filter.containsKey("name")) {
            if (!filter.isString("name")) {
                throw new IllegalArgumentException("When filtering collections on MongoDB versions < 3.0 the name field " + "must be a string");
            }
            transformedFilter = new BsonDocument();
            transformedFilter.putAll(filter);
            transformedFilter.put("name", new BsonString(format("%s.%s", databaseName, filter.getString("name").getValue())));
        } else {
            transformedFilter = filter;
        }
    }
    BsonDocument indexExcludingRegex = new BsonDocument("name", new BsonRegularExpression("^[^$]*$"));
    BsonDocument query = transformedFilter == null ? indexExcludingRegex : new BsonDocument("$and", new BsonArray(asList(indexExcludingRegex, transformedFilter)));
    document.put("$query", query);
    if (connectionDescription.getServerType() == SHARD_ROUTER && !readPreference.equals(primary())) {
        document.put("$readPreference", readPreference.toDocument());
    }
    if (maxTimeMS > 0) {
        document.put("$maxTimeMS", new BsonInt64(maxTimeMS));
    }
    return document;
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonRegularExpression(org.bson.BsonRegularExpression)

Aggregations

BsonRegularExpression (org.bson.BsonRegularExpression)11 Test (org.junit.Test)9 StringWriter (java.io.StringWriter)2 BsonDocument (org.bson.BsonDocument)2 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Date (java.util.Date)1 List (java.util.List)1 BsonArray (org.bson.BsonArray)1 BsonInt64 (org.bson.BsonInt64)1 BsonString (org.bson.BsonString)1 BsonTimestamp (org.bson.BsonTimestamp)1 Document (org.bson.Document)1 Binary (org.bson.types.Binary)1 Code (org.bson.types.Code)1 CodeWithScope (org.bson.types.CodeWithScope)1 MaxKey (org.bson.types.MaxKey)1 MinKey (org.bson.types.MinKey)1 ObjectId (org.bson.types.ObjectId)1