Search in sources :

Example 1 with Message

use of org.apache.aries.tx.control.itests.entity.Message in project aries by apache.

the class AbstractSimpleTransactionTest method testRequiredInheritsTx.

@Test
public void testRequiredInheritsTx() {
    Message message = new Message();
    message.message = "Hello World!";
    Message message2 = new Message();
    message2.message = "Hello Nested World!";
    txControl.required(() -> {
        em.persist(message);
        txControl.required(() -> {
            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);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Message(org.apache.aries.tx.control.itests.entity.Message) Test(org.junit.Test)

Example 2 with Message

use of org.apache.aries.tx.control.itests.entity.Message in project aries by apache.

the class AbstractSimpleTransactionTest method testNestedTxInnerRollback.

@Test
public void testNestedTxInnerRollback() {
    Message message = new Message();
    message.message = "Hello World!";
    Message message2 = new Message();
    message2.message = "Hello Nested World!";
    txControl.required(() -> {
        // This will end up in the database
        em.persist(message);
        // This nested transaction will not commit
        txControl.requiresNew(() -> {
            em.persist(message2);
            txControl.setRollbackOnly();
            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 World!", results.get(0).message);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Message(org.apache.aries.tx.control.itests.entity.Message) Test(org.junit.Test)

Example 3 with Message

use of org.apache.aries.tx.control.itests.entity.Message in project aries by apache.

the class AbstractSimpleTransactionTest method testTx.

@Test
public void testTx() {
    Message message = new Message();
    message.message = "Hello World!";
    txControl.required(() -> {
        em.persist(message);
        return null;
    });
    assertEquals("Hello World!", txControl.notSupported(() -> {
        return em.find(Message.class, message.id).message;
    }));
}
Also used : Message(org.apache.aries.tx.control.itests.entity.Message) Test(org.junit.Test)

Example 4 with Message

use of org.apache.aries.tx.control.itests.entity.Message in project aries by apache.

the class AbstractSimpleTransactionTest method testSuspendedTx.

@Test
public void testSuspendedTx() {
    Message message = new Message();
    message.message = "Hello World!";
    txControl.required(() -> {
        em.persist(message);
        assertEquals(Long.valueOf(0), txControl.notSupported(() -> {
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
            countQuery.select(cb.count(countQuery.from(Message.class)));
            return em.createQuery(countQuery).getSingleResult();
        }));
        return null;
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Message(org.apache.aries.tx.control.itests.entity.Message) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Test(org.junit.Test)

Example 5 with Message

use of org.apache.aries.tx.control.itests.entity.Message in project aries by apache.

the class AbstractSimpleTransactionTest method testRollback.

@Test
public void testRollback() {
    Message message = new Message();
    message.message = "Hello World!";
    txControl.required(() -> {
        em.persist(message);
        txControl.setRollbackOnly();
        return null;
    });
    assertEquals(Long.valueOf(0), txControl.notSupported(() -> {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
        countQuery.select(cb.count(countQuery.from(Message.class)));
        return em.createQuery(countQuery).getSingleResult();
    }));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Message(org.apache.aries.tx.control.itests.entity.Message) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Test(org.junit.Test)

Aggregations

Message (org.apache.aries.tx.control.itests.entity.Message)10 Test (org.junit.Test)9 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)6 ScopedWorkException (org.osgi.service.transaction.control.ScopedWorkException)3 TransactionException (org.osgi.service.transaction.control.TransactionException)3 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)2 Configuration (org.osgi.service.cm.Configuration)2 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)2 CoreOptions.mavenBundle (org.ops4j.pax.exam.CoreOptions.mavenBundle)1 Bundle (org.osgi.framework.Bundle)1 BundleException (org.osgi.framework.BundleException)1