Search in sources :

Example 6 with IdentityEntity

use of com.haulmont.cuba.testmodel.primary_keys.IdentityEntity in project cuba by cuba-platform.

the class IdentityTest method testEquality.

@Test
public void testEquality() throws Exception {
    IdentityEntity e1 = metadata.create(IdentityEntity.class);
    assertNotNull(e1.getId());
    assertNotNull(e1.getId().getUuid());
    IdentityEntity e2 = metadata.create(IdentityEntity.class);
    assertNotEquals(e1, e2);
    // e2.setUuid(e1.getUuid());
    // assertEquals(e1, e2);
    // assertTrue(e1.hashCode() == e2.hashCode());
    Field idField = BaseIdentityIdEntity.class.getDeclaredField("id");
    idField.setAccessible(true);
    // e1 & e3 are different instances with the same UUID
    IdentityEntity e3 = TestSupport.reserialize(e1);
    // one of them has an Id, other has not - this is the case when a newly committed instance returns from
    // middleware to the client
    idField.set(e3, 100L);
    // they should be equal and with the same hashCode
    assertEquals(e1, e3);
    assertTrue(e1.hashCode() == e3.hashCode());
    // e1 & e3 are different instances with the same Id
    e1 = metadata.create(IdentityEntity.class);
    idField.set(e1, 100L);
    e2 = metadata.create(IdentityEntity.class);
    idField.set(e2, 100L);
    // they should be equal and with the same hashCode
    assertEquals(e1, e2);
    assertTrue(e1.hashCode() == e2.hashCode());
}
Also used : Field(java.lang.reflect.Field) IdentityEntity(com.haulmont.cuba.testmodel.primary_keys.IdentityEntity) Test(org.junit.Test)

Example 7 with IdentityEntity

use of com.haulmont.cuba.testmodel.primary_keys.IdentityEntity in project cuba by cuba-platform.

the class IdentityTest method testReload.

@Test
public void testReload() throws Exception {
    // no ID
    IdentityEntity foo = metadata.create(IdentityEntity.class);
    foo.setName("foo");
    foo.setEmail("foo@mail.com");
    try (Transaction tx = persistence.createTransaction()) {
        IdentityEntity reloadedFoo = persistence.getEntityManager().reload(foo);
        tx.commit();
        assertNull(reloadedFoo);
    }
    // existing ID
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().persist(foo);
        tx.commit();
    }
    Long idVal = foo.getId().get();
    foo = metadata.create(IdentityEntity.class);
    foo.setName("foo");
    foo.setEmail("foo@mail.com");
    foo.setId(IdProxy.of(idVal));
    try (Transaction tx = persistence.createTransaction()) {
        IdentityEntity reloadedFoo = persistence.getEntityManager().reload(foo);
        tx.commit();
        assertEquals(reloadedFoo, foo);
    }
}
Also used : Transaction(com.haulmont.cuba.core.Transaction) IdentityEntity(com.haulmont.cuba.testmodel.primary_keys.IdentityEntity) Test(org.junit.Test)

Aggregations

IdentityEntity (com.haulmont.cuba.testmodel.primary_keys.IdentityEntity)7 Test (org.junit.Test)7 Transaction (com.haulmont.cuba.core.Transaction)5 Field (java.lang.reflect.Field)1 UUID (java.util.UUID)1