Package org.objectweb.speedo.runtime.collection

Source Code of org.objectweb.speedo.runtime.collection.TestEmployee

/**
* 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
*
* Authors: S.Chassande-Barrioz.
*
*/
package org.objectweb.speedo.runtime.collection;

import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.pobjects.collection.Employee;
import org.objectweb.speedo.pobjects.collection.Group;
import org.objectweb.speedo.pobjects.collection.User;
import org.objectweb.speedo.pobjects.collection.Ref2StringSet;
import org.objectweb.util.monolog.api.BasicLevel;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.JDOHelper;

import junit.framework.Assert;

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

public class TestEmployee extends SpeedoTestHelper {

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

  protected String getLoggerName() {
    return LOG_NAME + ".rt.collection.TestEmployee";
  }

  public void testCreation1() {
        logger.log(BasicLevel.DEBUG, "Start testCreation1");
        logger.log(BasicLevel.DEBUG, "Create Employee");
        Employee e1 = new Employee("e1");
        Employee e2 = new Employee("e2");
        e1.setBoss(e2);

        PersistenceManager pm = pmf.getPersistenceManager();
        pm.makePersistent(e1);
        Object eId = pm.getObjectId(e1);
        Assert.assertNotNull("null object identifier", eId);
        pm.close();
        e1 = null;
        e2 = null;
        pm = pmf.getPersistenceManager();
        e1 = (Employee) pm.getObjectById(eId, true);
        Assert.assertNotNull("null instance returned by getObjectById", e1);
        Assert.assertEquals("Bad employee name", "e1", e1.getName());
        Assert.assertNotNull("null instance returned by getObjectById", e1.getBoss());
        Assert.assertEquals("Bad boss name", "e2", e1.getBoss().getName());
        pm.currentTransaction().begin();
        pm.deletePersistent(e1);
        pm.deletePersistent(e1.getBoss());
        pm.currentTransaction().commit();
        pm.close();
  }

    public void testCreation2() {
        logger.log(BasicLevel.DEBUG, "Start testCreation2");
        logger.log(BasicLevel.DEBUG, "Create Employee");
        Employee e1 = new Employee("e1");
        e1.setBoss(e1);

        PersistenceManager pm = pmf.getPersistenceManager();
        pm.makePersistent(e1);
        Object eId = pm.getObjectId(e1);
        Assert.assertNotNull("null object identifier", eId);
        pm.close();

        e1 = null;
        pm = pmf.getPersistenceManager();
        e1 = (Employee) pm.getObjectById(eId, true);
        Assert.assertNotNull("null instance returned by getObjectById", e1);
        Assert.assertEquals("Bad employee name", "e1", e1.getName());
        Assert.assertNotNull("null instance returned by getObjectById", e1.getBoss());
        Assert.assertEquals("Bad boss", e1, e1.getBoss());
        pm.currentTransaction().begin();
        pm.deletePersistent(e1);
        pm.currentTransaction().commit();
        pm.close();
    }

