Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WFLY-3239 Tests for rebinding lookup and object-factory bindings #9273

Merged
merged 1 commit into from
Oct 18, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@
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.SIMPLE;
import static org.jboss.as.naming.subsystem.NamingSubsystemModel.TYPE;
import static org.jboss.as.naming.subsystem.NamingSubsystemModel.VALUE;
import static org.jboss.as.naming.subsystem.NamingSubsystemModel.REBIND;
import static org.jboss.as.test.shared.integration.ejb.security.PermissionUtils.createPermissionsXmlAsset;

import org.jboss.as.naming.NamingContext;

import java.net.SocketPermission;
import java.net.URL;
import javax.ejb.EJB;
Expand All @@ -61,7 +65,8 @@
import org.junit.runner.RunWith;

/**
* Test case for binding of {@link URL} (see AS7-5140). Uses AS controller to do the bind, lookup is through an EJB.
* 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
*/
Expand Down Expand Up @@ -98,91 +103,157 @@ public void testRebinding() throws Exception {

final String name = "java:global/rebind";
final String lookup = "java:global/lookup";
final ModelNode address = new ModelNode();
address.add(SUBSYSTEM, NamingExtension.SUBSYSTEM_NAME);
address.add(BINDING, name);


final ModelNode lookupAddress = new ModelNode();
lookupAddress.add(SUBSYSTEM, NamingExtension.SUBSYSTEM_NAME);
lookupAddress.add(BINDING, lookup);
// bind a URL
ModelNode operation = new ModelNode();
operation.get(OP).set(ADD);
operation.get(OP_ADDR).set(address);
operation.get(BINDING_TYPE).set(SIMPLE);
operation.get(VALUE).set("http://localhost");
operation.get(TYPE).set(URL.class.getName());
try {
ModelNode addResult = managementClient.getControllerClient().execute(operation);
Assert.assertFalse(addResult.get(FAILURE_DESCRIPTION).toString(), addResult.get(FAILURE_DESCRIPTION).isDefined());
Assert.assertEquals("http://localhost", bean.lookupBind(name).toString());

operation = new ModelNode();
operation.get(OP).set("rebind");
operation.get(OP_ADDR).set(address);
operation.get(BINDING_TYPE).set(SIMPLE);
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());
addResult = managementClient.getControllerClient().execute(operation);
Assert.assertFalse(addResult.get(FAILURE_DESCRIPTION).toString(), addResult.get(FAILURE_DESCRIPTION).isDefined());
Assert.assertEquals("http://localhost2", bean.lookupBind(name).toString());

operation = new ModelNode();
operation.get(OP).set("rebind");
operation.get(OP_ADDR).set(address);
operation.get(BINDING_TYPE).set(SIMPLE);
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");

addResult = managementClient.getControllerClient().execute(operation);
Assert.assertFalse(addResult.get(FAILURE_DESCRIPTION).toString(), addResult.get(FAILURE_DESCRIPTION).isDefined());
Assert.assertEquals(2, bean.lookupBind(name));

operation = new ModelNode();
operation.get(OP).set(ADD);
operation.get(OP_ADDR).set(lookupAddress);
operation.get(BINDING_TYPE).set(SIMPLE);
String lookedUp = "looked up";
operation.get(VALUE).set(lookedUp);
operation = prepareAddBindingOperation(lookup, SIMPLE);
operation.get(VALUE).set("looked up");
operation.get(TYPE).set(String.class.getName());
addResult = managementClient.getControllerClient().execute(operation);
Assert.assertFalse(addResult.get(FAILURE_DESCRIPTION).toString(), addResult.get(FAILURE_DESCRIPTION).isDefined());

operationResult = managementClient.getControllerClient().execute(operation);
verifyBinding(operationResult, lookup, "looked up");

operation = new ModelNode();
operation.get(OP).set("rebind");
operation.get(OP_ADDR).set(address);
operation.get(BINDING_TYPE).set(LOOKUP);
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());

} finally {
removeBinding(name);
removeBinding(lookup);
}
}

addResult = managementClient.getControllerClient().execute(operation);
Assert.assertFalse(addResult.get(FAILURE_DESCRIPTION).toString(), addResult.get(FAILURE_DESCRIPTION).isDefined());
Assert.assertEquals(lookedUp, bean.lookupBind(name));
@Test
public void testRebindingObjectFactory() throws Exception {

operation = new ModelNode();
operation.get(OP).set(READ_RESOURCE_OPERATION);
operation.get(OP_ADDR).set(address);
final String bindingName = "java:global/bind";

addResult = managementClient.getControllerClient().execute(operation);
Assert.assertFalse(addResult.get(FAILURE_DESCRIPTION).toString(), addResult.get(FAILURE_DESCRIPTION).isDefined());
Assert.assertEquals("java:global/lookup", addResult.get(RESULT).get(LOOKUP).asString());
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());

} finally {
// unbind it
final ModelNode bindingRemove = new ModelNode();
bindingRemove.get(OP).set(REMOVE);
bindingRemove.get(OP_ADDR).set(address);
bindingRemove.get(OPERATION_HEADERS).get(ALLOW_RESOURCE_SERVICE_RESTART).set(true);
ModelNode removeResult = managementClient.getControllerClient().execute(bindingRemove);
Assert.assertFalse(removeResult.get(FAILURE_DESCRIPTION).toString(), removeResult.get(FAILURE_DESCRIPTION)
.isDefined());

bindingRemove.get(OP_ADDR).set(lookupAddress);
removeResult = managementClient.getControllerClient().execute(bindingRemove);
Assert.assertFalse(removeResult.get(FAILURE_DESCRIPTION).toString(), removeResult.get(FAILURE_DESCRIPTION)
.isDefined());
removeBinding(bindingName);
}
}

@Test
public void testRebidingLookup() throws Exception {

final String simpleBindingName1 = "java:global/simple1";
final String simpleBindingName2 = "java:global/simple2";
final String lookupBindingName = "java:global/lookup";

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");

} finally {
removeBinding(simpleBindingName1);
removeBinding(simpleBindingName2);
removeBinding(lookupBindingName);
}
}

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) 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);
Assert.assertFalse(removeResult.get(FAILURE_DESCRIPTION).toString(), removeResult.get(FAILURE_DESCRIPTION)
.isDefined());
}

}