01: package org.xorm.tests;
02:
03: import junit.framework.*;
04:
05: import org.xorm.XORM;
06: import org.xorm.tests.xml.*;
07: import javax.jdo.PersistenceManager;
08: import javax.jdo.JDOHelper;
09: import java.util.Collection;
10: import java.util.Iterator;
11:
12: public class TestXMLDriver extends XORMTestCase {
13: public void testRead() {
14: PersistenceManager mgr = factory.getPersistenceManager();
15: Collection roots = (Collection) mgr.newQuery(Library.class)
16: .execute();
17: Library lib = (Library) roots.iterator().next();
18: System.out.println("Library name: " + lib.getName());
19: System.out.println("Books:");
20: Iterator i = lib.getBooks().iterator();
21: while (i.hasNext()) {
22: Book b = (Book) i.next();
23: System.out.println("Title: " + b.getTitle());
24: System.out.println("Author: " + b.getAuthor());
25: System.out.println("Description: " + b.getDescription());
26: }
27: mgr.close();
28: }
29:
30: public void testWrite() {
31: PersistenceManager mgr = factory.getPersistenceManager();
32: mgr.currentTransaction().begin();
33: Collection roots = (Collection) mgr.newQuery(Library.class)
34: .execute();
35: Library lib = (Library) roots.iterator().next();
36:
37: lib.setName("Nonfiction");
38: Book b = (Book) XORM.newInstance(mgr, Book.class);
39: b.setTitle("The Three Musketeers");
40: b.setAuthor("Alexandre Dumas");
41: b.setDescription("French men on adventures.");
42: mgr.makePersistent(b);
43:
44: // Add it to the library
45: lib.getBooks().add(b);
46: b.setLibrary(lib);
47:
48: mgr.currentTransaction().commit();
49:
50: System.out.println("Library name: " + lib.getName());
51: System.out.println("Books:");
52: Iterator i = lib.getBooks().iterator();
53: while (i.hasNext()) {
54: b = (Book) i.next();
55: System.out.println("Title: " + b.getTitle());
56: System.out.println("Author: " + b.getAuthor());
57: System.out.println("Description: " + b.getDescription());
58: }
59:
60: mgr.close();
61: }
62:
63: public static void main(String args[]) {
64: String[] testCaseName = { TestXMLDriver.class.getName() };
65: junit.textui.TestRunner.main(testCaseName);
66: }
67:
68: public static Test suite() {
69: return new TestSuite(TestXMLDriver.class);
70: }
71:
72: }
|