use of com.haulmont.cuba.core.model.common.Server in project jmix by jmix-framework.
the class PersistenceHelperTest method testCheckLoaded.
@Test
public void testCheckLoaded() {
Server server = new Server();
persistence.runInTransaction((em) -> {
em.persist(server);
});
FetchPlan view = new View(Server.class).addProperty("name").addProperty("data").setLoadPartialEntities(true);
Server reloadedServer = persistence.callInTransaction((em) -> {
return em.find(Server.class, server.getId(), view);
});
// fine
entityStates.checkLoaded(reloadedServer, "name");
try {
entityStates.checkLoaded(reloadedServer, "data", "running");
Assertions.fail("Must throw exception");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("Server.running"));
}
}
use of com.haulmont.cuba.core.model.common.Server in project jmix by jmix-framework.
the class PersistenceHelperTest method testEntityStates.
@Test
public void testEntityStates() {
try {
entityStates.isNew(null);
fail("isNew() should not accept null");
} catch (Exception e) {
//
}
try {
entityStates.isManaged(null);
fail("isManaged() should not accept null");
} catch (Exception e) {
//
}
try {
entityStates.isDetached(null);
fail("isDetached() should not accept null");
} catch (Exception e) {
//
}
assertTrue(entityStates.isNew(new EntityDiff(null)));
UUID id;
Server server;
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
assertNotNull(em);
server = new Server();
assertTrue(entityStates.isNew(server));
assertFalse(entityStates.isManaged(server));
assertFalse(entityStates.isDetached(server));
id = server.getId();
server.setName("localhost");
server.setRunning(true);
em.persist(server);
assertTrue(entityStates.isNew(server));
assertTrue(entityStates.isManaged(server));
assertFalse(entityStates.isDetached(server));
tx.commit();
} finally {
tx.end();
}
assertFalse(entityStates.isNew(server));
assertFalse(entityStates.isManaged(server));
assertTrue(entityStates.isDetached(server));
tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
server = em.find(Server.class, id);
assertNotNull(server);
assertEquals(id, server.getId());
assertFalse(entityStates.isNew(server));
assertTrue(entityStates.isManaged(server));
assertFalse(entityStates.isDetached(server));
server.setRunning(false);
tx.commit();
} finally {
tx.end();
}
assertFalse(entityStates.isNew(server));
assertFalse(entityStates.isManaged(server));
assertTrue(entityStates.isDetached(server));
tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
server = em.merge(server);
assertFalse(entityStates.isNew(server));
assertTrue(entityStates.isManaged(server));
assertFalse(entityStates.isDetached(server));
tx.commit();
} finally {
tx.end();
}
tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
server = em.find(Server.class, id);
assertNotNull(server);
assertEquals(id, server.getId());
em.remove(server);
assertFalse(entityStates.isNew(server));
// is it correct?
assertTrue(entityStates.isManaged(server));
assertFalse(entityStates.isDetached(server));
tx.commit();
} finally {
tx.end();
}
assertFalse(entityStates.isNew(server));
assertFalse(entityStates.isManaged(server));
// is it correct?
assertTrue(entityStates.isDetached(server));
}
use of com.haulmont.cuba.core.model.common.Server in project jmix by jmix-framework.
the class TransactionTest method __testRollback.
private void __testRollback() {
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
assertNotNull(em);
Server server = new Server();
server.setName("localhost");
server.setRunning(true);
em.persist(server);
throwException();
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.model.common.Server in project jmix by jmix-framework.
the class TransactionTest method testCommit.
@Test
public void testCommit() {
UUID id;
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
assertNotNull(em);
Server server = new Server();
id = server.getId();
server.setName("localhost");
server.setRunning(true);
em.persist(server);
tx.commit();
} finally {
tx.end();
}
tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
Server server = em.find(Server.class, id);
assertEquals(id, server.getId());
server.setRunning(false);
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.model.common.Server in project jmix by jmix-framework.
the class TransactionTest method testReadOnly.
@Test
public void testReadOnly() throws Exception {
try (Transaction tx = persistence.createTransaction(new TransactionParams().setReadOnly(true))) {
TypedQuery<User> query = persistence.getEntityManager().createQuery("select u from test$User u where u.id = ?1", User.class);
query.setParameter(1, user.getId());
User result = query.getSingleResult();
tx.commit();
}
// read-only transaction cannot be committed if it contains changed entities
UUID id = persistence.callInTransaction(em -> {
Server server = new Server();
server.setName("localhost");
em.persist(server);
return server.getId();
});
try (Transaction tx = persistence.createTransaction(new TransactionParams().setReadOnly(true))) {
TypedQuery<Server> query = persistence.getEntityManager().createQuery("select e from test$Server e where e.id = ?1", Server.class);
query.setParameter(1, id);
Server server = query.getSingleResult();
server.setName("changed");
try {
tx.commit();
fail();
} catch (IllegalStateException e) {
// ok
}
}
}
Aggregations