    public void testCreation3() {
        logger.log(BasicLevel.DEBUG, "Start testCreation3");
        logger.log(BasicLevel.DEBUG, "Create Employee");
        Employee e1 = new Employee("e1", null);
        Employee e2 = new Employee("e2");

        e1.addFriend(e2);
        Assert.assertEquals("Bad friends set size", e1.getFriendsCol().size(), 1);
        Assert.assertTrue("Bad friends set content", e1.getFriendsCol().contains(e2));

        e1.addInt(1);
        Assert.assertEquals("Bad ints set size", e1.getIntsCol().size(), 1);
        Assert.assertTrue("Bad ints set content", e1.getIntsCol().contains(new Integer(1)));

        PersistenceManager pm = pmf.getPersistenceManager();
        pm.makePersistent(e1);
        Object eId = pm.getObjectId(e1);
        Assert.assertNotNull("null object identifier", eId);
        pm.close();

        e1 = null;
        pm = pmf.getPersistenceManager();
        e1 = (Employee) pm.getObjectById(eId, true);
        Assert.assertNotNull("null instance returned by getObjectById", e1);
        Assert.assertEquals("Bad employee name", "e1", e1.getName());

        Assert.assertNotNull("null collection returned by getObjectById", e1.getFriendsCol());
        Assert.assertEquals("Bad friends set size", e1.getFriendsCol().size(), 1);
        Assert.assertTrue("Bad friends set content", e1.getFriendsCol().iterator().hasNext());
        Assert.assertNotNull("Bad friends set content", e1.getFriendsCol().iterator().next());
        Assert.assertEquals("Bad friends set content", ((Employee) e1.getFriendsCol().iterator().next()).getName(), "e2");

        Assert.assertNotNull("null collection returned by getObjectById", e1.getIntsCol());
        Assert.assertEquals("Bad ints set size", e1.getIntsCol().size(), 1);
        Assert.assertTrue("Bad ints set content", e1.getIntsCol().iterator().hasNext());
        Assert.assertNotNull("Bad ints set content", e1.getIntsCol().iterator().next());
        Assert.assertTrue("Bad ints set content", e1.getIntsCol().contains(new Integer(1)));
        pm.currentTransaction().begin();
        pm.deletePersistent(e2);
        pm.deletePersistent(e1);
        pm.currentTransaction().commit();
        pm.close();
    }

    public void testCreation4() {
        logger.log(BasicLevel.DEBUG, "Start testCreation4");
        logger.log(BasicLevel.DEBUG, "Create Employee");
        Employee e1 = new Employee("e1", null);

        PersistenceManager pm = pmf.getPersistenceManager();
        pm.makePersistent(e1);
        Object eId = pm.getObjectId(e1);
        Assert.assertNotNull("null object identifier", eId);

        Employee e2 = new Employee("e2");

        e1.addFriend(e2);
        Assert.assertEquals("Bad friends set size", e1.getFriendsCol().size(), 1);
        Assert.assertTrue("Bad friends set content", e1.getFriendsCol().contains(e2));

        e1.addInt(1);
        Assert.assertEquals("Bad ints set size", e1.getIntsCol().size(), 1);
        Assert.assertTrue("Bad ints set content", e1.getIntsCol().contains(new Integer(1)));

        pm.close();

        e1 = null;
        pm = pmf.getPersistenceManager();
        e1 = (Employee) pm.getObjectById(eId, true);
        Assert.assertNotNull("null instance returned by getObjectById", e1);
        Assert.assertEquals("Bad employee name", "e1", e1.getName());

        Assert.assertNotNull("null collection returned by getObjectById", e1.getFriendsCol());
        Assert.assertEquals("Bad friends set size", e1.getFriendsCol().size(), 1);
        Assert.assertTrue("Bad friends set content", e1.getFriendsCol().iterator().hasNext());
        Assert.assertNotNull("Bad friends set content", e1.getFriendsCol().iterator().next());
        Assert.assertEquals("Bad friends set content", ((Employee) e1.getFriendsCol().iterator().next()).getName(), "e2");

        Assert.assertNotNull("null collection returned by getObjectById", e1.getIntsCol());
        Assert.assertEquals("Bad ints set size", e1.getIntsCol().size(), 1);
        Assert.assertTrue("Bad ints set content", e1.getIntsCol().iterator().hasNext());
        Assert.assertNotNull("Bad ints set content", e1.getIntsCol().iterator().next());
        Assert.assertTrue("Bad ints set content", e1.getIntsCol().contains(new Integer(1)));
        Object o = e2.getName();
        pm.currentTransaction().begin();
        pm.deletePersistent(e1);
        pm.deletePersistent(e2);
        pm.currentTransaction().commit();
        pm.close();
    }

