Package org.objectweb.speedo.runtime.sequence.id

Source Code of org.objectweb.speedo.runtime.sequence.id.TestSequenceId

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

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

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.id.CompactDisc;
import org.objectweb.speedo.pobjects.sequence.id.Phone;
import org.objectweb.speedo.pobjects.sequence.id.Product;
import org.objectweb.speedo.pobjects.sequence.id.Suitcase;
import org.objectweb.util.monolog.api.BasicLevel;

/**
* @author Y.Bersihand
*/
public class TestSequenceId extends SpeedoTestHelper {
 
  public static final int ADDITIONAL = 100;
 
  public TestSequenceId(String s) {
    super(s);
  }

  protected String getLoggerName() {
    return LOG_NAME + ".rt.sequence.TestSequence";
  }
 
  public static final String PRODUCT_SEQ = "org.objectweb.speedo.pobjects.sequence.id.product_seq";
  public static final String PHONE_SEQ = "org.objectweb.speedo.pobjects.sequence.id.phone_seq";
  public static final String SUITCASE_SEQ = "org.objectweb.speedo.pobjects.sequence.id.suitcase_seq";
  public static final String CD_SEQ = "org.objectweb.speedo.pobjects.sequence.id.cd_seq";
 
  /**
   * Get a sequence and use it to make objects persistent.
   * A field has a value-strategy set to sequence.
   */
  public void testSequenceId1() {
    logger.log(BasicLevel.DEBUG, "***************testSequenceId1*****************");
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.getObjectIdClass(Product.class);
    //get the sequence
    Sequence s = pm.getSequence(PRODUCT_SEQ);
    assertNotNull("Sequence " + PRODUCT_SEQ + " should not be null.", s);
    Product p1 = new Product();
    p1.setName("product 1");
    Product p2 = new Product();
    p2.setName("product 2");
    //make persistent
    pm.currentTransaction().begin();
    pm.makePersistent(p1);
    pm.makePersistent(p2);
    pm.currentTransaction().commit();
    assertTrue(p1.getReference() < p2.getReference());
        pm.close();
  }
 
  /**
   * Get a sequence and use it to make objects persistent.
   * A datastore-identity has a strategy set to sequence.
   */
  public void testSequenceId2() {
    logger.log(BasicLevel.DEBUG, "***************testSequenceId2*****************");
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.getObjectIdClass(Phone.class);
    //get the sequence
    Sequence s = pm.getSequence(PHONE_SEQ);
    assertNotNull("Sequence " + PHONE_SEQ + " should not be null.", s);
    Phone p1 = new Phone();
    p1.setName("phone 1");
    Phone p2 = new Phone();
    p2.setName("phone 2");
    //make persistent
    pm.currentTransaction().begin();
    pm.makePersistent(p1);
    pm.makePersistent(p2);
    pm.currentTransaction().commit();
        pm.close();
  }
 
  public void testAllocateRdbSequence() {
    logger.log(BasicLevel.DEBUG, "***************testAllocateRdbSequence*****************");
    PersistenceManager pm = pmf.getPersistenceManager();
    try {
      pm.getObjectIdClass(Phone.class);
      //get the sequence
      Sequence s = pm.getSequence(PHONE_SEQ);
      assertNotNull("Sequence " + PHONE_SEQ + " should not be null.", s);
      Phone[] phones = new Phone[ADDITIONAL];
      long timeAllocate = System.currentTimeMillis();
      pm.currentTransaction().begin();
      //allocate
      s.allocate(ADDITIONAL);
      for (int i = 0; i < ADDITIONAL ; i++) {
        phones[i] = new Phone();
        phones[i].setName("phone " + i);
      }
      //make persistent
      pm.makePersistentAll(phones);
      pm.currentTransaction().commit();
      timeAllocate = System.currentTimeMillis() - timeAllocate;
      long timeNext = System.currentTimeMillis();
      pm.currentTransaction().begin();
      for (int i = ADDITIONAL; i < ADDITIONAL*2 ; i++) {
        phones[i - ADDITIONAL] = new Phone();
        phones[i - ADDITIONAL].setName("phone " + i);
      }
      //make persistent
      pm.makePersistentAll(phones);
      pm.currentTransaction().commit();
      timeNext = System.currentTimeMillis() - timeNext;
      assertTrue("Time with allocate should be <= to time with next. \nAllocate: " + timeAllocate
          + "\nNext: " + timeNext, timeAllocate <= timeNext);
    } catch (Exception e) {
      fail(e.getMessage());
    } finally {
      if (pm.currentTransaction().isActive())
        pm.currentTransaction().rollback();
      pm.close();
    }
  }
 
  /**
   * Test a sequence which is not rdb.
   * The id of the class is application.
   *
   */
  public void testApplicationId1() {
    logger.log(BasicLevel.DEBUG, "***************testApplicationId1*****************");
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.getObjectIdClass(Suitcase.class);
    //get the sequence
    Sequence s = pm.getSequence(SUITCASE_SEQ);
    assertNotNull("Sequence " + SUITCASE_SEQ + " should not be null.", s);
    Suitcase s1 = new Suitcase();
    s1.setColor("black");
    Suitcase s2 = new Suitcase();
    s2.setColor("grey");
    logger.log(BasicLevel.DEBUG, "Value is " + s.currentValue());
    //make persistent
    pm.currentTransaction().begin();
    pm.makePersistent(s1);
    pm.makePersistent(s2);
    pm.currentTransaction().commit();
    assertTrue(s1.getId() < s2.getId());
    logger.log(BasicLevel.DEBUG, "After commit, value is " + s.currentValue());
        pm.close();
  }
 
  /**
   * Get a non-rdb sequence and use it to make objects persistent.
   * A datastore-identity has a strategy set to sequence.
   */
  public void testApplicationId2() {
    logger.log(BasicLevel.DEBUG, "***************testApplicationId2*****************");
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.getObjectIdClass(CompactDisc.class);
    //get the sequence
    Sequence s = pm.getSequence(CD_SEQ);
    assertNotNull("Sequence " + CD_SEQ + " should not be null.", s);
    CompactDisc cd1 = new CompactDisc();
    cd1.setTitle("cd1");
    CompactDisc cd2 = new CompactDisc();
    cd1.setTitle("cd2");
    //make persistent
    pm.currentTransaction().begin();
    pm.makePersistent(cd1);
    pm.makePersistent(cd2);
    pm.currentTransaction().commit();
        pm.close();
  }
 
  /**
   * Remove all the persistent instances.
   */
  public void testRemovingOfPersistentObject() {
        PersistenceManager pm = pmf.getPersistenceManager();
        try {
            Class[] cs = new Class[]{Product.class,
                        Phone.class,
                  Suitcase.class,
                  CompactDisc.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.id.TestSequenceId

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.