Package org.objectweb.speedo.runtime.inheritance

Source Code of org.objectweb.speedo.runtime.inheritance.TestIndex

/**
* 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.inheritance;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

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

import junit.framework.Assert;

import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.api.ExceptionHelper;
import org.objectweb.speedo.pobjects.inheritance.index.Daughter;
import org.objectweb.speedo.pobjects.inheritance.index.Mother;
import org.objectweb.util.monolog.api.BasicLevel;

/**
* Test the queries and extent with prefetch in case of inheritance.
* The aim is to have coherent order between java fields and sql columns.
* @author Y.Bersihand
*/
public class TestIndex extends SpeedoTestHelper {
 
  public TestIndex(String s) {
    super(s);
  }

  protected String getLoggerName() {
    return LOG_NAME + ".rt.inheritance.TestIndex";
  }

  /**
   * create the objects
   */
  public void testCreate() {
    PersistenceManager pm = pmf.getPersistenceManager();
    try {
      Mother m1 = new Mother(1,"mother1", true);
      Mother m2 = new Mother(2,"mother2", true);
      Daughter d1 = new Daughter(3,"daughter1", false, new Long(3));
      Daughter d2 = new Daughter(4,"daughter2", false, new Long(4));
      Collection col = new ArrayList();
      col.add(m1);
      col.add(m2);
      col.add(d1);
      col.add(d2);
      //  make persistent all the objects
      pm.currentTransaction().begin();
      pm.makePersistentAll(col);
      pm.currentTransaction().commit();
    } catch (Exception e) {
      fail(e.getMessage());
    } finally {
      if (pm.currentTransaction().isActive())
        pm.currentTransaction().rollback();
      //  remove all the objects from the cache
      pm.evictAll();
      pm.close();
    }
  }

  /**
   * query: select super
   */
  public void testSelectSuper() {
    PersistenceManager pm = pmf.getPersistenceManager();
    try {
      Query query = pm.newQuery(Mother.class);
        String filter = "java1 > 0";
        query.setFilter(filter);
        Collection result = (Collection) query.execute();
        assertNotNull(result);
        Iterator it = result.iterator();
        while (it.hasNext()) {
          Mother m = (Mother) it.next();
          assertTrue("Field java1 should be superior to 0.", m.getJava1()>0);
        }
    } catch (Exception e) {
      fail(e.getMessage());
    } finally {
      pm.evictAll();
      pm.close();
    }
  }
 
  /**
   * query: select subclass
   */
  public void testSelectSubclass() {
    PersistenceManager pm = pmf.getPersistenceManager();
    try {
      Query query = pm.newQuery(Daughter.class);
        String filter = "java1 > 0";
        query.setFilter(filter);
        Collection result = (Collection) query.execute();
        assertNotNull(result);
        Iterator it = result.iterator();
        while (it.hasNext()) {
          Mother m = (Mother) it.next();
          assertTrue("Field java1 should be superior to 0.", m.getJava1()>0);
        }
    } catch (Exception e) {
      fail(e.getMessage());
    } finally {
      pm.evictAll();
      pm.close();
    }
  }
 
  /**
   * extent: super with subclasses
   */
  public void testExtentSuperWithSubclasses() {
    computeExtent(Mother.class, true);
  }
 
  /**
   * extent: super without subclasses
   */
  public void testExtentSuperWithoutSubclasses() {
    computeExtent(Mother.class, false);
  }
 
  /**
   * extent: subclass with subclasses
   */
  public void testExtentSubWithSubclasses() {
    computeExtent(Daughter.class, true);
  }
 
  /**
   * extent: subclass without subclasses
   */
  public void testExtentSubWithoutSubclasses() {
    computeExtent(Daughter.class, false);
  }
 
  /**
   * extent
   */
  public void computeExtent(Class cl, boolean withSubclasses) {
    PersistenceManager pm = pmf.getPersistenceManager();
    try {
      Extent extent = pm.getExtent(cl, withSubclasses);
      Iterator it = extent.iterator();
      while(it.hasNext()) {
        Mother m = (Mother) it.next();
        assertTrue(m.getJava1()>0);
      }
    } catch (Exception e) {
      fail(e.getMessage());
    } finally {
      pm.evictAll();
      pm.close();
    }
  }
  /**
   * Remove the objects from the database.
   *
   */
  public void testRemovingOfPersistentObject() {
        PersistenceManager pm = pmf.getPersistenceManager();
        try {
            Class[] cs = new Class[]{Mother.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();
                    Assert.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 {
            pm.close();
        }
    }
}
TOP

Related Classes of org.objectweb.speedo.runtime.inheritance.TestIndex

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.