/
RebindTestCase.java
275 lines (239 loc) · 12.9 KB
/
RebindTestCase.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
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.test.integration.naming;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ADD;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ALLOW_RESOURCE_SERVICE_RESTART;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILURE_DESCRIPTION;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OPERATION_HEADERS;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.READ_RESOURCE_OPERATION;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.REMOVE;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RESULT;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUBSYSTEM;
import static org.jboss.as.naming.subsystem.NamingSubsystemModel.BINDING;
import static org.jboss.as.naming.subsystem.NamingSubsystemModel.BINDING_TYPE;
import static org.jboss.as.naming.subsystem.NamingSubsystemModel.LOOKUP;
import static org.jboss.as.naming.subsystem.NamingSubsystemModel.OBJECT_FACTORY;
import static org.jboss.as.naming.subsystem.NamingSubsystemModel.REBIND;
import static org.jboss.as.naming.subsystem.NamingSubsystemModel.SIMPLE;
import static org.jboss.as.naming.subsystem.NamingSubsystemModel.TYPE;
import static org.jboss.as.naming.subsystem.NamingSubsystemModel.VALUE;
import static org.jboss.as.test.shared.integration.ejb.security.PermissionUtils.createPermissionsXmlAsset;
import java.net.SocketPermission;
import java.net.URL;
import javax.ejb.EJB;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.as.arquillian.container.ManagementClient;
import org.jboss.as.naming.NamingContext;
import org.jboss.as.naming.subsystem.NamingExtension;
import org.jboss.as.test.shared.TestSuiteEnvironment;
import org.jboss.dmr.ModelNode;
import org.jboss.remoting3.security.RemotingPermission;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Test case for changing JBDI bound values in the naming subsystem without forcing a reload/restart (see WFLY-3239).
* Uses AS controller to do the bind/rebind, lookup is through an EJB.
*
* @author Stuart Douglas
*/
@RunWith(Arquillian.class)
public class RebindTestCase {
@ArquillianResource
private ManagementClient managementClient;
@EJB(mappedName = "java:global/RebindTestCase/BindingLookupBean")
private BindingLookupBean bean;
@Deployment
public static Archive<?> deploy() {
JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "RebindTestCase.jar");
jar.addClasses(RebindTestCase.class, BindingLookupBean.class);
jar.addAsManifestResource(new StringAsset("Dependencies: org.jboss.as.controller, " +
"org.jboss.remoting3\n"
), "MANIFEST.MF");
jar.addAsManifestResource(createPermissionsXmlAsset(
new RemotingPermission("addConnectionProvider"),
new RemotingPermission("connect"),
new RemotingPermission("createEndpoint"),
new RuntimePermission("createXnioWorker"),
new SocketPermission(TestSuiteEnvironment.getServerAddress(), "connect,resolve")
), "permissions.xml");
return jar;
}
@Test
public void testRebinding() throws Exception {
final String name = "java:global/rebind";
final String lookup = "java:global/lookup";
Exception error = null;
try {
ModelNode operation = prepareAddBindingOperation(name, SIMPLE);
operation.get(VALUE).set("http://localhost");
operation.get(TYPE).set(URL.class.getName());
ModelNode operationResult = managementClient.getControllerClient().execute(operation);
verifyBinding(operationResult, name, "http://localhost");
operation = prepareRebindOperation(name, SIMPLE);
operation.get(VALUE).set("http://localhost2");
operation.get(TYPE).set(URL.class.getName());
operationResult = managementClient.getControllerClient().execute(operation);
verifyBinding(operationResult, name, "http://localhost2");
operation = prepareRebindOperation(name, SIMPLE);
operation.get(VALUE).set("2");
operation.get(TYPE).set(Integer.class.getName());
operationResult = managementClient.getControllerClient().execute(operation);
verifyBinding(operationResult, name, "2");
operation = prepareAddBindingOperation(lookup, SIMPLE);
operation.get(VALUE).set("looked up");
operation.get(TYPE).set(String.class.getName());
operationResult = managementClient.getControllerClient().execute(operation);
verifyBinding(operationResult, lookup, "looked up");
operation = prepareRebindOperation(name, LOOKUP);
operation.get(LOOKUP).set(lookup);
operationResult = managementClient.getControllerClient().execute(operation);
verifyBinding(operationResult, name, "looked up");
operation = prepareReadResourceOperation(name);
operationResult = managementClient.getControllerClient().execute(operation);
Assert.assertFalse(operationResult.get(FAILURE_DESCRIPTION).toString(), operationResult.get(FAILURE_DESCRIPTION).isDefined());
Assert.assertEquals("java:global/lookup", operationResult.get(RESULT).get(LOOKUP).asString());
} catch (Exception e) {
error = e;
throw e;
} finally {
removeBinding(name, error);
removeBinding(lookup, error);
}
}
@Test
public void testRebindingObjectFactory() throws Exception {
final String bindingName = "java:global/bind";
Exception error = null;
try {
ModelNode operation = prepareAddBindingOperation(bindingName, SIMPLE);
operation.get(VALUE).set("2");
operation.get(TYPE).set(Integer.class.getName());
ModelNode operationResult = managementClient.getControllerClient().execute(operation);
verifyBindingClass(operationResult, bindingName, Integer.class.getName());
operation = prepareRebindOperation(bindingName, OBJECT_FACTORY);
operation.get("module").set("org.jboss.as.naming");
operation.get("class").set("org.jboss.as.naming.interfaces.java.javaURLContextFactory");
operationResult = managementClient.getControllerClient().execute(operation);
verifyBindingClass(operationResult, bindingName, NamingContext.class.getName());
} catch (Exception e) {
error = e;
throw e;
} finally {
removeBinding(bindingName, error);
}
}
@Test
public void testRebindingLookup() throws Exception {
final String simpleBindingName1 = "java:global/simple1";
final String simpleBindingName2 = "java:global/simple2";
final String lookupBindingName = "java:global/lookup";
Exception error = null;
try {
ModelNode operation = prepareAddBindingOperation(simpleBindingName1, SIMPLE);
operation.get(VALUE).set("simple1");
operation.get(TYPE).set(String.class.getName());
ModelNode operationResult = managementClient.getControllerClient().execute(operation);
verifyBinding(operationResult, simpleBindingName1, "simple1");
operation = prepareAddBindingOperation(simpleBindingName2, SIMPLE);
operation.get(VALUE).set("simple2");
operation.get(TYPE).set(String.class.getName());
operationResult = managementClient.getControllerClient().execute(operation);
verifyBinding(operationResult, simpleBindingName2, "simple2");
operation = prepareAddBindingOperation(lookupBindingName, LOOKUP);
operation.get(LOOKUP).set(simpleBindingName1);
operationResult = managementClient.getControllerClient().execute(operation);
verifyBinding(operationResult, lookupBindingName, "simple1");
operation = prepareRebindOperation(lookupBindingName, LOOKUP);
operation.get(LOOKUP).set(simpleBindingName2);
operationResult = managementClient.getControllerClient().execute(operation);
verifyBinding(operationResult, lookupBindingName, "simple2");
} catch (Exception e) {
error = e;
throw e;
} finally {
removeBinding(simpleBindingName1, error);
removeBinding(simpleBindingName2, error);
removeBinding(lookupBindingName, error);
}
}
private void verifyBinding(ModelNode result, String bindingName, String bindingValue) throws Exception {
Assert.assertFalse(result.get(FAILURE_DESCRIPTION).toString(), result.get(FAILURE_DESCRIPTION).isDefined());
Assert.assertEquals(bindingValue, bean.lookupBind(bindingName).toString());
}
private void verifyBindingClass(ModelNode result, String bindingName, String bindingClassName) throws Exception {
Class bindingClass = Class.forName(bindingClassName);
Assert.assertFalse(result.get(FAILURE_DESCRIPTION).toString(), result.get(FAILURE_DESCRIPTION).isDefined());
Assert.assertTrue(bindingClass.isInstance(bean.lookupBind(bindingName)));
}
private ModelNode prepareAddBindingOperation(String bindingName, String bindingType) {
ModelNode operation = new ModelNode();
operation.get(OP).set(ADD);
operation.get(OP_ADDR).set(getBindingAddress(bindingName));
operation.get(BINDING_TYPE).set(bindingType);
return operation;
}
private ModelNode prepareRebindOperation(String bindingName, String bindingType) {
ModelNode operation = new ModelNode();
operation.get(OP).set(REBIND);
operation.get(OP_ADDR).set(getBindingAddress(bindingName));
operation.get(BINDING_TYPE).set(bindingType);
return operation;
}
private ModelNode prepareReadResourceOperation(String bindingName) {
ModelNode operation = new ModelNode();
operation.get(OP).set(READ_RESOURCE_OPERATION);
operation.get(OP_ADDR).set(getBindingAddress(bindingName));
return operation;
}
private ModelNode getBindingAddress(String bindingName) {
ModelNode bindingAddress = new ModelNode();
bindingAddress.add(SUBSYSTEM, NamingExtension.SUBSYSTEM_NAME);
bindingAddress.add(BINDING, bindingName);
return bindingAddress;
}
private void removeBinding (String bindingName, Exception testException) throws Exception {
ModelNode removeOperation = new ModelNode();
removeOperation.get(OP).set(REMOVE);
removeOperation.get(OP_ADDR).set(getBindingAddress(bindingName));
removeOperation.get(OPERATION_HEADERS).get(ALLOW_RESOURCE_SERVICE_RESTART).set(true);
ModelNode removeResult = managementClient.getControllerClient().execute(removeOperation);
if (testException == null) {
//Only error here if the test was successful
Assert.assertFalse(removeResult.get(FAILURE_DESCRIPTION).toString(), removeResult.get(FAILURE_DESCRIPTION)
.isDefined());
} else {
if (removeResult.get(FAILURE_DESCRIPTION).isDefined()) {
throw new Exception(removeResult.get(FAILURE_DESCRIPTION) +
" - there was an exisiting exception in the test, it is added as the cause", testException);
}
}
}
}