Package org.objectweb.speedo.runtime.fetchgroup

Source Code of org.objectweb.speedo.runtime.fetchgroup.TestPredefinedFetchGroup

/**
* Speedo: an implementation of JDO compliant personality on top of JORM generic
* I/O sub-system.
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
*
*
* Contact: speedo@objectweb.org
*
*/

package org.objectweb.speedo.runtime.fetchgroup;


import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

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

import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.api.ExceptionHelper;
import org.objectweb.speedo.pobjects.fetchgroup.Address;
import org.objectweb.speedo.pobjects.fetchgroup.Country;
import org.objectweb.speedo.pobjects.fetchgroup.Person;
import org.objectweb.util.monolog.api.BasicLevel;

/**
* Test the 4 pre-defined fetchgroups:
*   1- all : all the fields of the class are loaded
*   2- default: all the fields defined as default-fetch-group="true" in the jdo file are loaded
*   3- none: only the primary key of the class is loaded but int he case of speedo (and because of jorm)
*       all primitive fields are loaded.
*   4- values: all the fields that are included in the default fetch group by default
*       (primitives, wrappers, String, Date, etc...) are loaded.
* @author Y.Bersihand
*/
public class TestPredefinedFetchGroup extends SpeedoTestHelper {

  public TestPredefinedFetchGroup(String s) {
    super(s);
  }

  protected String getLoggerName() {
    return LOG_NAME + ".rt.fetchgroup.TestPredefinedFetchGroup";
  }
 
  /**
   * Test the loading with the "all" fetch group : all the fields of the class are loaded
   */
  public void testLoadingAll() {
    logger.log(BasicLevel.DEBUG, "***************testLoadingAll*****************");
    Country country = new Country("it","Italie");
    Address address = new Address("Rue Spiaggi", "Milan", country);
    Person parent = new Person();
    parent.setName("Del Piero Joel");
    parent.setAge(32);
    parent.setAddress(address);
    Person child1 = new Person("Del Piero Sophie", address, null, 14);
    Person child2 = new Person("Del Piero Mikael", address, null, 11);
    Set children = new HashSet();
    children.add(child1);
    children.add(child2);
    parent.setChildren(children);
   
    PersistenceManager pm = pmf.getPersistenceManager();
    FetchPlan fp = pm.getFetchPlan();
    fp.addGroup("all").removeGroup("default");
    pm.currentTransaction().begin();
    logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
    pm.makePersistent(parent);
    pm.currentTransaction().commit();
   
    FetchPlan f = pm.getFetchPlan();
    logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
   
    try{
      pm.currentTransaction().begin();
      Person detachedParent = (Person) pm.detachCopy(parent);
      logger.log(BasicLevel.DEBUG, "All fields can be accessed: " + detachedParent.toString());
      assertEquals(parent.getName(), detachedParent.getName());
      assertEquals(parent.getAge(), detachedParent.getAge());
      assertEquals(parent.getAddress().getCity(), detachedParent.getAddress().getCity());
      assertEquals(parent.getAddress().getCountry().getCode(), detachedParent.getAddress().getCountry().getCode());
      assertEquals(parent.getAddress().getCountry().getName(), detachedParent.getAddress().getCountry().getName());
      assertEquals(parent.getAddress().getStreet(), detachedParent.getAddress().getStreet());
      assertEquals(parent.getChildren().size(), detachedParent.getChildren().size());
    }
    catch(Exception e){
        logger.log(BasicLevel.DEBUG, "Error: " + e);
    } finally {
      if (pm.currentTransaction().isActive())
        pm.currentTransaction().rollback();
      pm.close();
    }
  }
 
