KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > cap > Test1


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.cap;
27
28 import java.util.ArrayList JavaDoc;
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.cap.JDOAction;
41 import org.objectweb.speedo.pobjects.cap.JDOAdminUser;
42 import org.objectweb.speedo.pobjects.cap.JDORole;
43 import org.objectweb.speedo.pobjects.cap.JDOScope;
44 import org.objectweb.util.monolog.api.BasicLevel;
45
46 /**
47  * This test aimed at solving a problem due to complicated relationships between objects.
48  * @author Y.Bersihand
49  */

50 public class Test1 extends SpeedoTestHelper {
51
52     
53     public Test1(String JavaDoc s) {
54         super(s);
55     }
56
57     
58     protected String JavaDoc getLoggerName() {
59         return null;
60     }
61     
62     
63     /**
64      * test a particular pattern with collection
65      */

66     public void testCapGemini(){
67         JDOScope scope1 = new JDOScope(1, "scope1");
68         JDOScope scope2 = new JDOScope(2, "scope2");
69         JDOScope scope3 = new JDOScope(3, "scope3");
70         JDOScope scope4 = new JDOScope(4, "scope4");
71         
72         Collection JavaDoc scopes1 = new ArrayList JavaDoc();
73         scopes1.add(scope1);
74         scopes1.add(scope3);
75         
76         Collection JavaDoc scopes2 = new ArrayList JavaDoc();
77         scopes2.add(scope2);
78         scopes2.add(scope4);
79         
80         JDOAction action1 = new JDOAction(1,"action1","/action1/");
81         JDOAction action2 = new JDOAction(2,"action2","/action2/");
82         JDOAction action3 = new JDOAction(3,"action3","/action3/");
83         JDOAction action4 = new JDOAction(4,"action4","/action4/");
84         
85         JDORole role1 = new JDORole(1, "role1", scopes1, new ArrayList JavaDoc());
86         JDORole role2 = new JDORole(2, "role2", scopes2, new ArrayList JavaDoc());
87         
88         role1.addAction(action1, true, 1);
89         role1.addAction(action3, true, 1);
90         
91         role2.addAction(action2, true, 2);
92         role2.addAction(action4, true, 2);
93         
94         Collection JavaDoc roles = new ArrayList JavaDoc();
95         roles.add(role1);
96         roles.add(role2);
97         
98         JDOAdminUser adminUser = new JDOAdminUser("id","login", "mail", roles);
99         
100         PersistenceManager pm = pmf.getPersistenceManager();
101         pm.currentTransaction().begin();
102         pm.makePersistent(adminUser);
103         pm.currentTransaction().commit();
104         pm.close();
105     }
106     
107     public void testRemovingOfPersistentObject() {
108         PersistenceManager pm = pmf.getPersistenceManager();
109         try {
110             Class JavaDoc[] cs = new Class JavaDoc[]{JDOAction.class,JDOAdminUser.class,JDORole.class, JDOScope.class};
111             pm.currentTransaction().begin();
112             for(int i=0; i<cs.length; i++) {
113                 Query query = pm.newQuery(cs[i]);
114                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
115                 Iterator JavaDoc it = col.iterator();
116                 while(it.hasNext()) {
117                     Object JavaDoc o = it.next();
118                     Assert.assertNotNull("null object in the query result"
119                         + cs[i].getName(), o);
120                     pm.deletePersistent(o);
121
122                 }
123                 query.close(col);
124             }
125             pm.currentTransaction().commit();
126         } catch (JDOException e) {
127             Exception JavaDoc ie = ExceptionHelper.getNested(e);
128             logger.log(BasicLevel.ERROR, "", ie);
129             fail(ie.getMessage());
130         } finally {
131             pm.close();
132         }
133     }
134
135 }
136
Popular Tags