diff --git a/.gitignore b/.gitignore index 3b34d16a8..be372b620 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ src/ant/.ant-targets-upload-dist.xml atlassian-ide-plugin.xml /.gradle/ /.idea/ +*.graphml \ No newline at end of file diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index fcef17658..5e935eb8d 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -232,6 +232,13 @@ test + + de.schauderhaft.degraph + degraph-check + 0.1.4 + test + + org.jetbrains.kotlin diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/GeoJsonConfiguration.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/GeoJsonConfiguration.java new file mode 100644 index 000000000..86317e15d --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/GeoJsonConfiguration.java @@ -0,0 +1,34 @@ +/* + * 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.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.data.mongodb.core.geo.GeoJsonModule; +import org.springframework.data.web.config.SpringDataJacksonModules; + +/** + * Configuration class to expose {@link GeoJsonModule} as a Spring bean. + * + * @author Oliver Gierke + * @author Jens Schauder + */ +public class GeoJsonConfiguration implements SpringDataJacksonModules { + + @Bean + public GeoJsonModule geoJsonModule() { + return new GeoJsonModule(); + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionOptions.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionOptions.java index 0d6b2ac39..aec3dde14 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionOptions.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionOptions.java @@ -17,6 +17,7 @@ package org.springframework.data.mongodb.core; import java.util.Optional; +import org.springframework.data.mongodb.core.query.Collation; import org.springframework.util.Assert; /** diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultBulkOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultBulkOperations.java index 5ab965edb..f60517ac6 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultBulkOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultBulkOperations.java @@ -31,6 +31,7 @@ import org.springframework.dao.support.PersistenceExceptionTranslator; import org.springframework.data.mongodb.core.convert.QueryMapper; import org.springframework.data.mongodb.core.convert.UpdateMapper; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; +import org.springframework.data.mongodb.core.query.Collation; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import org.springframework.data.util.Pair; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java index 3438e858b..7b46efd80 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java @@ -27,6 +27,7 @@ import org.springframework.data.mongodb.core.convert.QueryMapper; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.index.IndexDefinition; import org.springframework.data.mongodb.core.index.IndexInfo; +import org.springframework.data.mongodb.core.index.IndexOperations; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.util.Assert; @@ -89,7 +90,7 @@ public class DefaultIndexOperations implements IndexOperations { /* * (non-Javadoc) - * @see org.springframework.data.mongodb.core.IndexOperations#ensureIndex(org.springframework.data.mongodb.core.index.IndexDefinition) + * @see org.springframework.data.mongodb.core.index.IndexOperations#ensureIndex(org.springframework.data.mongodb.core.index.IndexDefinition) */ public String ensureIndex(final IndexDefinition indexDefinition) { @@ -136,7 +137,7 @@ public class DefaultIndexOperations implements IndexOperations { /* * (non-Javadoc) - * @see org.springframework.data.mongodb.core.IndexOperations#dropIndex(java.lang.String) + * @see org.springframework.data.mongodb.core.index.IndexOperations#dropIndex(java.lang.String) */ public void dropIndex(final String name) { @@ -149,7 +150,7 @@ public class DefaultIndexOperations implements IndexOperations { /* * (non-Javadoc) - * @see org.springframework.data.mongodb.core.IndexOperations#dropAllIndexes() + * @see org.springframework.data.mongodb.core.index.IndexOperations#dropAllIndexes() */ public void dropAllIndexes() { dropIndex("*"); @@ -157,7 +158,7 @@ public class DefaultIndexOperations implements IndexOperations { /* * (non-Javadoc) - * @see org.springframework.data.mongodb.core.IndexOperations#getIndexInfo() + * @see org.springframework.data.mongodb.core.index.IndexOperations#getIndexInfo() */ public List getIndexInfo() { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperationsProvider.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperationsProvider.java index 79ffe6c35..9412a3a65 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperationsProvider.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperationsProvider.java @@ -17,6 +17,8 @@ package org.springframework.data.mongodb.core; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.convert.QueryMapper; +import org.springframework.data.mongodb.core.index.IndexOperations; +import org.springframework.data.mongodb.core.index.IndexOperationsProvider; /** * {@link IndexOperationsProvider} to obtain {@link IndexOperations} from a given {@link MongoDbFactory}. TODO: Review @@ -38,7 +40,7 @@ class DefaultIndexOperationsProvider implements IndexOperationsProvider { } /* (non-Javadoc) - * @see org.springframework.data.mongodb.core.IndexOperationsProvider#reactiveIndexOps(java.lang.String) + * @see org.springframework.data.mongodb.core.index.IndexOperationsProvider#reactiveIndexOps(java.lang.String) */ @Override public IndexOperations indexOps(String collectionName) { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultReactiveIndexOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultReactiveIndexOperations.java index 4b5c4e43d..0ba81a33f 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultReactiveIndexOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultReactiveIndexOperations.java @@ -18,6 +18,7 @@ package org.springframework.data.mongodb.core; import org.bson.Document; import org.springframework.data.mongodb.core.index.IndexDefinition; import org.springframework.data.mongodb.core.index.IndexInfo; +import org.springframework.data.mongodb.core.index.IndexOperations; import org.springframework.util.Assert; import com.mongodb.reactivestreams.client.ListIndexesPublisher; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/FindAndModifyOptions.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/FindAndModifyOptions.java index 6a53f18fd..26edb6644 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/FindAndModifyOptions.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/FindAndModifyOptions.java @@ -17,6 +17,8 @@ package org.springframework.data.mongodb.core; import java.util.Optional; +import org.springframework.data.mongodb.core.query.Collation; + /** * @author Mark Pollak * @author Oliver Gierke diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/GeoJsonConfiguration.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/GeoJsonConfiguration.java index ccc81cd79..b1abe529d 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/GeoJsonConfiguration.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/GeoJsonConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2016 the original author or authors. + * 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. @@ -15,19 +15,17 @@ */ package org.springframework.data.mongodb.core; -import org.springframework.context.annotation.Bean; import org.springframework.data.mongodb.core.geo.GeoJsonModule; -import org.springframework.data.web.config.SpringDataJacksonModules; /** * Configuration class to expose {@link GeoJsonModule} as a Spring bean. - * + * * @author Oliver Gierke + * @author Jens Schauder + * + * @deprecated Use {@link org.springframework.data.mongodb.config.GeoJsonConfiguration} instead. */ -public class GeoJsonConfiguration implements SpringDataJacksonModules { +@Deprecated +public class GeoJsonConfiguration extends org.springframework.data.mongodb.config.GeoJsonConfiguration { - @Bean - public GeoJsonModule geoJsonModule() { - return new GeoJsonModule(); - } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/IndexConverters.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/IndexConverters.java index 66ddfdb0e..961fe330b 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/IndexConverters.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/IndexConverters.java @@ -125,7 +125,7 @@ abstract class IndexConverters { return null; } - return org.springframework.data.mongodb.core.Collation.from(source).toMongoCollation(); + return org.springframework.data.mongodb.core.query.Collation.from(source).toMongoCollation(); } private static Converter getDocumentIndexInfoConverter() { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/IndexOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/IndexOperations.java index 986314703..06a6f2555 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/IndexOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/IndexOperations.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2016 the original author or authors. + * 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. @@ -15,44 +15,17 @@ */ package org.springframework.data.mongodb.core; -import java.util.List; - -import org.springframework.data.mongodb.core.index.IndexDefinition; -import org.springframework.data.mongodb.core.index.IndexInfo; - /** * Index operations on a collection. - * + * * @author Mark Pollack * @author Oliver Gierke * @author Christoph Strobl + * @author Jens Schauder + * + * @deprecated Use {@link org.springframework.data.mongodb.core.index.IndexOperations} instead. */ -public interface IndexOperations { +@Deprecated +public interface IndexOperations extends org.springframework.data.mongodb.core.index.IndexOperations { - /** - * Ensure that an index for the provided {@link IndexDefinition} exists for the collection indicated by the entity - * class. If not it will be created. - * - * @param indexDefinition must not be {@literal null}. - */ - String ensureIndex(IndexDefinition indexDefinition); - - /** - * Drops an index from this collection. - * - * @param name name of index to drop - */ - void dropIndex(String name); - - /** - * Drops all indices from this collection. - */ - void dropAllIndexes(); - - /** - * Returns the index information on the collection. - * - * @return index information on the collection - */ - List getIndexInfo(); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/IndexOperationsAdapter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/IndexOperationsAdapter.java index e941f39cf..5811da826 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/IndexOperationsAdapter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/IndexOperationsAdapter.java @@ -20,6 +20,7 @@ import java.util.List; import org.springframework.data.mongodb.core.index.IndexDefinition; import org.springframework.data.mongodb.core.index.IndexInfo; +import org.springframework.data.mongodb.core.index.IndexOperations; import org.springframework.util.Assert; /** diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/IndexOperationsProvider.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/IndexOperationsProvider.java index 716f4a228..b926b1ca7 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/IndexOperationsProvider.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/IndexOperationsProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * 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. @@ -13,22 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.mongodb.core; -import org.springframework.data.mongodb.core.convert.QueryMapper; - /** - * TODO: Revisit for a better pattern. * @author Mark Paluch + * @author Jens Schauder * @since 2.0 + * + * @deprecated Use {@link org.springframework.data.mongodb.core.index.IndexOperationsProvider} instead. */ -public interface IndexOperationsProvider { +@Deprecated +public interface IndexOperationsProvider extends org.springframework.data.mongodb.core.index.IndexOperationsProvider { - /** - * Returns the operations that can be performed on indexes - * - * @return index operations on the named collection - */ - IndexOperations indexOps(String collectionName); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java index 9fbc1bed2..e7500d8ea 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java @@ -27,6 +27,7 @@ import org.springframework.data.mongodb.core.aggregation.AggregationOptions; import org.springframework.data.mongodb.core.aggregation.AggregationResults; import org.springframework.data.mongodb.core.aggregation.TypedAggregation; import org.springframework.data.mongodb.core.convert.MongoConverter; +import org.springframework.data.mongodb.core.index.IndexOperations; import org.springframework.data.mongodb.core.mapreduce.GroupBy; import org.springframework.data.mongodb.core.mapreduce.GroupByResults; import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java index 3b42a293a..d1e34e095 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java @@ -84,6 +84,8 @@ import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.data.mongodb.core.convert.MongoWriter; import org.springframework.data.mongodb.core.convert.QueryMapper; import org.springframework.data.mongodb.core.convert.UpdateMapper; +import org.springframework.data.mongodb.core.index.IndexOperations; +import org.springframework.data.mongodb.core.index.IndexOperationsProvider; import org.springframework.data.mongodb.core.index.MongoMappingEventPublisher; import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator; import org.springframework.data.mongodb.core.mapping.MongoMappingContext; @@ -102,6 +104,7 @@ import org.springframework.data.mongodb.core.mapreduce.GroupBy; import org.springframework.data.mongodb.core.mapreduce.GroupByResults; import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions; import org.springframework.data.mongodb.core.mapreduce.MapReduceResults; +import org.springframework.data.mongodb.core.query.Collation; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Meta; import org.springframework.data.mongodb.core.query.NearQuery; @@ -612,8 +615,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware, Document mappedQuery = queryMapper.getMappedObject(query.getQueryObject(), getPersistentEntity(entityClass)); - return execute(collectionName, new ExistsCallback(mappedQuery, - query.getCollation().map(org.springframework.data.mongodb.core.Collation::toMongoCollation).orElse(null))); + return execute(collectionName, + new ExistsCallback(mappedQuery, query.getCollation().map(Collation::toMongoCollation).orElse(null))); } // Find methods that take a Query to express the query and that return a List of objects. diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java index b279bfe19..9ed9ae3f0 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java @@ -56,6 +56,7 @@ import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.model.ConvertingPropertyAccessor; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory; +import org.springframework.data.mongodb.core.query.Collation; import org.springframework.data.mongodb.core.convert.DbRefProxyHandler; import org.springframework.data.mongodb.core.convert.DbRefResolver; import org.springframework.data.mongodb.core.convert.DbRefResolverCallback; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOptions.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOptions.java index d146a38cb..d88f3f5c3 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOptions.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOptions.java @@ -18,7 +18,7 @@ package org.springframework.data.mongodb.core.aggregation; import java.util.Optional; import org.bson.Document; -import org.springframework.data.mongodb.core.Collation; +import org.springframework.data.mongodb.core.query.Collation; import org.springframework.util.Assert; import com.mongodb.DBObject; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoExampleMapper.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoExampleMapper.java index e8bf01968..be945963e 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoExampleMapper.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoExampleMapper.java @@ -30,8 +30,7 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.core.query.MongoRegexCreator; import org.springframework.data.mongodb.core.query.SerializationUtils; -import org.springframework.data.repository.core.support.ExampleMatcherAccessor; -import org.springframework.data.repository.query.parser.Part.Type; +import org.springframework.data.support.ExampleMatcherAccessor; import org.springframework.data.util.TypeInformation; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; @@ -40,24 +39,18 @@ import org.springframework.util.StringUtils; /** * @author Christoph Strobl * @author Mark Paluch + * @author Jens Schauder * @since 1.8 */ public class MongoExampleMapper { private final MappingContext, MongoPersistentProperty> mappingContext; private final MongoConverter converter; - private final Map stringMatcherPartMapping = new HashMap(); public MongoExampleMapper(MongoConverter converter) { this.converter = converter; this.mappingContext = converter.getMappingContext(); - - stringMatcherPartMapping.put(StringMatcher.EXACT, Type.SIMPLE_PROPERTY); - stringMatcherPartMapping.put(StringMatcher.CONTAINING, Type.CONTAINING); - stringMatcherPartMapping.put(StringMatcher.STARTING, Type.STARTING_WITH); - stringMatcherPartMapping.put(StringMatcher.ENDING, Type.ENDING_WITH); - stringMatcherPartMapping.put(StringMatcher.REGEX, Type.REGEX); } /** @@ -201,7 +194,7 @@ public class MongoExampleMapper { continue; } - StringMatcher stringMatcher = exampleSpecAccessor.getDefaultStringMatcher(); + org.springframework.data.util.StringMatcher stringMatcher = exampleSpecAccessor.getDefaultStringMatcher().toNewStringMatcher(); Object value = entry.getValue(); boolean ignoreCase = exampleSpecAccessor.isIgnoreCaseEnabled(); @@ -210,7 +203,7 @@ public class MongoExampleMapper { mappedPropertyPath = exampleSpecAccessor.hasPropertySpecifier(propertyPath) ? propertyPath : getMappedPropertyPath(propertyPath, probeType); - stringMatcher = exampleSpecAccessor.getStringMatcherForPath(mappedPropertyPath); + stringMatcher = exampleSpecAccessor.getStringMatcherForPath(mappedPropertyPath).toNewStringMatcher(); ignoreCase = exampleSpecAccessor.isIgnoreCaseForPath(mappedPropertyPath); } @@ -239,11 +232,12 @@ public class MongoExampleMapper { return entry.getKey().equals("_id") && entry.getValue() == null || entry.getValue().equals(Optional.empty()); } - private void applyStringMatcher(Map.Entry entry, StringMatcher stringMatcher, boolean ignoreCase) { + private void applyStringMatcher(Map.Entry entry, + org.springframework.data.util.StringMatcher stringMatcher, boolean ignoreCase) { Document document = new Document(); - if (ObjectUtils.nullSafeEquals(StringMatcher.DEFAULT, stringMatcher)) { + if (org.springframework.data.util.StringMatcher.DEFAULT == stringMatcher) { if (ignoreCase) { document.put("$regex", Pattern.quote((String) entry.getValue())); @@ -251,8 +245,7 @@ public class MongoExampleMapper { } } else { - Type type = stringMatcherPartMapping.get(stringMatcher); - String expression = MongoRegexCreator.INSTANCE.toRegularExpression((String) entry.getValue(), type); + String expression = MongoRegexCreator.INSTANCE.toRegularExpression((String) entry.getValue(), stringMatcher); document.put("$regex", expression); entry.setValue(document); } @@ -261,4 +254,8 @@ public class MongoExampleMapper { document.put("$options", "i"); } } + + private org.springframework.data.util.StringMatcher convert(StringMatcher stringMatcher) { + return org.springframework.data.util.StringMatcher.valueOf(stringMatcher.name()); + } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/GeospatialIndex.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/GeospatialIndex.java index 4c34fae33..b4bfea841 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/GeospatialIndex.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/GeospatialIndex.java @@ -18,7 +18,7 @@ package org.springframework.data.mongodb.core.index; import java.util.Optional; import org.bson.Document; -import org.springframework.data.mongodb.core.Collation; +import org.springframework.data.mongodb.core.query.Collation; import org.springframework.util.Assert; import org.springframework.util.StringUtils; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java index 7805c0f18..7ec294372 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java @@ -23,7 +23,7 @@ import java.util.concurrent.TimeUnit; import org.bson.Document; import org.springframework.data.domain.Sort.Direction; -import org.springframework.data.mongodb.core.Collation; +import org.springframework.data.mongodb.core.query.Collation; import org.springframework.util.Assert; import org.springframework.util.StringUtils; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexOperations.java new file mode 100644 index 000000000..b1efa641d --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexOperations.java @@ -0,0 +1,56 @@ +/* + * 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. + * 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.index; + +import java.util.List; + +/** + * Index operations on a collection. + * + * @author Mark Pollack + * @author Oliver Gierke + * @author Christoph Strobl + * @author Jens Schauder + */ +public interface IndexOperations { + + /** + * Ensure that an index for the provided {@link IndexDefinition} exists for the collection indicated by the entity + * class. If not it will be created. + * + * @param indexDefinition must not be {@literal null}. + */ + String ensureIndex(IndexDefinition indexDefinition); + + /** + * Drops an index from this collection. + * + * @param name name of index to drop + */ + void dropIndex(String name); + + /** + * Drops all indices from this collection. + */ + void dropAllIndexes(); + + /** + * Returns the index information on the collection. + * + * @return index information on the collection + */ + List getIndexInfo(); +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexOperationsProvider.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexOperationsProvider.java new file mode 100644 index 000000000..09a582d52 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexOperationsProvider.java @@ -0,0 +1,34 @@ +/* + * 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. + * 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.index; + +/** + * TODO: Revisit for a better pattern. + * + * @author Mark Paluch + * @author Jens Schauder + * @since 2.0 + */ +public interface IndexOperationsProvider { + + /** + * Returns the operations that can be performed on indexes + * + * @return index operations on the named collection + */ + IndexOperations indexOps(String collectionName); +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java index ad4ae4d04..1056d19cf 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java @@ -28,8 +28,6 @@ import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.context.MappingContextEvent; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.UncategorizedMongoDbException; -import org.springframework.data.mongodb.core.IndexOperations; -import org.springframework.data.mongodb.core.IndexOperationsProvider; import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.IndexDefinitionHolder; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.MongoMappingContext; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/GroupBy.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/GroupBy.java index 3bc139f9f..3348bdf9e 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/GroupBy.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/GroupBy.java @@ -18,7 +18,7 @@ package org.springframework.data.mongodb.core.mapreduce; import java.util.Optional; import org.bson.Document; -import org.springframework.data.mongodb.core.Collation; +import org.springframework.data.mongodb.core.query.Collation; /** * Collects the parameters required to perform a group operation on a collection. The query condition and the input diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceOptions.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceOptions.java index 2bc76ccb4..e8909a6ad 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceOptions.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceOptions.java @@ -20,7 +20,7 @@ import java.util.Map; import java.util.Optional; import org.bson.Document; -import org.springframework.data.mongodb.core.Collation; +import org.springframework.data.mongodb.core.query.Collation; import com.mongodb.MapReduceCommand; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/Collation.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Collation.java similarity index 99% rename from spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/Collation.java rename to spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Collation.java index a0b7d9e51..32cb3d9ac 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/Collation.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Collation.java @@ -13,12 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.data.mongodb.core; - -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.RequiredArgsConstructor; +package org.springframework.data.mongodb.core.query; import java.util.Locale; import java.util.Optional; @@ -34,18 +29,24 @@ import com.mongodb.client.model.CollationCaseFirst; import com.mongodb.client.model.CollationMaxVariable; import com.mongodb.client.model.CollationStrength; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + /** * Central abstraction for MongoDB collation support.
* Allows fluent creation of a collation {@link Document} that can be used for creating collections & indexes as well as * querying data. - *

+ *

* NOTE: Please keep in mind that queries will only make use of an index with collation settings if the * query itself specifies the same collation. * * @author Christoph Strobl * @author Mark Paluch - * @since 2.0 + * @author Jens Schauder * @see MongoDB Reference - Collation + * @since 2.0 */ public class Collation { @@ -720,8 +721,8 @@ public class Collation { /** * ICU locale abstraction for usage with MongoDB {@link Collation}. * - * @since 2.0 * @see ICU - International Components for Unicode + * @since 2.0 */ @RequiredArgsConstructor(access = AccessLevel.PRIVATE) public static class CollationLocale { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/MongoRegexCreator.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/MongoRegexCreator.java index f3298a8af..b87d21231 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/MongoRegexCreator.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/MongoRegexCreator.java @@ -18,11 +18,12 @@ package org.springframework.data.mongodb.core.query; import java.util.regex.Pattern; import org.springframework.data.repository.query.parser.Part.Type; -import org.springframework.util.ObjectUtils; +import org.springframework.data.util.StringMatcher; /** * @author Christoph Strobl * @author Mark Paluch + * @author Jens Schauder * @since 1.8 */ public enum MongoRegexCreator { @@ -37,28 +38,40 @@ public enum MongoRegexCreator { * @param source the plain String * @param type * @return {@literal source} when {@literal source} or {@literal type} is {@literal null}. + * @deprecated use the {@link MongoRegexCreator#toRegularExpression(String, StringMatcher)} instead */ + @Deprecated public String toRegularExpression(String source, Type type) { - if (type == null || source == null) { + return toRegularExpression(source, convert(type)); + } + + /** + * Creates a regular expression String to be used with {@code $regex}. + * + * @param source the plain String + * @param matcherType the type of matching to perform + * @return {@literal source} when {@literal source} or {@literal matcherType} is {@literal null}. + */ + public String toRegularExpression(String source, StringMatcher matcherType) { + + if (matcherType == null || source == null) { return source; } - String regex = prepareAndEscapeStringBeforeApplyingLikeRegex(source, type); + String regex = prepareAndEscapeStringBeforeApplyingLikeRegex(source, matcherType); - switch (type) { - case STARTING_WITH: + switch (matcherType) { + case STARTING: regex = "^" + regex; break; - case ENDING_WITH: + case ENDING: regex = regex + "$"; break; case CONTAINING: - case NOT_CONTAINING: regex = ".*" + regex + ".*"; break; - case SIMPLE_PROPERTY: - case NEGATING_SIMPLE_PROPERTY: + case EXACT: regex = "^" + regex + "$"; default: } @@ -66,13 +79,13 @@ public enum MongoRegexCreator { return regex; } - private String prepareAndEscapeStringBeforeApplyingLikeRegex(String source, Type type) { + private String prepareAndEscapeStringBeforeApplyingLikeRegex(String source, StringMatcher matcherType) { - if (ObjectUtils.nullSafeEquals(Type.REGEX, type)) { + if (StringMatcher.REGEX == matcherType) { return source; } - if (!ObjectUtils.nullSafeEquals(Type.LIKE, type) && !ObjectUtils.nullSafeEquals(Type.NOT_LIKE, type)) { + if (StringMatcher.LIKE != matcherType) { return PUNCTATION_PATTERN.matcher(source).find() ? Pattern.quote(source) : source; } @@ -103,4 +116,49 @@ public enum MongoRegexCreator { return sb.toString(); } + private StringMatcher convert(Type type) { + + if (type == null) + return null; + + switch (type) { + + case NOT_LIKE: + case LIKE: + return StringMatcher.LIKE; + case STARTING_WITH: + return StringMatcher.STARTING; + case ENDING_WITH: + return StringMatcher.ENDING; + case NOT_CONTAINING: + case CONTAINING: + return StringMatcher.CONTAINING; + case REGEX: + return StringMatcher.REGEX; + case SIMPLE_PROPERTY: + case NEGATING_SIMPLE_PROPERTY: + return StringMatcher.EXACT; + case BETWEEN: + case IS_NOT_NULL: + case IS_NULL: + case LESS_THAN: + case LESS_THAN_EQUAL: + case GREATER_THAN: + case GREATER_THAN_EQUAL: + case BEFORE: + case AFTER: + case EXISTS: + case TRUE: + case FALSE: + case NOT_IN: + case IN: + case NEAR: + case WITHIN: + case IS_NOT_EMPTY: + case IS_EMPTY: + return StringMatcher.DEFAULT; + } + throw new IllegalStateException("Execution should never reach this position."); + } + } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java index 25e9dffeb..c52e0c0bd 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java @@ -34,7 +34,6 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Order; import org.springframework.data.mongodb.InvalidMongoDbApiUsageException; -import org.springframework.data.mongodb.core.Collation; import org.springframework.util.Assert; /** diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/IndexEnsuringQueryCreationListener.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/IndexEnsuringQueryCreationListener.java index 8259141e7..84421d437 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/IndexEnsuringQueryCreationListener.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/IndexEnsuringQueryCreationListener.java @@ -24,7 +24,7 @@ import org.slf4j.LoggerFactory; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Order; import org.springframework.data.domain.Sort.Direction; -import org.springframework.data.mongodb.core.IndexOperationsProvider; +import org.springframework.data.mongodb.core.index.IndexOperationsProvider; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.index.Index; import org.springframework.data.mongodb.repository.query.MongoEntityMetadata; diff --git a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensions.kt b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensions.kt index 7956df812..0018ed2b4 100644 --- a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensions.kt +++ b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensions.kt @@ -23,6 +23,7 @@ import org.springframework.data.geo.GeoResults import org.springframework.data.mongodb.core.BulkOperations.BulkMode import org.springframework.data.mongodb.core.aggregation.Aggregation import org.springframework.data.mongodb.core.aggregation.AggregationResults +import org.springframework.data.mongodb.core.index.IndexOperations import org.springframework.data.mongodb.core.mapreduce.GroupBy import org.springframework.data.mongodb.core.mapreduce.GroupByResults import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions diff --git a/spring-data-mongodb/src/main/resources/META-INF/spring.factories b/spring-data-mongodb/src/main/resources/META-INF/spring.factories index a581f2e3a..ab6539517 100644 --- a/spring-data-mongodb/src/main/resources/META-INF/spring.factories +++ b/spring-data-mongodb/src/main/resources/META-INF/spring.factories @@ -1,2 +1,2 @@ -org.springframework.data.web.config.SpringDataJacksonModules=org.springframework.data.mongodb.core.GeoJsonConfiguration +org.springframework.data.web.config.SpringDataJacksonModules=org.springframework.data.mongodb.config.GeoJsonConfiguration org.springframework.data.repository.core.support.RepositoryFactorySupport=org.springframework.data.mongodb.repository.support.MongoRepositoryFactory diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/DependencyTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/DependencyTests.java new file mode 100644 index 000000000..a548e87b1 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/DependencyTests.java @@ -0,0 +1,90 @@ +/* + * 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; + +import static de.schauderhaft.degraph.check.JCheck.*; +import static org.junit.Assert.*; + +import org.junit.Test; +import org.springframework.data.mongodb.core.GeoJsonConfiguration; +import org.springframework.data.mongodb.core.query.MongoRegexCreator; + +import de.schauderhaft.degraph.configuration.NamedPattern; + +/** + * Tests package dependency constraints. + * + * @author Jens Schauder + */ +public class DependencyTests { + + @Test + public void noInternalPackageCycles() { + + assertThat( + classpath() // + .noJars() // + .including("org.springframework.data.mongodb.**") // + // ignoring deprecated class that will be removed soon + .excluding(org.springframework.data.mongodb.core.IndexOperations.class.getCanonicalName()) + .excluding(org.springframework.data.mongodb.core.IndexOperationsProvider.class.getCanonicalName()) + .excluding(GeoJsonConfiguration.class.getCanonicalName()) + .filterClasspath("*target/classes") // + .printOnFailure("degraph.graphml"), // + violationFree() // + ); + } + + @Test + public void onlyConfigMayUseRepository() { + + assertThat( + classpath() // + .including("org.springframework.data.**") // + // ignoring the MongoRegexCreator for now, since it still + // needs the reference to Part.Type to maintain the old API + .excluding(MongoRegexCreator.class.getCanonicalName() + "*") + // ignoring deprecated class that will be removed soon + .excluding(org.springframework.data.mongodb.core.IndexOperations.class.getCanonicalName()) + .excluding(org.springframework.data.mongodb.core.IndexOperationsProvider.class.getCanonicalName()) + .excluding(GeoJsonConfiguration.class.getCanonicalName()) + .filterClasspath("*target/classes") // + .printOnFailure("onlyConfigMayUseRepository.graphml") // + .withSlicing("slices", // + "**.(config).**", // + new NamedPattern("**.cdi.**", "config"), // + "**.(repository).**", // + new NamedPattern("**", "other")) + .allow("config", "repository", "other"), // + violationFree() // + ); + } + + @Test + public void commonsInternaly() { + + assertThat( + classpath() // + .noJars() // + .including("org.springframework.data.**") // + .excluding("org.springframework.data.mongodb.**") // + .filterClasspath("*target/classes") // + .printTo("commons.graphml"), // + violationFree() // + ); + } + +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/GeoJsonConfigurationIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/GeoJsonConfigurationIntegrationTests.java index bed30fc8b..6b92dc897 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/GeoJsonConfigurationIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/GeoJsonConfigurationIntegrationTests.java @@ -22,7 +22,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; -import org.springframework.data.mongodb.core.GeoJsonConfiguration; import org.springframework.data.mongodb.core.geo.GeoJsonModule; import org.springframework.data.web.config.EnableSpringDataWebSupport; import org.springframework.test.context.ContextConfiguration; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/CollationUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/CollationUnitTests.java index 933409278..a525c6d85 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/CollationUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/CollationUnitTests.java @@ -21,10 +21,11 @@ import java.util.Locale; import org.bson.Document; import org.junit.Test; -import org.springframework.data.mongodb.core.Collation.Alternate; -import org.springframework.data.mongodb.core.Collation.CaseFirst; -import org.springframework.data.mongodb.core.Collation.CollationLocale; -import org.springframework.data.mongodb.core.Collation.ComparisonLevel; +import org.springframework.data.mongodb.core.query.Collation; +import org.springframework.data.mongodb.core.query.Collation.Alternate; +import org.springframework.data.mongodb.core.query.Collation.CaseFirst; +import org.springframework.data.mongodb.core.query.Collation.CollationLocale; +import org.springframework.data.mongodb.core.query.Collation.ComparisonLevel; /** * @author Christoph Strobl diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultBulkOperationsUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultBulkOperationsUnitTests.java index 755334d2d..55bb7aed9 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultBulkOperationsUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultBulkOperationsUnitTests.java @@ -44,6 +44,7 @@ import org.springframework.data.mongodb.core.convert.UpdateMapper; import org.springframework.data.mongodb.core.mapping.Field; import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.data.mongodb.core.query.BasicQuery; +import org.springframework.data.mongodb.core.query.Collation; import org.springframework.data.mongodb.core.query.Update; import com.mongodb.client.MongoCollection; 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 86f923c2f..f134d26e3 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 @@ -27,7 +27,8 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort.Direction; -import org.springframework.data.mongodb.core.Collation.CaseFirst; +import org.springframework.data.mongodb.core.query.Collation; +import org.springframework.data.mongodb.core.query.Collation.CaseFirst; import org.springframework.data.mongodb.core.convert.QueryMapper; import org.springframework.data.mongodb.core.index.Index; import org.springframework.data.mongodb.core.index.IndexDefinition; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultReactiveIndexOperationsTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultReactiveIndexOperationsTests.java index 91fb17588..1dbff6cf4 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultReactiveIndexOperationsTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultReactiveIndexOperationsTests.java @@ -29,7 +29,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration; -import org.springframework.data.mongodb.core.Collation.CaseFirst; +import org.springframework.data.mongodb.core.query.Collation; +import org.springframework.data.mongodb.core.query.Collation.CaseFirst; import org.springframework.data.mongodb.core.DefaultIndexOperationsIntegrationTests.DefaultIndexOperationsIntegrationTestsSample; import org.springframework.data.mongodb.core.index.Index; import org.springframework.data.mongodb.core.index.IndexDefinition; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateCollationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateCollationTests.java index 0bbfedfca..3d21409cb 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateCollationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateCollationTests.java @@ -28,8 +28,9 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.config.AbstractMongoConfiguration; -import org.springframework.data.mongodb.core.Collation.Alternate; -import org.springframework.data.mongodb.core.Collation.ComparisonLevel; +import org.springframework.data.mongodb.core.query.Collation; +import org.springframework.data.mongodb.core.query.Collation.Alternate; +import org.springframework.data.mongodb.core.query.Collation.ComparisonLevel; import org.springframework.data.mongodb.test.util.MongoVersionRule; import org.springframework.data.util.Version; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java index b9566795f..f4de63154 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java @@ -55,6 +55,7 @@ import org.springframework.data.domain.Sort; import org.springframework.data.geo.Point; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.aggregation.Aggregation; +import org.springframework.data.mongodb.core.query.Collation; import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver; import org.springframework.data.mongodb.core.convert.MappingMongoConverter; import org.springframework.data.mongodb.core.convert.MongoCustomConversions; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/QueryCursorPreparerUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/QueryCursorPreparerUnitTests.java index a6138f256..9ed56b160 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/QueryCursorPreparerUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/QueryCursorPreparerUnitTests.java @@ -32,6 +32,7 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoTemplate.QueryCursorPreparer; +import org.springframework.data.mongodb.core.query.Collation; import org.springframework.data.mongodb.core.query.BasicQuery; import org.springframework.data.mongodb.core.query.Meta; import org.springframework.data.mongodb.core.query.Query; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateCollationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateCollationTests.java index 5a737e343..2e193e949 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateCollationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateCollationTests.java @@ -31,6 +31,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration; +import org.springframework.data.mongodb.core.query.Collation; import org.springframework.data.mongodb.test.util.MongoVersionRule; import org.springframework.data.util.Version; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java index 67da4a807..7c02ec988 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java @@ -36,6 +36,7 @@ import org.reactivestreams.Publisher; import org.springframework.data.mongodb.core.MongoTemplateUnitTests.AutogenerateableId; import org.springframework.data.mongodb.core.ReactiveMongoTemplate.NoOpDbRefResolver; import org.springframework.data.mongodb.core.aggregation.Aggregation; +import org.springframework.data.mongodb.core.query.Collation; import org.springframework.data.mongodb.core.convert.MappingMongoConverter; import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.data.mongodb.core.query.BasicQuery; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoSpatial2DSphereTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoSpatial2DSphereTests.java index a069b183c..9bf740a88 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoSpatial2DSphereTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoSpatial2DSphereTests.java @@ -29,7 +29,7 @@ import org.springframework.data.geo.GeoResults; import org.springframework.data.geo.Metric; import org.springframework.data.geo.Metrics; import org.springframework.data.geo.Point; -import org.springframework.data.mongodb.core.IndexOperations; +import org.springframework.data.mongodb.core.index.IndexOperations; import org.springframework.data.mongodb.core.Venue; import org.springframework.data.mongodb.core.index.GeoSpatialIndexType; import org.springframework.data.mongodb.core.index.GeospatialIndex; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoSpatial2DTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoSpatial2DTests.java index 896871b34..a55f850ce 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoSpatial2DTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoSpatial2DTests.java @@ -26,7 +26,7 @@ import java.util.List; import org.junit.Test; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.geo.Point; -import org.springframework.data.mongodb.core.IndexOperations; +import org.springframework.data.mongodb.core.index.IndexOperations; import org.springframework.data.mongodb.core.Venue; import org.springframework.data.mongodb.core.index.GeoSpatialIndexType; import org.springframework.data.mongodb.core.index.GeospatialIndex; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoSpatialIndexTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoSpatialIndexTests.java index 337a5b0f7..8eb242604 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoSpatialIndexTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoSpatialIndexTests.java @@ -29,7 +29,7 @@ import org.springframework.dao.DataAccessException; import org.springframework.data.geo.Point; import org.springframework.data.mongodb.config.AbstractIntegrationTests; import org.springframework.data.mongodb.core.CollectionCallback; -import org.springframework.data.mongodb.core.IndexOperations; +import org.springframework.data.mongodb.core.index.IndexOperations; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.WriteResultChecking; import org.springframework.data.mongodb.core.index.GeoSpatialIndexType; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/TextIndexTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/TextIndexTests.java index 4123a2bdd..1eb2b5dba 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/TextIndexTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/TextIndexTests.java @@ -26,7 +26,6 @@ import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.mongodb.config.AbstractIntegrationTests; -import org.springframework.data.mongodb.core.IndexOperations; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Language; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/MongoRegexCreatorUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/MongoRegexCreatorUnitTests.java new file mode 100644 index 000000000..a902af233 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/MongoRegexCreatorUnitTests.java @@ -0,0 +1,116 @@ +package org.springframework.data.mongodb.core.query; + +import static java.util.Arrays.*; +import static org.springframework.data.mongodb.core.query.MongoRegexCreatorUnitTests.TestParameter.*; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.assertj.core.api.SoftAssertions; +import org.assertj.core.api.StringAssert; +import org.junit.Test; +import org.springframework.data.repository.query.parser.Part.Type; + +/** + * Tests the creation of Regex's in {@link MongoRegexCreator} + * + * @author Jens Schauder + */ +public class MongoRegexCreatorUnitTests { + + List testParameters = asList(TestParameter.test("anystring", null, "anystring", "type=null -> input"), + test(null, Type.AFTER, null, "source=null -> null"), // + test("anystring", Type.REGEX, "anystring", "REGEX -> input"), // + test("one.two?three", Type.AFTER, "\\Qone.two?three\\E", + "not(REGEX, LIKE, NOT_LIKE, PunctuationPattern -> quoted punctuation"), // + test("*", Type.LIKE, ".*", "LIKE * -> .*"), test("*", Type.NOT_LIKE, ".*", "LIKE * -> .*"), // + test("*.*", Type.LIKE, ".*\\Q.\\E.*", "Wildcards & Punctuation"), // + test("*.", Type.LIKE, ".*\\Q.\\E", "Leading Wildcard & Punctuation"), // + test(".*", Type.LIKE, "\\Q.\\E.*", "Trailing Wildcard & Punctuation"), // + test("other", Type.LIKE, "other", "No Wildcard & Other"), // + test("other*", Type.LIKE, "other.*", "Trailing Wildcard & Other"), // + test("*other", Type.LIKE, ".*other", "Leading Wildcard & Other"), // + test("o*t.*h.er", Type.LIKE, "\\Qo*t.*h.er\\E", "Dots & Stars"), // + test("other", Type.STARTING_WITH, "^other", "Dots & Stars"), // + test("other", Type.ENDING_WITH, "other$", "Dots & Stars"), // + test("other", Type.CONTAINING, ".*other.*", "Dots & Stars"), // + test("other", Type.NOT_CONTAINING, ".*other.*", "Dots & Stars"), // + test("other", Type.SIMPLE_PROPERTY, "^other$", "Dots & Stars"), // + test("other", Type.NEGATING_SIMPLE_PROPERTY, "^other$", "Dots & Stars")); + + Map expectedResultsForAllTypes = new HashMap<>(); + { + expectedResultsForAllTypes.put(Type.BETWEEN, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.IS_NOT_NULL, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.IS_NULL, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.LESS_THAN, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.LESS_THAN_EQUAL, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.GREATER_THAN, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.GREATER_THAN_EQUAL, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.BEFORE, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.AFTER, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.NOT_LIKE, "\\Qo*t.*h.er\\E.*"); + expectedResultsForAllTypes.put(Type.LIKE, "\\Qo*t.*h.er\\E.*"); + expectedResultsForAllTypes.put(Type.STARTING_WITH, "^\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.ENDING_WITH, "\\Qo*t.*h.er*\\E$"); + expectedResultsForAllTypes.put(Type.IS_NOT_EMPTY, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.IS_EMPTY, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.NOT_CONTAINING, ".*\\Qo*t.*h.er*\\E.*"); + expectedResultsForAllTypes.put(Type.CONTAINING, ".*\\Qo*t.*h.er*\\E.*"); + expectedResultsForAllTypes.put(Type.NOT_IN, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.IN, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.NEAR, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.WITHIN, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.REGEX, "o*t.*h.er*"); + expectedResultsForAllTypes.put(Type.EXISTS, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.TRUE, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.FALSE, "\\Qo*t.*h.er*\\E"); + expectedResultsForAllTypes.put(Type.NEGATING_SIMPLE_PROPERTY, "^\\Qo*t.*h.er*\\E$"); + expectedResultsForAllTypes.put(Type.SIMPLE_PROPERTY, "^\\Qo*t.*h.er*\\E$"); + + } + + @Test + public void testSpecialCases() { + SoftAssertions.assertSoftly(sa -> testParameters.forEach(tp -> tp.check(sa))); + } + + @Test + public void testAllTypes() { + SoftAssertions.assertSoftly( + sa -> Arrays.stream(Type.values()).forEach(t -> // + test("o*t.*h.er*", t, expectedResultsForAllTypes.getOrDefault(t,"missed one"), t.toString())// + .check(sa))); + } + + static class TestParameter { + + TestParameter(String source, Type type, String expectedResult, String comment) { + this.source = source; + this.type = type; + this.expectedResult = expectedResult; + this.comment = comment; + } + + static TestParameter test(String source, Type type, String expectedResult, String comment) { + return new TestParameter(source, type, expectedResult, comment); + } + + private final String source; + private final Type type; + private final String expectedResult; + private final String comment; + + private StringAssert check(SoftAssertions sa) { + + return sa + .assertThat( // + MongoRegexCreator.INSTANCE.toRegularExpression(source, type)) // + .describedAs(comment) // + .isEqualTo(expectedResult); + } + } + +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/TextQueryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/TextQueryTests.java index 4a1d176c7..3289a86fa 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/TextQueryTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/TextQueryTests.java @@ -35,7 +35,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.annotation.Id; import org.springframework.data.domain.PageRequest; import org.springframework.data.mongodb.config.AbstractIntegrationTests; -import org.springframework.data.mongodb.core.IndexOperations; +import org.springframework.data.mongodb.core.index.IndexOperations; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.index.IndexDefinition; import org.springframework.data.mongodb.core.mapping.Field;