注意,存儲整個家庭所做的工作仍然不比存儲單個 Person 對象所做的工作多。您可能還記得,在上一篇文章中,由于存儲的對象具有遞歸的性質,當把 bruce 引用傳遞給 db.set() 調用時,從 bruce 可達的所有對象都被存儲。不過眼見為實,讓我們看看當運行我那個簡單的探察測試時,實際上會出現什么情況。首先,我將測試當調用隨 Person 存儲的各種 Address 時,是否可以找到它們。然后,我將測試是否孩子們也被存儲。
清單 5. 搜索住房和家庭
@Test public void testTheStorageOfAddresses()
{
List<Person> maleTates =
db.query(new Predicate<Person>() {
public boolean match(Person candidate) {
return candidate.getLastName().equals("Tate") &&
candidate.getGender().equals(Gender.MALE);
}
});
Person bruce = maleTates.get(0);
Address homeAndWork =
new Address("5 Maple Drive", "Austin",
"TX", "12345");
Address vacation =
new Address("10 Wanahokalugi Way", "Oahu",
"HA", "11223");
assertTrue(bruce.getHomeAddress().equals(homeAndWork));
assertTrue(bruce.getWorkAddress().equals(homeAndWork));
assertTrue(bruce.getVacationAddress().equals(vacation));
}
@Test public void testTheStorageOfChildren()
{
List<Person> maleTates =
db.query(new Predicate<Person>() {
public boolean match(Person candidate) {
return candidate.getLastName().equals("Tate") &&
candidate.getGender().equals(Gender.MALE);
}
});
Person bruce = maleTates.get(0);
int n = 0;
for (Iterator<Person> children = bruce.getChildren();
children.hasNext();
)
{
Person child = children.next();
System.out.println(child);
if (n==0) assertTrue(child.getFirstName().equals("Kayla"));
if (n==1) assertTrue(child.getFirstName().equals("Julia"));
n++;
}
}
文章來源于領測軟件測試網 http://www.kjueaiud.com/