@ -15,96 +15,164 @@
* /
* /
package org.springframework.data.aot ;
package org.springframework.data.aot ;
import static org.mockito.Mockito.* ;
import java.util.Collection ;
import java.util.Collection ;
import java.util.List ;
import java.util.List ;
import java.util.function.Consumer ;
import org.assertj.core.api.Assertions ;
import org.assertj.core.api.Assertions ;
import org.junit.jupiter.api.Test ;
import org.junit.jupiter.params.ParameterizedTest ;
import org.junit.jupiter.params.ParameterizedTest ;
import org.junit.jupiter.params.provider.CsvSource ;
import org.junit.jupiter.params.provider.CsvSource ;
import org.mockito.Mock ;
import org.mockito.Mockito ;
import org.mockito.Mockito ;
import org.mockito.junit.jupiter.MockitoSettings ;
import org.springframework.aot.hint.TypeReference ;
import org.springframework.aot.hint.TypeReference ;
import org.springframework.aot.test.generate.TestGenerationContext ;
import org.springframework.beans.factory.BeanFactory ;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory ;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory ;
import org.springframework.core.env.Environment ;
import org.springframework.core.env.Environment ;
import org.springframework.data.aot.types.Address ;
import org.springframework.data.aot.types.Customer ;
import org.springframework.data.aot.types.EmptyType1 ;
import org.springframework.mock.env.MockEnvironment ;
import org.springframework.mock.env.MockEnvironment ;
import org.springframework.util.StringUtils ;
import org.springframework.util.StringUtils ;
/ * *
/ * *
* Tests for { @link AotContext } .
* Unit tests for { @link AotContext } .
*
*
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Christoph Strobl
* /
* /
@MockitoSettings ( strictness = org . mockito . quality . Strictness . LENIENT )
class AotContextUnitTests {
class AotContextUnitTests {
@ParameterizedTest // GH-3322
@Mock BeanFactory beanFactory ;
@CsvSource ( { //
"'spring.aot.repositories.enabled', '', '', '', true" , //
@Mock AotMappingContext mappingContext ;
"'spring.aot.repositories.enabled', 'true', '', '', true" , //
"'spring.aot.repositories.enabled', 'false', '', '', false" , //
MockEnvironment mockEnvironment = new MockEnvironment ( ) ;
"'spring.aot.repositories.enabled', '', 'commons', 'true', true" , //
"'spring.aot.repositories.enabled', 'true', 'commons', 'true', true" , //
@Test // GH-2595
"'spring.aot.repositories.enabled', '', 'commons', 'false', false" , //
void shouldContributeAccessorByDefault ( ) {
"'spring.aot.repositories.enabled', 'false', 'commons', 'true', false" //
} )
contributeAccessor ( Address . class ) ;
void considersEnvironmentSettingsForGeneratedRepositories ( String generalFlag , String generalValue , String storeName ,
verify ( mappingContext ) . contribute ( Address . class ) ;
String storeValue , boolean enabled ) {
}
MockAotContext ctx = new MockAotContext ( ) ;
@Test // GH-2595
if ( StringUtils . hasText ( generalFlag ) & & StringUtils . hasText ( generalValue ) ) {
void shouldConsiderDisabledAccessors ( ) {
ctx . withProperty ( generalFlag , generalValue ) ;
}
mockEnvironment . setProperty ( "spring.aot.data.accessors.enabled" , "false" ) ;
if ( StringUtils . hasText ( storeName ) & & StringUtils . hasText ( storeValue ) ) {
ctx . withProperty ( "spring.aot.%s.repositories.enabled" . formatted ( storeName ) , storeValue ) ;
contributeAccessor ( Address . class ) ;
}
verifyNoInteractions ( mappingContext ) ;
Assertions . assertThat ( ctx . isGeneratedRepositoriesEnabled ( storeName ) ) . isEqualTo ( enabled ) ;
}
}
@Test // GH-2595
static class MockAotContext implements AotContext {
void shouldApplyExcludeFilters ( ) {
private final MockEnvironment environment ;
mockEnvironment . setProperty ( "spring.aot.data.accessors.exclude" ,
Customer . class . getName ( ) + " , " + EmptyType1 . class . getName ( ) ) ;
public MockAotContext ( ) {
this . environment = new MockEnvironment ( ) ;
contributeAccessor ( Address . class , Customer . class , EmptyType1 . class ) ;
}
verify ( mappingContext ) . contribute ( Address . class ) ;
MockAotContext withProperty ( String key , String value ) {
verifyNoMoreInteractions ( mappingContext ) ;
environment . setProperty ( key , value ) ;
}
return this ;
}
@Test // GH-2595
void shouldApplyIncludeExcludeFilters ( ) {
@Override
public ConfigurableListableBeanFactory getBeanFactory ( ) {
mockEnvironment . setProperty ( "spring.aot.data.accessors.include" , Customer . class . getPackageName ( ) + ".Add*" ) ;
return Mockito . mock ( ConfigurableListableBeanFactory . class ) ;
mockEnvironment . setProperty ( "spring.aot.data.accessors.exclude" , Customer . class . getPackageName ( ) + ".**" ) ;
}
contributeAccessor ( Address . class , Customer . class , EmptyType1 . class ) ;
@Override
public TypeIntrospector introspectType ( String typeName ) {
verify ( mappingContext ) . contribute ( Address . class ) ;
return Mockito . mock ( TypeIntrospector . class ) ;
verifyNoMoreInteractions ( mappingContext ) ;
}
}
@Override
private void contributeAccessor ( Class < ? > . . . classes ) {
public IntrospectedBeanDefinition introspectBeanDefinition ( String beanName ) {
return Mockito . mock ( IntrospectedBeanDefinition . class ) ;
DefaultAotContext context = new DefaultAotContext ( beanFactory , mockEnvironment ) ;
}
for ( Class < ? > aClass : classes ) {
@Override
context . typeConfiguration ( aClass , AotTypeConfiguration : : contributeAccessors ) ;
public InstantiationCreator instantiationCreator ( TypeReference typeReference ) {
}
return Mockito . mock ( InstantiationCreator . class ) ;
}
context . typeConfigurations ( ) . forEach ( it - > it . contribute ( mockEnvironment , new TestGenerationContext ( ) ) ) ;
}
@Override
public AotTypeConfiguration typeConfiguration ( TypeReference typeReference ) {
@ParameterizedTest // GH-3322
return null ;
@CsvSource ( { //
}
"'spring.aot.repositories.enabled', '', '', '', true" , //
"'spring.aot.repositories.enabled', 'true', '', '', true" , //
@Override
"'spring.aot.repositories.enabled', 'false', '', '', false" , //
public Collection < AotTypeConfiguration > typeConfigurations ( ) {
"'spring.aot.repositories.enabled', '', 'commons', 'true', true" , //
return List . of ( ) ;
"'spring.aot.repositories.enabled', 'true', 'commons', 'true', true" , //
}
"'spring.aot.repositories.enabled', '', 'commons', 'false', false" , //
"'spring.aot.repositories.enabled', 'false', 'commons', 'true', false" //
@Override
} )
public Environment getEnvironment ( ) {
void considersEnvironmentSettingsForGeneratedRepositories ( String generalFlag , String generalValue , String storeName ,
return environment ;
String storeValue , boolean enabled ) {
}
}
MockAotContext ctx = new MockAotContext ( ) ;
if ( StringUtils . hasText ( generalFlag ) & & StringUtils . hasText ( generalValue ) ) {
ctx . withProperty ( generalFlag , generalValue ) ;
}
if ( StringUtils . hasText ( storeName ) & & StringUtils . hasText ( storeValue ) ) {
ctx . withProperty ( "spring.aot.%s.repositories.enabled" . formatted ( storeName ) , storeValue ) ;
}
Assertions . assertThat ( ctx . isGeneratedRepositoriesEnabled ( storeName ) ) . isEqualTo ( enabled ) ;
}
static class MockAotContext implements AotContext {
private final MockEnvironment environment ;
public MockAotContext ( ) {
this . environment = new MockEnvironment ( ) ;
}
MockAotContext withProperty ( String key , String value ) {
environment . setProperty ( key , value ) ;
return this ;
}
@Override
public ConfigurableListableBeanFactory getBeanFactory ( ) {
return Mockito . mock ( ConfigurableListableBeanFactory . class ) ;
}
@Override
public TypeIntrospector introspectType ( String typeName ) {
return Mockito . mock ( TypeIntrospector . class ) ;
}
@Override
public IntrospectedBeanDefinition introspectBeanDefinition ( String beanName ) {
return Mockito . mock ( IntrospectedBeanDefinition . class ) ;
}
@Override
public void typeConfiguration ( Class < ? > type , Consumer < AotTypeConfiguration > configurationConsumer ) {
}
@Override
public Collection < AotTypeConfiguration > typeConfigurations ( ) {
return List . of ( ) ;
}
@Override
public Environment getEnvironment ( ) {
return environment ;
}
}
}
}