use of org.jvnet.hk2.config.TransactionFailure in project Payara by payara.
the class ParentConfigListenerTest method addHttpListenerTest.
@Test
public void addHttpListenerTest() throws TransactionFailure {
NetworkListenersContainer container = habitat.getService(NetworkListenersContainer.class);
ConfigSupport.apply(new SingleConfigCode<NetworkListeners>() {
public Object run(NetworkListeners param) throws TransactionFailure {
NetworkListener newListener = param.createChild(NetworkListener.class);
newListener.setName("Funky-Listener");
newListener.setPort("8078");
param.getNetworkListener().add(newListener);
return null;
}
}, container.httpService);
getHabitat().<Transactions>getService(Transactions.class).waitForDrain();
assertTrue(container.received);
ObservableBean bean = (ObservableBean) ConfigSupport.getImpl(container.httpService);
// let's check that my newly added listener is available in the habitat.
List<ServiceHandle<NetworkListener>> networkListeners = habitat.getAllServiceHandles(NetworkListener.class);
boolean found = false;
for (ServiceHandle<NetworkListener> nlSH : networkListeners) {
NetworkListener nl = (NetworkListener) nlSH.getService();
if (nl.getName().equals("Funky-Listener")) {
found = true;
}
}
Assert.assertTrue("Newly added listener not found", found);
// direct access.
NetworkListener nl = habitat.getService(NetworkListener.class, "Funky-Listener");
Assert.assertTrue("Direct access to newly added listener failed", nl != null);
bean.removeListener(container);
}
use of org.jvnet.hk2.config.TransactionFailure in project Payara by payara.
the class TransactionCallBackTest method doTest.
public void doTest() throws TransactionFailure {
ConfigBean serviceBean = (ConfigBean) ConfigBean.unwrap(habitat.<NetworkListeners>getService(NetworkListeners.class));
Map<String, String> configChanges = new HashMap<String, String>();
configChanges.put("name", "funky-listener");
ConfigSupport.createAndSet(serviceBean, NetworkListener.class, configChanges, new TransactionCallBack<WriteableView>() {
@SuppressWarnings({ "unchecked" })
public void performOn(WriteableView param) throws TransactionFailure {
// if you know the type...
NetworkListener listener = param.getProxy(NetworkListener.class);
listener.setName("Aleksey");
// if you don't know the type
Method m;
try {
m = param.getProxyType().getMethod("setAddress", String.class);
m.invoke(param.getProxy(param.getProxyType()), "localhost");
} catch (NoSuchMethodException e) {
throw new TransactionFailure("Cannot find getProperty method", e);
} catch (IllegalAccessException e) {
throw new TransactionFailure("Cannot call getProperty method", e);
} catch (InvocationTargetException e) {
throw new TransactionFailure("Cannot call getProperty method", e);
}
}
});
}
use of org.jvnet.hk2.config.TransactionFailure in project Payara by payara.
the class FieldsValidationTest method testNotNullField.
@Test
public void testNotNullField() {
AdminService admin = super.getHabitat().getService(AdminService.class);
Assert.assertNotNull(admin);
try {
ConfigSupport.apply(new SingleConfigCode<AdminService>() {
@Override
public Object run(AdminService wAdmin) throws PropertyVetoException, TransactionFailure {
wAdmin.setDasConfig(null);
return null;
}
}, admin);
Assert.fail("Exception not raised when setting a @NotNull annotated field with null");
} catch (TransactionFailure e) {
if (e.getCause() != null) {
Assert.assertTrue(e.getCause() instanceof ConstraintViolationException);
}
}
}
use of org.jvnet.hk2.config.TransactionFailure in project Payara by payara.
the class ReferenceConstrainClusterTest method clusterServerRefInvalid.
@Test
public void clusterServerRefInvalid() throws TransactionFailure {
Cluster cluster = habitat.getService(Cluster.class, "clusterA");
assertNotNull(cluster);
ServerRef sref = cluster.getServerRef().get(0);
ConfigBean serverConfig = (ConfigBean) ConfigBean.unwrap(sref);
Map<ConfigBean, Map<String, String>> changes = new HashMap<ConfigBean, Map<String, String>>();
Map<String, String> configChanges = new HashMap<String, String>();
configChanges.put("ref", "server-nonexist");
changes.put(serverConfig, configChanges);
try {
ConfigSupport cs = getHabitat().getService(ConfigSupport.class);
cs.apply(changes);
fail("Can not reach this point");
} catch (TransactionFailure tf) {
ConstraintViolationException cv = findConstrViolation(tf);
assertNotNull(cv);
}
}
use of org.jvnet.hk2.config.TransactionFailure in project Payara by payara.
the class ReferenceConstrainClusterTest method clusterServerRefValid.
@Test
public void clusterServerRefValid() throws TransactionFailure {
Cluster cluster = habitat.getService(Cluster.class, "clusterA");
assertNotNull(cluster);
ServerRef sref = cluster.getServerRef().get(0);
ConfigBean serverConfig = (ConfigBean) ConfigBean.unwrap(sref);
Map<ConfigBean, Map<String, String>> changes = new HashMap<ConfigBean, Map<String, String>>();
Map<String, String> configChanges = new HashMap<String, String>();
configChanges.put("ref", "server");
changes.put(serverConfig, configChanges);
try {
ConfigSupport cs = getHabitat().getService(ConfigSupport.class);
cs.apply(changes);
} catch (TransactionFailure tf) {
fail("Can not reach this point");
}
}
Aggregations