From 242460df4cff749aed8cdf8c9b147f2d1e3fcdeb Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 31 Jan 2017 15:21:33 +0100 Subject: [PATCH] DATAES-329 - Remove references to Assert single-arg methods. Replace references to Assert single-arg methods with references to methods accepting the test object and message. Related ticket: SPR-15196. --- .../MappingElasticsearchConverter.java | 8 +- .../core/query/AbstractQuery.java | 7 +- .../AbstractElasticsearchRepository.java | 11 +- ...ticsearchEntityInformationCreatorImpl.java | 7 +- .../ElasticsearchRepositoryFactory.java | 234 +++++++++--------- .../ElasticsearchRepositoryFactoryBean.java | 7 +- 6 files changed, 147 insertions(+), 127 deletions(-) diff --git a/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java b/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java index 60cdc9412..bb0b1e780 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-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. @@ -31,8 +31,8 @@ import org.springframework.util.Assert; * * @author Rizwan Idrees * @author Mohsin Husen + * @author Mark Paluch */ - public class MappingElasticsearchConverter implements ElasticsearchConverter, ApplicationContextAware { private final MappingContext, ElasticsearchPersistentProperty> mappingContext; @@ -43,7 +43,9 @@ public class MappingElasticsearchConverter implements ElasticsearchConverter, Ap public MappingElasticsearchConverter( MappingContext, ElasticsearchPersistentProperty> mappingContext) { - Assert.notNull(mappingContext); + + Assert.notNull(mappingContext, "MappingContext must not be null!"); + this.mappingContext = mappingContext; this.conversionService = new DefaultConversionService(); } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/query/AbstractQuery.java b/src/main/java/org/springframework/data/elasticsearch/core/query/AbstractQuery.java index 2031902c7..030ce9b10 100755 --- a/src/main/java/org/springframework/data/elasticsearch/core/query/AbstractQuery.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/query/AbstractQuery.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2016 the original author or authors. + * Copyright 2013-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. @@ -29,6 +29,7 @@ import org.springframework.util.Assert; * * @author Rizwan Idrees * @author Mohsin Husen + * @author Mark Paluch */ abstract class AbstractQuery implements Query { @@ -55,7 +56,9 @@ abstract class AbstractQuery implements Query { @Override public final T setPageable(Pageable pageable) { - Assert.notNull(pageable); + + Assert.notNull(pageable, "Pageable must not be null!"); + this.pageable = pageable; return (T) this.addSort(pageable.getSort()); } diff --git a/src/main/java/org/springframework/data/elasticsearch/repository/support/AbstractElasticsearchRepository.java b/src/main/java/org/springframework/data/elasticsearch/repository/support/AbstractElasticsearchRepository.java index d6bede718..0b79f58db 100644 --- a/src/main/java/org/springframework/data/elasticsearch/repository/support/AbstractElasticsearchRepository.java +++ b/src/main/java/org/springframework/data/elasticsearch/repository/support/AbstractElasticsearchRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2016 the original author or authors. + * Copyright 2013-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. @@ -43,6 +43,7 @@ import org.springframework.util.Assert; * @author Mohsin Husen * @author Ryan Henszey * @author Kevin Leturc + * @author Mark Paluch */ public abstract class AbstractElasticsearchRepository implements ElasticsearchRepository { @@ -56,14 +57,18 @@ public abstract class AbstractElasticsearchRepository metadata, ElasticsearchOperations elasticsearchOperations) { this(elasticsearchOperations); - Assert.notNull(metadata); + + Assert.notNull(metadata, "ElasticsearchEntityInformation must not be null!"); + this.entityInformation = metadata; setEntityClass(this.entityInformation.getJavaType()); try { diff --git a/src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchEntityInformationCreatorImpl.java b/src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchEntityInformationCreatorImpl.java index 8426b2f2f..6be8669ff 100644 --- a/src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchEntityInformationCreatorImpl.java +++ b/src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchEntityInformationCreatorImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014 the original author or authors. + * Copyright 2013-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. @@ -28,6 +28,7 @@ import org.springframework.util.Assert; * @author Rizwan Idrees * @author Mohsin Husen * @author Oliver Gierke + * @author Mark Paluch */ public class ElasticsearchEntityInformationCreatorImpl implements ElasticsearchEntityInformationCreator { @@ -35,7 +36,9 @@ public class ElasticsearchEntityInformationCreatorImpl implements ElasticsearchE public ElasticsearchEntityInformationCreatorImpl( MappingContext, ElasticsearchPersistentProperty> mappingContext) { - Assert.notNull(mappingContext); + + Assert.notNull(mappingContext, "MappingContext must not be null!"); + this.mappingContext = mappingContext; } diff --git a/src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchRepositoryFactory.java b/src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchRepositoryFactory.java index 9e1263022..1bd915948 100644 --- a/src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchRepositoryFactory.java +++ b/src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchRepositoryFactory.java @@ -1,115 +1,119 @@ -/* - * Copyright 2013 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.elasticsearch.repository.support; - -import static org.springframework.data.querydsl.QueryDslUtils.*; - -import java.io.Serializable; -import java.lang.reflect.Method; - -import org.springframework.data.elasticsearch.core.ElasticsearchOperations; -import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; -import org.springframework.data.elasticsearch.repository.query.ElasticsearchPartQuery; -import org.springframework.data.elasticsearch.repository.query.ElasticsearchQueryMethod; -import org.springframework.data.elasticsearch.repository.query.ElasticsearchStringQuery; -import org.springframework.data.projection.ProjectionFactory; -import org.springframework.data.querydsl.QueryDslPredicateExecutor; -import org.springframework.data.repository.core.NamedQueries; -import org.springframework.data.repository.core.RepositoryInformation; -import org.springframework.data.repository.core.RepositoryMetadata; -import org.springframework.data.repository.core.support.RepositoryFactorySupport; -import org.springframework.data.repository.query.QueryLookupStrategy; -import org.springframework.data.repository.query.RepositoryQuery; -import org.springframework.util.Assert; - -/** - * Factory to create {@link ElasticsearchRepository} - * - * @author Rizwan Idrees - * @author Mohsin Husen - * @author Ryan Henszey - */ -public class ElasticsearchRepositoryFactory extends RepositoryFactorySupport { - - private final ElasticsearchOperations elasticsearchOperations; - private final ElasticsearchEntityInformationCreator entityInformationCreator; - - public ElasticsearchRepositoryFactory(ElasticsearchOperations elasticsearchOperations) { - Assert.notNull(elasticsearchOperations); - this.elasticsearchOperations = elasticsearchOperations; - this.entityInformationCreator = new ElasticsearchEntityInformationCreatorImpl(elasticsearchOperations - .getElasticsearchConverter().getMappingContext()); - } - - @Override - public ElasticsearchEntityInformation getEntityInformation(Class domainClass) { - return entityInformationCreator.getEntityInformation(domainClass); - } - - @Override - @SuppressWarnings({"rawtypes", "unchecked"}) - protected Object getTargetRepository(RepositoryInformation metadata) { - return getTargetRepositoryViaReflection(metadata,getEntityInformation(metadata.getDomainType()), elasticsearchOperations); - } - - @Override - protected Class getRepositoryBaseClass(RepositoryMetadata metadata) { - if (isQueryDslRepository(metadata.getRepositoryInterface())) { - throw new IllegalArgumentException("QueryDsl Support has not been implemented yet."); - } - if (Integer.class.isAssignableFrom(metadata.getIdType()) - || Long.class.isAssignableFrom(metadata.getIdType()) - || Double.class.isAssignableFrom(metadata.getIdType())) { - return NumberKeyedRepository.class; - } else if (metadata.getIdType() == String.class) { - return SimpleElasticsearchRepository.class; - } else { - throw new IllegalArgumentException("Unsuppored ID type " + metadata.getIdType()); - } - } - - private static boolean isQueryDslRepository(Class repositoryInterface) { - return QUERY_DSL_PRESENT && QueryDslPredicateExecutor.class.isAssignableFrom(repositoryInterface); - } - - @Override - protected QueryLookupStrategy getQueryLookupStrategy(QueryLookupStrategy.Key key) { - return new ElasticsearchQueryLookupStrategy(); - } - - private class ElasticsearchQueryLookupStrategy implements QueryLookupStrategy { - - /* - * (non-Javadoc) - * @see org.springframework.data.repository.query.QueryLookupStrategy#resolveQuery(java.lang.reflect.Method, org.springframework.data.repository.core.RepositoryMetadata, org.springframework.data.projection.ProjectionFactory, org.springframework.data.repository.core.NamedQueries) - */ - @Override - public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory, - NamedQueries namedQueries) { - - ElasticsearchQueryMethod queryMethod = new ElasticsearchQueryMethod(method, metadata, factory); - String namedQueryName = queryMethod.getNamedQueryName(); - - if (namedQueries.hasQuery(namedQueryName)) { - String namedQuery = namedQueries.getQuery(namedQueryName); - return new ElasticsearchStringQuery(queryMethod, elasticsearchOperations, namedQuery); - } else if (queryMethod.hasAnnotatedQuery()) { - return new ElasticsearchStringQuery(queryMethod, elasticsearchOperations, queryMethod.getAnnotatedQuery()); - } - return new ElasticsearchPartQuery(queryMethod, elasticsearchOperations); - } - } -} +/* + * Copyright 2013-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.elasticsearch.repository.support; + +import static org.springframework.data.querydsl.QueryDslUtils.*; + +import java.io.Serializable; +import java.lang.reflect.Method; + +import org.springframework.data.elasticsearch.core.ElasticsearchOperations; +import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; +import org.springframework.data.elasticsearch.repository.query.ElasticsearchPartQuery; +import org.springframework.data.elasticsearch.repository.query.ElasticsearchQueryMethod; +import org.springframework.data.elasticsearch.repository.query.ElasticsearchStringQuery; +import org.springframework.data.projection.ProjectionFactory; +import org.springframework.data.querydsl.QueryDslPredicateExecutor; +import org.springframework.data.repository.core.NamedQueries; +import org.springframework.data.repository.core.RepositoryInformation; +import org.springframework.data.repository.core.RepositoryMetadata; +import org.springframework.data.repository.core.support.RepositoryFactorySupport; +import org.springframework.data.repository.query.QueryLookupStrategy; +import org.springframework.data.repository.query.RepositoryQuery; +import org.springframework.util.Assert; + +/** + * Factory to create {@link ElasticsearchRepository} + * + * @author Rizwan Idrees + * @author Mohsin Husen + * @author Ryan Henszey + * @author Gad Akuka + * @author Mark Paluch + */ +public class ElasticsearchRepositoryFactory extends RepositoryFactorySupport { + + private final ElasticsearchOperations elasticsearchOperations; + private final ElasticsearchEntityInformationCreator entityInformationCreator; + + public ElasticsearchRepositoryFactory(ElasticsearchOperations elasticsearchOperations) { + + Assert.notNull(elasticsearchOperations, "ElasticsearchOperations must not be null!"); + + this.elasticsearchOperations = elasticsearchOperations; + this.entityInformationCreator = new ElasticsearchEntityInformationCreatorImpl(elasticsearchOperations + .getElasticsearchConverter().getMappingContext()); + } + + @Override + public ElasticsearchEntityInformation getEntityInformation(Class domainClass) { + return entityInformationCreator.getEntityInformation(domainClass); + } + + @Override + @SuppressWarnings({"rawtypes", "unchecked"}) + protected Object getTargetRepository(RepositoryInformation metadata) { + return getTargetRepositoryViaReflection(metadata,getEntityInformation(metadata.getDomainType()), elasticsearchOperations); + } + + @Override + protected Class getRepositoryBaseClass(RepositoryMetadata metadata) { + if (isQueryDslRepository(metadata.getRepositoryInterface())) { + throw new IllegalArgumentException("QueryDsl Support has not been implemented yet."); + } + if (Integer.class.isAssignableFrom(metadata.getIdType()) + || Long.class.isAssignableFrom(metadata.getIdType()) + || Double.class.isAssignableFrom(metadata.getIdType())) { + return NumberKeyedRepository.class; + } else if (metadata.getIdType() == String.class) { + return SimpleElasticsearchRepository.class; + } else { + throw new IllegalArgumentException("Unsuppored ID type " + metadata.getIdType()); + } + } + + private static boolean isQueryDslRepository(Class repositoryInterface) { + return QUERY_DSL_PRESENT && QueryDslPredicateExecutor.class.isAssignableFrom(repositoryInterface); + } + + @Override + protected QueryLookupStrategy getQueryLookupStrategy(QueryLookupStrategy.Key key) { + return new ElasticsearchQueryLookupStrategy(); + } + + private class ElasticsearchQueryLookupStrategy implements QueryLookupStrategy { + + /* + * (non-Javadoc) + * @see org.springframework.data.repository.query.QueryLookupStrategy#resolveQuery(java.lang.reflect.Method, org.springframework.data.repository.core.RepositoryMetadata, org.springframework.data.projection.ProjectionFactory, org.springframework.data.repository.core.NamedQueries) + */ + @Override + public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory, + NamedQueries namedQueries) { + + ElasticsearchQueryMethod queryMethod = new ElasticsearchQueryMethod(method, metadata, factory); + String namedQueryName = queryMethod.getNamedQueryName(); + + if (namedQueries.hasQuery(namedQueryName)) { + String namedQuery = namedQueries.getQuery(namedQueryName); + return new ElasticsearchStringQuery(queryMethod, elasticsearchOperations, namedQuery); + } else if (queryMethod.hasAnnotatedQuery()) { + return new ElasticsearchStringQuery(queryMethod, elasticsearchOperations, queryMethod.getAnnotatedQuery()); + } + return new ElasticsearchPartQuery(queryMethod, elasticsearchOperations); + } + } +} diff --git a/src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchRepositoryFactoryBean.java b/src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchRepositoryFactoryBean.java index fd07313e9..7ef89b870 100644 --- a/src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchRepositoryFactoryBean.java +++ b/src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchRepositoryFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-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. @@ -29,6 +29,7 @@ import org.springframework.util.Assert; * * @author Rizwan Idrees * @author Mohsin Husen + * @author Mark Paluch */ public class ElasticsearchRepositoryFactoryBean, S, ID extends Serializable> extends RepositoryFactoryBeanSupport { @@ -41,7 +42,9 @@ public class ElasticsearchRepositoryFactoryBean, S, * @param operations the operations to set */ public void setElasticsearchOperations(ElasticsearchOperations operations) { - Assert.notNull(operations); + + Assert.notNull(operations, "ElasticsearchOperations must not be null!"); + setMappingContext(operations.getElasticsearchConverter().getMappingContext()); this.operations = operations; }