Package org.objectweb.speedo.runtime.ref

Source Code of org.objectweb.speedo.runtime.ref.TestSimpleRef

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

import junit.framework.Assert;
import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.pobjects.ref.Department;
import org.objectweb.speedo.pobjects.ref.Employee;

import javax.jdo.PersistenceManager;

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

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

  protected String getLoggerName() {
    return LOG_NAME + "rt.ref.SimpleRef";
  }
  public void test1() {
    String eName = "seb";
    String dName = "ASR";
    PersistenceManager pm = pmf.getPersistenceManager();
    Employee e = new Employee(eName, new Department(dName));
    pm.makePersistent(e);
    Object eId = pm.getObjectId(e);
    Assert.assertNotNull("null object identifier", eId);
    pm.close();

    e = null;
    pm = pmf.getPersistenceManager();
    e = (Employee) pm.getObjectById(eId, true);
    Assert.assertNotNull("null instance returned by getObjectById", e);
    Assert.assertEquals("Bad employee name", eName, e.getName());
    Assert.assertNotNull("null instance returned by e.dept", e.getDept());
    Assert.assertEquals("Bad department name", dName, e.getDept().getName());
    pm.currentTransaction().begin();
    pm.deletePersistent(e.getDept());
    pm.deletePersistent(e);
    pm.currentTransaction().commit();
    pm.close();
  }

    public void testNullRef() {
        String eName = "nullRefEmp";
        PersistenceManager pm = pmf.getPersistenceManager();
        Employee e = new Employee(eName, null);
        pm.makePersistent(e);
        Object eId = pm.getObjectId(e);
        Assert.assertNotNull("null object identifier", eId);
        pm.close();

        e = null;
        pm = pmf.getPersistenceManager();
        e = (Employee) pm.getObjectById(eId, true);
        Assert.assertNotNull("null instance returned by getObjectById", e);
        Assert.assertEquals("Bad employee name", eName, e.getName());
        Assert.assertNull("not null instance returned by e.dept", e.getDept());
        pm.currentTransaction().begin();
        pm.deletePersistent(e);
        pm.currentTransaction().commit();
        pm.close();
    }
    public void testDeleteReferencedObjectWithEvict() {
        testDeleteReferencedObject(true);
    }
    public void testDeleteReferencedObject() {
        testDeleteReferencedObject(false);
    }
    public void testDeleteReferencedObject(boolean withEvict) {
        PersistenceManager pm = pmf.getPersistenceManager();

        pm.currentTransaction().begin();
        Employee e = new Employee("employee testDeleteReferencedObject",
                new Department("department testDeleteReferencedObject"));
        pm.makePersistent(e);
        long eid = e.getOid();
        Long did = e.getDept().getOid();
        e= null;
        pm.currentTransaction().commit();

        pm.currentTransaction().begin();
        Department d = (Department) pm.getObjectById(
                pm.newObjectIdInstance(Department.class, "" + did), true);
        pm.deletePersistent(d);
        d = null;
        pm.currentTransaction().commit();
       
        if (withEvict) {
            pm.evictAll();
        }
        pm.currentTransaction().begin();
        e = (Employee) pm.getObjectById(
                pm.newObjectIdInstance(Employee.class, "" + eid), true);
        boolean isNull = (null == e.getDept());
        pm.currentTransaction().commit();
       
        pm.currentTransaction().begin();
        pm.deletePersistent(e);
        pm.currentTransaction().commit();
        pm.close();
        assertTrue("Reference is not null", isNull);
    }
}
TOP

Related Classes of org.objectweb.speedo.runtime.ref.TestSimpleRef

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.