面向 Java 开发人员的 db4o 指南: 超越简单对象
2010-04-01 00:00:00 来源:WEB开发网我在进行 db4o 探察测试时,在控制台模式使用 JUnit 4 测试库。写任何测试代码前,StructuredObjectTest 类如清单 4 所示:
清单 4. 影响 db4o API 的测试
import java.io.*;
import java.util.*;
import com.db4o.*;
import com.db4o.query.*;
import com.tedneward.model.*;
import org.junit.Before;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
public class StructuredObjectsTest
{
ObjectContainer db;
@Before public void prepareDatabase()
{
db = Db4o.openFile("persons.data");
Person ben = new Person("Ben", "Galbraith",
Gender.MALE, 29, Mood.HAPPY);
Person jess = new Person("Jessica", "Smith",
Gender.FEMALE, 29, Mood.HAPPY);
ben.setSpouse(jess);
db.set(ben);
db.commit();
}
@After public void deleteDatabase()
{
db.close();
new File("persons.data").delete();
}
@Test public void testSimpleRetrieval()
{
List<Person> maleGalbraiths =
db.query(new Predicate<Person>() {
public boolean match(Person candidate) {
return candidate.getLastName().equals("Galbraith") &&
candidate.getGender().equals(Gender.MALE);
}
});
// Should only have one in the returned set
assertEquals(maleGalbraiths.size(), 1);
// (Shouldn't display to the console in a unit test, but this is an
// exploration test, not a real unit test)
for (Person p : maleGalbraiths)
{
System.out.println("Found " + p);
}
}
}
更多精彩
赞助商链接