11/*
2- * Copyright 2002-2024 the original author or authors.
2+ * Copyright 2002-2025 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1616
1717package org .springframework .context .annotation .configuration ;
1818
19- import java .util .Collections ;
20-
2119import org .aspectj .lang .annotation .Aspect ;
2220import org .aspectj .lang .annotation .Before ;
2321import org .junit .jupiter .api .Test ;
3028import org .springframework .context .annotation .Bean ;
3129import org .springframework .context .annotation .Configuration ;
3230import org .springframework .context .annotation .ImportResource ;
33- import org .springframework .core .env .MapPropertySource ;
34- import org .springframework .core .env .PropertySource ;
31+ import org .springframework .core .testfixture .env .MockPropertySource ;
3532
3633import static org .assertj .core .api .Assertions .assertThat ;
3734
3835/**
39- * Integration tests for {@link ImportResource} support.
36+ * Integration tests for {@link ImportResource @ImportResource } support.
4037 *
4138 * @author Chris Beams
4239 * @author Juergen Hoeller
4542class ImportResourceTests {
4643
4744 @ Test
48- void importXml () {
49- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportXmlConfig .class );
50- assertThat (ctx .containsBean ("javaDeclaredBean" )).as ("did not contain java-declared bean" ).isTrue ();
51- assertThat (ctx .containsBean ("xmlDeclaredBean" )).as ("did not contain xml-declared bean" ).isTrue ();
52- TestBean tb = ctx .getBean ("javaDeclaredBean" , TestBean .class );
53- assertThat (tb .getName ()).isEqualTo ("myName" );
54- ctx . close ();
45+ void importResource () {
46+ try ( AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportXmlConfig .class )) {
47+ assertThat (ctx .containsBean ("javaDeclaredBean" )).as ("did not contain java-declared bean" ).isTrue ();
48+ assertThat (ctx .containsBean ("xmlDeclaredBean" )).as ("did not contain xml-declared bean" ).isTrue ();
49+ TestBean tb = ctx .getBean ("javaDeclaredBean" , TestBean .class );
50+ assertThat (tb .getName ()).isEqualTo ("myName" );
51+ }
5552 }
5653
5754 @ Test
58- void importXmlIsInheritedFromSuperclassDeclarations () {
59- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (FirstLevelSubConfig .class );
60- assertThat (ctx .containsBean ("xmlDeclaredBean" )).isTrue ();
61- ctx . close ();
55+ void importResourceIsInheritedFromSuperclassDeclarations () {
56+ try ( AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (FirstLevelSubConfig .class )) {
57+ assertThat (ctx .containsBean ("xmlDeclaredBean" )).isTrue ();
58+ }
6259 }
6360
6461 @ Test
65- void importXmlIsMergedFromSuperclassDeclarations () {
66- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (SecondLevelSubConfig .class );
67- assertThat (ctx .containsBean ("secondLevelXmlDeclaredBean" )).as ("failed to pick up second-level-declared XML bean" ).isTrue ();
68- assertThat (ctx .containsBean ("xmlDeclaredBean" )).as ("failed to pick up parent-declared XML bean" ).isTrue ();
69- ctx . close ();
62+ void importResourceIsMergedFromSuperclassDeclarations () {
63+ try ( AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (SecondLevelSubConfig .class )) {
64+ assertThat (ctx .containsBean ("secondLevelXmlDeclaredBean" )).as ("failed to pick up second-level-declared XML bean" ).isTrue ();
65+ assertThat (ctx .containsBean ("xmlDeclaredBean" )).as ("failed to pick up parent-declared XML bean" ).isTrue ();
66+ }
7067 }
7168
7269 @ Test
73- void importXmlWithNamespaceConfig () {
74- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportXmlWithAopNamespaceConfig .class );
75- Object bean = ctx .getBean ("proxiedXmlBean" );
76- assertThat (AopUtils .isAopProxy (bean )).isTrue ();
77- ctx . close ();
70+ void importResourceWithNamespaceConfig () {
71+ try ( AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportXmlWithAopNamespaceConfig .class )) {
72+ Object bean = ctx .getBean ("proxiedXmlBean" );
73+ assertThat (AopUtils .isAopProxy (bean )).isTrue ();
74+ }
7875 }
7976
8077 @ Test
81- void importXmlWithOtherConfigurationClass () {
82- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportXmlWithConfigurationClass .class );
83- assertThat (ctx .containsBean ("javaDeclaredBean" )).as ("did not contain java-declared bean" ).isTrue ();
84- assertThat (ctx .containsBean ("xmlDeclaredBean" )).as ("did not contain xml-declared bean" ).isTrue ();
85- TestBean tb = ctx .getBean ("javaDeclaredBean" , TestBean .class );
86- assertThat (tb .getName ()).isEqualTo ("myName" );
87- ctx . close ();
78+ void importResourceWithOtherConfigurationClass () {
79+ try ( AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportXmlWithConfigurationClass .class )) {
80+ assertThat (ctx .containsBean ("javaDeclaredBean" )).as ("did not contain java-declared bean" ).isTrue ();
81+ assertThat (ctx .containsBean ("xmlDeclaredBean" )).as ("did not contain xml-declared bean" ).isTrue ();
82+ TestBean tb = ctx .getBean ("javaDeclaredBean" , TestBean .class );
83+ assertThat (tb .getName ()).isEqualTo ("myName" );
84+ }
8885 }
8986
9087 @ Test
9188 void importWithPlaceholder () {
92- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
93- PropertySource <?> propertySource = new MapPropertySource ("test" ,
94- Collections .<String , Object > singletonMap ("test" , "springframework" ));
95- ctx .getEnvironment ().getPropertySources ().addFirst (propertySource );
96- ctx .register (ImportXmlConfig .class );
97- ctx .refresh ();
98- assertThat (ctx .containsBean ("xmlDeclaredBean" )).as ("did not contain xml-declared bean" ).isTrue ();
99- ctx .close ();
89+ try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ()) {
90+ ctx .getEnvironment ().getPropertySources ().addFirst (new MockPropertySource ("test" ).withProperty ("test" , "springframework" ));
91+ ctx .register (ImportXmlConfig .class );
92+ ctx .refresh ();
93+ assertThat (ctx .containsBean ("xmlDeclaredBean" )).as ("did not contain xml-declared bean" ).isTrue ();
94+ }
10095 }
10196
10297 @ Test
103- void importXmlWithAutowiredConfig () {
104- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportXmlAutowiredConfig .class );
105- String name = ctx .getBean ("xmlBeanName" , String .class );
106- assertThat (name ).isEqualTo ("xml.declared" );
107- ctx . close ();
98+ void importResourceWithAutowiredConfig () {
99+ try ( AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportXmlAutowiredConfig .class )) {
100+ String name = ctx .getBean ("xmlBeanName" , String .class );
101+ assertThat (name ).isEqualTo ("xml.declared" );
102+ }
108103 }
109104
110105 @ Test
111106 void importNonXmlResource () {
112- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportNonXmlResourceConfig .class );
113- assertThat (ctx .containsBean ("propertiesDeclaredBean" )).isTrue ();
114- ctx . close ();
107+ try ( AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportNonXmlResourceConfig .class )) {
108+ assertThat (ctx .containsBean ("propertiesDeclaredBean" )).isTrue ();
109+ }
115110 }
116111
117112
118113 @ Configuration
119114 @ ImportResource ("classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml" )
120115 static class ImportXmlConfig {
116+
121117 @ Value ("${name}" )
122118 private String name ;
119+
123120 @ Bean public TestBean javaDeclaredBean () {
124121 return new TestBean (this .name );
125122 }
@@ -146,6 +143,7 @@ static class ImportXmlWithAopNamespaceConfig {
146143
147144 @ Aspect
148145 static class AnAspect {
146+
149147 @ Before ("execution(* org.springframework.beans.testfixture.beans.TestBean.*(..))" )
150148 public void advice () { }
151149 }
@@ -158,16 +156,19 @@ static class ImportXmlWithConfigurationClass {
158156 @ Configuration
159157 @ ImportResource ("classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml" )
160158 static class ImportXmlAutowiredConfig {
161- @ Autowired TestBean xmlDeclaredBean ;
162159
163- @ Bean public String xmlBeanName () {
160+ @ Autowired
161+ TestBean xmlDeclaredBean ;
162+
163+ @ Bean
164+ public String xmlBeanName () {
164165 return xmlDeclaredBean .getName ();
165166 }
166167 }
167168
168169 @ SuppressWarnings ("deprecation" )
169170 @ Configuration
170- @ ImportResource (locations = "classpath: org/springframework/context/annotation/configuration/ImportNonXmlResourceConfig-context .properties" ,
171+ @ ImportResource (locations = "org/springframework/context/annotation/configuration/ImportNonXmlResourceConfig.properties" ,
171172 reader = org .springframework .beans .factory .support .PropertiesBeanDefinitionReader .class )
172173 static class ImportNonXmlResourceConfig {
173174 }
0 commit comments