Search in sources :

Example 1 with Many2ManyA

use of com.haulmont.cuba.testmodel.many2many.Many2ManyA in project cuba by cuba-platform.

the class SoftDeleteMany2ManyTest method test_PL_3452.

/**
 * @see <a href="https://youtrack.haulmont.com/issue/PL-3452">PL-3452</a>
 */
@Test
public void test_PL_3452() throws Exception {
    Many2ManyA a1;
    Many2ManyA a2;
    Many2ManyB b1;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        a1 = em.find(Many2ManyA.class, this.a1.getId());
        assertNotNull(a1);
        assertEquals(2, a1.getCollectionOfB().size());
        a2 = em.find(Many2ManyA.class, this.a2.getId());
        assertNotNull(a2);
        assertEquals(1, a2.getCollectionOfB().size());
        tx.commitRetaining();
        em = cont.entityManager();
        b1 = em.find(Many2ManyB.class, this.b1.getId());
        em.remove(b1);
        tx.commitRetaining();
        em = cont.entityManager();
        a1 = em.find(Many2ManyA.class, this.a1.getId());
        assertNotNull(a1);
        assertEquals(1, a1.getCollectionOfB().size());
        tx.commit();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Many2ManyB(com.haulmont.cuba.testmodel.many2many.Many2ManyB) Many2ManyA(com.haulmont.cuba.testmodel.many2many.Many2ManyA) Test(org.junit.Test)

Example 2 with Many2ManyA

use of com.haulmont.cuba.testmodel.many2many.Many2ManyA in project cuba by cuba-platform.

the class SoftDeleteMany2ManyTest method setUp.

@Before
public void setUp() throws Exception {
    DataManager dataManager = AppBeans.get(DataManager.class);
    many2ManyA = cont.metadata().create(Many2ManyA.class);
    many2ManyB = cont.metadata().create(Many2ManyB.class);
    many2ManyA.setCollectionOfB(new HashSet<>());
    many2ManyA.getCollectionOfB().add(many2ManyB);
    dataManager.commit(new CommitContext(many2ManyA, many2ManyB));
    many2ManyA2 = cont.metadata().create(Many2ManyA.class);
    many2ManyB2 = cont.metadata().create(Many2ManyB.class);
    many2ManyA2.setCollectionOfB2(new HashSet<>());
    many2ManyA2.getCollectionOfB2().add(many2ManyB2);
    dataManager.commit(new CommitContext(many2ManyA2, many2ManyB2));
    a1 = cont.metadata().create(Many2ManyA.class);
    a2 = cont.metadata().create(Many2ManyA.class);
    b1 = cont.metadata().create(Many2ManyB.class);
    b2 = cont.metadata().create(Many2ManyB.class);
    b3 = cont.metadata().create(Many2ManyB.class);
    a1.setCollectionOfB(new HashSet<>());
    a1.getCollectionOfB().add(b1);
    a1.getCollectionOfB().add(b2);
    a2.setCollectionOfB(new HashSet<>());
    a2.getCollectionOfB().add(b3);
    dataManager.commit(new CommitContext(a1, a2, b1, b2, b3));
}
Also used : Many2ManyB(com.haulmont.cuba.testmodel.many2many.Many2ManyB) CommitContext(com.haulmont.cuba.core.global.CommitContext) DataManager(com.haulmont.cuba.core.global.DataManager) Many2ManyA(com.haulmont.cuba.testmodel.many2many.Many2ManyA) Before(org.junit.Before)

Example 3 with Many2ManyA

use of com.haulmont.cuba.testmodel.many2many.Many2ManyA in project cuba by cuba-platform.

the class SoftDeleteMany2ManyTest method testMany2ManyUnlink.

@Test
public void testMany2ManyUnlink() throws Exception {
    try (Transaction tx = cont.persistence().createTransaction()) {
        Many2ManyA a = cont.entityManager().find(Many2ManyA.class, this.many2ManyA.getId());
        assertNotNull(a);
        assertFalse(a.getCollectionOfB().isEmpty());
        assertEquals(many2ManyB, a.getCollectionOfB().iterator().next());
        tx.commit();
    }
    try (Transaction tx = cont.persistence().createTransaction()) {
        Many2ManyB b = cont.entityManager().find(Many2ManyB.class, this.many2ManyB.getId());
        cont.entityManager().remove(b);
        tx.commit();
    }
    try (Transaction tx = cont.persistence().createTransaction()) {
        Many2ManyA a = cont.entityManager().find(Many2ManyA.class, this.many2ManyA.getId());
        assertNotNull(a);
        assertTrue(a.getCollectionOfB().isEmpty());
        tx.commit();
    }
}
Also used : Transaction(com.haulmont.cuba.core.Transaction) Many2ManyB(com.haulmont.cuba.testmodel.many2many.Many2ManyB) Many2ManyA(com.haulmont.cuba.testmodel.many2many.Many2ManyA) Test(org.junit.Test)

Example 4 with Many2ManyA

use of com.haulmont.cuba.testmodel.many2many.Many2ManyA in project cuba by cuba-platform.

the class SoftDeleteMany2ManyTest method testMany2ManyDeny.

@Test
public void testMany2ManyDeny() throws Exception {
    try (Transaction tx = cont.persistence().createTransaction()) {
        Many2ManyA a = cont.entityManager().find(Many2ManyA.class, many2ManyA2.getId());
        assertNotNull(a);
        assertFalse(a.getCollectionOfB2().isEmpty());
        assertEquals(many2ManyB2, a.getCollectionOfB2().iterator().next());
        tx.commit();
    }
    try (Transaction tx = cont.persistence().createTransaction()) {
        Many2ManyA a = cont.entityManager().find(Many2ManyA.class, many2ManyA2.getId());
        cont.entityManager().remove(a);
        tx.commit();
        fail();
    } catch (Exception e) {
        Throwable rootCause = ExceptionUtils.getRootCause(e);
        if (rootCause == null)
            rootCause = e;
        if (!(rootCause instanceof DeletePolicyException))
            rootCause.printStackTrace();
        assertTrue(rootCause instanceof DeletePolicyException);
    }
    try (Transaction tx = cont.persistence().createTransaction()) {
        Many2ManyA a = cont.entityManager().find(Many2ManyA.class, many2ManyA2.getId());
        assertNotNull(a);
        assertFalse(a.getCollectionOfB2().isEmpty());
        assertEquals(many2ManyB2, a.getCollectionOfB2().iterator().next());
        tx.commit();
    }
}
Also used : Transaction(com.haulmont.cuba.core.Transaction) Many2ManyA(com.haulmont.cuba.testmodel.many2many.Many2ManyA) DeletePolicyException(com.haulmont.cuba.core.global.DeletePolicyException) DeletePolicyException(com.haulmont.cuba.core.global.DeletePolicyException) Test(org.junit.Test)

Aggregations

Many2ManyA (com.haulmont.cuba.testmodel.many2many.Many2ManyA)4 Transaction (com.haulmont.cuba.core.Transaction)3 Many2ManyB (com.haulmont.cuba.testmodel.many2many.Many2ManyB)3 Test (org.junit.Test)3 EntityManager (com.haulmont.cuba.core.EntityManager)1 CommitContext (com.haulmont.cuba.core.global.CommitContext)1 DataManager (com.haulmont.cuba.core.global.DataManager)1 DeletePolicyException (com.haulmont.cuba.core.global.DeletePolicyException)1 Before (org.junit.Before)1