Search in sources :

Example 91 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class SetOfStrings method storeLoadCollection.

@Test
public void storeLoadCollection() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        Item someItem = new Item();
        someItem.getImages().add("foo.jpg");
        someItem.getImages().add("bar.jpg");
        someItem.getImages().add("baz.jpg");
        // Duplicate, filtered at Java level by HashSet!
        someItem.getImages().add("baz.jpg");
        em.persist(someItem);
        tx.commit();
        em.close();
        Long ITEM_ID = someItem.getId();
        tx.begin();
        em = JPA.createEntityManager();
        Item item = em.find(Item.class, ITEM_ID);
        assertEquals(item.getImages().size(), 3);
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.collections.setofstrings.Item) EntityManager(javax.persistence.EntityManager) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 92 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class SortedSetOfStrings method storeLoadCollection.

@Test
public void storeLoadCollection() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        Item someItem = new Item();
        someItem.getImages().add("foo.jpg");
        someItem.getImages().add("bar.jpg");
        someItem.getImages().add("baz.jpg");
        // Duplicate, filtered at Java level by HashSet!
        someItem.getImages().add("baz.jpg");
        em.persist(someItem);
        tx.commit();
        em.close();
        Long ITEM_ID = someItem.getId();
        tx.begin();
        em = JPA.createEntityManager();
        Item item = em.find(Item.class, ITEM_ID);
        assertEquals(item.getImages().size(), 3);
        // Sorted in-memory with TreeSet
        Iterator<String> it = item.getImages().iterator();
        String image;
        image = it.next();
        assertEquals(image, "bar.jpg");
        image = it.next();
        assertEquals(image, "baz.jpg");
        image = it.next();
        assertEquals(image, "foo.jpg");
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.collections.sortedsetofstrings.Item) EntityManager(javax.persistence.EntityManager) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 93 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class CompositeKeyManyToOne method storeLoad.

@Test
public void storeLoad() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        {
            UserId id = new UserId("johndoe", "123");
            User user = new User(id);
            em.persist(user);
            Item item = new Item("Some Item");
            item.setSeller(user);
            em.persist(item);
        }
        tx.commit();
        em.close();
        tx.begin();
        em = JPA.createEntityManager();
        {
            UserId id = new UserId("johndoe", "123");
            User user = em.find(User.class, id);
            assertEquals(user.getId().getDepartmentNr(), "123");
            Item item = (Item) em.createQuery("select i from Item i where i.seller = :u").setParameter("u", user).getSingleResult();
            assertEquals(item.getName(), "Some Item");
        }
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.complexschemas.compositekey.manytoone.Item) EntityManager(javax.persistence.EntityManager) User(org.jpwh.model.complexschemas.compositekey.manytoone.User) UserId(org.jpwh.model.complexschemas.compositekey.manytoone.UserId) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 94 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class CustomSchema method storeLoadDomainInvalid.

// All these tests are testing for failure
@Test(expectedExceptions = org.hibernate.exception.ConstraintViolationException.class)
public void storeLoadDomainInvalid() throws Throwable {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        // This will fail and therefore validate that we actually
        // have a custom SQL datatype in the DDL
        User user = new User();
        user.setEmail("@invalid.address");
        user.setUsername("someuser");
        em.persist(user);
        try {
            em.flush();
        } catch (Exception ex) {
            throw unwrapCauseOfType(ex, org.hibernate.exception.ConstraintViolationException.class);
        }
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) EntityManager(javax.persistence.EntityManager) User(org.jpwh.model.complexschemas.custom.User) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 95 with UserTransaction

use of javax.transaction.UserTransaction in project microservices by pwillhan.

the class CustomSchema method storeLoadCheckColumnInvalid.

@Test(expectedExceptions = org.hibernate.exception.ConstraintViolationException.class)
public void storeLoadCheckColumnInvalid() throws Throwable {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        User user = new User();
        user.setEmail("valid@test.com");
        user.setUsername("adminPretender");
        em.persist(user);
        try {
            em.flush();
        } catch (Exception ex) {
            throw unwrapCauseOfType(ex, org.hibernate.exception.ConstraintViolationException.class);
        }
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) EntityManager(javax.persistence.EntityManager) User(org.jpwh.model.complexschemas.custom.User) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Aggregations

UserTransaction (javax.transaction.UserTransaction)642 EntityManager (javax.persistence.EntityManager)244 Test (org.testng.annotations.Test)187 Test (org.junit.Test)157 JPATest (org.jpwh.env.JPATest)138 InitialContext (javax.naming.InitialContext)62 BigDecimal (java.math.BigDecimal)53 HashMap (java.util.HashMap)52 IOException (java.io.IOException)50 Context (javax.naming.Context)49 List (java.util.List)47 FacesContext (javax.faces.context.FacesContext)43 NamingException (javax.naming.NamingException)43 NodeRef (org.alfresco.service.cmr.repository.NodeRef)43 Node (javax.jcr.Node)42 KieSession (org.kie.api.runtime.KieSession)42 Query (javax.persistence.Query)38 QueryingTest (org.jpwh.test.querying.QueryingTest)36 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)35 Item (org.jpwh.model.querying.Item)35