  /**
   * Test the loading with the "default" fetch group
   */
  public void testLoadingDefault() {
    logger.log(BasicLevel.DEBUG, "***************testLoadingDefault*****************");
    Country country = new Country("fr","France");
    Address address = new Address("rue Anatole France", "Tours", country);
    Person parent = new Person();
    parent.setName("Bordes Joel");
    parent.setAge(32);
    parent.setAddress(address);
    Person child1 = new Person("Bordes Sophie", address, null, 14);
    Person child2 = new Person("Bordes Mikael", address, null, 11);
    Set children = new HashSet();
    children.add(child1);
    children.add(child2);
    parent.setChildren(children);
   
    PersistenceManager pm = pmf.getPersistenceManager();
    FetchPlan fp = pm.getFetchPlan();
    fp.removeGroup("all").addGroup("default");
    pm.currentTransaction().begin();
    logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
    pm.makePersistent(parent);
    pm.currentTransaction().commit();
   
    FetchPlan f = pm.getFetchPlan();
    logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
   
    try{   
      pm.currentTransaction().begin();
      Person detachedParent = (Person) pm.detachCopy(parent);
      logger.log(BasicLevel.DEBUG, "Name can be accessed: " + detachedParent.getName());
      logger.log(BasicLevel.DEBUG, "Age can be accessed: " + detachedParent.getAge());
      logger.log(BasicLevel.DEBUG, "Address can be accessed: " + detachedParent.getAddress().toString());
      assertEquals(parent.getName(), detachedParent.getName());
      assertEquals(parent.getAge(), detachedParent.getAge());
      assertEquals(parent.getAddress().getCity(), detachedParent.getAddress().getCity());
      assertEquals(parent.getAddress().getCountry().getCode(), detachedParent.getAddress().getCountry().getCode());
      assertEquals(parent.getAddress().getCountry().getName(), detachedParent.getAddress().getCountry().getName());
      assertEquals(parent.getAddress().getStreet(), detachedParent.getAddress().getStreet());
      logger.log(BasicLevel.DEBUG, "Children should not be accessed: " + detachedParent.getChildren());
    }
    catch(Exception e){
      assertEquals(e.getClass(), JDODetachedFieldAccessException.class);
      pm.close();
      if (e instanceof JDODetachedFieldAccessException)
        logger.log(BasicLevel.DEBUG, "Correct exception type caught: " + e);
      else
        logger.log(BasicLevel.DEBUG, "Incorrect exception type caught: " + e);
    } finally {
      if (pm.currentTransaction().isActive())
        pm.currentTransaction().rollback();
      pm.close();
    }
  }

 
  /**
   * Test the loading with the "none" fetch group : only the pk is loaded
   */
  public void testLoadingNone() {
    logger.log(BasicLevel.DEBUG, "***************testLoadingNone*****************");
    Country country = new Country("uk","Royaume-Uni");
    Address address = new Address("Sharrow Street", "Luton", country);
    Person parent = new Person();
    parent.setName("None Joel");
    parent.setAge(32);
    parent.setAddress(address);
    Person child1 = new Person("None Sophie", address, null, 14);
    Person child2 = new Person("None Mikael", address, null, 11);
    Set children = new HashSet();
    children.add(child1);
    children.add(child2);
    parent.setChildren(children);
   
    PersistenceManager pm = pmf.getPersistenceManager();
    FetchPlan fp = pm.getFetchPlan();
    fp.removeGroup("default").addGroup("none");
    pm.currentTransaction().begin();
    logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
    pm.makePersistent(parent);
    pm.currentTransaction().commit();
   
    FetchPlan f = pm.getFetchPlan();
    logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
   
    try
      pm.currentTransaction().begin();
      Person detachedParent = (Person) pm.detachCopy(parent);
      logger.log(BasicLevel.DEBUG, "Name can be accessed: " + detachedParent.getName());
      logger.log(BasicLevel.DEBUG, "Age can be accessed: " + detachedParent.getAge());
      assertEquals(parent.getName(), detachedParent.getName());
      assertEquals(parent.getAge(), detachedParent.getAge());
      logger.log(BasicLevel.DEBUG, "Address should not be accessed: " + detachedParent.getAddress());
      logger.log(BasicLevel.DEBUG, "Children should not be accessed: " + detachedParent.getChildren());
    }
    catch(Exception e){
      assertEquals(e.getClass(), JDODetachedFieldAccessException.class);
      if(e instanceof JDODetachedFieldAccessException)
        logger.log(BasicLevel.DEBUG, "Correct exception type caught: " + e);
      else
        logger.log(BasicLevel.DEBUG, "Incorrect exception type caught: " + e);
    } finally {
      if (pm.currentTransaction().isActive())
        pm.currentTransaction().rollback();
      pm.close();
    }
  }
 
  /**
   * Test the loading with the "values" fetch group : only the values field of the default are loaded
   */
  public void testLoadingValues() {
    logger.log(BasicLevel.DEBUG, "***************testLoadingValues*****************");
    Country country = new Country("gr","Grece");
    Address address = new Address("Feta", "Athenes", country);
    Person parent = new Person();
    parent.setName("Kapic Joel");
    parent.setAge(32);
    parent.setAddress(address);
    Person child1 = new Person("Kapic Sophie", address, null, 14);
    Person child2 = new Person("Kapic Mikael", address, null, 11);
    Set children = new HashSet();
    children.add(child1);
    children.add(child2);
    parent.setChildren(children);
   
    PersistenceManager pm = pmf.getPersistenceManager();
    FetchPlan fp = pm.getFetchPlan();
    fp.removeGroup("none").addGroup("values");
    pm.currentTransaction().begin();
    logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
    pm.makePersistent(parent);
    pm.currentTransaction().commit();
   
    FetchPlan f = pm.getFetchPlan();
    logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
   
    try{
      pm.currentTransaction().begin();
      Person detachedParent = (Person) pm.detachCopy(parent);
      logger.log(BasicLevel.DEBUG, "Name can be accessed: " + detachedParent.getName());
      logger.log(BasicLevel.DEBUG, "Age can be accessed: " + detachedParent.getAge());
      assertEquals(parent.getName(), detachedParent.getName());
      assertEquals(parent.getAge(), detachedParent.getAge());
      logger.log(BasicLevel.DEBUG, "Address should not be accessed: " + detachedParent.getAddress());
      logger.log(BasicLevel.DEBUG, "Children should not be accessed: " + detachedParent.getChildren());
    }
    catch(Exception e){
      assertEquals(e.getClass(), JDODetachedFieldAccessException.class);
      if(e instanceof JDODetachedFieldAccessException)
        logger.log(BasicLevel.DEBUG, "Correct exception type caught: " + e);
      else
        logger.log(BasicLevel.DEBUG, "Incorrect exception type caught: " + e);
    } finally {
      if (pm.currentTransaction().isActive())
        pm.currentTransaction().rollback();
      pm.close();
    }
  }
 
 
  public void testRemovingOfPersistentObject() {
        PersistenceManager pm = pmf.getPersistenceManager();
        try {
            Class[] cs = new Class[]{Person.class, Address.class, Country.class};
          pm.currentTransaction().begin();
            for(int i=0; i<cs.length; i++) {
                Query query = pm.newQuery(cs[i]);
                Collection col = (Collection) query.execute();
                Iterator it = col.iterator();
                while(it.hasNext()) {
                    Object o = it.next();
                    assertNotNull("null object in the query result"
                        + cs[i].getName(), o);
                    pm.deletePersistent(o);

                }
                query.close(col);
            }
          pm.currentTransaction().commit();
        } catch (JDOException e) {
            Exception ie = ExceptionHelper.getNested(e);
            logger.log(BasicLevel.ERROR, "", ie);
            fail(ie.getMessage());
        } finally {
          if (pm.currentTransaction().isActive())
        pm.currentTransaction().rollback();
            pm.close();
        }
    }
}
TOP

Related Classes of org.objectweb.speedo.runtime.fetchgroup.TestPredefinedFetchGroup

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.