@ -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,135 +47,167 @@ class DelegatingSmartContextLoaderTests {
private final DelegatingSmartContextLoader loader = new DelegatingSmartContextLoader ( ) ;
private final DelegatingSmartContextLoader loader = new DelegatingSmartContextLoader ( ) ;
@Nested
class SmartContextLoaderSpiTests {
private static void assertEmpty ( Object [ ] array ) {
@Test
assertThat ( ObjectUtils . isEmpty ( array ) ) . isTrue ( ) ;
void processContextConfigurationWithDefaultXmlConfigGeneration ( ) {
}
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes (
XmlTestCase . class , EMPTY_STRING_ARRAY , EMPTY_CLASS_ARRAY , true , null , true , ContextLoader . class ) ;
loader . processContextConfiguration ( configAttributes ) ;
assertThat ( configAttributes . getLocations ( ) ) . hasSize ( 1 ) ;
assertThat ( configAttributes . getClasses ( ) ) . isEmpty ( ) ;
}
// --- SmartContextLoader - processContextConfiguration() ------------------
@Test
void processContextConfigurationWithDefaultConfigurationClassGeneration ( ) {
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes (
ConfigClassTestCase . class , EMPTY_STRING_ARRAY , EMPTY_CLASS_ARRAY , true , null , true , ContextLoader . class ) ;
loader . processContextConfiguration ( configAttributes ) ;
assertThat ( configAttributes . getClasses ( ) ) . hasSize ( 1 ) ;
assertThat ( configAttributes . getLocations ( ) ) . isEmpty ( ) ;
}
@Test
@Test
void processContextConfigurationWithDefaultXmlConfigGeneration ( ) {
void processContextConfigurationWithDefaultXmlConfigAndConfigurationClassGeneration ( ) {
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes (
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes (
XmlTestCase . class , EMPTY_STRING_ARRAY , EMPTY_CLASS_ARRAY , true , null , true , ContextLoader . class ) ;
ImproperDuplicateDefaultXmlAndConfigClassTestCase . class , EMPTY_STRING_ARRAY , EMPTY_CLASS_ARRAY ,
loader . processContextConfiguration ( configAttributes ) ;
true , null , true , ContextLoader . class ) ;
assertThat ( configAttributes . getLocations ( ) . length ) . isEqualTo ( 1 ) ;
assertThatIllegalStateException ( )
assertEmpty ( configAttributes . getClasses ( ) ) ;
. isThrownBy ( ( ) - > loader . processContextConfiguration ( configAttributes ) )
}
. withMessageContaining ( "both default locations AND default configuration classes were detected" ) ;
}
@Test
@Test
void processContextConfigurationWithDefaultConfigurationClassGeneration ( ) {
void processContextConfigurationWithLocation ( ) {
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes (
String [ ] locations = new String [ ] { "classpath:/foo.xml" } ;
ConfigClassTestCase . class , EMPTY_STRING_ARRAY , EMPTY_CLASS_ARRAY , true , null , true , ContextLoader . class ) ;
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes (
loader . processContextConfiguration ( configAttributes ) ;
getClass ( ) , locations , EMPTY_CLASS_ARRAY , true , null , true , ContextLoader . class ) ;
assertThat ( configAttributes . getClasses ( ) . length ) . isEqualTo ( 1 ) ;
loader . processContextConfiguration ( configAttributes ) ;
assertEmpty ( configAttributes . getLocations ( ) ) ;
assertThat ( configAttributes . getLocations ( ) ) . isEqualTo ( locations ) ;
}
assertThat ( configAttributes . getClasses ( ) ) . isEmpty ( ) ;
}
@Test
@Test
void processContextConfigurationWithDefaultXmlConfigAnd ConfigurationClassGeneration ( ) {
void processContextConfigurationWithConfigurationClass ( ) {
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes (
Class < ? > [ ] classes = new Class < ? > [ ] { getClass ( ) } ;
ImproperDuplicateDefaultXmlAndConfigClassTestCase . class , EMPTY_STRING_ARRAY , EMPTY_CLASS_ARRAY ,
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes (
true , null , true , ContextLoader . class ) ;
getClass ( ) , EMPTY_STRING_ARRAY , classes , true , null , true , ContextLoader . class ) ;
assertThatIllegalStateException ( ) . isThrownBy ( ( ) - >
loader . processContextConfiguration ( configAttributes ) ;
loader . processContextConfiguration ( configAttributes ) )
assertThat ( configAttributes . getClasses ( ) ) . isEqualTo ( classes ) ;
. withMessageContaining ( "both default locations AND default configuration classes were detected" ) ;
assertThat ( configAttributes . getLocations ( ) ) . isEmpty ( ) ;
}
}
@Test
@Test
void processContextConfigurationWithLocation ( ) {
void loadContextWithNullConfig ( ) throws Exception {
String [ ] locations = new String [ ] { "classpath:/foo.xml" } ;
assertThatIllegalArgumentException ( ) . isThrownBy ( ( ) - > loader . loadContext ( null , true ) ) ;
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes (
}
getClass ( ) , locations , EMPTY_CLASS_ARRAY , true , null , true , ContextLoader . class ) ;
loader . processContextConfiguration ( configAttributes ) ;
assertThat ( configAttributes . getLocations ( ) ) . isEqualTo ( locations ) ;
assertEmpty ( configAttributes . getClasses ( ) ) ;
}
@Test
@Test
void processContextConfigurationWithConfigurationClass ( ) {
void loadContextWithoutLocationsAndConfigurationClasses ( ) throws Exception {
Class < ? > [ ] classes = new Class < ? > [ ] { getClass ( ) } ;
MergedContextConfiguration mergedConfig = new MergedContextConfiguration (
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes (
getClass ( ) , EMPTY_STRING_ARRAY , EMPTY_CLASS_ARRAY , EMPTY_STRING_ARRAY , loader ) ;
getClass ( ) , EMPTY_STRING_ARRAY , classes , true , null , true , ContextLoader . class ) ;
assertThatIllegalStateException ( )
loader . processContextConfiguration ( configAttributes ) ;
. isThrownBy ( ( ) - > loader . loadContext ( mergedConfig , true ) )
assertThat ( configAttributes . getClasses ( ) ) . isEqualTo ( classes ) ;
. withMessageStartingWith ( "Neither" )
assertEmpty ( configAttributes . getLocations ( ) ) ;
. withMessageContaining ( "was able to load an ApplicationContext from" ) ;
}
}
// --- SmartContextLoader - loadContext() ----------------------------------
/ * *
* @since 4 . 1
* /
@Test
void loadContextWithLocationsAndConfigurationClasses ( ) throws Exception {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration ( getClass ( ) ,
new String [ ] { "test.xml" } , new Class < ? > [ ] { getClass ( ) } , EMPTY_STRING_ARRAY , loader ) ;
assertThatIllegalStateException ( )
. isThrownBy ( ( ) - > loader . loadContext ( mergedConfig , true ) )
. withMessageStartingWith ( "Neither" )
. withMessageContaining ( "declare either 'locations' or 'classes' but not both." ) ;
}
@Test
@Test
void loadContextWithNullConfig ( ) throws Exception {
void loadContextWithXmlConfig ( ) throws Exception {
assertThatIllegalArgumentException ( ) . isThrownBy ( ( ) - >
MergedContextConfiguration mergedConfig = new MergedContextConfiguration (
loader . loadContext ( ( MergedContextConfiguration ) null ) ) ;
XmlTestCase . class ,
}
new String [ ] { "classpath:/org/springframework/test/context/support/DelegatingSmartContextLoaderTests$XmlTestCase-context.xml" } ,
EMPTY_CLASS_ARRAY , EMPTY_STRING_ARRAY , loader ) ;
@Test
assertApplicationContextLoadsAndContainsFooString ( mergedConfig ) ;
void loadContextWithoutLocationsAndConfigurationClasses ( ) throws Exception {
}
MergedContextConfiguration mergedConfig = new MergedContextConfiguration (
getClass ( ) , EMPTY_STRING_ARRAY , EMPTY_CLASS_ARRAY , EMPTY_STRING_ARRAY , loader ) ;
assertThatIllegalStateException ( ) . isThrownBy ( ( ) - >
loader . loadContext ( mergedConfig ) )
. withMessageStartingWith ( "Neither" )
. withMessageContaining ( "was able to load an ApplicationContext from" ) ;
}
/ * *
@Test
* @since 4 . 1
void loadContextWithConfigurationClass ( ) throws Exception {
* /
MergedContextConfiguration mergedConfig = new MergedContextConfiguration ( ConfigClassTestCase . class ,
@Test
EMPTY_STRING_ARRAY , new Class < ? > [ ] { ConfigClassTestCase . Config . class } , EMPTY_STRING_ARRAY , loader ) ;
void loadContextWithLocationsAndConfigurationClasses ( ) throws Exception {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration ( getClass ( ) ,
new String [ ] { "test.xml" } , new Class < ? > [ ] { getClass ( ) } , EMPTY_STRING_ARRAY , loader ) ;
assertThatIllegalStateException ( ) . isThrownBy ( ( ) - >
loader . loadContext ( mergedConfig ) )
. withMessageStartingWith ( "Neither" )
. withMessageContaining ( "declare either 'locations' or 'classes' but not both." ) ;
}
private void assertApplicationContextLoadsAndContainsFooString ( MergedContextConfiguration mergedConfig )
assertApplicationContextLoadsAndContainsFooString ( mergedConfig ) ;
throws Exception {
}
ApplicationContext applicationContext = loader . loadContext ( mergedConfig ) ;
private void assertApplicationContextLoadsAndContainsFooString ( MergedContextConfiguration mergedConfig )
assertThat ( applicationContext ) . isNotNull ( ) ;
throws Exception {
assertThat ( applicationContext . getBean ( String . class ) ) . isEqualTo ( "foo" ) ;
boolean condition = applicationContext instanceof ConfigurableApplicationContext ;
assertThat ( condition ) . isTrue ( ) ;
( ( ConfigurableApplicationContext ) applicationContext ) . close ( ) ;
}
@Test
ApplicationContext applicationContext = loader . loadContext ( mergedConfig , true ) ;
void loadContextWithXmlConfig ( ) throws Exception {
assertThat ( applicationContext ) . isInstanceOf ( ConfigurableApplicationContext . class ) ;
MergedContextConfiguration mergedConfig = new MergedContextConfiguration (
assertThat ( applicationContext . getBean ( String . class ) ) . isEqualTo ( "foo" ) ;
XmlTestCase . class ,
ConfigurableApplicationContext cac = ( ConfigurableApplicationContext ) applicationContext ;
new String [ ] { "classpath:/org/springframework/test/context/support/DelegatingSmartContextLoaderTests$XmlTestCase-context.xml" } ,
cac . close ( ) ;
EMPTY_CLASS_ARRAY , EMPTY_STRING_ARRAY , loader ) ;
}
assertApplicationContextLoadsAndContainsFooString ( mergedConfig ) ;
}
@Test
/ * *
void loadContextWithConfigurationClass ( ) throws Exception {
* @since 6 . 0
MergedContextConfiguration mergedConfig = new MergedContextConfiguration ( ConfigClassTestCase . class ,
* /
EMPTY_STRING_ARRAY , new Class < ? > [ ] { ConfigClassTestCase . Config . class } , EMPTY_STRING_ARRAY , loader ) ;
@Test
assertApplicationContextLoadsAndContainsFooString ( mergedConfig ) ;
void loadContextWithXmlConfigWithoutRefresh ( ) 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 ) ;
assertApplicationContextLoadsWithoutRefresh ( mergedConfig , "foo" ) ;
}
// --- ContextLoader -------------------------------------------------------
/ * *
* @since 6 . 0
* /
@Test
void loadContextWithConfigurationClassWithoutRefresh ( ) throws Exception {
MergedContextConfiguration mergedConfig = new MergedContextConfiguration ( ConfigClassTestCase . class ,
EMPTY_STRING_ARRAY , new Class < ? > [ ] { ConfigClassTestCase . Config . class } , EMPTY_STRING_ARRAY , loader ) ;
@Test
assertApplicationContextLoadsWithoutRefresh ( mergedConfig , "ConfigClassTestCase.Config" ) ;
void processLocations ( ) {
}
assertThatExceptionOfType ( UnsupportedOperationException . class ) . isThrownBy ( ( ) - >
loader . processLocations ( getClass ( ) , EMPTY_STRING_ARRAY ) ) ;
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 ( ) ;
}
@Test
void loadContextFromLocations ( ) {
assertThatExceptionOfType ( UnsupportedOperationException . class ) . isThrownBy ( ( ) - >
loader . loadContext ( EMPTY_STRING_ARRAY ) ) ;
}
}
@Nested
class ContextLoaderSpiTests {
@Test
void processLocations ( ) {
assertThatExceptionOfType ( UnsupportedOperationException . class )
. isThrownBy ( ( ) - > loader . processLocations ( getClass ( ) , EMPTY_STRING_ARRAY ) ) ;
}
@Test
void loadContextFromLocations ( ) {
assertThatExceptionOfType ( UnsupportedOperationException . class )
. isThrownBy ( ( ) - > loader . loadContext ( EMPTY_STRING_ARRAY ) ) ;
}
}
// -------------------------------------------------------------------------
static class XmlTestCase {
static class XmlTestCase {
}
}