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.cap;
025:
026: import java.sql.Connection;
027: import java.sql.DriverManager;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Iterator;
031:
032: import javax.jdo.JDOException;
033: import javax.jdo.PersistenceManager;
034: import javax.jdo.Query;
035:
036: import junit.framework.Assert;
037:
038: import org.hsqldb.Server;
039: import org.objectweb.speedo.SpeedoTestHelper;
040: import org.objectweb.speedo.api.ExceptionHelper;
041: import org.objectweb.speedo.pobjects.cap.JDOAction;
042: import org.objectweb.speedo.pobjects.cap.JDOAdminUser;
043: import org.objectweb.speedo.pobjects.cap.JDORole;
044: import org.objectweb.speedo.pobjects.cap.JDOScope;
045: import org.objectweb.util.monolog.api.BasicLevel;
046:
047: /**
048: * @author Y.Bersihand
049: */
050: public class TestHsql extends SpeedoTestHelper {
051:
052: public TestHsql(String s) {
053: super (s);
054: }
055:
056: protected String getLoggerName() {
057: return LOG_NAME + ".rt.cap.TestHsql";
058: }
059:
060: /** to test, move this code to TestHelper
061: * and call the startServer() method in the constructor of TestHelper
062: * before startServer()
063: */
064: String serverProps;
065: String url;
066: String user = "sa";
067: String password = "";
068: Server server;
069: boolean isNetwork = true;
070:
071: protected void startServer() {
072: System.out.println("setup db server");
073: if (isNetwork) {
074: serverProps = "database.0=mem:memorydb";
075: url = "jdbc:hsqldb:hsql://localhost/memorydb";
076: server = new Server();
077: server.putPropertiesFromString(serverProps);
078: server.setLogWriter(null);
079: server.setErrWriter(null);
080: server.start();
081: System.out.println("server " + url + " started");
082: } else {
083: url = "jdbc:hsqldb:file:memorydb";
084: }
085:
086: try {
087: Class.forName("org.hsqldb.jdbcDriver");
088: } catch (Exception e) {
089: e.printStackTrace();
090: System.out.println(this + ".setUp() error: "
091: + e.getMessage());
092: }
093: }
094:
095: protected void tearDown() {
096:
097: if (isNetwork) {
098: server.stop();
099: server = null;
100: }
101: }
102:
103: Connection newConnection() throws Exception {
104: return DriverManager.getConnection("jdbc:hsqldb:file:memorydb",
105: user, password);
106: }
107:
108: /** end of code to move to TestHelper*/
109:
110: /**
111: * test a particular pattern with collection
112: */
113: public void testCapGemini() {
114:
115: JDOScope scope1 = new JDOScope(1, "scope1");
116: JDOScope scope2 = new JDOScope(2, "scope2");
117: JDOScope scope3 = new JDOScope(3, "scope3");
118: JDOScope scope4 = new JDOScope(4, "scope4");
119:
120: Collection scopes1 = new ArrayList();
121: scopes1.add(scope1);
122: scopes1.add(scope3);
123:
124: Collection scopes2 = new ArrayList();
125: scopes2.add(scope2);
126: scopes2.add(scope4);
127:
128: JDOAction action1 = new JDOAction(1, "action1", "/action1/");
129: JDOAction action2 = new JDOAction(2, "action2", "/action2/");
130: JDOAction action3 = new JDOAction(3, "action3", "/action3/");
131: JDOAction action4 = new JDOAction(4, "action4", "/action4/");
132:
133: JDORole role1 = new JDORole(1, "role1", scopes1,
134: new ArrayList());
135: JDORole role2 = new JDORole(2, "role2", scopes2,
136: new ArrayList());
137:
138: role1.addAction(action1, true, 1);
139: role1.addAction(action3, true, 1);
140:
141: role2.addAction(action2, true, 2);
142: role2.addAction(action4, true, 2);
143:
144: Collection roles = new ArrayList();
145: roles.add(role1);
146: roles.add(role2);
147:
148: JDOAdminUser adminUser = new JDOAdminUser("id", "login",
149: "mail", roles);
150:
151: PersistenceManager pm = pmf.getPersistenceManager();
152: pm.currentTransaction().begin();
153: pm.makePersistent(adminUser);
154: pm.currentTransaction().commit();
155: pm.close();
156: }
157:
158: public void testRemovingOfPersistentObject() {
159: PersistenceManager pm = pmf.getPersistenceManager();
160: try {
161: Class[] cs = new Class[] { JDOAction.class,
162: 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 col = (Collection) query.execute();
167: Iterator it = col.iterator();
168: while (it.hasNext()) {
169: Object o = it.next();
170: Assert.assertNotNull(
171: "null object in the query result"
172: + cs[i].getName(), o);
173: pm.deletePersistent(o);
174:
175: }
176: query.close(col);
177: }
178: pm.currentTransaction().commit();
179: } catch (JDOException e) {
180: Exception ie = ExceptionHelper.getNested(e);
181: logger.log(BasicLevel.ERROR, "", ie);
182: fail(ie.getMessage());
183: } finally {
184: pm.close();
185: }
186: }
187: }
|