diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/SimpleMongoDbFactory.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/SimpleMongoDbFactory.java index 0db2b51ef..978a787b2 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/SimpleMongoDbFactory.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/SimpleMongoDbFactory.java @@ -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; } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/SimpleMongoDbFactoryUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/SimpleMongoDbFactoryUnitTests.java new file mode 100644 index 000000000..609f264b9 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/SimpleMongoDbFactoryUnitTests.java @@ -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) { + + } + } +}