use of org.apache.aries.tx.control.itests.entity.Message in project aries by apache.
the class AbstractSimpleTransactionTest method testNestedTx.
@Test
public void testNestedTx() {
Message message = new Message();
message.message = "Hello World!";
Message message2 = new Message();
message2.message = "Hello Nested World!";
txControl.required(() -> {
em.persist(message);
txControl.requiresNew(() -> {
em.persist(message2);
return null;
});
return null;
});
List<Message> results = txControl.notSupported(() -> {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Message> query = cb.createQuery(Message.class);
query.orderBy(cb.asc(query.from(Message.class).get("message")));
return em.createQuery(query).getResultList();
});
System.out.println(results);
assertEquals(2, results.size());
assertEquals("Hello Nested World!", results.get(0).message);
assertEquals("Hello World!", results.get(1).message);
}
use of org.apache.aries.tx.control.itests.entity.Message in project aries by apache.
the class AbstractSimpleTransactionTest method testNestedTxOuterRollback.
@Test
public void testNestedTxOuterRollback() {
Message message = new Message();
message.message = "Hello World!";
Message message2 = new Message();
message2.message = "Hello Nested World!";
txControl.required(() -> {
// This will not end up in the database
em.persist(message);
// This should only apply to the current transaction level
txControl.setRollbackOnly();
// This nested transaction will commit
txControl.requiresNew(() -> {
em.persist(message2);
return null;
});
return null;
});
List<Message> results = txControl.notSupported(() -> {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Message> query = cb.createQuery(Message.class);
query.orderBy(cb.asc(query.from(Message.class).get("message")));
return em.createQuery(query).getResultList();
});
System.out.println(results);
assertEquals(1, results.size());
assertEquals("Hello Nested World!", results.get(0).message);
}
use of org.apache.aries.tx.control.itests.entity.Message in project aries by apache.
the class JPALifecycleTest method testDeleteOfConfig.
@Test
public void testDeleteOfConfig() throws Exception {
Message m = new Message();
m.message = "Hello World";
txControl.required(() -> {
em.persist(m);
return null;
});
assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
ConfigurationAdmin cm = getService(ConfigurationAdmin.class, 5000);
Configuration[] configurations = cm.listConfigurations("(service.factoryPid=org.apache.aries.tx.control.jpa.*)");
assertNotNull(configurations);
assertEquals(1, configurations.length);
configurations[0].delete();
Thread.sleep(2000);
try {
assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
fail("Should not be accessible " + (Boolean.getBoolean(IS_XA) ? "xa" : "local"));
} catch (ScopedWorkException swe) {
assertTrue(swe.getCause().toString(), swe.getCause() instanceof TransactionException);
assertEquals("There was a problem getting hold of a database connection", swe.getCause().getMessage());
}
}
use of org.apache.aries.tx.control.itests.entity.Message in project aries by apache.
the class JPALifecycleTest method testUpdateOfConfig.
@Test
public void testUpdateOfConfig() throws Exception {
Message m = new Message();
m.message = "Hello World";
txControl.required(() -> {
em.persist(m);
return null;
});
assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
ConfigurationAdmin cm = getService(ConfigurationAdmin.class, 5000);
Configuration[] configurations = cm.listConfigurations("(service.factoryPid=org.apache.aries.tx.control.jpa.*)");
assertNotNull(configurations);
assertEquals(1, configurations.length);
configurations[0].update();
Thread.sleep(2000);
try {
assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
fail("Should not be accessible " + (Boolean.getBoolean(IS_XA) ? "xa" : "local"));
} catch (ScopedWorkException swe) {
assertTrue(swe.getCause().toString(), swe.getCause() instanceof TransactionException);
assertEquals("There was a problem getting hold of a database connection", swe.getCause().getMessage());
}
}
use of org.apache.aries.tx.control.itests.entity.Message in project aries by apache.
the class JPALifecycleTest method doBundleStoppingTest.
private void doBundleStoppingTest(Predicate<Bundle> p, String exceptionMessage) {
Message m = new Message();
m.message = "Hello World";
txControl.required(() -> {
em.persist(m);
return null;
});
assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
List<Bundle> toStop = Arrays.stream(context.getBundles()).filter(p).collect(toList());
System.out.println(toStop);
try {
toStop.stream().forEach(b -> {
System.out.println("Stopping " + b.getSymbolicName());
try {
b.stop();
} catch (BundleException e) {
}
});
try {
assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
fail("Should not be accessible " + (Boolean.getBoolean(IS_XA) ? "xa" : "local"));
} catch (ScopedWorkException swe) {
assertTrue(swe.getCause().toString(), swe.getCause() instanceof TransactionException);
assertEquals(exceptionMessage, swe.getCause().getMessage());
} catch (TransactionException te) {
assertEquals(exceptionMessage, te.getMessage());
}
} finally {
toStop.stream().forEach(b -> {
System.out.println("Restarting " + b.getSymbolicName());
try {
b.start();
} catch (BundleException e) {
}
});
getService(JPAEntityManagerProvider.class, 5000);
}
}
Aggregations