Search in sources :

Example 1 with Server

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"));
    }
}
Also used : Server(com.haulmont.cuba.core.model.common.Server) FetchPlan(io.jmix.core.FetchPlan) View(com.haulmont.cuba.core.global.View) Test(org.junit.jupiter.api.Test) CoreTest(com.haulmont.cuba.core.testsupport.CoreTest)

Example 2 with Server

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));
}
Also used : Server(com.haulmont.cuba.core.model.common.Server) UUID(java.util.UUID) EntityDiff(com.haulmont.cuba.core.model.common.EntityDiff) Test(org.junit.jupiter.api.Test) CoreTest(com.haulmont.cuba.core.testsupport.CoreTest)

Example 3 with 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();
    }
}
Also used : Server(com.haulmont.cuba.core.model.common.Server)

Example 4 with Server

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();
    }
}
Also used : Server(com.haulmont.cuba.core.model.common.Server) UUID(java.util.UUID) CoreTest(com.haulmont.cuba.core.testsupport.CoreTest) Test(org.junit.jupiter.api.Test)

Example 5 with Server

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
        }
    }
}
Also used : User(com.haulmont.cuba.core.model.common.User) Server(com.haulmont.cuba.core.model.common.Server) UUID(java.util.UUID) CoreTest(com.haulmont.cuba.core.testsupport.CoreTest) Test(org.junit.jupiter.api.Test)

Aggregations

Server (com.haulmont.cuba.core.model.common.Server)27 CoreTest (com.haulmont.cuba.core.testsupport.CoreTest)24 Test (org.junit.jupiter.api.Test)24 UUID (java.util.UUID)10 CommitContext (com.haulmont.cuba.core.global.CommitContext)8 Entity (io.jmix.core.Entity)8 DataManager (com.haulmont.cuba.core.global.DataManager)2 LoadContext (com.haulmont.cuba.core.global.LoadContext)2 View (com.haulmont.cuba.core.global.View)2 User (com.haulmont.cuba.core.model.common.User)2 FetchPlan (io.jmix.core.FetchPlan)2 Date (java.util.Date)2 AppBeans (com.haulmont.cuba.core.global.AppBeans)1 Metadata (com.haulmont.cuba.core.global.Metadata)1 CascadeDeletionPolicyEntity (com.haulmont.cuba.core.model.CascadeDeletionPolicyEntity)1 EntityDiff (com.haulmont.cuba.core.model.common.EntityDiff)1 FileDescriptor (com.haulmont.cuba.core.model.common.FileDescriptor)1 Group (com.haulmont.cuba.core.model.common.Group)1 Customer (com.haulmont.cuba.core.model.sales.Customer)1 Order (com.haulmont.cuba.core.model.sales.Order)1