    public void testCollectionLoading() {
        logger.log(BasicLevel.DEBUG, "Start testCollectionLoading");
        PersistenceManager pm = pmf.getPersistenceManager();
        Query q = pm.newQuery(Employee.class);
        q.declareParameters("String en");
        q.setFilter("(en == name)");
        Collection c = (Collection) q.execute(POBuilder.EMPLOYEE_NAME);
        Assert.assertNotNull("Collection returned by the query is null", c);
        Iterator it = c.iterator();
        Assert.assertNotNull("Iterator over the collection returned by the query is null", it);
        if (!it.hasNext()) {
            fail("Run the POBuilder test before this test in order to create objects");
        }
        Object o = it.next();
        q.closeAll();

        Assert.assertNotNull("Null object returned by the iterator", o);
        Assert.assertEquals("bad object type", Employee.class, o.getClass());
        Employee e = (Employee) o;
        Collection elems = e.getFriendsCol();
        Assert.assertNotNull("Null collection field", elems);
        Iterator elemIt = elems.iterator();
        Assert.assertNotNull("Null iterator over elements", elemIt);
        ArrayList elemNames = new ArrayList();
        while(elemIt.hasNext()) {
            Object elem = elemIt.next();
            Assert.assertNotNull("Null element", elem);
            Assert.assertEquals("bad elem type", Employee.class, elem.getClass());
            elemNames.add(((Employee) elem).getName());
        }
        assertSameCollection("Bad element names: ", POBuilder.getElementNames(), elemNames);
        pm.close();
    }

    public void testCheckSetCollection() {
        logger.log(BasicLevel.DEBUG, "Start testCheckSetCollection");
        PersistenceManager pm = pmf.getPersistenceManager();
        Query q = pm.newQuery(Employee.class);
        q.declareParameters("String en");
        q.setFilter("(en == name)");
        Collection c = (Collection) q.execute(POBuilder.EMPLOYEE_NAME2);
        Assert.assertNotNull("Collection returned by the query is null", c);
        Iterator it = c.iterator();
        Assert.assertNotNull("Iterator over the collection returned by the query is null", it);
        if (!it.hasNext()) {
            fail("Run the POBuilder test before this test in order to create objects");
        }
        Object o = it.next();
        q.closeAll();

        Assert.assertNotNull("Null object returned by the iterator", o);
        Assert.assertEquals("bad object type", Employee.class, o.getClass());
        Employee e = (Employee) o;
        Collection elems = e.getFriendsCol();
        Assert.assertNotNull("Null collection field", elems);
        Iterator elemIt = elems.iterator();
        Assert.assertNotNull("Null iterator over elements", elemIt);
        ArrayList elemNames = new ArrayList();
        while(elemIt.hasNext()) {
            Object elem = elemIt.next();
            Assert.assertNotNull("Null element", elem);
            Assert.assertEquals("bad elem type", Employee.class, elem.getClass());
            elemNames.add(((Employee) elem).getName());
        }
        assertSameCollection("Bad element names: ", POBuilder.getElementNames2(), elemNames);
        pm.close();
    }

  public void testReachability() {
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.currentTransaction().begin();

        final Group group = new Group("Group One");

        final User user = new User("Tom");
        user.setE_mail("tom@example.com");
        group.getUsers().add(user);
    assertTrue("User already marked as persistent before the makePersitent action: ",
      !JDOHelper.isPersistent(user));
    assertTrue("Group already marked as persistent before the makePersitent action: ",
      !JDOHelper.isPersistent(group));

        pm.makePersistent(group);

    assertTrue("Group is not marked as persistent after the makePersitent action: ",
      JDOHelper.isPersistent(group));
    assertTrue("User is marked as persistent after the makePersitent action (back hole)",
      JDOHelper.isPersistent(user));
    assertTrue("User is not marked as New after the makePersitent action: ",
      JDOHelper.isNew(user));
    assertTrue("User is not marked as Dirty after the makePersitent action: ",
      JDOHelper.isDirty(user));
        pm.currentTransaction().commit();

    pm.currentTransaction().begin();
    pm.deletePersistent(user);
    pm.deletePersistent(group);
        pm.currentTransaction().commit();

        pm.close();

  }

