From f5d5a813f338f61f3192e9799f6d6eb7615ec020 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 3 Apr 2018 13:22:49 +0200 Subject: [PATCH] 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. --- .../data/mongodb/MongoDbFactory.java | 10 +++++----- .../mongodb/core/SimpleMongoDbFactory.java | 19 +++++++++++-------- .../core/SimpleMongoDbFactoryUnitTests.java | 15 +++++++++------ 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/MongoDbFactory.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/MongoDbFactory.java index 6e760e6a5..b043a15ae 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/MongoDbFactory.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/MongoDbFactory.java @@ -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(); 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 6690ffab2..806a12c78 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 @@ -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!"); + } } 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 index a28d7c083..77ef12bc6 100644 --- 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 @@ -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) {} } }