Skip to content

Commit f250fd5

Browse files
committed
1 parent 214d4fd commit f250fd5

File tree

9 files changed

+137
-0
lines changed

9 files changed

+137
-0
lines changed

spring-core-environment-abstraction/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@
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>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.jsbd.profiles;
2+
3+
public interface DataSource {
4+
public String getName();
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

0 commit comments

Comments
 (0)