Package abook.data

Source Code of abook.data.TestBook

/**
* Copyright (C) 2001-2005 France Telecom R&D
*/
package abook.data;

import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.api.SpeedoProperties;
import org.objectweb.util.monolog.api.BasicLevel;

import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

public class TestBook extends SpeedoTestHelper {

    public TestBook() {
        this("TestBook");
    }

    public TestBook(String n) {
        super(n);
    }

    protected String getLoggerName() {
        return "TestBook";
    }

    public Properties getPMFProperties() {
        Properties p = super.getPMFProperties();
        p.setProperty(SpeedoProperties.MAPPING_STRUCTURE, SpeedoProperties.MAPPING_STRUCTURE_DD);
        return p;
    }
    private Collection loadNDetachBooks(String property,
            boolean ascending,
            int index,
            int pageSize) {
        PersistenceManager pm = pmf.getPersistenceManager();
        Query query = pm.newQuery(Book.class);
        if (ascending) {
            query.setOrdering(property + " ascending");
        } else {
            query.setOrdering(property + " descending");
        }
        query.setRange(index, index + pageSize);
        logger.log(BasicLevel.DEBUG, "FetchPlan=" + query.getFetchPlan().toString());
        Collection catalog = (Collection) query.execute();
        catalog = (Collection) pm.detachCopyAll(catalog);
        query.closeAll();
        pm.close();
        return catalog;

    }

    public void testFGAfterQuery() {
        final int NB_BOOKS = 20;
        final int NB_AUTHORS = 10;
        final int NB_AUTHORS_PER_BOOK = 3;
        {
            PersistenceManager pm = pmf.getPersistenceManager();
            pm.currentTransaction().begin();
            //Create authors
            List authors = new ArrayList(NB_AUTHORS);
            for(int i=0; i<10; i++) {
                String id = i2s(i, length(NB_AUTHORS));
                authors.add(new Author("FN_" + id, "LN_" + id, i));
            }
            pm.makePersistentAll(authors);

            //Create books
            for(int i=0; i<NB_BOOKS; i++) {
                Book book = new Book("Book_" + i2s(i, length(NB_BOOKS)), i);
                pm.makePersistent(book);
                for (int j = 0; j < NB_AUTHORS_PER_BOOK; j++) {
                     book.addAuthor((Author)
                             authors.get(((i % NB_AUTHORS) + j) % NB_AUTHORS));
                }
            }
            authors.clear();
            pm.currentTransaction().commit();
            pm.evictAll(); //empty the L2 cache
        }


        {
            PersistenceManager pm = pmf.getPersistenceManager();
            pm.getFetchPlan().setGroup("bookInfo");
            pm.detachCopy(pm.getObjectById(Book.class, new Long(0)));
            pm.close();
        }

        {
            Collection books = loadNDetachBooks("shortTitle", true, 0, Integer.MAX_VALUE);
           
            for (Iterator iter = books.iterator(); iter.hasNext();) {
                Book b = (Book) iter.next();
                assertNotNull("Null book", b);
                logger.log(BasicLevel.DEBUG, "Book: " + b.getBookId());
                try {
                    Collection authors = b.getAuthors();
                    for (Iterator authIt = authors.iterator(); authIt.hasNext();) {
                        Author a = (Author) authIt.next();
                        assertNotNull("Null book", a);
                    }
                } catch (JDOException e) {
                    fail("Fail to load the authors fields: " + e.getMessage());
                }
            }
        }
    }

}
TOP

Related Classes of abook.data.TestBook

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.