www.goodsgy.com www.goodsgy.com 填充和测试对象模型
对于清单 2 中的 Person 类,需要重点注意的是,如果以关系的方式,使用父与子之间分层的、循环的引用来建模,那肯定会比较笨拙。通过一个实例化的对象模型可以更清楚地看到我所谈到的复杂性,所以我将编写一个探察测试来实例化 Person 类。注意,清单 3 中省略了 JUnit 支架(scaffolding);我假设您可以从其他地方,包括本系列之前的文章学习 JUnit 4 API。通过阅读本文的源代码,还可以学到更多东西。
清单 3. 幸福家庭测试
@Test public void testTheModel() { Person bruce = new Person("Bruce", "Tate", Gender.MALE, 29, Mood.HAPPY); Person maggie = new Person("Maggie", "Tate", Gender.FEMALE, 29, Mood.HAPPY); bruce.setSpouse(maggie);
Person kayla = maggie.haveBaby("Kayla", Gender.FEMALE);
Person julia = maggie.haveBaby("Julia", Gender.FEMALE);
assertTrue(julia.getFather() == bruce); assertTrue(kayla.getFather() == bruce); assertTrue(julia.getMother() == maggie); assertTrue(kayla.getMother() == maggie);
int n = 0; for (Iterator<Person> kids = bruce.getChildren(); kids.hasNext(); ) { Person child = kids.next();
if (n == 0) assertTrue(child == kayla); if (n == 1) assertTrue(child == julia);
n++; } } |
目前一切尚好。所有方面都能通过测试,包括小孩 ArrayList 的使用中的长嗣身份。但是,当我增加 @Before 和 @After 条件,以便用我的测试数据填充 db4o 数据库时,事情开始变得更有趣。
清单 4. 将孩子发送到数据库
@Before public void prepareDatabase() { db = Db4o.openFile("persons.data");
Person bruce = new Person("Bruce", "Tate", Gender.MALE, 29, Mood.HAPPY); Person maggie = new Person("Maggie", "Tate", Gender.FEMALE, 29, Mood.HAPPY); bruce.setSpouse(maggie);
bruce.setHomeAddress(new Address("5 Maple Drive", "Austin", "TX", "12345")); bruce.setWorkAddress( new Address("5 Maple Drive", "Austin", "TX", "12345")); bruce.setVacationAddress(new Address("10 Wanahokalugi Way", "Oahu", "HA", "11223"));
Person kayla = maggie.haveBaby("Kayla", Gender.FEMALE); kayla.setAge(8);
Person julia = maggie.haveBaby("Julia", Gender.FEMALE); julia.setAge(6);
db.set(bruce);
db.commit(); } |
注意,存储整个家庭所做的工作仍然不比存储单个 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++; } } |
www.goodsgy.com www.goodsgy.com 上一页 [1] [2] [3] 下一页
|