Package org.objectweb.speedo.runtime.inheritance

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

/**
* 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
*/
package org.objectweb.speedo.runtime.inheritance;

import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.pobjects.inheritance.filtered.datastorelid.RootClass;
import org.objectweb.speedo.pobjects.inheritance.filtered.datastorelid.Subclass1;
import org.objectweb.speedo.pobjects.inheritance.filtered.datastorelid.Subclass12;
import org.objectweb.speedo.pobjects.inheritance.filtered.datastorelid.Subclass2;
import org.objectweb.util.monolog.api.BasicLevel;
import javax.jdo.Extent;
import javax.jdo.Query;
import javax.jdo.PersistenceManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
*
* @author S.Chassande-Barrioz
*/
public class TestDatastorelid  extends SpeedoTestHelper {

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

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

  public void testA() {
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.currentTransaction().begin();
    ArrayList rcs = new ArrayList();
    rcs.add(new RootClass("rf1"));
    rcs.add(new RootClass("rf2"));
    rcs.add(new Subclass1("rf3", "f1_3"));
    rcs.add(new Subclass12("rf4", "f1_4", "f12_4"));
    rcs.add(new Subclass2("rf5", "f2_5"));
    pm.makePersistentAll(rcs);
    pm.currentTransaction().commit();

    pm.currentTransaction().begin();
    checkNumberOfInstanceWithExtent(RootClass.class, false, 2, pm);
    checkNumberOfInstanceWithExtent(RootClass.class, true, 5, pm);

    checkNumberOfInstanceWithExtent(Subclass1.class, false, 1, pm);
    checkNumberOfInstanceWithExtent(Subclass1.class, true, 2, pm);

    checkNumberOfInstanceWithExtent(Subclass12.class, true, 1, pm);
    checkNumberOfInstanceWithExtent(Subclass12.class, false, 1, pm);

    checkNumberOfInstanceWithExtent(Subclass2.class, false, 1, pm);
    checkNumberOfInstanceWithExtent(Subclass2.class, true, 1, pm);
    pm.currentTransaction().commit();

    pm.currentTransaction().begin();
    for(int i=0; i<rcs.size(); i++) {
      rcs.set(i, pm.getObjectId(rcs.get(i)));
    }
    pm.currentTransaction().commit();
    pm.evictAll();

    pm.currentTransaction().begin();
    for(int i=0; i<rcs.size(); i++) {
      pm.getObjectById(rcs.get(i), false);
    }
    pm.currentTransaction().commit();

    pm.evictAll();

    pm.currentTransaction().begin();
    Query query = pm.newQuery(RootClass.class);
    Collection col = (Collection) query.execute();
    Iterator it = col.iterator();
    int size = 0;
    while(it.hasNext()) {
      RootClass rc = (RootClass) it.next();
      size ++;
      logger.log(BasicLevel.DEBUG, "rc.rootField=" + rc.getRootField());
      if ("rf1".equals(rc.getRootField())) {
      } else if ("rf2".equals(rc.getRootField())) {
      } else if ("rf3".equals(rc.getRootField())) {
        assertTrue("Bad class instance" + rc.getClass().getName(), rc instanceof Subclass1);
        assertEquals("Bad f1 value", "f1_3", ((Subclass1) rc).getF1());
      } else if ("rf4".equals(rc.getRootField())) {
        assertTrue("Bad class instance" + rc.getClass().getName(), rc instanceof Subclass12);
        assertEquals("Bad f1 value", "f1_4", ((Subclass12) rc).getF1());
        assertEquals("Bad f1 value", "f12_4", ((Subclass12) rc).getF12());
      } else if ("rf5".equals(rc.getRootField())) {
        assertTrue("Bad class instance" + rc.getClass().getName(), rc instanceof Subclass2);
        assertEquals("Bad f1 value", "f2_5", ((Subclass2) rc).getF2());
      } else {
        fail("Unmanaged rootField value: " + rc.getRootField());
      }
    }
    query.closeAll();
    assertEquals("bad query result size", 5, size);
    pm.currentTransaction().commit();
   
    pm.currentTransaction().begin();
    for(int i=0; i<rcs.size(); i++) {
      pm.deletePersistent(pm.getObjectById(rcs.get(i), false));
    }
    pm.currentTransaction().commit();
    pm.close();

  }

  private void checkNumberOfInstanceWithExtent(Class clazz,
                         boolean withSubClass,
                         int expected,
                         PersistenceManager pm) {
    Extent e = pm.getExtent(clazz, withSubClass);
    Iterator it = e.iterator();
    int cpt = 0;
    ArrayList rfs = new ArrayList(expected);
    while(it.hasNext()) {
      rfs.add(((RootClass) it.next()).getRootField());
      cpt ++;
    }
    e.closeAll();
    assertEquals("Bad number of " + clazz.getName()
      + " instance (withSubclass: " + withSubClass + "): rfs=" + rfs,
      expected, cpt);
  }
}
TOP

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

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.