Package org.objectweb.speedo.runtime.sequence.basic

Source Code of org.objectweb.speedo.runtime.sequence.basic.TestSequence

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

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

import javax.jdo.JDODataStoreException;
import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.datastore.Sequence;

import junit.framework.Assert;

import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.api.ExceptionHelper;
import org.objectweb.speedo.pobjects.sequence.basic.Cow;
import org.objectweb.util.monolog.api.BasicLevel;

/**
*
* @author Y.Bersihand
*/
public class TestSequence extends SpeedoTestHelper {
 
  public TestSequence(String s) {
    super(s);
  }

  protected String getLoggerName() {
    return LOG_NAME + ".rt.sequence.TestSequence";
  }
 
  public static final String COW_SEQ = "org.objectweb.speedo.pobjects.sequence.basic.cowseq";
 
  public static final String COW_SEQ_TRANSACTIONAL = "org.objectweb.speedo.pobjects.sequence.basic.cowseq_transactional";
 
  public static final String COW_SEQ_DATASTORE = "org.objectweb.speedo.pobjects.sequence.basic.cowseq_datastore";
 
  public static final int ADDITIONAL = 5;
 
  public static final int COMPARE_NUMBER = 100;
  /**
   * Get a sequence and use it to make objects persistent.
   */
  public void testSequence() {
    logger.log(BasicLevel.DEBUG, "***************testSequence*****************");
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.getObjectIdClass(Cow.class);
    //get the sequence
    Sequence s = pm.getSequence(COW_SEQ);
    assertNotNull("Sequence " + COW_SEQ + " should not be null.", s);
    Cow c1 = new Cow("cowSeq1", ((Long)s.next()).longValue());
    Cow c2 = new Cow("cowSeq2", ((Long)s.next()).longValue());
    Cow c3 = new Cow("cowSeq3", ((Long)s.next()).longValue());
    assertTrue(c1.getNbOfLegs() < c2.getNbOfLegs());
    assertTrue(c2.getNbOfLegs() < c3.getNbOfLegs());
    Collection c = new ArrayList();
    c.add(c1);
    c.add(c2);
    c.add(c3);
    //make persistent
    pm.currentTransaction().begin();
    pm.makePersistentAll(c);
    pm.currentTransaction().commit();
    logger.log(BasicLevel.DEBUG, "c1: " + c1.getNbOfLegs());
    logger.log(BasicLevel.DEBUG, "c2: " + c2.getNbOfLegs());
    logger.log(BasicLevel.DEBUG, "c3: " + c3.getNbOfLegs());
        pm.close();
  }
 
  /**
   * Test the allocate method.
   */
  public void testAllocate() {
    logger.log(BasicLevel.DEBUG, "***************testAllocate*****************");
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.getObjectIdClass(Cow.class);
    //get the sequence
    Sequence s = pm.getSequence(COW_SEQ);
    assertNotNull("Sequence " + COW_SEQ + " should not be null.", s);
    long beforeAllocate = ((Long) s.current()).longValue();
    Collection col = new ArrayList();
    Cow cow = null;
    for (int i = 1; i <= ADDITIONAL; i++) {
      cow = new Cow("cowAllocate" + i, ((Long)s.next()).longValue());
      col.add(cow);
    }
    assertEquals(beforeAllocate + ADDITIONAL, ((Long) s.current()).longValue());
    cow = new Cow("cowAfterAllocate", ((Long)s.next()).longValue());
    col.add(cow);
    pm.currentTransaction().begin();
    //make persistent
    pm.makePersistentAll(col);
    s.current();
    pm.currentTransaction().commit();
    Iterator it = col.iterator();
    while (it.hasNext()) {
      Cow c = (Cow) it.next();
      logger.log(BasicLevel.DEBUG, "cow " + c.getName() + ": " + c.getNbOfLegs());
    }
        pm.close();
  }
 
  /**
   * Compare the allocate method to the next mathod in terms of time elapsed.
   * The allocate should be faster than next.
   *
   */
  public void testCompareNextToAllocate() {
    logger.log(BasicLevel.DEBUG, "***************testCompareNextToAllocate*****************");
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.getObjectIdClass(Cow.class);
    //get the sequence
    Sequence s = pm.getSequence(COW_SEQ);
    assertNotNull("Sequence " + COW_SEQ + " should not be null.", s);
    long timeToGetManyNext = System.currentTimeMillis();
    for (int i  = 0; i< COMPARE_NUMBER; i++) {
      s.next();
    }
    timeToGetManyNext = System.currentTimeMillis() - timeToGetManyNext;
    long timeToGetManyAllocate = System.currentTimeMillis();
    s.allocate(COMPARE_NUMBER);
    for (int i  = 0; i< COMPARE_NUMBER; i++) {
      s.next();
    }
    timeToGetManyAllocate = System.currentTimeMillis() - timeToGetManyAllocate;
    logger.log(BasicLevel.DEBUG, COMPARE_NUMBER + " ids generated:\nwith allocate: " + timeToGetManyAllocate
        + "\nwith next: " + timeToGetManyNext);
    assertTrue("The time to get many ids with an allocate should be inferior to the time to get many ids with next()",
        timeToGetManyAllocate<timeToGetManyNext);
  }
 
