001: /**
002: * Copyright (C) 2001-2005 France Telecom R&D
003: */package abook.data;
004:
005: import org.objectweb.speedo.SpeedoTestHelper;
006: import org.objectweb.speedo.api.SpeedoProperties;
007: import org.objectweb.util.monolog.api.BasicLevel;
008:
009: import java.io.ObjectInputStream.GetField;
010: import java.util.ArrayList;
011: import java.util.Collection;
012: import java.util.Iterator;
013: import java.util.List;
014: import java.util.Properties;
015:
016: import javax.jdo.JDOException;
017: import javax.jdo.PersistenceManager;
018: import javax.jdo.Query;
019:
020: public class TestBook extends SpeedoTestHelper {
021:
022: public TestBook() {
023: this ("TestBook");
024: }
025:
026: public TestBook(String n) {
027: super (n);
028: }
029:
030: protected String getLoggerName() {
031: return "TestBook";
032: }
033:
034: public Properties getPMFProperties() {
035: Properties p = super .getPMFProperties();
036: p.setProperty(SpeedoProperties.MAPPING_STRUCTURE,
037: SpeedoProperties.MAPPING_STRUCTURE_DD);
038: return p;
039: }
040:
041: private Collection loadNDetachBooks(String property,
042: boolean ascending, int index, int pageSize) {
043: PersistenceManager pm = pmf.getPersistenceManager();
044: Query query = pm.newQuery(Book.class);
045: if (ascending) {
046: query.setOrdering(property + " ascending");
047: } else {
048: query.setOrdering(property + " descending");
049: }
050: query.setRange(index, index + pageSize);
051: logger.log(BasicLevel.DEBUG, "FetchPlan="
052: + query.getFetchPlan().toString());
053: Collection catalog = (Collection) query.execute();
054: catalog = (Collection) pm.detachCopyAll(catalog);
055: query.closeAll();
056: pm.close();
057: return catalog;
058:
059: }
060:
061: public void testFGAfterQuery() {
062: final int NB_BOOKS = 20;
063: final int NB_AUTHORS = 10;
064: final int NB_AUTHORS_PER_BOOK = 3;
065: {
066: PersistenceManager pm = pmf.getPersistenceManager();
067: pm.currentTransaction().begin();
068: //Create authors
069: List authors = new ArrayList(NB_AUTHORS);
070: for (int i = 0; i < 10; i++) {
071: String id = i2s(i, length(NB_AUTHORS));
072: authors.add(new Author("FN_" + id, "LN_" + id, i));
073: }
074: pm.makePersistentAll(authors);
075:
076: //Create books
077: for (int i = 0; i < NB_BOOKS; i++) {
078: Book book = new Book(
079: "Book_" + i2s(i, length(NB_BOOKS)), i);
080: pm.makePersistent(book);
081: for (int j = 0; j < NB_AUTHORS_PER_BOOK; j++) {
082: book.addAuthor((Author) authors
083: .get(((i % NB_AUTHORS) + j) % NB_AUTHORS));
084: }
085: }
086: authors.clear();
087: pm.currentTransaction().commit();
088: pm.evictAll(); //empty the L2 cache
089: }
090:
091: {
092: PersistenceManager pm = pmf.getPersistenceManager();
093: pm.getFetchPlan().setGroup("bookInfo");
094: pm.detachCopy(pm.getObjectById(Book.class, new Long(0)));
095: pm.close();
096: }
097:
098: {
099: Collection books = loadNDetachBooks("shortTitle", true, 0,
100: Integer.MAX_VALUE);
101:
102: for (Iterator iter = books.iterator(); iter.hasNext();) {
103: Book b = (Book) iter.next();
104: assertNotNull("Null book", b);
105: logger.log(BasicLevel.DEBUG, "Book: " + b.getBookId());
106: try {
107: Collection authors = b.getAuthors();
108: for (Iterator authIt = authors.iterator(); authIt
109: .hasNext();) {
110: Author a = (Author) authIt.next();
111: assertNotNull("Null book", a);
112: }
113: } catch (JDOException e) {
114: fail("Fail to load the authors fields: "
115: + e.getMessage());
116: }
117: }
118: }
119: }
120:
121: }
|