package jpaoletti.jpm.hibernate;
import jpaoletti.jpm.core.Entity;
import jpaoletti.jpm.core.EntityFilter;
import jpaoletti.jpm.core.FilterOperation;
import jpaoletti.jpm.core.PMContext;
import jpaoletti.jpm.core.PersistenceManager;
import jpaoletti.jpm.core.PresentationManager;
import jpaoletti.jpm.test.SimpleClass;
import org.hibernate.Session;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author jpaoletti
*/
public class HibernateTest {
private static final String TEST_ENTITY = "simpleclass";
private Session session;
public HibernateTest() {
}
@Test
public void list() throws Exception {
final PMContext ctx = init();
assertEquals("There must be 2 items in " + TEST_ENTITY + " table",
2, getTestEntity().getList(ctx).size());
ctx.getPersistenceManager().finish(null);
}
@Test
public void get() throws Exception {
final PMContext ctx = init();
assertNotNull("Row with id 1 must exist",
getTestEntity().getDataAccess().getItem(ctx, "id", "1"));
assertNotNull("Row with description T1 must exist",
getTestEntity().getDataAccess().getItem(ctx, "description", "T1"));
ctx.getPersistenceManager().finish(null);
}
@Test
public void filter() throws Exception {
final PMContext ctx = init();
final EntityFilter filter = getTestEntity().getDataAccess().createFilter(ctx);
filter.addFilter("id", 1L, FilterOperation.EQ);
filter.process(getTestEntity());
assertEquals("There must be 1 items in " + TEST_ENTITY + " table after filter",
1, getTestEntity().getList(ctx, filter).size());
ctx.getPersistenceManager().finish(null);
}
@Test
public void addEditDelete() throws Exception {
final PMContext ctx = init();
final SimpleClass item = new SimpleClass();
item.setDescription("Generated by test");
//Add
Object tx = ctx.getPersistenceManager().startTransaction(null);
try {
ctx.getDataAccess().add(ctx, item);
ctx.getPersistenceManager().commit(null, tx);
} catch (Exception e) {
ctx.getPersistenceManager().rollback(null, tx);
throw e;
}
//Edit
tx = ctx.getPersistenceManager().startTransaction(null);
try {
item.setDescription("Modified by test");
ctx.getDataAccess().update(ctx, item);
ctx.getPersistenceManager().commit(null, tx);
} catch (Exception e) {
ctx.getPersistenceManager().rollback(null, tx);
throw e;
}
//Delete
tx = ctx.getPersistenceManager().startTransaction(null);
try {
ctx.getDataAccess().delete(ctx, item);
ctx.getPersistenceManager().commit(null, tx);
} catch (Exception e) {
ctx.getPersistenceManager().rollback(null, tx);
throw e;
}
ctx.getPersistenceManager().finish(null);
}
protected PMContext init() throws Exception {
final String sid = "1";
PresentationManager.getPm().registerSession(sid);
final PMContext ctx = new PMContext(sid);
ctx.setEntityContainer(ctx.getEntityContainer(TEST_ENTITY));
final PersistenceManager mgr = ctx.getPersistenceManager();
mgr.init(getSession());
assertNotNull("Entity " + TEST_ENTITY + " must be defined",
getTestEntity());
return ctx;
}
@Before
public void setUp() {
session = HibernateUtil.getSession();
}
@After
public void tearDown() {
if (session.isOpen()) {
session.close();
}
}
@BeforeClass
public static void setUpClass() throws Exception {
PresentationManager.start("jpm-config.xml");
}
public Session getSession() {
return session;
}
private Entity getTestEntity() {
return PresentationManager.getPm().getEntity(TEST_ENTITY);
}
}