@ -15,9 +15,11 @@
@@ -15,9 +15,11 @@
* /
package org.springframework.data.repository.aot.generate ;
import static org.assertj.core.api.Assertions.* ;
import static org.mockito.ArgumentMatchers.* ;
import static org.mockito.Mockito.* ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.mockito.ArgumentMatchers.any ;
import static org.mockito.ArgumentMatchers.argThat ;
import static org.mockito.Mockito.mock ;
import static org.mockito.Mockito.when ;
import example.UserRepository ;
import example.UserRepositoryExtension ;
@ -33,6 +35,7 @@ import java.util.Set;
@@ -33,6 +35,7 @@ import java.util.Set;
import org.jspecify.annotations.Nullable ;
import org.junit.jupiter.api.Test ;
import org.springframework.aot.test.generate.TestGenerationContext ;
import org.springframework.core.test.tools.ResourceFile ;
import org.springframework.core.test.tools.TestCompiler ;
import org.springframework.data.aot.CodeContributionAssert ;
import org.springframework.data.repository.CrudRepository ;
@ -137,6 +140,82 @@ class RepositoryContributorUnitTests {
@@ -137,6 +140,82 @@ class RepositoryContributorUnitTests {
new CodeContributionAssert ( generationContext ) . contributesReflectionFor ( expectedTypeName ) ;
}
@Test // GH-3354
void doesNotWriteCapturedQueryMetadataToResourcesIfDisabled ( ) {
DummyModuleAotRepositoryContext aotContext = new DummyModuleAotRepositoryContext ( UserRepository . class , null ) ;
aotContext . getEnvironment ( ) . setProperty ( "spring.aot.repositories.metadata.enabled" , "false" ) ;
RepositoryContributor repositoryContributor = new RepositoryContributor ( aotContext ) {
@Override
protected @Nullable MethodContributor < ? extends QueryMethod > contributeQueryMethod ( Method method ) {
return MethodContributor
. forQueryMethod (
new QueryMethod ( method , getRepositoryInformation ( ) , getProjectionFactory ( ) , DefaultParameters : : new ) )
. withMetadata ( ( ) - > Map . of ( "filter" , "FILTER(%s > $1)" . formatted ( method . getName ( ) ) , "project" ,
Arrays . stream ( method . getParameters ( ) ) . map ( Parameter : : getName ) . toList ( ) ) )
. contribute ( context - > {
CodeBlock . Builder builder = CodeBlock . builder ( ) ;
if ( ! ClassUtils . isVoidType ( method . getReturnType ( ) ) ) {
builder . addStatement ( "return null" ) ;
}
return builder . build ( ) ;
} ) ;
}
} ;
TestGenerationContext generationContext = new TestGenerationContext ( UserRepository . class ) ;
repositoryContributor . contribute ( generationContext ) ;
generationContext . writeGeneratedContent ( ) ;
TestCompiler . forSystem ( ) . with ( generationContext ) . compile ( compiled - > {
assertThat ( compiled . getResourceFiles ( ) ) . isEmpty ( ) ;
} ) ;
}
@Test // GH-3354
void doesNotWriteCapturedQueryMetadataToResourcesIfAlreadyExists ( ) {
DummyModuleAotRepositoryContext aotContext = new DummyModuleAotRepositoryContext ( UserRepository . class , null ) ;
RepositoryContributor repositoryContributor = new RepositoryContributor ( aotContext ) {
@Override
protected @Nullable MethodContributor < ? extends QueryMethod > contributeQueryMethod ( Method method ) {
return MethodContributor
. forQueryMethod (
new QueryMethod ( method , getRepositoryInformation ( ) , getProjectionFactory ( ) , DefaultParameters : : new ) )
. withMetadata ( ( ) - > Map . of ( "filter" , "FILTER(%s > $1)" . formatted ( method . getName ( ) ) , "project" ,
Arrays . stream ( method . getParameters ( ) ) . map ( Parameter : : getName ) . toList ( ) ) )
. contribute ( context - > {
CodeBlock . Builder builder = CodeBlock . builder ( ) ;
if ( ! ClassUtils . isVoidType ( method . getReturnType ( ) ) ) {
builder . addStatement ( "return null" ) ;
}
return builder . build ( ) ;
} ) ;
}
} ;
TestGenerationContext generationContext = new TestGenerationContext ( UserRepository . class ) ;
repositoryContributor . contribute ( generationContext ) ;
generationContext . writeGeneratedContent ( ) ;
ResourceFile rf = ResourceFile . of ( UserRepository . class . getName ( ) . replace ( '.' , '/' ) + ".json" ,
"But you're untouchable, burning brighter than the sun" ) ;
TestCompiler . forSystem ( ) . with ( generationContext ) . withResources ( rf ) . compile ( compiled - > {
String content = compiled . getResourceFile ( ) . getContent ( ) ;
assertThat ( content ) . contains ( "you're untouchable" ) . doesNotContain ( "FILTER(doSomething > $1)" ) ;
} ) ;
}
@Test // GH-3279
void callsMethodContributionForQueryMethod ( ) {
@ -175,7 +254,6 @@ class RepositoryContributorUnitTests {
@@ -175,7 +254,6 @@ class RepositoryContributorUnitTests {
contributor . contribute ( testGenerationContext ) ;
testGenerationContext . writeGeneratedContent ( ) ;
contributor . verifyContributedMethods ( ) . isNotEmpty ( ) . doesNotContainKey ( "findByFirstname" ) ;
}