From 19c878837630658a98be6216c9bcf610f229def6 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 3 Apr 2017 14:46:15 +0200 Subject: [PATCH] DATAMONGO-1643 - Polishing. Fix documentation for namespace types. Remove unused ReflectiveDBCollectionInvoker. Simplify tests, align bean name to mongoClient. Remove injection of Mongo in favor of injecting MongoTemplate. Remove throws declaration from AbstractMongoConfiguration.mongoClient(). MongoClient creation does not throw checked exceptions so throwing an Exception is no longer required. Replace Mongo by MongoClient in reference documentation. Original pull request: #451. --- .../config/AbstractMongoConfiguration.java | 22 +++--- .../config/MongoConfigurationSupport.java | 10 +-- .../core/ReflectiveDBCollectionInvoker.java | 74 ------------------- .../data/mongodb/config/spring-mongo-2.0.xsd | 28 ++++--- .../java/ConfigClassInDefaultPackage.java | 8 +- .../config/AbstractIntegrationTests.java | 2 +- .../AbstractMongoConfigurationUnitTests.java | 4 +- ...uditingViaJavaConfigRepositoriesTests.java | 4 +- ...efaultIndexOperationsIntegrationTests.java | 27 ++++++- .../core/DefaultScriptOperationsTests.java | 5 +- .../core/MongoAdminIntegrationTests.java | 4 +- .../data/mongodb/core/NoExplicitIdTests.java | 2 +- .../mongodb/core/PersonExampleAppConfig.java | 5 +- .../mongodb/core/TestMongoConfiguration.java | 18 ++++- .../core/geo/AbstractGeoSpatialTests.java | 2 +- .../data/mongodb/core/geo/GeoJsonTests.java | 2 +- .../core/mapping/GeoIndexedAppConfig.java | 4 +- ...ApplicationContextEventTestsAppConfig.java | 5 +- .../mongodb/core/mapreduce/GroupByTests.java | 34 +-------- .../core/mapreduce/MapReduceTests.java | 29 +------- .../ComplexIdRepositoryIntegrationTests.java | 2 +- ...oRepositoryTextSearchIntegrationTests.java | 4 +- .../repository/cdi/MongoTemplateProducer.java | 9 +-- .../resources/namespace/db-factory-bean.xml | 18 +---- src/main/asciidoc/reference/mapping.adoc | 4 +- src/main/asciidoc/reference/mongo-3.adoc | 2 +- .../reference/mongo-repositories.adoc | 8 +- src/main/asciidoc/reference/mongodb.adoc | 30 ++++---- 28 files changed, 127 insertions(+), 239 deletions(-) delete mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReflectiveDBCollectionInvoker.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/AbstractMongoConfiguration.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/AbstractMongoConfiguration.java index f6ccc1f90..00f381e32 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/AbstractMongoConfiguration.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/AbstractMongoConfiguration.java @@ -29,7 +29,7 @@ import com.mongodb.MongoClient; /** * Base class for Spring Data MongoDB configuration using JavaConfig. - * + * * @author Mark Pollack * @author Oliver Gierke * @author Thomas Darimont @@ -39,22 +39,21 @@ import com.mongodb.MongoClient; * @see MongoConfigurationSupport */ @Configuration -public abstract class AbstractMongoConfiguration extends MongoConfigurationSupport { +public abstract class +AbstractMongoConfiguration extends MongoConfigurationSupport { /** * Return the {@link MongoClient} instance to connect to. Annotate with {@link Bean} in case you want to expose a * {@link MongoClient} instance to the {@link org.springframework.context.ApplicationContext}. - * + * * @return - * @throws Exception */ - public abstract MongoClient mongoClient() throws Exception; + public abstract MongoClient mongoClient(); /** * Creates a {@link MongoTemplate}. - * + * * @return - * @throws Exception */ @Bean public MongoTemplate mongoTemplate() throws Exception { @@ -64,14 +63,13 @@ public abstract class AbstractMongoConfiguration extends MongoConfigurationSuppo /** * Creates a {@link SimpleMongoDbFactory} to be used by the {@link MongoTemplate}. Will use the {@link MongoClient} * instance configured in {@link #mongo()}. - * - * @see #mongo() + * + * @see #mongoClient() * @see #mongoTemplate() * @return - * @throws Exception */ @Bean - public MongoDbFactory mongoDbFactory() throws Exception { + public MongoDbFactory mongoDbFactory() { return new SimpleMongoDbFactory(mongoClient(), getDatabaseName()); } @@ -95,7 +93,7 @@ public abstract class AbstractMongoConfiguration extends MongoConfigurationSuppo /** * Creates a {@link MappingMongoConverter} using the configured {@link #mongoDbFactory()} and * {@link #mongoMappingContext()}. Will get {@link #customConversions()} applied. - * + * * @see #customConversions() * @see #mongoMappingContext() * @see #mongoDbFactory() diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoConfigurationSupport.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoConfigurationSupport.java index b948bdfdc..27c8088cb 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoConfigurationSupport.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoConfigurationSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 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. @@ -62,7 +62,7 @@ public abstract class MongoConfigurationSupport { /** * Return the name of the database to connect to. - * + * * @return must not be {@literal null}. */ protected abstract String getDatabaseName(); @@ -72,7 +72,7 @@ public abstract class MongoConfigurationSupport { * configuration class' (the concrete class, not this one here) by default. So if you have a * {@code com.acme.AppConfig} extending {@link MongoConfigurationSupport} the base package will be considered * {@code com.acme} unless the method is overridden to implement alternate behavior. - * + * * @return the base packages to scan for mapped {@link Document} classes or an empty collection to not enable scanning * for entities. * @since 1.10 @@ -85,8 +85,8 @@ public abstract class MongoConfigurationSupport { /** * Creates a {@link MongoMappingContext} equipped with entity classes scanned from the mapping base package. - * - * @see #getMappingBasePackage() + * + * @see #getMappingBasePackages() * @return * @throws ClassNotFoundException */ diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReflectiveDBCollectionInvoker.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReflectiveDBCollectionInvoker.java deleted file mode 100644 index ca1595f67..000000000 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReflectiveDBCollectionInvoker.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2015-2017 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 org.bson.Document; -import org.springframework.data.mongodb.util.MongoClientVersion; - -import com.mongodb.DBCollection; - -/** - * {@link ReflectiveDBCollectionInvoker} provides reflective access to {@link DBCollection} API that is not consistently - * available for various driver versions. - * - * @author Christoph Strobl - * @author Oliver Gierke - * @since 1.7 - */ -class ReflectiveDBCollectionInvoker { - - private ReflectiveDBCollectionInvoker() {} - - /** - * Convenience method to generate an index name from the set of fields it is over. Will fall back to a MongoDB Java - * driver version 2 compatible way of generating index name in case of {@link MongoClientVersion#isMongo3Driver()}. - * - * @param keys the names of the fields used in this index - * @return - */ - public static String generateIndexName(Document keys) { - return genIndexName(keys); - } - - /** - * Borrowed from MongoDB Java driver version 2. See - * - * http://github.com/mongodb/mongo-java-driver/blob/r2.13.0/src/main/com/mongodb/DBCollection.java#L754 - * - * @param keys - * @return - */ - private static String genIndexName(Document keys) { - - StringBuilder name = new StringBuilder(); - - for (String s : keys.keySet()) { - - if (name.length() > 0) { - name.append('_'); - } - - name.append(s).append('_'); - Object val = keys.get(s); - - if (val instanceof Number || val instanceof String) { - name.append(val.toString().replace(' ', '_')); - } - } - - return name.toString(); - } -} diff --git a/spring-data-mongodb/src/main/resources/org/springframework/data/mongodb/config/spring-mongo-2.0.xsd b/spring-data-mongodb/src/main/resources/org/springframework/data/mongodb/config/spring-mongo-2.0.xsd index bca922e1c..03041f37e 100644 --- a/spring-data-mongodb/src/main/resources/org/springframework/data/mongodb/config/spring-mongo-2.0.xsd +++ b/spring-data-mongodb/src/main/resources/org/springframework/data/mongodb/config/spring-mongo-2.0.xsd @@ -1,10 +1,8 @@ @@ -38,13 +36,13 @@ Defines a MongoDbFactory for connecting to a specific database +The name of the MongoDbFactory definition (by default "mongoDbFactory").]]> @@ -120,7 +118,7 @@ The MongoClientURI string.]]> + domain objects to and from Mongo's Document]]> @@ -211,7 +209,7 @@ Defines a JMX Model MBeans for monitoring a MongoDB server'. +The name of the MongoClient object that determines what server to monitor. (by default "mongoClient").]]> @@ -350,7 +348,7 @@ The Mongo driver options +The name of the MongoClient definition (by default "mongoClient").]]> @@ -406,7 +404,7 @@ The minimum number of connections per host. @@ -422,7 +420,7 @@ then 50 threads can block more than that and an exception will be thrown. @@ -575,20 +573,20 @@ The SSLSocketFactory to use for the SSL connection. If none is configured here, +The name of the MongoTemplate definition (by default "mongoTemplate").]]> @@ -628,20 +626,20 @@ The reference to a Mongoconverter instance. +The name of the GridFsTemplate definition (by default "gridFsTemplate").]]> diff --git a/spring-data-mongodb/src/test/java/ConfigClassInDefaultPackage.java b/spring-data-mongodb/src/test/java/ConfigClassInDefaultPackage.java index 4e1d6d4a0..f80be3297 100644 --- a/spring-data-mongodb/src/test/java/ConfigClassInDefaultPackage.java +++ b/spring-data-mongodb/src/test/java/ConfigClassInDefaultPackage.java @@ -21,13 +21,13 @@ import com.mongodb.MongoClient; /** * Sample configuration class in default package. - * + * * @author Oliver Gierke */ @Configuration public class ConfigClassInDefaultPackage extends AbstractMongoConfiguration { - /* + /* * (non-Javadoc) * @see org.springframework.data.mongodb.config.AbstractMongoConfiguration#getDatabaseName() */ @@ -36,12 +36,12 @@ public class ConfigClassInDefaultPackage extends AbstractMongoConfiguration { return "default"; } - /* + /* * (non-Javadoc) * @see org.springframework.data.mongodb.config.AbstractMongoConfiguration#mongo() */ @Override - public MongoClient mongoClient() throws Exception { + public MongoClient mongoClient() { return new MongoClient(); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AbstractIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AbstractIntegrationTests.java index 79344c90b..d163a7402 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AbstractIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AbstractIntegrationTests.java @@ -51,7 +51,7 @@ public abstract class AbstractIntegrationTests { } @Override - public MongoClient mongoClient() throws Exception { + public MongoClient mongoClient() { return new MongoClient(); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AbstractMongoConfigurationUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AbstractMongoConfigurationUnitTests.java index 64b12e5dd..4d3fe3d5a 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AbstractMongoConfigurationUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AbstractMongoConfigurationUnitTests.java @@ -157,7 +157,7 @@ public class AbstractMongoConfigurationUnitTests { } @Override - public MongoClient mongoClient() throws Exception { + public MongoClient mongoClient() { return new MongoClient(); } @@ -185,7 +185,7 @@ public class AbstractMongoConfigurationUnitTests { } @Override - public MongoClient mongoClient() throws Exception { + public MongoClient mongoClient() { return new MongoClient(); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AuditingViaJavaConfigRepositoriesTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AuditingViaJavaConfigRepositoriesTests.java index 84612c5f4..ad8759e8f 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AuditingViaJavaConfigRepositoriesTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AuditingViaJavaConfigRepositoriesTests.java @@ -64,7 +64,7 @@ public class AuditingViaJavaConfigRepositoriesTests { } @Override - public MongoClient mongoClient() throws Exception { + public MongoClient mongoClient() { return new MongoClient(); } @@ -119,7 +119,7 @@ public class AuditingViaJavaConfigRepositoriesTests { static class SimpleConfig extends AbstractMongoConfiguration { @Override - public MongoClient mongoClient() throws Exception { + public MongoClient mongoClient() { return new MongoClient(); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultIndexOperationsIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultIndexOperationsIntegrationTests.java index 632205c90..001db507b 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultIndexOperationsIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultIndexOperationsIntegrationTests.java @@ -18,13 +18,11 @@ package org.springframework.data.mongodb.core; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import static org.junit.Assume.*; -import static org.springframework.data.mongodb.core.ReflectiveDBCollectionInvoker.*; import static org.springframework.data.mongodb.core.index.PartialIndexFilter.*; import static org.springframework.data.mongodb.core.query.Criteria.*; import org.bson.Document; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -43,7 +41,7 @@ import com.mongodb.client.MongoCollection; /** * Integration tests for {@link DefaultIndexOperations}. - * + * * @author Christoph Strobl * @author Oliver Gierke */ @@ -153,7 +151,7 @@ public class DefaultIndexOperationsIntegrationTests { } private static IndexInfo findAndReturnIndexInfo(Iterable candidates, org.bson.Document keys) { - return findAndReturnIndexInfo(candidates, generateIndexName(keys)); + return findAndReturnIndexInfo(candidates, genIndexName(keys)); } private static IndexInfo findAndReturnIndexInfo(Iterable candidates, String name) { @@ -166,6 +164,27 @@ public class DefaultIndexOperationsIntegrationTests { throw new AssertionError(String.format("Index with %s was not found", name)); } + private static String genIndexName(Document keys) { + + StringBuilder name = new StringBuilder(); + + for (String s : keys.keySet()) { + + if (name.length() > 0) { + name.append('_'); + } + + name.append(s).append('_'); + Object val = keys.get(s); + + if (val instanceof Number || val instanceof String) { + name.append(val.toString().replace(' ', '_')); + } + } + + return name.toString(); + } + @org.springframework.data.mongodb.core.mapping.Document(collection = "default-index-operations-tests") static class DefaultIndexOperationsIntegrationTestsSample { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultScriptOperationsTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultScriptOperationsTests.java index 23bef3d7e..21e2c7157 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultScriptOperationsTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultScriptOperationsTests.java @@ -35,12 +35,11 @@ import org.springframework.data.mongodb.core.script.NamedMongoScript; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.mongodb.Mongo; import com.mongodb.MongoClient; /** * Integration tests for {@link DefaultScriptOperations}. - * + * * @author Christoph Strobl * @author Oliver Gierke * @since 1.7 @@ -55,7 +54,7 @@ public class DefaultScriptOperationsTests { private static final String DB_NAME = "script-tests"; @Bean - public MongoClient mongoClient() throws Exception { + public MongoClient mongoClient() { return new MongoClient(); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoAdminIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoAdminIntegrationTests.java index 6611d9540..73c689d1c 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoAdminIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoAdminIntegrationTests.java @@ -29,7 +29,7 @@ import com.mongodb.MongoClient; /** * This test class assumes that you are already running the MongoDB server. - * + * * @author Mark Pollack */ @RunWith(SpringJUnit4ClassRunner.class) @@ -55,7 +55,7 @@ public class MongoAdminIntegrationTests { } @Test - public void datanaseStats() { + public void databaseStats() { logger.info(mongoAdmin.getDatabaseStats("testAdminDb")); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/NoExplicitIdTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/NoExplicitIdTests.java index 01cf6d4ab..cb3f14bc4 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/NoExplicitIdTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/NoExplicitIdTests.java @@ -56,7 +56,7 @@ public class NoExplicitIdTests { } @Override - public MongoClient mongoClient() throws Exception { + public MongoClient mongoClient() { return new MongoClient(); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonExampleAppConfig.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonExampleAppConfig.java index 06b6f3390..edad8a52d 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonExampleAppConfig.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonExampleAppConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2017 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. @@ -18,14 +18,13 @@ package org.springframework.data.mongodb.core; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import com.mongodb.Mongo; import com.mongodb.MongoClient; @Configuration public class PersonExampleAppConfig { @Bean - public MongoClient mongoClient() throws Exception { + public MongoClient mongoClient() { return new MongoClient("localhost"); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/TestMongoConfiguration.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/TestMongoConfiguration.java index c946b79aa..4bfd2be45 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/TestMongoConfiguration.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/TestMongoConfiguration.java @@ -1,3 +1,18 @@ +/* + * Copyright 2017 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 java.util.ArrayList; @@ -9,7 +24,6 @@ import org.springframework.data.mongodb.config.AbstractMongoConfiguration; import org.springframework.data.mongodb.core.convert.CustomConversions; import org.springframework.data.mongodb.core.mapping.MongoMappingContext; -import com.mongodb.Mongo; import com.mongodb.MongoClient; public class TestMongoConfiguration extends AbstractMongoConfiguration { @@ -21,7 +35,7 @@ public class TestMongoConfiguration extends AbstractMongoConfiguration { @Override @Bean - public MongoClient mongoClient() throws Exception { + public MongoClient mongoClient() { return new MongoClient("127.0.0.1", 27017); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/AbstractGeoSpatialTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/AbstractGeoSpatialTests.java index ffee1cbd4..9f1621c25 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/AbstractGeoSpatialTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/AbstractGeoSpatialTests.java @@ -65,7 +65,7 @@ public abstract class AbstractGeoSpatialTests { } @Override - public MongoClient mongoClient() throws Exception { + public MongoClient mongoClient() { return new MongoClient(); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonTests.java index b22b9baad..48b60ecb1 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonTests.java @@ -70,7 +70,7 @@ public class GeoJsonTests { } @Override - public MongoClient mongoClient() throws Exception { + public MongoClient mongoClient() { return new MongoClient(); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/GeoIndexedAppConfig.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/GeoIndexedAppConfig.java index a485cd872..c09553cab 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/GeoIndexedAppConfig.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/GeoIndexedAppConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 the original author or authors. + * Copyright 2011-2017 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. @@ -34,7 +34,7 @@ public class GeoIndexedAppConfig extends AbstractMongoConfiguration { @Override @Bean - public MongoClient mongoClient() throws Exception { + public MongoClient mongoClient() { return new MongoClient("127.0.0.1"); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTestsAppConfig.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTestsAppConfig.java index f3750afae..a0e155558 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTestsAppConfig.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTestsAppConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2011 the original author or authors. + * Copyright 2010-2017 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. @@ -19,7 +19,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.config.AbstractMongoConfiguration; -import com.mongodb.Mongo; import com.mongodb.MongoClient; @Configuration @@ -32,7 +31,7 @@ public class ApplicationContextEventTestsAppConfig extends AbstractMongoConfigur @Override @Bean - public MongoClient mongoClient() throws Exception { + public MongoClient mongoClient() { return new MongoClient("127.0.0.1"); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/GroupByTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/GroupByTests.java index cc9add28a..fe3b45151 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/GroupByTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/GroupByTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2015 the original author or authors. + * Copyright 2011-2017 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. @@ -20,9 +20,6 @@ import static org.junit.Assert.*; import static org.springframework.data.mongodb.core.mapreduce.GroupBy.*; import static org.springframework.data.mongodb.core.query.Criteria.*; -import java.util.Arrays; -import java.util.HashSet; - import org.bson.Document; import org.junit.After; import org.junit.Assert; @@ -30,22 +27,15 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; -import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.core.convert.DbRefResolver; -import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver; -import org.springframework.data.mongodb.core.convert.MappingMongoConverter; -import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.mongodb.Mongo; import com.mongodb.client.MongoCollection; /** * Integration tests for group-by operations. - * + * * @author Mark Pollack * @author Oliver Gierke * @author Christoph Strobl @@ -54,25 +44,7 @@ import com.mongodb.client.MongoCollection; @ContextConfiguration("classpath:infrastructure.xml") public class GroupByTests { - @Autowired MongoDbFactory factory; - @Autowired ApplicationContext applicationContext; - - MongoTemplate mongoTemplate; - - @Autowired - public void setMongo(Mongo mongo) throws Exception { - - MongoMappingContext mappingContext = new MongoMappingContext(); - mappingContext.setInitialEntitySet(new HashSet>(Arrays.asList(XObject.class))); - mappingContext.initialize(); - - DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory); - MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, mappingContext); - mappingConverter.afterPropertiesSet(); - - this.mongoTemplate = new MongoTemplate(factory, mappingConverter); - mongoTemplate.setApplicationContext(applicationContext); - } + @Autowired MongoTemplate mongoTemplate; @Before public void setUp() { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/MapReduceTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/MapReduceTests.java index 4a8c3904c..d77bb8d75 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/MapReduceTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/MapReduceTests.java @@ -22,7 +22,6 @@ import static org.springframework.data.mongodb.core.query.Criteria.*; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; @@ -34,22 +33,16 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.geo.Box; -import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.core.convert.DbRefResolver; -import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver; -import org.springframework.data.mongodb.core.convert.MappingMongoConverter; -import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.data.mongodb.core.query.Query; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.mongodb.Mongo; import com.mongodb.client.MongoCollection; /** * Integration test for {@link MongoTemplate}'s Map-Reduce operations - * + * * @author Mark Pollack * @author Thomas Darimont */ @@ -61,23 +54,7 @@ public class MapReduceTests { private String reduceFunction = "function(key,values){ var sum=0; for( var i=0; i>(Arrays.asList(ValueObject.class))); - mappingContext.initialize(); - - DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory); - MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, mappingContext); - mappingConverter.afterPropertiesSet(); - this.mongoTemplate = new MongoTemplate(factory, mappingConverter); - } + @Autowired MongoTemplate mongoTemplate; @Before public void setUp() { @@ -191,7 +168,7 @@ public class MapReduceTests { { "_id" : 3, "document_id" : "Resume", "author" : "Author", "content" : "...", "version" : 6 } { "_id" : 4, "document_id" : "Schema", "author" : "Someone Else", "content" : "...", "version" : 0.9 } { "_id" : 5, "document_id" : "Schema", "author" : "Someone Else", "content" : "...", "version" : 1 } - + */ ContentAndVersion cv1 = new ContentAndVersion(); cv1.setDocumentId("mongoDB How-To"); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ComplexIdRepositoryIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ComplexIdRepositoryIntegrationTests.java index fbfe91a02..290e8015d 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ComplexIdRepositoryIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ComplexIdRepositoryIntegrationTests.java @@ -57,7 +57,7 @@ public class ComplexIdRepositoryIntegrationTests { } @Override - public MongoClient mongoClient() throws Exception { + public MongoClient mongoClient() { return new MongoClient(); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MongoRepositoryTextSearchIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MongoRepositoryTextSearchIntegrationTests.java index 657bf2a23..a3a879120 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MongoRepositoryTextSearchIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/MongoRepositoryTextSearchIntegrationTests.java @@ -55,7 +55,7 @@ import com.mongodb.MongoClient; /** * Integration tests for text searches on repository. - * + * * @author Christoph Strobl * @author Oliver Gierke */ @@ -207,7 +207,7 @@ public class MongoRepositoryTextSearchIntegrationTests { } @Override - public MongoClient mongoClient() throws Exception { + public MongoClient mongoClient() { return new MongoClient(); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/cdi/MongoTemplateProducer.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/cdi/MongoTemplateProducer.java index ddc1bcbe1..198a829c2 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/cdi/MongoTemplateProducer.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/cdi/MongoTemplateProducer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 the original author or authors. + * Copyright 2012-2017 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. @@ -15,8 +15,6 @@ */ package org.springframework.data.mongodb.repository.cdi; -import java.net.UnknownHostException; - import javax.enterprise.context.ApplicationScoped; import javax.enterprise.inject.Produces; @@ -26,18 +24,17 @@ import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import com.mongodb.MongoClient; -import com.mongodb.MongoException; /** * Simple component exposing a {@link MongoOperations} instance as CDI bean. - * + * * @author Oliver Gierke */ class MongoTemplateProducer { @Produces @ApplicationScoped - public MongoOperations createMongoTemplate() throws UnknownHostException, MongoException { + public MongoOperations createMongoTemplate() { MongoDbFactory factory = new SimpleMongoDbFactory(new MongoClient(), "database"); return new MongoTemplate(factory); diff --git a/spring-data-mongodb/src/test/resources/namespace/db-factory-bean.xml b/spring-data-mongodb/src/test/resources/namespace/db-factory-bean.xml index 15641c366..245004df8 100644 --- a/spring-data-mongodb/src/test/resources/namespace/db-factory-bean.xml +++ b/spring-data-mongodb/src/test/resources/namespace/db-factory-bean.xml @@ -5,20 +5,10 @@ xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> - + - + - - - - \ No newline at end of file + + diff --git a/src/main/asciidoc/reference/mapping.adoc b/src/main/asciidoc/reference/mapping.adoc index b54311eaa..80951832c 100644 --- a/src/main/asciidoc/reference/mapping.adoc +++ b/src/main/asciidoc/reference/mapping.adoc @@ -260,8 +260,8 @@ You can configure the `MappingMongoConverter` as well as `com.mongodb.MongoClien public class GeoSpatialAppConfig extends AbstractMongoConfiguration { @Bean - public Mongo mongo() throws Exception { - return new Mongo("localhost"); + public MongoClient mongoClient() { + return new MongoClient("localhost"); } @Override diff --git a/src/main/asciidoc/reference/mongo-3.adoc b/src/main/asciidoc/reference/mongo-3.adoc index 41d37542b..3ab427b4d 100644 --- a/src/main/asciidoc/reference/mongo-3.adoc +++ b/src/main/asciidoc/reference/mongo-3.adoc @@ -58,7 +58,7 @@ public class ApplicationContextEventTestsAppConfig extends AbstractMongoConfigur @Override @Bean - public Mongo mongo() throws Exception { + public MongoClient mongoClient() { return new MongoClient(singletonList(new ServerAddress("127.0.0.1", 27017)), singletonList(MongoCredential.createCredential("name", "db", "pwd".toCharArray()))); } diff --git a/src/main/asciidoc/reference/mongo-repositories.adoc b/src/main/asciidoc/reference/mongo-repositories.adoc index 149b7aa1b..ca0b9c6b3 100644 --- a/src/main/asciidoc/reference/mongo-repositories.adoc +++ b/src/main/asciidoc/reference/mongo-repositories.adoc @@ -87,8 +87,8 @@ class ApplicationConfig extends AbstractMongoConfiguration { } @Override - public Mongo mongo() throws Exception { - return new Mongo(); + public MongoClient mongoClient() { + return new MongoClient(); } @Override @@ -553,9 +553,9 @@ class MongoTemplateProducer { @Produces @ApplicationScoped - public MongoOperations createMongoTemplate() throws UnknownHostException, MongoException { + public MongoOperations createMongoTemplate() { - MongoDbFactory factory = new SimpleMongoDbFactory(new Mongo(), "database"); + MongoDbFactory factory = new SimpleMongoDbFactory(new MongoClient(), "database"); return new MongoTemplate(factory); } } diff --git a/src/main/asciidoc/reference/mongodb.adoc b/src/main/asciidoc/reference/mongodb.adoc index e96b12931..f897352ba 100644 --- a/src/main/asciidoc/reference/mongodb.adoc +++ b/src/main/asciidoc/reference/mongodb.adoc @@ -186,14 +186,14 @@ public class AppConfig { /* * Use the standard Mongo driver API to create a com.mongodb.MongoClient instance. */ - public @Bean MongoClient mongoClient() throws UnknownHostException { + public @Bean MongoClient mongoClient() { return new MongoClient("localhost"); } } ---- ==== -This approach allows you to use the standard `com.mongodb.MongoClient` instance with the container using Spring's `MongoClientFactoryBean`. As compared to instantiating a `com.mongodb.Mongo` instance directly, the FactoryBean approach does not throw a checked exception and has the added advantage of also providing the container with an ExceptionTranslator implementation that translates MongoDB exceptions to exceptions in Spring's portable `DataAccessException` hierarchy for data access classes annotated with the `@Repository` annotation. This hierarchy and use of `@Repository` is described in http://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/html/dao.html[Spring's DAO support features]. +This approach allows you to use the standard `com.mongodb.MongoClient` instance with the container using Spring's `MongoClientFactoryBean`. As compared to instantiating a `com.mongodb.MongoClient` instance directly, the FactoryBean has the added advantage of also providing the container with an ExceptionTranslator implementation that translates MongoDB exceptions to exceptions in Spring's portable `DataAccessException` hierarchy for data access classes annotated with the `@Repository` annotation. This hierarchy and use of `@Repository` is described in http://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/html/dao.html[Spring's DAO support features]. An example of a Java based bean metadata that supports exception translation on `@Repository` annotated classes is shown below: @@ -311,7 +311,7 @@ public class MongoApp { public static void main(String[] args) throws Exception { - MongoOperations mongoOps = new MongoTemplate(*new SimpleMongoDbFactory(new Mongo(), "database")*); + MongoOperations mongoOps = new MongoTemplate(*new SimpleMongoDbFactory(new MongoClient(), "database")*); mongoOps.insert(new Person("Joe", 34)); @@ -334,8 +334,8 @@ To register a MongoDbFactory instance with the container, you write code much li @Configuration public class MongoConfiguration { - public @Bean MongoDbFactory mongoDbFactory() throws Exception { - return new SimpleMongoDbFactory(new Mongo(), "database"); + public @Bean MongoDbFactory mongoDbFactory() { + return new SimpleMongoDbFactory(new MongoClient(), "database"); } } ---- @@ -354,7 +354,7 @@ public class ApplicationContextEventTestsAppConfig extends AbstractMongoConfigur @Override @Bean - public Mongo mongo() throws Exception { + public MongoClient mongoClient() { return new MongoClient(singletonList(new ServerAddress("127.0.0.1", 27017)), singletonList(MongoCredential.createCredential("name", "db", "pwd".toCharArray()))); } @@ -435,11 +435,11 @@ You can use Java to create and register an instance of `MongoTemplate` as shown @Configuration public class AppConfig { - public @Bean MongoClient mongoClient() throws Exception { + public @Bean MongoClient mongoClient() { return new MongoClient("localhost"); } - public @Bean MongoTemplate mongoTemplate() throws Exception { + public @Bean MongoTemplate mongoTemplate() { return new MongoTemplate(mongoClient(), "mydatabase"); } } @@ -471,7 +471,7 @@ NOTE: The preferred way to reference the operations on `MongoTemplate` instance [[mongo-template.writeresultchecking]] === WriteResultChecking Policy -When in development it is very handy to either log or throw an exception if the `com.mongodb.WriteResult` returned from any MongoDB operation contains an error. It is quite common to forget to do this during development and then end up with an application that looks like it runs successfully but in fact the database was not modified according to your expectations. Set MongoTemplate's property to an enum with the following values, `LOG`, `EXCEPTION`, or `NONE` to either log the error, throw and exception or do nothing. The default is to use a `WriteResultChecking` value of `NONE`. +When in development it is very handy to either log or throw an exception if the `com.mongodb.WriteResult` returned from any MongoDB operation contains an error. It is quite common to forget to do this during development and then end up with an application that looks like it runs successfully but in fact the database was not modified according to your expectations. Set MongoTemplate's property to an enum with the following values, `EXCEPTION`, or `NONE` to either throw an Exception or do nothing. The default is to use a `WriteResultChecking` value of `NONE`. [[mongo-template.writeconcern]] === WriteConcern @@ -571,7 +571,7 @@ public class MongoApp { private static final Log log = LogFactory.getLog(MongoApp.class); - public static void main(String[] args) throws Exception { + public static void main(String[] args) { MongoOperations mongoOps = new MongoTemplate(new SimpleMongoDbFactory(new MongoClient(), "database")); @@ -721,8 +721,8 @@ class SampleMongoConfiguration extends AbstractMongoConfiguration { } @Override - public Mongo mongo() throws Exception { - return new Mongo(); + public MongoClient mongoClient() { + return new MongoClient(); } @Bean @@ -2440,7 +2440,7 @@ class GridFsClient { GridFsOperations operations; @Test - public void storeFileToGridFs { + public void storeFileToGridFs() { FileMetadata metadata = new FileMetadata(); // populate metadata @@ -2466,7 +2466,7 @@ class GridFsClient { GridFsOperations operations; @Test - public void findFilesInGridFs { + public void findFilesInGridFs() { List result = operations.find(query(whereFilename().is("filename.txt"))) } } @@ -2487,7 +2487,7 @@ class GridFsClient { GridFsOperations operations; @Test - public void readFilesFromGridFs { + public void readFilesFromGridFs() { GridFsResources[] txtFiles = operations.getResources("*.txt"); } }