/
ConnectorProxyTest.java
298 lines (240 loc) · 9.52 KB
/
ConnectorProxyTest.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.openejb.config;
import org.apache.openejb.jee.ConnectionDefinition;
import org.apache.openejb.jee.Connector;
import org.apache.openejb.jee.OutboundResourceAdapter;
import org.apache.openejb.jee.ResourceAdapter;
import org.apache.openejb.junit.ApplicationComposer;
import org.apache.openejb.loader.SystemInstance;
import org.apache.openejb.spi.ContainerSystem;
import org.apache.openejb.testing.Module;
import org.apache.openejb.testing.SimpleLog;
import org.junit.Test;
import org.junit.runner.RunWith;
import javax.naming.NamingException;
import javax.naming.Reference;
import javax.resource.ResourceException;
import javax.resource.cci.Connection;
import javax.resource.cci.ConnectionFactory;
import javax.resource.cci.ConnectionMetaData;
import javax.resource.cci.ConnectionSpec;
import javax.resource.cci.Interaction;
import javax.resource.cci.RecordFactory;
import javax.resource.cci.ResourceAdapterMetaData;
import javax.resource.cci.ResultSetInfo;
import javax.resource.spi.ActivationSpec;
import javax.resource.spi.BootstrapContext;
import javax.resource.spi.ConnectionEventListener;
import javax.resource.spi.ConnectionManager;
import javax.resource.spi.ConnectionRequestInfo;
import javax.resource.spi.LocalTransaction;
import javax.resource.spi.ManagedConnection;
import javax.resource.spi.ManagedConnectionFactory;
import javax.resource.spi.ManagedConnectionMetaData;
import javax.resource.spi.ResourceAdapterInternalException;
import javax.resource.spi.endpoint.MessageEndpointFactory;
import javax.security.auth.Subject;
import javax.transaction.xa.XAResource;
import java.io.PrintWriter;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
// dev note: the RA impl is very suspicious but it tests the fact we subclass connections
@SimpleLog
@RunWith(ApplicationComposer.class)
public class ConnectorProxyTest {
@Module
public Connector connector() {
final ConnectionDefinition connectionDefinition = new ConnectionDefinition();
connectionDefinition.setId("cf");
connectionDefinition.setConnectionImplClass(MyCon.class.getName());
connectionDefinition.setConnectionInterface(MyConAPI.class.getName());
connectionDefinition.setConnectionFactoryImplClass(MyMcf.class.getName());
connectionDefinition.setConnectionFactoryInterface(ConnectionFactory.class.getName());
connectionDefinition.setManagedConnectionFactoryClass(MyMcf.class.getName());
final OutboundResourceAdapter out = new OutboundResourceAdapter();
out.getConnectionDefinition().add(connectionDefinition);
final ResourceAdapter ra = new ResourceAdapter();
ra.setResourceAdapterClass(MyRa.class.getName());
ra.setOutboundResourceAdapter(out);
final Connector connector = new Connector();
connector.setVersion("1.7");
connector.setResourceAdapter(ra);
return connector;
}
@Test
public void run() throws NamingException, ResourceException {
final MyCf jndi = MyCf.class.cast(SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext().lookup("openejb:Resource/cf"));
assertNotNull(jndi);
final Connection connection = jndi.getConnection();
assertTrue(MyConAPI.class.isInstance(connection));
assertTrue(MyCon.class.isInstance(connection));
final MyCon myCon = MyCon.class.cast(connection);
assertEquals("yes", myCon.specific());
}
public static class MyRa implements javax.resource.spi.ResourceAdapter {
@Override
public void start(final BootstrapContext ctx) throws ResourceAdapterInternalException {
// no-op
}
@Override
public void stop() {
// no-op
}
@Override
public void endpointActivation(final MessageEndpointFactory endpointFactory, final ActivationSpec spec) throws ResourceException {
// no-op
}
@Override
public void endpointDeactivation(final MessageEndpointFactory endpointFactory, final ActivationSpec spec) {
// no-op
}
@Override
public XAResource[] getXAResources(final ActivationSpec[] specs) throws ResourceException {
return new XAResource[0];
}
}
public static class MyCf implements ConnectionFactory {
private final ConnectionManager mgr;
private final ManagedConnectionFactory mcf;
public MyCf(final MyMcf myMcf, final ConnectionManager cxManager) {
this.mcf = myMcf;
this.mgr = cxManager;
}
@Override
public Connection getConnection() throws ResourceException {
return MyCon.class/*impl, this is what we want to test*/.cast(mgr.allocateConnection(mcf, new ConnectionRequestInfo() {
}));
}
@Override
public Connection getConnection(ConnectionSpec properties) throws ResourceException {
return getConnection();
}
@Override
public RecordFactory getRecordFactory() throws ResourceException {
return null;
}
@Override
public ResourceAdapterMetaData getMetaData() throws ResourceException {
return null;
}
@Override
public void setReference(Reference reference) {
}
@Override
public Reference getReference() throws NamingException {
return null;
}
}
public static class MyMcf implements ManagedConnectionFactory {
@Override
public Object createConnectionFactory(final ConnectionManager cxManager) throws ResourceException {
return new MyCf(this, cxManager);
}
@Override
public Object createConnectionFactory() throws ResourceException {
return new MyCf(this, null);
}
@Override
public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException {
return new MyMC();
}
@Override
public ManagedConnection matchManagedConnections(Set connectionSet, Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException {
return null;
}
@Override
public void setLogWriter(PrintWriter out) throws ResourceException {
}
@Override
public PrintWriter getLogWriter() throws ResourceException {
return null;
}
}
public interface MyConAPI extends Connection {
}
public static class MyCon implements MyConAPI {
private final String arg;
public MyCon(String arg) {
this.arg = arg;
}
public String specific() {
return arg;
}
@Override
public Interaction createInteraction() throws ResourceException {
return null;
}
@Override
public javax.resource.cci.LocalTransaction getLocalTransaction() throws ResourceException {
return null;
}
@Override
public ConnectionMetaData getMetaData() throws ResourceException {
return null;
}
@Override
public ResultSetInfo getResultSetInfo() throws ResourceException {
return null;
}
@Override
public void close() throws ResourceException {
}
}
public static class MyMC implements ManagedConnection {
@Override
public Object getConnection(final Subject subject, final ConnectionRequestInfo cxRequestInfo) throws ResourceException {
return new MyCon("yes");
}
@Override
public void destroy() throws ResourceException {
}
@Override
public void cleanup() throws ResourceException {
}
@Override
public void associateConnection(Object connection) throws ResourceException {
}
@Override
public void addConnectionEventListener(ConnectionEventListener listener) {
}
@Override
public void removeConnectionEventListener(ConnectionEventListener listener) {
}
@Override
public XAResource getXAResource() throws ResourceException {
return null;
}
@Override
public LocalTransaction getLocalTransaction() throws ResourceException {
return null;
}
@Override
public ManagedConnectionMetaData getMetaData() throws ResourceException {
return null;
}
@Override
public void setLogWriter(PrintWriter out) throws ResourceException {
}
@Override
public PrintWriter getLogWriter() throws ResourceException {
return null;
}
}
}