Search in sources :

Example 11 with Server

use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.

the class NonDetachedTest method testCreateNew_DM.

@Test
public void testCreateNew_DM() throws Exception {
    // check versioned entity
    Group companyGroupCopy = metadata.getTools().copy(companyGroup);
    assertTrue(PersistenceHelper.isNew(companyGroupCopy));
    PersistenceHelper.makeDetached(companyGroupCopy);
    User user = persistence.callInTransaction((em) -> em.find(User.class, this.user.getId()));
    User userCopy = metadata.getTools().copy(user);
    assertNull(userCopy.getGroup());
    assertTrue(PersistenceHelper.isNew(userCopy));
    PersistenceHelper.makeDetached(userCopy);
    userCopy.setId(UuidProvider.createUuid());
    userCopy.setVersion(null);
    userCopy.setGroup(companyGroupCopy);
    userCopy.setName("new name");
    try {
        AppBeans.get(DataManager.class).commit(userCopy);
        user = persistence.callInTransaction((em) -> em.find(User.class, userCopy.getId()));
        assertEquals("new name", user.getName());
    } finally {
        cont.deleteRecord(userCopy);
    }
    // check non-versioned entity
    Server server = metadata.create(Server.class);
    server.setName("server-" + server.getId());
    assertTrue(PersistenceHelper.isNew(server));
    PersistenceHelper.makeDetached(server);
    try {
        AppBeans.get(DataManager.class).commit(server);
        Server loaded = persistence.callInTransaction(em -> em.find(Server.class, server.getId()));
        assertNotNull(loaded);
    } finally {
        cont.deleteRecord(server);
    }
}
Also used : Customer(com.haulmont.cuba.testmodel.sales.Customer) Server(com.haulmont.cuba.core.entity.Server) TestSupport(com.haulmont.cuba.testsupport.TestSupport) Date(java.util.Date) Test(org.junit.Test) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) User(com.haulmont.cuba.security.entity.User) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) HashSet(java.util.HashSet) Group(com.haulmont.cuba.security.entity.Group) BigDecimal(java.math.BigDecimal) DateUtils(org.apache.commons.lang.time.DateUtils) Order(com.haulmont.cuba.testmodel.sales.Order) After(org.junit.After) CascadeEntity(com.haulmont.cuba.testmodel.cascadedelete.CascadeEntity) ClassRule(org.junit.ClassRule) Assert(org.junit.Assert) TestContainer(com.haulmont.cuba.testsupport.TestContainer) Before(org.junit.Before) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) Server(com.haulmont.cuba.core.entity.Server) Test(org.junit.Test)

Example 12 with Server

use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.

the class TransactionTest method __testRollbackAndCatch.

private void __testRollbackAndCatch() {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        assertNotNull(em);
        Server server = new Server();
        server.setName("localhost");
        server.setRunning(true);
        em.persist(server);
        throwException();
        tx.commit();
    } catch (RuntimeException e) {
        System.out.println("Caught exception: " + e.getMessage());
        throw e;
    } finally {
        tx.end();
    }
}
Also used : Server(com.haulmont.cuba.core.entity.Server)

Example 13 with Server

use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.

the class TransactionTest method __testRollback.

private void __testRollback() {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.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.entity.Server)

Example 14 with Server

use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.

the class TransactionTest method testCommit.

@Test
public void testCommit() {
    UUID id;
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.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 = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.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.entity.Server) UUID(java.util.UUID) Test(org.junit.Test)

Example 15 with Server

use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.

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 sec$User u where u.id = ?1", User.class);
        query.setParameter(1, TestSupport.ADMIN_USER_ID);
        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 sys$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.security.entity.User) Server(com.haulmont.cuba.core.entity.Server) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

Server (com.haulmont.cuba.core.entity.Server)33 Test (org.junit.Test)25 UUID (java.util.UUID)10 Entity (com.haulmont.cuba.core.entity.Entity)8 Before (org.junit.Before)4 UserSession (com.haulmont.cuba.security.global.UserSession)3 TestUserSessionSource (com.haulmont.cuba.testsupport.TestUserSessionSource)3 Date (java.util.Date)3 EntityManager (com.haulmont.cuba.core.EntityManager)2 Transaction (com.haulmont.cuba.core.Transaction)2 LoginWorker (com.haulmont.cuba.security.app.LoginWorker)2 User (com.haulmont.cuba.security.entity.User)2 QueryRunner (com.haulmont.bali.db.QueryRunner)1 Instance (com.haulmont.chile.core.model.Instance)1 DataService (com.haulmont.cuba.core.app.DataService)1 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)1 EntitySnapshot (com.haulmont.cuba.core.entity.EntitySnapshot)1 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)1 EntityDiff (com.haulmont.cuba.core.entity.diff.EntityDiff)1 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)1