Search in sources :

Example 16 with Atom

use of org.nutz.trans.Atom in project nutz by nutzam.

the class FieldFilterTest method test_query_by_filter.

@Test
public void test_query_by_filter() {
    dao.update(dao.fetch(Pet.class, "xb").setNickName("XiaoBai"));
    assertEquals("XiaoBai", dao.fetch(Pet.class, "xb").getNickName());
    final List<Pet> pets = new ArrayList<Pet>();
    FieldFilter.create(Pet.class, "id|name").run(new Atom() {

        public void run() {
            pets.add(dao.query(Pet.class, null, null).get(0));
        }
    });
    assertNull(pets.get(0).getNickName());
}
Also used : ArrayList(java.util.ArrayList) Pet(org.nutz.dao.test.meta.Pet) Atom(org.nutz.trans.Atom) Test(org.junit.Test)

Example 17 with Atom

use of org.nutz.trans.Atom in project nutz by nutzam.

the class FieldFilterTest method test_filter_no_field_match.

// Issue 435
@Test(expected = DaoException.class)
public void test_filter_no_field_match() {
    dao.create(EmtryObject.class, true);
    final EmtryObject obj = new EmtryObject();
    // 应该抛出一个DaoException,因为没有任何的字段需要插入!
    FieldFilter.create(EmtryObject.class, "id").run(new Atom() {

        public void run() {
            dao.insert(obj);
        }
    });
}
Also used : EmtryObject(org.nutz.dao.test.meta.EmtryObject) Atom(org.nutz.trans.Atom) Test(org.junit.Test)

Example 18 with Atom

use of org.nutz.trans.Atom in project nutz by nutzam.

the class FieldFilterTest method test_insert_by_filter.

@Test
public void test_insert_by_filter() {
    // insert one pet
    final Pet p = pet("xh").setNickName("XiaoHei");
    FieldFilter.create(Pet.class, "id|name").run(new Atom() {

        public void run() {
            dao.insert(p);
        }
    });
    Pet p2 = dao.fetch(Pet.class, p.getId());
    assertNull(p2.getNickName());
}
Also used : Pet(org.nutz.dao.test.meta.Pet) Atom(org.nutz.trans.Atom) Test(org.junit.Test)

Example 19 with Atom

use of org.nutz.trans.Atom in project nutz by nutzam.

the class FieldFilterTest method test_select_by_filter.

@Test
public void test_select_by_filter() {
    dao.update(dao.fetch(Pet.class, "xb").setNickName("XiaoBai"));
    assertEquals("XiaoBai", dao.fetch(Pet.class, "xb").getNickName());
    final Pet[] pets = new Pet[1];
    FieldFilter.create(Pet.class, "id|name").run(new Atom() {

        public void run() {
            pets[0] = dao.fetch(Pet.class, "xb");
        }
    });
    assertNull(pets[0].getNickName());
}
Also used : Pet(org.nutz.dao.test.meta.Pet) Atom(org.nutz.trans.Atom) Test(org.junit.Test)

Example 20 with Atom

use of org.nutz.trans.Atom in project nutz by nutzam.

the class InsertTest method test_fastinsert_as_rollback.

/**
     * Github Issue #131
     */
@Test
public void test_fastinsert_as_rollback() {
    dao.create(Pet.class, true);
    // 在插入数据中有错误 ...
    final Pet[] pets = Pet.create(10);
    pets[4].setName(Strings.dup('t', 1024));
    try {
        Trans.exec(new Atom() {

            public void run() {
                dao.fastInsert(pets);
            }
        });
    } catch (RuntimeException e) {
    }
    assertEquals(0, dao.count(Pet.class));
    // 插入后,主动抛出一个错误,看回滚不回滚
    try {
        final Pet[] pets2 = Pet.create(10);
        Trans.exec(new Atom() {

            public void run() {
                dao.fastInsert(pets2);
                throw new RuntimeException("I am ok!");
            }
        });
    } catch (RuntimeException e) {
        assertEquals("I am ok!", e.getMessage());
    }
    assertEquals(0, dao.count(Pet.class));
}
Also used : Pet(org.nutz.dao.test.meta.Pet) Atom(org.nutz.trans.Atom) Test(org.junit.Test)

Aggregations

Atom (org.nutz.trans.Atom)20 Test (org.junit.Test)15 Pet (org.nutz.dao.test.meta.Pet)9 NutDao (org.nutz.dao.impl.NutDao)4 Stopwatch (org.nutz.lang.Stopwatch)3 Connection (java.sql.Connection)2 ArrayList (java.util.ArrayList)2 SimpleDataSource (org.nutz.dao.impl.SimpleDataSource)2 File (java.io.File)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 DataSource (javax.sql.DataSource)1 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)1 Ignore (org.junit.Ignore)1 DaoInterceptorChain (org.nutz.dao.DaoInterceptorChain)1 SqlNotFoundException (org.nutz.dao.SqlNotFoundException)1 FileSqlManager (org.nutz.dao.impl.FileSqlManager)1