001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: */package org.objectweb.speedo.runtime.detach;
025:
026: import java.util.Collection;
027: import java.util.Iterator;
028:
029: import javax.jdo.JDOException;
030: import javax.jdo.PersistenceManager;
031: import javax.jdo.Query;
032:
033: import junit.framework.Assert;
034:
035: import org.objectweb.speedo.SpeedoTestHelper;
036: import org.objectweb.speedo.api.ExceptionHelper;
037: import org.objectweb.speedo.pobjects.detach.Address;
038: import org.objectweb.speedo.pobjects.detach.Person;
039: import org.objectweb.util.monolog.api.BasicLevel;
040:
041: /**
042: * @author Y.Bersihand
043: */
044: public class TestSwiss extends SpeedoTestHelper {
045:
046: public TestSwiss(String s) {
047: super (s);
048: }
049:
050: protected String getLoggerName() {
051: return LOG_NAME + ".rt.detach.TestSwiss";
052: }
053:
054: /**
055: * Test the detach method
056: */
057: public void testDetachManyTimes() {
058: try {
059: long personId = createPersonLong();
060: Person detached1 = getPersonById(personId);
061: logger.log(BasicLevel.DEBUG, "detached1 address: "
062: + detached1.getAddress().getType());
063: Person detached2 = getPersonById(personId);
064: logger.log(BasicLevel.DEBUG, "detached2 address: "
065: + detached2.getAddress().getType());
066: assertEquals(detached1.getId(), detached2.getId());
067: assertEquals(detached1.getAddress().getType(), detached2
068: .getAddress().getType());
069: } catch (Exception e) {
070: e.printStackTrace();
071: }
072: }
073:
074: public void testRemovingOfPersistentObject() {
075: PersistenceManager pm = pmf.getPersistenceManager();
076: try {
077: Class[] cs = new Class[] { Address.class, Person.class };
078: pm.currentTransaction().begin();
079: for (int i = 0; i < cs.length; i++) {
080: Query query = pm.newQuery(cs[i]);
081: Collection col = (Collection) query.execute();
082: Iterator it = col.iterator();
083: while (it.hasNext()) {
084: Object o = it.next();
085: Assert.assertNotNull(
086: "null object in the query result"
087: + cs[i].getName(), o);
088: pm.deletePersistent(o);
089:
090: }
091: query.close(col);
092: }
093: pm.currentTransaction().commit();
094: } catch (JDOException e) {
095: Exception ie = ExceptionHelper.getNested(e);
096: logger.log(BasicLevel.ERROR, "", ie);
097: fail(ie.getMessage());
098: } finally {
099: pm.close();
100: }
101: }
102:
103: /*----------------- PRIVATE METHODS----------------------------*/
104: private Object createPerson() {
105: logger.log(BasicLevel.DEBUG,
106: "***************createPerson Object*****************");
107: Address address = new Address("mail");
108: Person person = new Person(address);
109: PersistenceManager pm = pmf.getPersistenceManager();
110: try {
111: pm.currentTransaction().begin();
112: pm.makePersistent(person);
113: pm.currentTransaction().commit();
114: return pm.getObjectId(person);
115: } catch (Exception e) {
116: fail(e.getMessage());
117: return null;
118: } finally {
119: pm.close();
120: }
121: }
122:
123: private long createPersonLong() {
124: logger.log(BasicLevel.DEBUG,
125: "***************createPersonLong*****************");
126: Address address = new Address("mail");
127: Person person = new Person(address);
128: PersistenceManager pm = pmf.getPersistenceManager();
129: try {
130: pm.currentTransaction().begin();
131: pm.makePersistent(person);
132: pm.currentTransaction().commit();
133: return person.getId();
134: } catch (Exception e) {
135: fail(e.getMessage());
136: return -1;
137: } finally {
138: pm.close();
139: }
140: }
141:
142: private Person getPersonById(Object personId) {
143: logger.log(BasicLevel.DEBUG,
144: "***************getPersonById*****************");
145: Person result = null;
146: PersistenceManager pm = pmf.getPersistenceManager();
147: try {
148: //Object oid = pm.newObjectIdInstance(Person.class, "" + personId);
149: Person p = (Person) pm.getObjectById(personId, false);
150: if (p != null) {
151: p.getAddress();
152: result = (Person) pm.detachCopy(p);
153: }
154: } catch (Exception e) {
155: logger.log(BasicLevel.DEBUG, "Person with Key=" + personId
156: + " not found.");
157: } finally {
158: pm.close();
159: }
160: return result;
161: }
162:
163: private Person getPersonById(long personId) {
164: logger.log(BasicLevel.DEBUG,
165: "***************getPersonById*****************");
166: Person result = null;
167: PersistenceManager pm = pmf.getPersistenceManager();
168: try {
169: Object oid = pm.newObjectIdInstance(Person.class, ""
170: + personId);
171: Person p = (Person) pm.getObjectById(oid, false);
172: if (p != null) {
173: p.getAddress();
174: result = (Person) pm.detachCopy(p);
175: }
176: } catch (Exception e) {
177: logger.log(BasicLevel.DEBUG, "Person with Key=" + personId
178: + " not found.");
179: } finally {
180: pm.close();
181: }
182: return result;
183: }
184: }
|