  /**
   * Get a transactional sequence and use it outside a transaction.
   */
  public void testSequenceTransactional1() {
    logger.log(BasicLevel.DEBUG, "***************testSequenceTransactional1*****************");
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.getObjectIdClass(Cow.class);
    //get the sequence
    Sequence s = pm.getSequence(COW_SEQ_TRANSACTIONAL);
    assertNotNull("Sequence " + COW_SEQ_TRANSACTIONAL + " should not be null.", s);
    try {
      //use the sequence outside a tx
      Cow c1 = new Cow("cowSeqNC1", ((Long)s.next()).longValue());
      fail("An exception should be caught: a transactional sequence is used outside a transaction.");
    } catch (Exception e) {
      if (!JDODataStoreException.class.equals(e.getClass())) {
        logger.log(BasicLevel.ERROR, "Found exception: ", e);
      }
      assertEquals("Should be a JDODatastoreException due to the fact the  transactional"
          + "sequence has been used outside of a transaction. " + e.getMessage(), JDODataStoreException.class, e.getClass());
    } finally {
      if (pm.currentTransaction().isActive())
        pm.currentTransaction().rollback();
      pm.close();
    }
  }
 
  /**
   * Get a sequence and use it to make objects persistent.
   */
  public void testSequenceTransactional2() {
    logger.log(BasicLevel.DEBUG, "***************testSequenceTransactional2*****************");
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.getObjectIdClass(Cow.class);
    //get the sequence
    Sequence s = pm.getSequence(COW_SEQ_TRANSACTIONAL);
    assertNotNull("Sequence " + COW_SEQ_TRANSACTIONAL + " should not be null.", s);
    //begin the tx
    pm.currentTransaction().begin();
    Cow c1 = new Cow("cowSeqNC1", ((Long)s.next()).longValue());
    Cow c2 = new Cow("cowSeqNC2", ((Long)s.next()).longValue());
    Cow c3 = new Cow("cowSeqNC3", ((Long)s.next()).longValue());
    assertTrue(c1.getNbOfLegs() < c2.getNbOfLegs());
    assertTrue(c2.getNbOfLegs() < c3.getNbOfLegs());
    Collection c = new ArrayList();
    c.add(c1);
    c.add(c2);
    c.add(c3);
    //make persistent
    pm.makePersistentAll(c);
    pm.currentTransaction().commit();
    //begin a tx
    pm.currentTransaction().begin();
    Cow c4 = new Cow("cowSeqNC4", ((Long)s.next()).longValue());
    //keep the number
    long index = c4.getNbOfLegs();
    pm.makePersistent(c4);
    //rollback
    pm.currentTransaction().rollback();
    //begin a tx
    pm.currentTransaction().begin();
    //check the next number generated is not the one used for the rollback : gap
    assertTrue(((Long)s.next()).longValue() != index);
    pm.currentTransaction().commit();
        pm.close();
  }
 
  /**
   * Get a sequence linked to a datastore and use it to make objects persistent.
   */
  public void testSequenceDatastore() {
    logger.log(BasicLevel.DEBUG, "***************testSequenceDatastore*****************");
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.getObjectIdClass(Cow.class);
    //get the sequence
    Sequence s = pm.getSequence(COW_SEQ_DATASTORE);
    assertNotNull("Sequence " + COW_SEQ_DATASTORE + " should not be null.", s);
    //begin the tx
    pm.currentTransaction().begin();
    Cow c1 = new Cow("cowSeqDS1", ((Long)s.next()).longValue());
    Cow c2 = new Cow("cowSeqDS2", ((Long)s.next()).longValue());
    Cow c3 = new Cow("cowSeqDS3", ((Long)s.next()).longValue());
    assertTrue(c1.getNbOfLegs() < c2.getNbOfLegs());
    assertTrue(c2.getNbOfLegs() < c3.getNbOfLegs());
    Collection c = new ArrayList();
    c.add(c1);
    c.add(c2);
    c.add(c3);
    //make persistent
    pm.makePersistentAll(c);
    pm.currentTransaction().commit();
        pm.close();
  }
  /**
   * Remove all the persistence instances.
   */
  public void testRemovingOfPersistentObject() {
        PersistenceManager pm = pmf.getPersistenceManager();
        try {
            Class[] cs = new Class[]{Cow.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.sequence.basic.TestSequence

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.