KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > detach > TestSwiss


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  */

25
26 package org.objectweb.speedo.runtime.detach;
27
28
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.jdo.JDOException;
33 import javax.jdo.PersistenceManager;
34 import javax.jdo.Query;
35
36 import junit.framework.Assert;
37
38 import org.objectweb.speedo.SpeedoTestHelper;
39 import org.objectweb.speedo.api.ExceptionHelper;
40 import org.objectweb.speedo.pobjects.detach.Address;
41 import org.objectweb.speedo.pobjects.detach.Person;
42 import org.objectweb.util.monolog.api.BasicLevel;
43
44 /**
45  * @author Y.Bersihand
46  */

47 public class TestSwiss extends SpeedoTestHelper {
48
49     public TestSwiss(String JavaDoc s) {
50         super(s);
51     }
52
53     protected String JavaDoc getLoggerName() {
54         return LOG_NAME + ".rt.detach.TestSwiss";
55     }
56     
57     /**
58      * Test the detach method
59      */

60     public void testDetachManyTimes() {
61         try {
62             long personId = createPersonLong();
63             Person detached1 = getPersonById(personId);
64             logger.log(BasicLevel.DEBUG, "detached1 address: " + detached1.getAddress().getType());
65             Person detached2 = getPersonById(personId);
66             logger.log(BasicLevel.DEBUG, "detached2 address: " + detached2.getAddress().getType());
67             assertEquals(detached1.getId(), detached2.getId());
68             assertEquals(detached1.getAddress().getType(), detached2.getAddress().getType());
69         } catch (Exception JavaDoc e) {
70             e.printStackTrace();
71         }
72     }
73     
74     public void testRemovingOfPersistentObject() {
75         PersistenceManager pm = pmf.getPersistenceManager();
76         try {
77             Class JavaDoc[] cs = new Class JavaDoc[]{Address.class, Person.class};
78             pm.currentTransaction().begin();
79             for(int i=0; i<cs.length; i++) {
80                 Query query = pm.newQuery(cs[i]);
81                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
82                 Iterator JavaDoc it = col.iterator();
83                 while(it.hasNext()) {
84                     Object JavaDoc o = it.next();
85                     Assert.assertNotNull("null object in the query result"
86                         + cs[i].getName(), o);
87                     pm.deletePersistent(o);
88
89                 }
90                 query.close(col);
91             }
92             pm.currentTransaction().commit();
93         } catch (JDOException e) {
94             Exception JavaDoc ie = ExceptionHelper.getNested(e);
95             logger.log(BasicLevel.ERROR, "", ie);
96             fail(ie.getMessage());
97         } finally {
98             pm.close();
99         }
100     }
101     /*----------------- PRIVATE METHODS----------------------------*/
102     private Object JavaDoc createPerson(){
103         logger.log(BasicLevel.DEBUG, "***************createPerson Object*****************");
104         Address address = new Address("mail");
105         Person person = new Person(address);
106         PersistenceManager pm = pmf.getPersistenceManager();
107         try {
108             pm.currentTransaction().begin();
109             pm.makePersistent(person);
110             pm.currentTransaction().commit();
111             return pm.getObjectId(person);
112         } catch (Exception JavaDoc e) {
113             fail(e.getMessage());
114             return null;
115         } finally {
116             pm.close();
117         }
118     }
119     
120     private long createPersonLong(){
121         logger.log(BasicLevel.DEBUG, "***************createPersonLong*****************");
122         Address address = new Address("mail");
123         Person person = new Person(address);
124         PersistenceManager pm = pmf.getPersistenceManager();
125         try {
126             pm.currentTransaction().begin();
127             pm.makePersistent(person);
128             pm.currentTransaction().commit();
129             return person.getId();
130         } catch (Exception JavaDoc e) {
131             fail(e.getMessage());
132             return -1;
133         } finally {
134             pm.close();
135         }
136     }
137     
138     private Person getPersonById(Object JavaDoc personId) {
139         logger.log(BasicLevel.DEBUG, "***************getPersonById*****************");
140         Person result = null;
141         PersistenceManager pm = pmf.getPersistenceManager();
142         try {
143           //Object oid = pm.newObjectIdInstance(Person.class, "" + personId);
144
Person p = (Person) pm.getObjectById(personId, false);
145           if (p != null) {
146             p.getAddress();
147             result = (Person) pm.detachCopy(p);
148           }
149         } catch (Exception JavaDoc e) {
150           logger.log(BasicLevel.DEBUG, "Person with Key=" + personId + " not found.");
151         } finally {
152           pm.close();
153         }
154         return result;
155      }
156     
157     private Person getPersonById(long personId) {
158         logger.log(BasicLevel.DEBUG, "***************getPersonById*****************");
159         Person result = null;
160         PersistenceManager pm = pmf.getPersistenceManager();
161         try {
162           Object JavaDoc oid = pm.newObjectIdInstance(Person.class, "" + personId);
163           Person p = (Person) pm.getObjectById(oid, false);
164           if (p != null) {
165             p.getAddress();
166             result = (Person) pm.detachCopy(p);
167           }
168         } catch (Exception JavaDoc e) {
169           logger.log(BasicLevel.DEBUG, "Person with Key=" + personId + " not found.");
170         } finally {
171           pm.close();
172         }
173         return result;
174      }
175 }
176
Popular Tags