Search in sources :

Example 11 with Item

use of org.jpwh.model.advanced.Item in project microservices by pwillhan.

the class GeneratedProperties method storeLoadInitialPrice.

@Test
public void storeLoadInitialPrice() throws Exception {
    long ITEM_ID = storeItemAndBids();
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        Item item = em.find(Item.class, ITEM_ID);
        tx.commit();
        em.close();
        assertEquals(item.getInitialPrice().compareTo(new BigDecimal("1")), 0);
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.advanced.Item) EntityManager(javax.persistence.EntityManager) BigDecimal(java.math.BigDecimal) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 12 with Item

use of org.jpwh.model.advanced.Item in project microservices by pwillhan.

the class Temporal method storeLoadTemporal.

@Test
public void storeLoadTemporal() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        Item someItem = new Item();
        someItem.setName("Some item");
        someItem.setDescription("This is some description.");
        // someItem.setReviewedOn(Instant.now().plusSeconds(60)); // A future time
        em.persist(someItem);
        tx.commit();
        em.close();
        Long ITEM_ID = someItem.getId();
        Date ORIGINAL_CREATION_DATE = someItem.getCreatedOn();
        tx.begin();
        em = JPA.createEntityManager();
        Item item = em.find(Item.class, ITEM_ID);
        // java.util.Date and java.sql.Timestamp are not symmetric!
        assertFalse(item.getCreatedOn().equals(ORIGINAL_CREATION_DATE));
        assertFalse(item.getCreatedOn().getClass().equals(ORIGINAL_CREATION_DATE.getClass()));
        // This is how you properly compare time values in Java...
        assertEquals(ORIGINAL_CREATION_DATE.getTime(), item.getCreatedOn().getTime());
        // Or use the slightly less annoying but quite awful Calendar API
        Calendar oldDate = new GregorianCalendar();
        oldDate.setTime(ORIGINAL_CREATION_DATE);
        Calendar newDate = new GregorianCalendar();
        newDate.setTime(item.getCreatedOn());
        assertEquals(oldDate, newDate);
        // Or the Java 8 API
        // assertTrue(item.getReviewedOn().isAfter(item.getCreatedOn().toInstant()));
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.advanced.Item) EntityManager(javax.persistence.EntityManager) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) Date(java.util.Date) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 13 with Item

use of org.jpwh.model.advanced.Item in project microservices by pwillhan.

the class TransformingColumns method storeLoadTransform.

@Test
public void storeLoadTransform() throws Exception {
    final long ITEM_ID = storeItemAndBids();
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        {
            Item item = em.find(Item.class, ITEM_ID);
            assertEquals(item.getMetricWeight(), 2.0);
            final boolean[] tests = new boolean[1];
            em.unwrap(Session.class).doWork(new Work() {

                @Override
                public void execute(Connection connection) throws SQLException {
                    PreparedStatement statement = null;
                    ResultSet result = null;
                    try {
                        statement = connection.prepareStatement("select IMPERIALWEIGHT from ITEM where ID = ?");
                        statement.setLong(1, ITEM_ID);
                        result = statement.executeQuery();
                        while (result.next()) {
                            Double imperialWeight = result.getDouble("IMPERIALWEIGHT");
                            assertEquals(imperialWeight, 4.40924);
                            tests[0] = true;
                        }
                    } finally {
                        if (result != null)
                            result.close();
                        if (statement != null)
                            statement.close();
                    }
                }
            });
            assertTrue(tests[0]);
        }
        em.clear();
        {
            List<Item> result = em.createQuery("select i from Item i where i.metricWeight = :w").setParameter("w", 2.0).getResultList();
            assertEquals(result.size(), 1);
        }
        em.clear();
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.advanced.Item) EntityManager(javax.persistence.EntityManager) Work(org.hibernate.jdbc.Work) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) List(java.util.List) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 14 with Item

use of org.jpwh.model.advanced.Item in project microservices by pwillhan.

the class BooleanOverride method storeLoadOverride.

@Test
public void storeLoadOverride() throws Exception {
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        Item someItem = new Item();
        someItem.setName("Some item");
        someItem.setDescription("This is some description.");
        someItem.setVerified(true);
        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);
        assertTrue(item.isVerified());
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.advanced.Item) EntityManager(javax.persistence.EntityManager) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 15 with Item

use of org.jpwh.model.advanced.Item in project microservices by pwillhan.

the class DerivedProperties method storeLoadFormula.

@Test
public void storeLoadFormula() throws Exception {
    long ITEM_ID = storeItemAndBids();
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        Item item = em.find(Item.class, ITEM_ID);
        assertEquals(item.getShortDescription(), "This is some...");
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.advanced.Item) EntityManager(javax.persistence.EntityManager) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Aggregations

EntityManager (javax.persistence.EntityManager)16 UserTransaction (javax.transaction.UserTransaction)16 Item (org.jpwh.model.advanced.Item)16 JPATest (org.jpwh.env.JPATest)12 Test (org.testng.annotations.Test)12 BigDecimal (java.math.BigDecimal)6 Bid (org.jpwh.model.advanced.Bid)4 Date (java.util.Date)2 Random (java.util.Random)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 Blob (java.sql.Blob)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 List (java.util.List)1 Query (javax.persistence.Query)1