File tree Expand file tree Collapse file tree 9 files changed +137
-0
lines changed
spring-core-environment-abstraction
main/java/com/jsbd/profiles
test/java/com/jsbd/profiles Expand file tree Collapse file tree 9 files changed +137
-0
lines changed Original file line number Diff line number Diff line change 2121 <groupId >org.springframework</groupId >
2222 <artifactId >spring-context</artifactId >
2323 </dependency >
24+
25+ <dependency >
26+ <groupId >org.junit.jupiter</groupId >
27+ <artifactId >junit-jupiter-api</artifactId >
28+ <scope >test</scope >
29+ </dependency >
30+
31+ <dependency >
32+ <groupId >org.junit.platform</groupId >
33+ <artifactId >junit-platform-runner</artifactId >
34+ <scope >test</scope >
35+ </dependency >
36+
37+ <dependency >
38+ <groupId >org.springframework</groupId >
39+ <artifactId >spring-test</artifactId >
40+ <scope >test</scope >
41+ </dependency >
2442 </dependencies >
2543
2644 <dependencyManagement >
Original file line number Diff line number Diff line change 1+ package com .jsbd .profiles ;
2+
3+ public interface DataSource {
4+ public String getName ();
5+ }
Original file line number Diff line number Diff line change 1+ package com .jsbd .profiles ;
2+
3+ import org .springframework .context .annotation .Profile ;
4+ import org .springframework .stereotype .Component ;
5+
6+ @ Component
7+ @ Profile ("graphQl" )
8+ public class GraphQlDataSource implements DataSource {
9+ @ Override
10+ public String getName () {
11+ return "GraphQlDataSource" ;
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ package com .jsbd .profiles ;
2+
3+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
4+
5+ public class ProfilesApp {
6+ public static void main (String [] args ) {
7+ AnnotationConfigApplicationContext context
8+ = new AnnotationConfigApplicationContext ();
9+ context .getEnvironment ().setActiveProfiles ("graphQl" );
10+ context .register (ProfilesConfig .class );
11+ context .refresh ();
12+ DataSource dataSource = context .getBean (DataSource .class );
13+ System .out .println (dataSource .getClass ());
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package com .jsbd .profiles ;
2+
3+ import org .springframework .context .annotation .ComponentScan ;
4+ import org .springframework .context .annotation .Configuration ;
5+
6+ @ Configuration
7+ @ ComponentScan ("com.jsbd.profiles" )
8+ public class ProfilesConfig {
9+ }
Original file line number Diff line number Diff line change 1+ package com .jsbd .profiles ;
2+
3+ import org .springframework .context .annotation .Profile ;
4+ import org .springframework .stereotype .Component ;
5+
6+ @ Component
7+ @ Profile ({"rest" , "default" })
8+ public class RestDataSource implements DataSource {
9+
10+ //Logic to fetch data from API
11+ @ Override
12+ public String getName () {
13+ return "RestDataSource" ;
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package com .jsbd .profiles ;
2+
3+ import org .junit .Assert ;
4+ import org .junit .jupiter .api .Test ;
5+ import org .springframework .beans .factory .annotation .Autowired ;
6+ import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
7+
8+ @ SpringJUnitConfig (ProfilesConfig .class )
9+ public class TestDefaultProfile {
10+ @ Autowired
11+ private DataSource dataSource ;
12+
13+ @ Test
14+ public void testDefaultProfile () {
15+ Assert .assertNotNull (dataSource );
16+ Assert .assertEquals (dataSource .getClass (), RestDataSource .class );
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ package com .jsbd .profiles ;
2+
3+ import org .junit .Assert ;
4+ import org .junit .jupiter .api .Test ;
5+ import org .springframework .beans .factory .annotation .Autowired ;
6+ import org .springframework .test .context .ActiveProfiles ;
7+ import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
8+
9+ @ SpringJUnitConfig (ProfilesConfig .class )
10+ @ ActiveProfiles ("graphQl" )
11+ public class TestGraphQlProfile {
12+
13+ @ Autowired
14+ private DataSource dataSource ;
15+
16+ @ Test
17+ public void testGraphQlProfile () {
18+ System .out .println (dataSource );
19+ Assert .assertNotNull (dataSource );
20+ Assert .assertEquals (dataSource .getClass (), GraphQlDataSource .class );
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ package com .jsbd .profiles ;
2+
3+ import org .junit .Assert ;
4+ import org .junit .jupiter .api .Test ;
5+ import org .springframework .beans .factory .annotation .Autowired ;
6+ import org .springframework .test .context .ActiveProfiles ;
7+ import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
8+
9+ @ SpringJUnitConfig (ProfilesConfig .class )
10+ @ ActiveProfiles ("rest" )
11+ public class TestRestProfile {
12+
13+ @ Autowired
14+ private DataSource dataSource ;
15+
16+ @ Test
17+ public void testDataSource (){
18+ Assert .assertNotNull (dataSource );
19+ Assert .assertEquals (dataSource .getClass (), RestDataSource .class );
20+ System .out .println (dataSource .getClass ());
21+ }
22+ }
You can’t perform that action at this time.
0 commit comments