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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations