DATAMONGO-1903 - Polishing.
Remove client side operating system check as operating system-dependant constraints depend on the server. Add check on whitespaces. Add author tags. Extend tests. Extract database name assertion in an own method. Original pull request: #546.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2013 the original author or authors.
|
||||
* Copyright 2011-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,7 +23,7 @@ import com.mongodb.DB;
|
||||
|
||||
/**
|
||||
* Interface for factories creating {@link DB} instances.
|
||||
*
|
||||
*
|
||||
* @author Mark Pollack
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@@ -31,7 +31,7 @@ public interface MongoDbFactory {
|
||||
|
||||
/**
|
||||
* Creates a default {@link DB} instance.
|
||||
*
|
||||
*
|
||||
* @return
|
||||
* @throws DataAccessException
|
||||
*/
|
||||
@@ -39,7 +39,7 @@ public interface MongoDbFactory {
|
||||
|
||||
/**
|
||||
* Creates a {@link DB} instance to access the database with the given name.
|
||||
*
|
||||
*
|
||||
* @param dbName must not be {@literal null} or empty.
|
||||
* @return
|
||||
* @throws DataAccessException
|
||||
@@ -48,7 +48,7 @@ public interface MongoDbFactory {
|
||||
|
||||
/**
|
||||
* Exposes a shared {@link MongoExceptionTranslator}.
|
||||
*
|
||||
*
|
||||
* @return will never be {@literal null}.
|
||||
*/
|
||||
PersistenceExceptionTranslator getExceptionTranslator();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2015 the original author or authors.
|
||||
* Copyright 2011-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -41,6 +41,8 @@ import com.mongodb.WriteConcern;
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
* @author George Moraitis
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
|
||||
|
||||
@@ -140,8 +142,7 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
|
||||
|
||||
Assert.notNull(mongo, "Mongo must not be null");
|
||||
Assert.hasText(databaseName, "Database name must not be empty");
|
||||
Assert.isTrue(databaseName.matches("[^/\\\\.$\"]+"),
|
||||
"Database name must not contain any of the symbols[" + "[^/\\\\.$\"]+" + "]");
|
||||
assertDatabaseName(databaseName);
|
||||
|
||||
this.mongo = mongo;
|
||||
this.databaseName = databaseName;
|
||||
@@ -163,13 +164,9 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
|
||||
*/
|
||||
private SimpleMongoDbFactory(MongoClient client, String databaseName, boolean mongoInstanceCreated) {
|
||||
|
||||
Boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
|
||||
String validNamePattern = isWindows ? "[^/\\\\.$*<>:|?\"]+" : "[^/\\\\.$\"]+";
|
||||
|
||||
Assert.notNull(client, "MongoClient must not be null!");
|
||||
Assert.hasText(databaseName, "Database name must not be empty!");
|
||||
Assert.isTrue(databaseName.matches(validNamePattern),
|
||||
"Database name must not contain any of the symbols[" + (isWindows ? "/\\.$*<>:|?\"" : "/\\.$\"") + "]");
|
||||
assertDatabaseName(databaseName);
|
||||
|
||||
this.mongo = client;
|
||||
this.databaseName = databaseName;
|
||||
@@ -237,4 +234,10 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
|
||||
public PersistenceExceptionTranslator getExceptionTranslator() {
|
||||
return this.exceptionTranslator;
|
||||
}
|
||||
|
||||
private static void assertDatabaseName(String databaseName) {
|
||||
|
||||
Assert.isTrue(databaseName.matches("[^/\\\\.$\"\\s]+"),
|
||||
"Database name must not contain slashes, dots, spaces, quotes, or dollar signs!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2017 the original author or authors.
|
||||
* Copyright 2011-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -39,7 +39,7 @@ import com.mongodb.MongoURI;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link SimpleMongoDbFactory}.
|
||||
*
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@@ -49,10 +49,15 @@ public class SimpleMongoDbFactoryUnitTests {
|
||||
public @Rule ExpectedException expectedException = ExpectedException.none();
|
||||
@Mock Mongo mongo;
|
||||
|
||||
@Test // DATADOC-254
|
||||
@Test // DATADOC-254, DATAMONGO-1903
|
||||
public void rejectsIllegalDatabaseNames() {
|
||||
|
||||
rejectsDatabaseName("foo.bar");
|
||||
rejectsDatabaseName("foo$bar");
|
||||
rejectsDatabaseName("foo\\bar");
|
||||
rejectsDatabaseName("foo//bar");
|
||||
rejectsDatabaseName("foo bar");
|
||||
rejectsDatabaseName("foo\"bar");
|
||||
}
|
||||
|
||||
@Test // DATADOC-254
|
||||
@@ -134,8 +139,6 @@ public class SimpleMongoDbFactoryUnitTests {
|
||||
try {
|
||||
new SimpleMongoDbFactory(mongo, databaseName);
|
||||
fail("Expected database name " + databaseName + " to be rejected!");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
|
||||
}
|
||||
} catch (IllegalArgumentException ex) {}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user