-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimpleCdiInstanceAdapterTests.java
More file actions
137 lines (105 loc) · 3.72 KB
/
SimpleCdiInstanceAdapterTests.java
File metadata and controls
137 lines (105 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package demo;
import static demo.QualifierUtils.qualifier;
import static org.assertj.core.api.Assertions.assertThat;
import javax.enterprise.inject.Instance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import demo.SimpleCdiInstanceAdapterTests.Config;
import demo.greet.CountryStyle;
import demo.greet.CountryStyleGreeter;
import demo.greet.DefaultGreeter;
import demo.greet.Formal;
import demo.greet.FormalGreeter;
import demo.greet.Greeter;
import demo.greet.HighestPriorityGreeter;
import demo.greet.Official;
import demo.greet.PriorityGreeter;
import demo.greet.SimpleGreeter;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { CdiInstanceExampleApp.class, Config.class }, initializers = CustomApplicationContextInitializer.class)
public class SimpleCdiInstanceAdapterTests {
public static class Config {
@Bean
public Greeter simpleGreeter() {
return new SimpleGreeter();
}
@Bean
public Greeter formalGreater() {
return new FormalGreeter();
}
@Bean
public Greeter prioGreeter() {
return new PriorityGreeter();
}
@Bean
public Greeter highestPrioGreeter() {
return new HighestPriorityGreeter();
}
@Primary
// @Default
@Bean
public Greeter defaultGreeter() {
return new DefaultGreeter();
}
@Bean
public Greeter countryStyleGreeter() {
return new CountryStyleGreeter();
}
}
@Autowired
private Instance<Greeter> greeters;
@Autowired
@Official
private Instance<Greeter> officialGreeters;
@Autowired
private ListableBeanFactory beanFactory;
@Test
public void initializeGreeter() throws Exception {
assertThat(greeters).isNotNull();
}
@Test
public void returnedDefaultInstanceIsPrimaryOrDefault() throws Exception {
assertThat(greeters.get()).isInstanceOf(DefaultGreeter.class);
}
@Test
public void returnFormalGreeter() throws Exception {
Instance<Greeter> formalGreeters = greeters.select(qualifier(Formal.class));
assertThat(formalGreeters.isUnsatisfied()).isFalse();
assertThat(formalGreeters.isAmbiguous()).isFalse();
assertThat(formalGreeters.get()).isInstanceOf(FormalGreeter.class);
}
@Test
public void returnCountryStyleGreeter() throws Exception {
assertThat(greeters.select(qualifier(CountryStyle.class)).get()).isInstanceOf(CountryStyleGreeter.class);
}
@Test
public void listInstances() throws Exception {
System.out.println("List instances:");
greeters.iterator().forEachRemaining(System.out::println);
assertThat(greeters.iterator()).hasSize(beanFactory.getBeansOfType(Greeter.class).size());
}
@Test
public void ambiguousMatch() throws Exception {
Instance<Greeter> officalGreeters = greeters.select(qualifier(Official.class));
assertThat(officalGreeters.isUnsatisfied()).isFalse();
assertThat(officalGreeters.isAmbiguous()).isTrue();
assertThat(officalGreeters.get()).isInstanceOf(Greeter.class); //first match
}
@Test
public void matchMultipleQualifiers() throws Exception {
Instance<Greeter> officalFormalGreeters = greeters.select(qualifier(Official.class), qualifier(Formal.class));
assertThat(officalFormalGreeters.isUnsatisfied()).isFalse();
assertThat(officalFormalGreeters.isAmbiguous()).isFalse();
assertThat(officalFormalGreeters.get()).isInstanceOf(FormalGreeter.class); //first match
}
@Test
public void matchOnlyOfficialGreeters() {
assertThat(officialGreeters.iterator()).hasSize(2);
}
}