use of org.bson.BsonRegularExpression in project mongo-java-driver by mongodb.
the class CollectionAcceptanceTest method shouldAcceptDocumentsWithAllValidValueTypes.
@Test
public void shouldAcceptDocumentsWithAllValidValueTypes() {
Document doc = new Document();
doc.append("_id", new ObjectId());
doc.append("bool", true);
doc.append("int", 3);
doc.append("long", 5L);
doc.append("str", "Hello MongoDB");
doc.append("double", 1.1);
doc.append("date", new Date());
doc.append("ts", new BsonTimestamp(5, 1));
doc.append("pattern", new BsonRegularExpression("abc"));
doc.append("minKey", new MinKey());
doc.append("maxKey", new MaxKey());
doc.append("js", new Code("code"));
doc.append("jsWithScope", new CodeWithScope("code", new Document()));
doc.append("null", null);
doc.append("binary", new Binary((byte) 42, new byte[] { 10, 11, 12 }));
doc.append("list", Arrays.asList(7, 8, 9));
doc.append("doc list", Arrays.asList(new Document("x", 1), new Document("x", 2)));
collection.insertOne(doc);
Document found = collection.find().first();
assertNotNull(found);
assertEquals(ObjectId.class, found.get("_id").getClass());
assertEquals(Boolean.class, found.get("bool").getClass());
assertEquals(Integer.class, found.get("int").getClass());
assertEquals(Long.class, found.get("long").getClass());
assertEquals(String.class, found.get("str").getClass());
assertEquals(Double.class, found.get("double").getClass());
assertEquals(Date.class, found.get("date").getClass());
assertEquals(BsonTimestamp.class, found.get("ts").getClass());
assertEquals(BsonRegularExpression.class, found.get("pattern").getClass());
assertEquals(MinKey.class, found.get("minKey").getClass());
assertEquals(MaxKey.class, found.get("maxKey").getClass());
assertEquals(Code.class, found.get("js").getClass());
assertEquals(CodeWithScope.class, found.get("jsWithScope").getClass());
assertNull(found.get("null"));
assertEquals(Binary.class, found.get("binary").getClass());
assertTrue(found.get("list") instanceof List);
assertTrue(found.get("doc list") instanceof List);
}
use of org.bson.BsonRegularExpression in project mongo-java-driver by mongodb.
the class JsonScanner method scanRegularExpression.
/**
* Reads {@code RegularExpressionToken} from source. The following variants of lexemes are possible:
* <pre>
* /pattern/
* /\(pattern\)/
* /pattern/ims
* </pre>
* Options can include 'i','m','x','s'
*
* @return The regular expression token.
* @throws JsonParseException if regular expression representation is not valid.
*/
private JsonToken scanRegularExpression() {
int start = buffer.getPosition() - 1;
int options = -1;
RegularExpressionState state = RegularExpressionState.IN_PATTERN;
while (true) {
int c = buffer.read();
switch(state) {
case IN_PATTERN:
switch(c) {
case -1:
state = RegularExpressionState.INVALID;
break;
case '/':
state = RegularExpressionState.IN_OPTIONS;
options = buffer.getPosition();
break;
case '\\':
state = RegularExpressionState.IN_ESCAPE_SEQUENCE;
break;
default:
state = RegularExpressionState.IN_PATTERN;
break;
}
break;
case IN_ESCAPE_SEQUENCE:
state = RegularExpressionState.IN_PATTERN;
break;
case IN_OPTIONS:
switch(c) {
case 'i':
case 'm':
case 'x':
case 's':
state = RegularExpressionState.IN_OPTIONS;
break;
case ',':
case '}':
case ']':
case ')':
case -1:
state = RegularExpressionState.DONE;
break;
default:
if (Character.isWhitespace(c)) {
state = RegularExpressionState.DONE;
} else {
state = RegularExpressionState.INVALID;
}
break;
}
break;
default:
break;
}
switch(state) {
case DONE:
buffer.unread(c);
int end = buffer.getPosition();
BsonRegularExpression regex = new BsonRegularExpression(buffer.substring(start + 1, options - 1), buffer.substring(options, end));
return new JsonToken(JsonTokenType.REGULAR_EXPRESSION, regex);
case INVALID:
throw new JsonParseException("Invalid JSON regular expression. Position: %d.", buffer.getPosition());
default:
}
}
}
use of org.bson.BsonRegularExpression in project mongo-java-driver by mongodb.
the class JsonScannerTest method testRegularExpressionEmpty.
@Test
public void testRegularExpressionEmpty() {
String json = "\t //,";
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("", regularExpression.getPattern());
assertEquals("", regularExpression.getOptions());
assertEquals(',', buffer.read());
}
use of org.bson.BsonRegularExpression in project mongo-java-driver by mongodb.
the class JsonWriterTest method testRegularExpressionShell.
@Test
public void testRegularExpressionShell() {
List<TestData<BsonRegularExpression>> tests;
tests = asList(new TestData<BsonRegularExpression>(new BsonRegularExpression(""), "/(?:)/"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a"), "/a/"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a/b"), "/a\\/b/"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a\\b"), "/a\\b/"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "i"), "/a/i"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "m"), "/a/m"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "x"), "/a/x"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "s"), "/a/s"), new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "imxs"), "/a/imsx"));
for (final TestData<BsonRegularExpression> cur : tests) {
stringWriter = new StringWriter();
writer = new JsonWriter(stringWriter, JsonWriterSettings.builder().outputMode(JsonMode.SHELL).build());
writer.writeStartDocument();
writer.writeRegularExpression("regex", cur.value);
writer.writeEndDocument();
String expected = "{ \"regex\" : " + cur.expected + " }";
assertEquals(expected, stringWriter.toString());
}
}
use of org.bson.BsonRegularExpression in project mongo-java-driver by mongodb.
the class JsonReaderTest method testRegularExpressionShell.
@Test
public void testRegularExpressionShell() {
String json = "/pattern/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());
}
Aggregations