DATADOC-254 - Reject invalid MongoDB database names.
Only letters, numbers underscores and dashes are now allowed.
This commit is contained in:
@@ -52,6 +52,7 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
|
||||
public SimpleMongoDbFactory(Mongo mongo, String databaseName) {
|
||||
Assert.notNull(mongo, "Mongo 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!");
|
||||
this.mongo = mongo;
|
||||
this.databaseName = databaseName;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import com.mongodb.Mongo;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link SimpleMongoDbFactory}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SimpleMongoDbFactoryUnitTests {
|
||||
|
||||
@Mock
|
||||
Mongo mongo;
|
||||
|
||||
/**
|
||||
* @see DATADOC-254
|
||||
*/
|
||||
@Test
|
||||
public void rejectsIllegalDatabaseNames() {
|
||||
rejectsDatabaseName("foo.bar");
|
||||
rejectsDatabaseName("foo!bar");
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATADOC-254
|
||||
*/
|
||||
@Test
|
||||
public void allowsDatabaseNames() {
|
||||
new SimpleMongoDbFactory(mongo, "foo-bar");
|
||||
new SimpleMongoDbFactory(mongo, "foo_bar");
|
||||
new SimpleMongoDbFactory(mongo, "foo01231bar");
|
||||
}
|
||||
|
||||
private void rejectsDatabaseName(String databaseName) {
|
||||
|
||||
try {
|
||||
new SimpleMongoDbFactory(mongo, databaseName);
|
||||
fail("Expected database name " + databaseName + " to be rejected!");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user