@ -16,6 +16,9 @@
package org.springframework.test.context.support ;
package org.springframework.test.context.support ;
import java.util.Arrays ;
import org.junit.jupiter.api.Nested ;
import org.junit.jupiter.api.Test ;
import org.junit.jupiter.api.Test ;
import org.springframework.context.ApplicationContext ;
import org.springframework.context.ApplicationContext ;
@ -25,7 +28,6 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfigurationAttributes ;
import org.springframework.test.context.ContextConfigurationAttributes ;
import org.springframework.test.context.ContextLoader ;
import org.springframework.test.context.ContextLoader ;
import org.springframework.test.context.MergedContextConfiguration ;
import org.springframework.test.context.MergedContextConfiguration ;
import org.springframework.util.ObjectUtils ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType ;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType ;
@ -45,20 +47,16 @@ class DelegatingSmartContextLoaderTests {
private final DelegatingSmartContextLoader loader = new DelegatingSmartContextLoader ( ) ;
private final DelegatingSmartContextLoader loader = new DelegatingSmartContextLoader ( ) ;
@Nested
private static void assertEmpty ( Object [ ] array ) {
class SmartContextLoaderSpiTests {
assertThat ( ObjectUtils . isEmpty ( array ) ) . isTrue ( ) ;
}
// --- SmartContextLoader - processContextConfiguration() ------------------
@Test
@Test
void processContextConfigurationWithDefaultXmlConfigGeneration ( ) {
void processContextConfigurationWithDefaultXmlConfigGeneration ( ) {
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes (
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes (
XmlTestCase . class , EMPTY_STRING_ARRAY , EMPTY_CLASS_ARRAY , true , null , true , ContextLoader . class ) ;
XmlTestCase . class , EMPTY_STRING_ARRAY , EMPTY_CLASS_ARRAY , true , null , true , ContextLoader . class ) ;
loader . processContextConfiguration ( configAttributes ) ;
loader . processContextConfiguration ( configAttributes ) ;
assertThat ( configAttributes . getLocations ( ) . length ) . isEqualTo ( 1 ) ;
assertThat ( configAttributes . getLocations ( ) ) . hasSize ( 1 ) ;
assertEmpty ( configAttributes . getClasses ( ) ) ;
assertThat ( configAttributes . getClasses ( ) ) . isEmpty ( ) ;
}
}
@Test
@Test
@ -66,8 +64,8 @@ class DelegatingSmartContextLoaderTests {
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes (
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes (
ConfigClassTestCase . class , EMPTY_STRING_ARRAY , EMPTY_CLASS_ARRAY , true , null , true , ContextLoader . class ) ;
ConfigClassTestCase . class , EMPTY_STRING_ARRAY , EMPTY_CLASS_ARRAY , true , null , true , ContextLoader . class ) ;
loader . processContextConfiguration ( configAttributes ) ;
loader . processContextConfiguration ( configAttributes ) ;
assertThat ( configAttributes . getClasses ( ) . length ) . isEqualTo ( 1 ) ;
assertThat ( configAttributes . getClasses ( ) ) . hasSize ( 1 ) ;
assertEmpty ( configAttributes . getLocations ( ) ) ;
assertThat ( configAttributes . getLocations ( ) ) . isEmpty ( ) ;
}
}
@Test
@Test
@ -75,8 +73,8 @@ class DelegatingSmartContextLoaderTests {
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes (
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes (
ImproperDuplicateDefaultXmlAndConfigClassTestCase . class , EMPTY_STRING_ARRAY , EMPTY_CLASS_ARRAY ,
ImproperDuplicateDefaultXmlAndConfigClassTestCase . class , EMPTY_STRING_ARRAY , EMPTY_CLASS_ARRAY ,
true , null , true , ContextLoader . class ) ;
true , null , true , ContextLoader . class ) ;
assertThatIllegalStateException ( ) . isThrownBy ( ( ) - >
assertThatIllegalStateException ( )
loader . processContextConfiguration ( configAttributes ) )
. isThrownBy ( ( ) - > loader . processContextConfiguration ( configAttributes ) )
. withMessageContaining ( "both default locations AND default configuration classes were detected" ) ;
. withMessageContaining ( "both default locations AND default configuration classes were detected" ) ;
}
}
@ -87,7 +85,7 @@ class DelegatingSmartContextLoaderTests {
getClass ( ) , locations , EMPTY_CLASS_ARRAY , true , null , true , ContextLoader . class ) ;
getClass ( ) , locations , EMPTY_CLASS_ARRAY , true , null , true , ContextLoader . class ) ;
loader . processContextConfiguration ( configAttributes ) ;
loader . processContextConfiguration ( configAttributes ) ;
assertThat ( configAttributes . getLocations ( ) ) . isEqualTo ( locations ) ;
assertThat ( configAttributes . getLocations ( ) ) . isEqualTo ( locations ) ;
assertEmpty ( configAttributes . getClasses ( ) ) ;
assertThat ( configAttributes . getClasses ( ) ) . isEmpty ( ) ;
}
}
@Test
@Test
@ -97,23 +95,20 @@ class DelegatingSmartContextLoaderTests {
getClass ( ) , EMPTY_STRING_ARRAY , classes , true , null , true , ContextLoader . class ) ;
getClass ( ) , EMPTY_STRING_ARRAY , classes , true , null , true , ContextLoader . class ) ;
loader . processContextConfiguration ( configAttributes ) ;
loader . processContextConfiguration ( configAttributes ) ;
assertThat ( configAttributes . getClasses ( ) ) . isEqualTo ( classes ) ;
assertThat ( configAttributes . getClasses ( ) ) . isEqualTo ( classes ) ;
assertEmpty ( configAttributes . getLocations ( ) ) ;
assertThat ( configAttributes . getLocations ( ) ) . isEmpty ( ) ;
}
}
// --- SmartContextLoader - loadContext() ----------------------------------
@Test
@Test
void loadContextWithNullConfig ( ) throws Exception {
void loadContextWithNullConfig ( ) throws Exception {
assertThatIllegalArgumentException ( ) . isThrownBy ( ( ) - >
assertThatIllegalArgumentException ( ) . isThrownBy ( ( ) - > loader . loadContext ( null , true ) ) ;
loader . loadContext ( ( MergedContextConfiguration ) null ) ) ;
}
}
@Test
@Test
void loadContextWithoutLocationsAndConfigurationClasses ( ) throws Exception {
void loadContextWithoutLocationsAndConfigurationClasses ( ) throws Exception {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration (
MergedContextConfiguration mergedConfig = new MergedContextConfiguration (
getClass ( ) , EMPTY_STRING_ARRAY , EMPTY_CLASS_ARRAY , EMPTY_STRING_ARRAY , loader ) ;
getClass ( ) , EMPTY_STRING_ARRAY , EMPTY_CLASS_ARRAY , EMPTY_STRING_ARRAY , loader ) ;
assertThatIllegalStateException ( ) . isThrownBy ( ( ) - >
assertThatIllegalStateException ( )
loader . loadContext ( mergedConfig ) )
. isThrownBy ( ( ) - > loader . loadContext ( mergedConfig , true ) )
. withMessageStartingWith ( "Neither" )
. withMessageStartingWith ( "Neither" )
. withMessageContaining ( "was able to load an ApplicationContext from" ) ;
. withMessageContaining ( "was able to load an ApplicationContext from" ) ;
}
}
@ -125,55 +120,94 @@ class DelegatingSmartContextLoaderTests {
void loadContextWithLocationsAndConfigurationClasses ( ) throws Exception {
void loadContextWithLocationsAndConfigurationClasses ( ) throws Exception {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration ( getClass ( ) ,
MergedContextConfiguration mergedConfig = new MergedContextConfiguration ( getClass ( ) ,
new String [ ] { "test.xml" } , new Class < ? > [ ] { getClass ( ) } , EMPTY_STRING_ARRAY , loader ) ;
new String [ ] { "test.xml" } , new Class < ? > [ ] { getClass ( ) } , EMPTY_STRING_ARRAY , loader ) ;
assertThatIllegalStateException ( ) . isThrownBy ( ( ) - >
assertThatIllegalStateException ( )
loader . loadContext ( mergedConfig ) )
. isThrownBy ( ( ) - > loader . loadContext ( mergedConfig , true ) )
. withMessageStartingWith ( "Neither" )
. withMessageStartingWith ( "Neither" )
. withMessageContaining ( "declare either 'locations' or 'classes' but not both." ) ;
. withMessageContaining ( "declare either 'locations' or 'classes' but not both." ) ;
}
}
@Test
void loadContextWithXmlConfig ( ) throws Exception {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration (
XmlTestCase . class ,
new String [ ] { "classpath:/org/springframework/test/context/support/DelegatingSmartContextLoaderTests$XmlTestCase-context.xml" } ,
EMPTY_CLASS_ARRAY , EMPTY_STRING_ARRAY , loader ) ;
assertApplicationContextLoadsAndContainsFooString ( mergedConfig ) ;
}
@Test
void loadContextWithConfigurationClass ( ) throws Exception {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration ( ConfigClassTestCase . class ,
EMPTY_STRING_ARRAY , new Class < ? > [ ] { ConfigClassTestCase . Config . class } , EMPTY_STRING_ARRAY , loader ) ;
assertApplicationContextLoadsAndContainsFooString ( mergedConfig ) ;
}
private void assertApplicationContextLoadsAndContainsFooString ( MergedContextConfiguration mergedConfig )
private void assertApplicationContextLoadsAndContainsFooString ( MergedContextConfiguration mergedConfig )
throws Exception {
throws Exception {
ApplicationContext applicationContext = loader . loadContext ( mergedConfig ) ;
ApplicationContext applicationContext = loader . loadContext ( mergedConfig , true ) ;
assertThat ( applicationContext ) . isNotNull ( ) ;
assertThat ( applicationContext ) . isInstanceOf ( ConfigurableApplicationContext . class ) ;
assertThat ( applicationContext . getBean ( String . class ) ) . isEqualTo ( "foo" ) ;
assertThat ( applicationContext . getBean ( String . class ) ) . isEqualTo ( "foo" ) ;
boolean condition = applicationContext instanceof ConfigurableApplicationContext ;
ConfigurableApplicationContext cac = ( ConfigurableApplicationContext ) applicationContext ;
assertThat ( condition ) . isTrue ( ) ;
cac . close ( ) ;
( ( ConfigurableApplicationContext ) applicationContext ) . close ( ) ;
}
}
/ * *
* @since 6 . 0
* /
@Test
@Test
void loadContextWithXmlConfig ( ) throws Exception {
void loadContextWithXmlConfigWithoutRefresh ( ) throws Exception {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration (
MergedContextConfiguration mergedConfig = new MergedContextConfiguration (
XmlTestCase . class ,
XmlTestCase . class ,
new String [ ] { "classpath:/org/springframework/test/context/support/DelegatingSmartContextLoaderTests$XmlTestCase-context.xml" } ,
new String [ ] { "classpath:/org/springframework/test/context/support/DelegatingSmartContextLoaderTests$XmlTestCase-context.xml" } ,
EMPTY_CLASS_ARRAY , EMPTY_STRING_ARRAY , loader ) ;
EMPTY_CLASS_ARRAY , EMPTY_STRING_ARRAY , loader ) ;
assertApplicationContextLoadsAndContainsFooString ( mergedConfig ) ;
assertApplicationContextLoadsWithoutRefresh ( mergedConfig , "foo" ) ;
}
}
/ * *
* @since 6 . 0
* /
@Test
@Test
void loadContextWithConfigurationClass ( ) throws Exception {
void loadContextWithConfigurationClassWithoutRefresh ( ) throws Exception {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration ( ConfigClassTestCase . class ,
MergedContextConfiguration mergedConfig = new MergedContextConfiguration ( ConfigClassTestCase . class ,
EMPTY_STRING_ARRAY , new Class < ? > [ ] { ConfigClassTestCase . Config . class } , EMPTY_STRING_ARRAY , loader ) ;
EMPTY_STRING_ARRAY , new Class < ? > [ ] { ConfigClassTestCase . Config . class } , EMPTY_STRING_ARRAY , loader ) ;
assertApplicationContextLoadsAndContainsFooString ( mergedConfig ) ;
assertApplicationContextLoadsWithoutRefresh ( mergedConfig , "ConfigClassTestCase.Config" ) ;
}
}
// --- ContextLoader -------------------------------------------------------
private void assertApplicationContextLoadsWithoutRefresh ( MergedContextConfiguration mergedConfig ,
String expectedBeanDefName ) throws Exception {
ApplicationContext context = loader . loadContext ( mergedConfig , false ) ;
assertThat ( context ) . isInstanceOf ( ConfigurableApplicationContext . class ) ;
ConfigurableApplicationContext cac = ( ConfigurableApplicationContext ) context ;
assertThat ( cac . isActive ( ) ) . as ( "ApplicationContext is active" ) . isFalse ( ) ;
assertThat ( Arrays . stream ( context . getBeanDefinitionNames ( ) ) ) . anyMatch ( name - > name . contains ( expectedBeanDefName ) ) ;
cac . close ( ) ;
}
}
@Nested
class ContextLoaderSpiTests {
@Test
@Test
void processLocations ( ) {
void processLocations ( ) {
assertThatExceptionOfType ( UnsupportedOperationException . class ) . isThrownBy ( ( ) - >
assertThatExceptionOfType ( UnsupportedOperationException . class )
loader . processLocations ( getClass ( ) , EMPTY_STRING_ARRAY ) ) ;
. isThrownBy ( ( ) - > loader . processLocations ( getClass ( ) , EMPTY_STRING_ARRAY ) ) ;
}
}
@Test
@Test
void loadContextFromLocations ( ) {
void loadContextFromLocations ( ) {
assertThatExceptionOfType ( UnsupportedOperationException . class ) . isThrownBy ( ( ) - >
assertThatExceptionOfType ( UnsupportedOperationException . class )
loader . loadContext ( EMPTY_STRING_ARRAY ) ) ;
. isThrownBy ( ( ) - > loader . loadContext ( EMPTY_STRING_ARRAY ) ) ;
}
}
}
// -------------------------------------------------------------------------
static class XmlTestCase {
static class XmlTestCase {
}
}