DATAMONGO-1903 - Align database name check in SimpleMongoDbFactory with MongoDB limitations.

We now test database names against the current (3.6) MongoDB specifications for database names.

Original pull request: #546.
This commit is contained in:
George Moraitis
2018-04-01 23:31:29 +03:00
committed by Mark Paluch
parent 3ed0bd7a18
commit 78429eb33d
2 changed files with 6 additions and 3 deletions

View File

@@ -77,10 +77,13 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
*/
private SimpleMongoDbFactory(MongoClient mongoClient, String databaseName, boolean mongoInstanceCreated) {
Boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
String validNamePattern = isWindows ? "[^/\\\\.$*<>:|?\"]+" : "[^/\\\\.$\"]+";
Assert.notNull(mongoClient, "MongoClient must not be null!");
Assert.hasText(databaseName, "Database name must not be empty!");
Assert.isTrue(databaseName.matches("[\\w-]+"),
"Database name must only contain letters, numbers, underscores and dashes!");
Assert.isTrue(databaseName.matches(validNamePattern),
"Database name must not contain any of the symbols[" + (isWindows ? "/\\.$*<>:|?\"" : "/\\.$\"") + "]");
this.mongoClient = mongoClient;
this.databaseName = databaseName;

View File

@@ -52,7 +52,7 @@ public class SimpleMongoDbFactoryUnitTests {
@Test // DATADOC-254
public void rejectsIllegalDatabaseNames() {
rejectsDatabaseName("foo.bar");
rejectsDatabaseName("foo!bar");
rejectsDatabaseName("foo$bar");
}
@Test // DATADOC-254