Search in sources :

Example 1 with User

use of org.mx.test.entity.User in project main by JohnPeng739.

the class TestDatabase method testUserInterface.

@Test
public void testUserInterface() {
    GeneralDictAccessor accessor = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
    assertNotNull(accessor);
    try {
        assertEquals(0, accessor.count(User.class));
        User user = EntityFactory.createEntity(User.class);
        user.setCode("john");
        user.setName("John Peng");
        user.setAddress("address");
        user.setEmail("email");
        user.setPostCode("zip");
        user.setDesc("description");
        User check = accessor.save(user);
        assertNotNull(check);
        assertNotNull(check.getId());
        assertEquals(user.getCode(), check.getCode());
        assertEquals(user.getName(), check.getName());
        assertEquals(user.getAddress(), check.getAddress());
        assertTrue(user.getCreatedTime() > 0);
        assertEquals(1, accessor.count(User.class));
        check = accessor.getById(user.getId(), User.class);
        assertNotNull(check);
        assertEquals(user.getCode(), check.getCode());
        assertEquals(user.getName(), check.getName());
        check = accessor.getByCode(user.getCode(), User.class);
        assertNotNull(check);
        assertEquals(user.getId(), check.getId());
        assertEquals(user.getCode(), check.getCode());
        assertEquals(user.getName(), check.getName());
        user = accessor.getByCode("john", User.class);
        user.setCode("john-1");
        user.setName(user.getName() + "(editable)");
        user.setDesc("I'm John Peng.");
        check = accessor.save(user);
        assertNotNull(check);
        assertEquals(1, accessor.count(User.class));
        assertEquals(user.getId(), check.getId());
        assertEquals(user.getCode(), check.getCode());
        assertEquals(user.getName(), check.getName());
        assertEquals(user.getDesc(), check.getDesc());
        check = accessor.getByCode("john", User.class);
        assertNotNull(check);
        check = accessor.getById(user.getId(), User.class);
        assertNotNull(check);
        assertEquals(user.getCode(), check.getCode());
        check = accessor.getByCode("john-1", User.class);
        // 证明修改代码无效
        assertNull(check);
        user = EntityFactory.createEntity(User.class);
        user.setId(DigestUtils.uuid());
        user.setCode("josh");
        user.setName("Josh Peng");
        check = accessor.save(user);
        assertNotNull(check);
        assertEquals(2, accessor.count(User.class));
        check = accessor.getByCode("john", User.class);
        assertNotNull(check);
        assertTrue(check.isValid());
        check = accessor.getByCode("josh", User.class);
        assertNotNull(check);
        assertTrue(check.isValid());
        user = accessor.getByCode("john", User.class);
        assertNotNull(user);
        check = accessor.remove(user);
        assertNotNull(check);
        assertEquals(1, accessor.count(User.class));
        assertEquals(2, accessor.count(User.class, false));
        assertFalse(check.isValid());
        List<User> list = ((GeneralAccessor) accessor).find(Arrays.asList(new GeneralAccessor.ConditionTuple("code", "john"), new GeneralAccessor.ConditionTuple("valid", true)), User.class);
        assertEquals(0, list.size());
        list = ((GeneralAccessor) accessor).find(Arrays.asList(new GeneralAccessor.ConditionTuple("code", "josh"), new GeneralAccessor.ConditionTuple("valid", true)), User.class);
        assertEquals(1, list.size());
        user = accessor.getByCode("john", User.class);
        check = accessor.remove(user, false);
        assertNotNull(check);
        assertEquals(1, accessor.count(User.class));
        check = accessor.getById(user.getId(), User.class);
        assertNull(check);
        check = accessor.getByCode(user.getCode(), User.class);
        assertNull(check);
        check = accessor.getByCode("josh", User.class);
        assertNotNull(check);
        user = accessor.getByCode("josh", User.class);
        accessor.remove(user, false);
        assertEquals(0, accessor.count(User.class));
    } catch (UserInterfaceException ex) {
        fail(ex.getMessage());
    }
}
Also used : User(org.mx.test.entity.User) GeneralDictAccessor(org.mx.dal.service.GeneralDictAccessor) UserInterfaceException(org.mx.error.UserInterfaceException) GeneralAccessor(org.mx.dal.service.GeneralAccessor) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 GeneralAccessor (org.mx.dal.service.GeneralAccessor)1 GeneralDictAccessor (org.mx.dal.service.GeneralDictAccessor)1 UserInterfaceException (org.mx.error.UserInterfaceException)1 User (org.mx.test.entity.User)1