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
This commit is contained in:
Christoph Strobl
2023-10-12 10:35:43 +02:00
parent 5d2564588f
commit 5afb8ada85
7 changed files with 54 additions and 39 deletions

View File

@@ -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;
* execution.
*
* @author Mark Paluch
* @author Christoph Strobl
* @since 4.2
*/
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();
}

View File

@@ -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
*/
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
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
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
public Optional<ReadPreference> getReadPreference() {
return readPreference;
}
@Override
public Method getMethod() {
return method;
}
}
private static class ThreadBoundTargetSource implements TargetSource {

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
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;
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Christoph Strobl
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@@ -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);

View File

@@ -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 {
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);

View File

@@ -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 {
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 {
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();