Browse Source

Reduce public API surface.

Limit exposure of API methods to package visibility.
Also remove fields and methods not directly required to support current feature scope.
Finally add missing since tags and provide additional tests.

See: #2971
pull/4528/head
Christoph Strobl 2 years ago
parent
commit
5afb8ada85
No known key found for this signature in database
GPG Key ID: 8CC1AB53391458C8
  1. 8
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadata.java
  2. 23
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadataPostProcessor.java
  3. 3
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java
  4. 3
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java
  5. 19
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactoryUnitTests.java
  6. 22
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/ReactiveMongoRepositoryFactoryUnitTests.java
  7. 15
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepositoryUnitTests.java

8
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadata.java

@ -15,7 +15,6 @@ @@ -15,7 +15,6 @@
*/
package org.springframework.data.mongodb.repository.support;
import java.lang.reflect.Method;
import java.util.Optional;
import com.mongodb.ReadPreference;
@ -25,6 +24,7 @@ import com.mongodb.ReadPreference; @@ -25,6 +24,7 @@ import com.mongodb.ReadPreference;
* execution.
*
* @author Mark Paluch
* @author Christoph Strobl
* @since 4.2
*/
public interface CrudMethodMetadata {
@ -36,10 +36,4 @@ public interface CrudMethodMetadata { @@ -36,10 +36,4 @@ public interface CrudMethodMetadata {
*/
Optional<ReadPreference> getReadPreference();
/**
* Returns the {@link Method} that this metadata applies to.
*
* @return the {@link Method} that this metadata applies to.
*/
Method getMethod();
}

23
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadataPostProcessor.java

@ -45,6 +45,8 @@ import com.mongodb.ReadPreference; @@ -45,6 +45,8 @@ import com.mongodb.ReadPreference;
* information or query hints on them.
*
* @author Mark Paluch
* @author Christoph Strobl
* @since 4.2
*/
class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, BeanClassLoaderAware {
@ -103,13 +105,15 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B @@ -103,13 +105,15 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B
*/
static MethodInvocation currentInvocation() throws IllegalStateException {
MethodInvocation mi = currentInvocation.get();
MethodInvocation invocation = currentInvocation.get();
if (mi == null)
throw new IllegalStateException(
"No MethodInvocation found: Check that an AOP invocation is in progress, and that the "
+ "CrudMethodMetadataPopulatingMethodInterceptor is upfront in the interceptor chain.");
return mi;
if (invocation != null) {
return invocation;
}
throw new IllegalStateException(
"No MethodInvocation found: Check that an AOP invocation is in progress, and that the "
+ "CrudMethodMetadataPopulatingMethodInterceptor is upfront in the interceptor chain.");
}
@Override
@ -163,7 +167,6 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B @@ -163,7 +167,6 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B
static class DefaultCrudMethodMetadata implements CrudMethodMetadata {
private final Optional<ReadPreference> readPreference;
private final Method method;
/**
* Creates a new {@link DefaultCrudMethodMetadata} for the given {@link Method}.
@ -175,7 +178,6 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B @@ -175,7 +178,6 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B
Assert.notNull(method, "Method must not be null");
this.readPreference = findReadPreference(method);
this.method = method;
}
private Optional<ReadPreference> findReadPreference(Method method) {
@ -201,11 +203,6 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B @@ -201,11 +203,6 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B
public Optional<ReadPreference> getReadPreference() {
return readPreference;
}
@Override
public Method getMethod() {
return method;
}
}
private static class ThreadBoundTargetSource implements TargetSource {

3
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java

@ -357,8 +357,9 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> { @@ -357,8 +357,9 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
* applied to queries.
*
* @param crudMethodMetadata
* @since 4.2
*/
public void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) {
void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) {
this.crudMethodMetadata = crudMethodMetadata;
}

3
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java

@ -434,8 +434,9 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement @@ -434,8 +434,9 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement
* applied to queries.
*
* @param crudMethodMetadata
* @since 4.2
*/
public void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) {
void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) {
this.crudMethodMetadata = crudMethodMetadata;
}

19
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactoryUnitTests.java

@ -20,6 +20,7 @@ import static org.mockito.Mockito.*; @@ -20,6 +20,7 @@ import static org.mockito.Mockito.*;
import java.io.Serializable;
import java.util.Optional;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -41,6 +42,7 @@ import org.springframework.data.mongodb.core.query.Query; @@ -41,6 +42,7 @@ import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.Person;
import org.springframework.data.mongodb.repository.ReadPreference;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.repository.ListCrudRepository;
import org.springframework.data.repository.Repository;
/**
@ -48,6 +50,7 @@ import org.springframework.data.repository.Repository; @@ -48,6 +50,7 @@ import org.springframework.data.repository.Repository;
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Christoph Strobl
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@ -92,7 +95,21 @@ public class MongoRepositoryFactoryUnitTests { @@ -92,7 +95,21 @@ public class MongoRepositoryFactoryUnitTests {
assertThat(value.getReadPreference()).isEqualTo(com.mongodb.ReadPreference.secondary());
}
interface MyPersonRepository extends Repository<Person, Long> {
@Test // GH-2971
void ignoresCrudMethodMetadataOnNonAnnotatedMethods() {
MongoRepositoryFactory factory = new MongoRepositoryFactory(template);
MyPersonRepository repository = factory.getRepository(MyPersonRepository.class);
repository.findAllById(Set.of(42L));
ArgumentCaptor<Query> captor = ArgumentCaptor.forClass(Query.class);
verify(template).find(captor.capture(), eq(Person.class), eq("person"));
Query value = captor.getValue();
assertThat(value.getReadPreference()).isNull();
}
interface MyPersonRepository extends ListCrudRepository<Person, Long> {
@ReadPreference("secondary")
Optional<Person> findById(Long id);

22
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/ReactiveMongoRepositoryFactoryUnitTests.java

@ -18,6 +18,10 @@ package org.springframework.data.mongodb.repository.support; @@ -18,6 +18,10 @@ package org.springframework.data.mongodb.repository.support;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.Set;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.junit.jupiter.api.BeforeEach;
@ -72,7 +76,23 @@ public class ReactiveMongoRepositoryFactoryUnitTests { @@ -72,7 +76,23 @@ public class ReactiveMongoRepositoryFactoryUnitTests {
assertThat(value.getReadPreference()).isEqualTo(com.mongodb.ReadPreference.secondary());
}
interface MyPersonRepository extends Repository<Person, Long> {
@Test // GH-2971
void ignoresCrudMethodMetadataOnNonAnnotatedMethods() {
when(template.find(any(), any(), anyString())).thenReturn(Flux.empty());
ReactiveMongoRepositoryFactory factory = new ReactiveMongoRepositoryFactory(template);
MyPersonRepository repository = factory.getRepository(MyPersonRepository.class);
repository.findAllById(Set.of(42L));
ArgumentCaptor<Query> captor = ArgumentCaptor.forClass(Query.class);
verify(template).find(captor.capture(), eq(Person.class), eq("person"));
Query value = captor.getValue();
assertThat(value.getReadPreference()).isNull();
}
interface MyPersonRepository extends ReactiveCrudRepository<Person, Long> {
@ReadPreference("secondary")
Mono<Person> findById(Long id);

15
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepositoryUnitTests.java

@ -161,11 +161,6 @@ class SimpleReactiveMongoRepositoryUnitTests { @@ -161,11 +161,6 @@ class SimpleReactiveMongoRepositoryUnitTests {
public Optional<com.mongodb.ReadPreference> getReadPreference() {
return Optional.of(com.mongodb.ReadPreference.secondaryPreferred());
}
@Override
public Method getMethod() {
return null;
}
});
when(mongoOperations.find(any(), any(), any())).thenReturn(Flux.just("ok"));
@ -186,11 +181,6 @@ class SimpleReactiveMongoRepositoryUnitTests { @@ -186,11 +181,6 @@ class SimpleReactiveMongoRepositoryUnitTests {
public Optional<com.mongodb.ReadPreference> getReadPreference() {
return Optional.of(com.mongodb.ReadPreference.secondaryPreferred());
}
@Override
public Method getMethod() {
return null;
}
});
when(mongoOperations.find(any(), any(), any())).thenReturn(Flux.just("ok"));
@ -218,11 +208,6 @@ class SimpleReactiveMongoRepositoryUnitTests { @@ -218,11 +208,6 @@ class SimpleReactiveMongoRepositoryUnitTests {
public Optional<com.mongodb.ReadPreference> getReadPreference() {
return Optional.of(com.mongodb.ReadPreference.secondaryPreferred());
}
@Override
public Method getMethod() {
return null;
}
});
repository.findBy(Example.of(new TestDummy()), FluentQuery.ReactiveFluentQuery::all).subscribe();

Loading…
Cancel
Save