  public void testRef2StringSet() {
    PersistenceManager pm = pmf.getPersistenceManager();

    pm.currentTransaction().begin();
    Ref2StringSet rss = new Ref2StringSet("testRef2StringSet");
    rss.getStrings().add("str1");
    rss.getStrings().add("str2");
    rss.getStrings().add("str3");
    assertTrue("Bad return value for a new value",
      !rss.getStrings().add("str3"));
    pm.makePersistent(rss);
    pm.currentTransaction().commit();

    assertTrue("Bad return value for a new value",
      rss.getStrings().add("str4"));
    assertTrue("Bad return value for an existing value",
      !rss.getStrings().add("str3"));

    pm.currentTransaction().begin();
    pm.deletePersistent(rss);
    pm.currentTransaction().commit();

    pm.close();

  }
 
  public void testColModifNSet() {
        Employee e1 = new Employee("e1");
        Employee e2 = new Employee("e2");
        Employee e3 = new Employee("e3");
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.currentTransaction().begin();
        pm.makePersistent(e1);
        Object oid1 = pm.getObjectId(e1);
        pm.makePersistent(e2);
        Object oid2 = pm.getObjectId(e2);
        pm.makePersistent(e3);
        Object oid3 = pm.getObjectId(e3);
        e1.addFriend(e2);
    pm.currentTransaction().commit();
       
    pm.currentTransaction().begin();
    Collection friends = e1.getFriendsCol();
    friends.remove(e2);
    friends.add(e3);
    e1.setFriends(friends);
    pm.currentTransaction().commit();

    e1 = null;
    e2 = null;
    e3 = null;
    pm.evictAll();
   
    pm.currentTransaction().begin();
    e1 = (Employee) pm.getObjectById(oid1, false);
    e2 = (Employee) pm.getObjectById(oid2, false);
    e3 = (Employee) pm.getObjectById(oid3, false);
    int s = e1.getFriendsCol().size();
    boolean containse2 = e1.getFriendsCol().contains(e2);
    boolean containse3 = e1.getFriendsCol().contains(e3);
    pm.deletePersistent(e1);
    pm.deletePersistent(e2);
    pm.deletePersistent(e3);
    pm.currentTransaction().commit();
    Assert.assertEquals("Bad collection size", 1, s);
    Assert.assertTrue("E2 again in friends", !containse2);
    Assert.assertTrue("E3 not in friends", containse3);
  }
  public void testDeleteColElemWithoutRelationWithEvictAll() {
      testDeleteColElemWithoutRelation(true);
 
  public void testDeleteColElemWithoutRelation() {
      testDeleteColElemWithoutRelation(false);
 
  public void testDeleteColElemWithoutRelation(boolean withEvictAll) {
      String gn = "group testDeleteColElemWithoutRelation" + withEvictAll;
      String un = "user testDeleteColElemWithoutRelation" + withEvictAll;
      Group group = new Group(gn);
      User user = new User(un);
      group.getUsers().add(user);
      PersistenceManager pm = pmf.getPersistenceManager();
      pm.currentTransaction().begin();
      pm.makePersistent(group);
      group = null;
      user = null;
      pm.currentTransaction().commit();
      if (withEvictAll) {
          pm.evictAll();
      }
      pm.currentTransaction().begin();
      user = (User) pm.getObjectById(
              pm.newObjectIdInstance(User.class, un), false);
      pm.deletePersistent(user);
      pm.currentTransaction().commit();
     
      pm.currentTransaction().begin();
      group = (Group) pm.getObjectById(
              pm.newObjectIdInstance(Group.class, gn), false);
      ArrayList al = new ArrayList();
      for(Iterator it = group.getUsers().iterator(); it.hasNext();) {
          user = (User) it.next();
          al.add(user.getName());
      }
      pm.deletePersistent(group);
      pm.currentTransaction().commit();
      pm.close();
      assertTrue("Group not empty: " + al, al.isEmpty());
  }
}
TOP

Related Classes of org.objectweb.speedo.runtime.collection.TestEmployee

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.