KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.Connection JavaDoc;
29 import java.sql.DriverManager JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import javax.jdo.JDOException;
35 import javax.jdo.PersistenceManager;
36 import javax.jdo.Query;
37
38 import junit.framework.Assert;
39
40 import org.hsqldb.Server;
41 import org.objectweb.speedo.SpeedoTestHelper;
42 import org.objectweb.speedo.api.ExceptionHelper;
43 import org.objectweb.speedo.pobjects.cap.JDOAction;
44 import org.objectweb.speedo.pobjects.cap.JDOAdminUser;
45 import org.objectweb.speedo.pobjects.cap.JDORole;
46 import org.objectweb.speedo.pobjects.cap.JDOScope;
47 import org.objectweb.util.monolog.api.BasicLevel;
48
49
50 /**
51  * @author Y.Bersihand
52  */

53 public class TestHsql extends SpeedoTestHelper {
54
55     
56     public TestHsql(String JavaDoc s) {
57         super(s);
58     }
59
60     
61     protected String JavaDoc getLoggerName() {
62         return LOG_NAME + ".rt.cap.TestHsql";
63     }
64     
65     
66     /** to test, move this code to TestHelper
67      * and call the startServer() method in the constructor of TestHelper
68      * before startServer()
69      */

70     String JavaDoc serverProps;
71     String JavaDoc url;
72     String JavaDoc user = "sa";
73     String JavaDoc password = "";
74     Server server;
75     boolean isNetwork = true;
76
77     protected void startServer() {
78         System.out.println("setup db server");
79         if (isNetwork) {
80             serverProps = "database.0=mem:memorydb";
81             url = "jdbc:hsqldb:hsql://localhost/memorydb";
82             server = new Server();
83             server.putPropertiesFromString(serverProps);
84             server.setLogWriter(null);
85             server.setErrWriter(null);
86             server.start();
87             System.out.println("server " + url + " started");
88         } else {
89             url = "jdbc:hsqldb:file:memorydb";
90         }
91
92         try {
93             Class.forName("org.hsqldb.jdbcDriver");
94         } catch (Exception JavaDoc e) {
95             e.printStackTrace();
96             System.out.println(this + ".setUp() error: " + e.getMessage());
97         }
98     }
99
100     protected void tearDown() {
101
102         if (isNetwork) {
103             server.stop();
104             server = null;
105         }
106     }
107     
108     Connection JavaDoc newConnection() throws Exception JavaDoc {
109         return DriverManager.getConnection("jdbc:hsqldb:file:memorydb", user, password);
110     }
111    /** end of code to move to TestHelper*/
112     
113     /**
114      * test a particular pattern with collection
115      */

116     public void testCapGemini(){
117         
118         
119         JDOScope scope1 = new JDOScope(1, "scope1");
120         JDOScope scope2 = new JDOScope(2, "scope2");
121         JDOScope scope3 = new JDOScope(3, "scope3");
122         JDOScope scope4 = new JDOScope(4, "scope4");
123         
124         Collection JavaDoc scopes1 = new ArrayList JavaDoc();
125         scopes1.add(scope1);
126         scopes1.add(scope3);
127         
128         Collection JavaDoc scopes2 = new ArrayList JavaDoc();
129         scopes2.add(scope2);
130         scopes2.add(scope4);
131         
132         JDOAction action1 = new JDOAction(1,"action1","/action1/");
133         JDOAction action2 = new JDOAction(2,"action2","/action2/");
134         JDOAction action3 = new JDOAction(3,"action3","/action3/");
135         JDOAction action4 = new JDOAction(4,"action4","/action4/");
136         
137         JDORole role1 = new JDORole(1, "role1", scopes1, new ArrayList JavaDoc());
138         JDORole role2 = new JDORole(2, "role2", scopes2, new ArrayList JavaDoc());
139         
140         role1.addAction(action1, true, 1);
141         role1.addAction(action3, true, 1);
142         
143         role2.addAction(action2, true, 2);
144         role2.addAction(action4, true, 2);
145         
146         Collection JavaDoc roles = new ArrayList JavaDoc();
147         roles.add(role1);
148         roles.add(role2);
149         
150         JDOAdminUser adminUser = new JDOAdminUser("id","login", "mail", roles);
151         
152         PersistenceManager pm = pmf.getPersistenceManager();
153         pm.currentTransaction().begin();
154         pm.makePersistent(adminUser);
155         pm.currentTransaction().commit();
156         pm.close();
157     }
158
159     public void testRemovingOfPersistentObject() {
160         PersistenceManager pm = pmf.getPersistenceManager();
161         try {
162             Class JavaDoc[] cs = new Class JavaDoc[]{JDOAction.class,JDOAdminUser.class,JDORole.class, JDOScope.class};
163             pm.currentTransaction().begin();
164             for(int i=0; i<cs.length; i++) {
165                 Query query = pm.newQuery(cs[i]);
166                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
167                 Iterator JavaDoc it = col.iterator();
168                 while(it.hasNext()) {
169                     Object JavaDoc o = it.next();
170                     Assert.assertNotNull("null object in the query result"
171                         + cs[i].getName(), o);
172                     pm.deletePersistent(o);
173
174                 }
175                 query.close(col);
176             }
177             pm.currentTransaction().commit();
178         } catch (JDOException e) {
179             Exception JavaDoc ie = ExceptionHelper.getNested(e);
180             logger.log(BasicLevel.ERROR, "", ie);
181             fail(ie.getMessage());
182         } finally {
183             pm.close();
184         }
185     }
186 }
187
Popular Tags