forked from eclipse-ee4j/jersey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientInstanceInjectionTest.java
226 lines (179 loc) · 7.45 KB
/
ClientInstanceInjectionTest.java
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.inject.weld.internal.managed;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Vetoed;
import javax.inject.Inject;
import javax.ws.rs.RuntimeType;
import org.glassfish.jersey.inject.weld.managed.CdiInjectionManagerFactory;
import org.glassfish.jersey.internal.inject.InjectionManager;
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
public class ClientInstanceInjectionTest extends TestParent {
@Test
public void testInject() {
InjectionManager clientInjectionManager1 = new CdiInjectionManagerFactory().create(null, RuntimeType.CLIENT);
InjectionManager clientInjectionManager2 = new CdiInjectionManagerFactory().create(null, RuntimeType.CLIENT);
BindingTestHelper.bind(injectionManager, binder -> {
binder.bind(new StringInjectable(5)).to(Injectable.class);
});
BindingTestHelper.bind(clientInjectionManager1, binder -> {
binder.bind(new StringInjectable(10)).to(Injectable.class);
});
BindingTestHelper.bind(clientInjectionManager2, binder -> {
binder.bind(new StringInjectable(15)).to(Injectable.class);
});
InjectedBean bean = injectionManager.getInstance(InjectedBean.class);
Assert.assertNotNull(bean);
Assert.assertEquals("6", bean.get());
InjectedBean bean1 = clientInjectionManager1.getInstance(InjectedBean.class);
Assert.assertNotNull(bean1);
Assert.assertEquals("11", bean1.get());
InjectedBean bean2 = clientInjectionManager2.getInstance(InjectedBean.class);
Assert.assertNotNull(bean2);
Assert.assertEquals("16", bean2.get());
injectionManager.shutdown();
clientInjectionManager1.shutdown();
clientInjectionManager2.shutdown();
}
@Vetoed
static class InjectedBean {
@Inject
private Injectable injectable;
public String get() {
return injectable.get();
}
}
static class StringInjectable extends AbstractStringInjectable implements Injectable {
StringInjectable(int i) {
super(i);
}
}
static interface Injectable {
String get();
}
@Test
public void testSupplierInject() {
InjectionManager clientInjectionManager1 = new CdiInjectionManagerFactory().create(null, RuntimeType.CLIENT);
InjectionManager clientInjectionManager2 = new CdiInjectionManagerFactory().create(null, RuntimeType.CLIENT);
BindingTestHelper.bind(injectionManager, binder -> {
binder.bindFactory(new StringInjectableSupplier2(5)).to(Injectable2.class);
});
BindingTestHelper.bind(clientInjectionManager1, binder -> {
binder.bindFactory(new StringInjectableSupplier2(10)).to(Injectable2.class);
});
BindingTestHelper.bind(clientInjectionManager2, binder -> {
binder.bindFactory(new StringInjectableSupplier2(15)).to(Injectable2.class);
});
InjectedSupplierBean bean = injectionManager.getInstance(InjectedSupplierBean.class);
Assert.assertNotNull(bean);
Assert.assertEquals("6", bean.get());
InjectedSupplierBean bean1 = clientInjectionManager1.getInstance(InjectedSupplierBean.class);
Assert.assertNotNull(bean1);
Assert.assertEquals("11", bean1.get());
InjectedSupplierBean bean2 = clientInjectionManager2.getInstance(InjectedSupplierBean.class);
Assert.assertNotNull(bean2);
Assert.assertEquals("16", bean2.get());
}
@Dependent
static class InjectedSupplierBean {
@Inject
private Supplier<Injectable2> injectable;
public String get() {
return injectable.get().get();
}
}
static class StringInjectableSupplier2 implements Supplier<Injectable2> {
private final int i;
StringInjectableSupplier2(int i) {
this.i = i;
}
@Override
public Injectable2 get() {
return new StringInjectable2(i);
}
}
private static class StringInjectable2 extends AbstractStringInjectable implements Injectable2 {
StringInjectable2(int value) {
super(value);
}
}
static interface Injectable2 {
String get();
}
abstract static class AbstractStringInjectable {
protected AtomicInteger atomicInteger = new AtomicInteger(0);
AbstractStringInjectable(int value) {
atomicInteger.set(value);
}
public String get() {
return String.valueOf(atomicInteger.incrementAndGet());
}
}
@Test
public void testSupplierClassBeanOnClientAndServer() {
InjectionManager clientInjectionManager = new CdiInjectionManagerFactory().create(null, RuntimeType.CLIENT);
BindingTestHelper.bind(injectionManager, binder -> {
binder.bindFactory(InjectableClientServerSupplierServer.class).to(InjectableClientServer.class);
});
BindingTestHelper.bind(clientInjectionManager, binder -> {
binder.bindFactory(InjectableClientServerSupplierClient.class).to(InjectableClientServer.class);
});
InjectedClientServerSupplierBean beanServer = injectionManager.getInstance(InjectedClientServerSupplierBean.class);
Assert.assertNotNull(beanServer);
Assert.assertEquals("SERVER", beanServer.get());
InjectedClientServerSupplierBean beanClient = clientInjectionManager.getInstance(InjectedClientServerSupplierBean.class);
Assert.assertNotNull(beanClient);
Assert.assertEquals("CLIENT", beanClient.get());
}
@Dependent
static class InjectedClientServerSupplierBean {
@Inject
private Supplier<InjectableClientServer> injectable;
public String get() {
return injectable.get().get();
}
}
static class InjectableClientServerSupplierServer implements Supplier<InjectableClientServer> {
@Override
public InjectableClientServer get() {
return new InjectableClientServerServer();
}
}
static class InjectableClientServerSupplierClient implements Supplier<InjectableClientServer> {
@Override
public InjectableClientServer get() {
return new InjectableClientServerClient();
}
}
private static class InjectableClientServerServer implements InjectableClientServer {
@Override
public String get() {
return "SERVER";
}
}
private static class InjectableClientServerClient implements InjectableClientServer {
@Override
public String get() {
return "CLIENT";
}
}
static interface InjectableClientServer {
String get();
}
}