001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.ejb;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Properties;
025: import java.util.StringTokenizer;
026:
027: import javax.persistence.EntityManager;
028: import javax.persistence.EntityManagerFactory;
029: import javax.persistence.Persistence;
030:
031: import junit.framework.Assert;
032: import junit.framework.TestCase;
033:
034: import org.objectweb.fractal.api.Component;
035: import org.objectweb.fractal.api.Interface;
036: import org.objectweb.fractal.util.Fractal;
037: import org.objectweb.jorm.api.PMapper;
038: import org.objectweb.perseus.cache.api.CacheAttributeController;
039: import org.objectweb.speedo.AbstractSpeedo;
040: import org.objectweb.speedo.api.SpeedoProperties;
041: import org.objectweb.util.monolog.Monolog;
042: import org.objectweb.util.monolog.api.BasicLevel;
043: import org.objectweb.util.monolog.api.Logger;
044: import org.objectweb.util.monolog.api.LoggerFactory;
045:
046: /**
047: * Provide common code for all tests covering EJB3. It is inherited by most
048: * of the test cases of EJB3.
049: *
050: * @author P. Dechamboux
051: */
052: public abstract class SpeedoTestHelper extends TestCase {
053:
054: public final static String LOG_NAME = "org.objectweb.speedo.test";
055:
056: protected static EntityManagerFactory emf = null;
057: protected static boolean emfProblem = false;
058: public Logger logger = null;
059: protected LoggerFactory loggerFactory = null;
060: protected Properties emfProp;
061:
062: public SpeedoTestHelper(String s, String emname) {
063: super (s);
064: if (!emfProblem) {
065: getEMF(emname);
066: }
067: loggerFactory = Monolog.monologFactory;
068: logger = loggerFactory.getLogger(getLoggerName());
069: }
070:
071: protected abstract String getLoggerName();
072:
073: public Logger getLogger() {
074: return logger;
075: }
076:
077: public EntityManagerFactory getEMF(String emname) {
078: if (emf == null) {
079: synchronized (SpeedoTestHelper.class) {
080: if (!emfProblem && emf == null) {
081: try {
082: emf = Persistence
083: .createEntityManagerFactory(emname);
084: } catch (Error e) {
085: emfProblem = true;
086: e.printStackTrace();
087: throw e;
088: }
089: }
090: }
091: }
092: return emf;
093: }
094:
095: public Properties getEMFPropertiesFromFile() {
096: emfProp = new Properties();
097: InputStream is = getClass().getClassLoader()
098: .getResourceAsStream("speedo-ejb.properties");
099: if (is == null) {
100: return null;
101: }
102: try {
103: emfProp.load(is);
104: } catch (IOException e) {
105: return null;
106: }
107: return emfProp;
108: }
109:
110: public String getConnectionDriverNameProperty(Properties p) {
111: String dcn = p
112: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME);
113: if (dcn == null) {
114: dcn = p
115: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD);
116: if (dcn != null) {
117: System.err
118: .println("The property '"
119: + SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD
120: + "' is deprecated, you have to use '"
121: + SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME
122: + "'.");
123: } else {
124: dcn = p
125: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD2);
126: if (dcn == null) {
127: System.out.println(p);
128: fail("No driver class name specified");
129: } else {
130: System.err
131: .println("The property '"
132: + SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD2
133: + "' is deprecated, you have to use '"
134: + SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME
135: + "'.");
136:
137: }
138: }
139: }
140: return dcn;
141: }
142:
143: public Properties getPMFProperties() {
144: if (Boolean.getBoolean("org.objectweb.speedo.useFile")) {
145: emfProp = getEMFPropertiesFromFile();
146: return emfProp;
147: }
148: String debug = System.getProperty("org.objectweb.speedo.debug",
149: "false");
150: String dcn = getConnectionDriverNameProperty(System
151: .getProperties());
152: String user = System.getProperty(
153: SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, "");
154: String pass = System.getProperty(
155: SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, "");
156: String url = System
157: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL);
158: if (url == null)
159: Assert.fail("No database url specified");
160: emfProp = new Properties();
161: emfProp
162: .setProperty(
163: SpeedoProperties.JDO_PERSISTENCE_MANAGER_FACTORY_CLASS,
164: System
165: .getProperty(
166: SpeedoProperties.JDO_PERSISTENCE_MANAGER_FACTORY_CLASS,
167: "org.objectweb.speedo.Speedo"));
168: emfProp.setProperty("org.objectweb.speedo.debug", debug);
169: emfProp
170: .setProperty(
171: SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME,
172: dcn);
173: emfProp.setProperty(
174: SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, user);
175: emfProp.setProperty(
176: SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, pass);
177: emfProp.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL,
178: url);
179: emfProp.setProperty(SpeedoProperties.CACHE_SIZE, "nolimit");
180: return emfProp;
181: }
182:
183: public void assertSameCollection(String msg, Collection expected,
184: Collection found) {
185: try {
186: if (expected == null) {
187: assertNull(msg + " null collection expected", found);
188: return;
189: }
190: Assert.assertNotNull(msg + " non null collection expected",
191: found);
192: Assert.assertEquals(msg + " not same size",
193: expected.size(), found.size());
194: Iterator it = expected.iterator();
195: while (it.hasNext()) {
196: Object element = it.next();
197: Assert.assertTrue(msg + " the found collection ("
198: + found + ") does not contains the element "
199: + element, found.contains(element));
200: }
201: it = found.iterator();
202: while (it.hasNext()) {
203: Object element = it.next();
204: assertTrue(
205: msg
206: + " the found collection contains an unexpected element "
207: + element, expected.contains(element));
208: }
209: } catch (Error e) {
210: throw e;
211: }
212: }
213:
214: public void assertSameList(String msg, List expected, List found) {
215: if (expected == null) {
216: Assert.assertNull(msg + " null list expected", found);
217: return;
218: }
219: Assert.assertNotNull(msg + " non null list expected", found);
220: Assert.assertEquals(msg + " not same size", expected.size(),
221: found.size());
222: for (int i = 0; i < expected.size(); i++) {
223: Assert.assertEquals(msg + " bad element at the index" + i,
224: expected.get(i), found.get(i));
225: }
226: }
227:
228: public PMapper getMapper(EntityManagerFactory _emf)
229: throws Exception {
230: Interface fcemf = (Interface) ((AbstractSpeedo) _emf)
231: .getDelegate();
232: return (PMapper) getSubComponent(fcemf.getFcItfOwner(),
233: "mapper").getFcInterface("mapper");
234: }
235:
236: protected CacheAttributeController getCacheAttributeController(
237: EntityManagerFactory _emf) throws Exception {
238: Interface fcemf = (Interface) ((AbstractSpeedo) _emf)
239: .getDelegate();
240: Component c = getSubComponent(((Interface) fcemf)
241: .getFcItfOwner(), "tpm.cache-manager.cache-manager");
242: return (CacheAttributeController) Fractal
243: .getAttributeController(c);
244: }
245:
246: private Component getSubComponent(Component parent, String path)
247: throws Exception {
248: //System.out.println("path: " + path);
249:
250: StringTokenizer st = new StringTokenizer(path, ".", false);
251: Component res = parent;
252: while (st.hasMoreTokens()) {
253: String commponenentname = st.nextToken();
254: Component[] children = Fractal.getContentController(res)
255: .getFcSubComponents();
256: int i = 0;
257: //System.out.println("search: " + commponenentname);
258: while (i < children.length
259: && !Fractal.getNameController(children[i])
260: .getFcName().equals(commponenentname)) {
261: //System.out.println("current: " + ((NameController) children[i].getFcInterface("name-controller")).getFcName());
262: i++;
263: }
264: if (i < children.length) {
265: //System.out.println("found: " + ((NameController) children[i].getFcInterface("name-controller")).getFcName());
266: res = children[i];
267: } else {
268: //System.out.println("not found");
269: return null;
270: }
271: }
272: return res;
273: }
274:
275: public Component getSubComponent(Component parent, String path,
276: String s) throws Exception {
277: //System.out.println("path: " + path);
278: StringTokenizer st = new StringTokenizer(path, ".", false);
279: Component res = parent;
280: while (st.hasMoreTokens()) {
281: String commponenentname = st.nextToken();
282: Component[] children = Fractal.getContentController(res)
283: .getFcSubComponents();
284: int i = 0;
285: //System.out.println("search: " + commponenentname);
286: while (i < children.length
287: && !Fractal.getNameController(children[i])
288: .getFcName().equals(commponenentname)) {
289: //System.out.println("current: " + ((NameController) children[i].getFcInterface("name-controller")).getFcName());
290: i++;
291: }
292: if (i < children.length) {
293: //System.out.println("found: " + ((NameController) children[i].getFcInterface("name-controller")).getFcName());
294: res = children[i];
295: } else {
296: //System.out.println("not found");
297: return null;
298: }
299: }
300: return res;
301: }
302:
303: protected int deleteAllInstances(Class clazz) {
304: EntityManager em = emf.getEntityManager();
305: em.getTransaction().begin();
306: int i = 0;
307: /* Extent e = em.getExtent(clazz, true);
308: Iterator it = e.iterator();
309: while(it.hasNext()) {
310: em.remove(it.next());
311: i++;
312: }
313: e.closeAll();*/
314: em.getTransaction().commit();
315: em.close();
316: logger.log(BasicLevel.DEBUG, "Delete " + i
317: + " instance(s) of the class " + clazz.getName());
318: return i;
319: }
320:
321: public static int getIntProperty(String propertyName,
322: int defaultValue) {
323: String v = System.getProperty(propertyName);
324: if (v == null) {
325: return defaultValue;
326: }
327: try {
328: return Integer.parseInt(v);
329: } catch (NumberFormatException e) {
330: return defaultValue;
331: }
332: }
333:
334: public final static int length(final int val) {
335: int length = 0;
336: int c = val;
337: while (c > 0) {
338: c = c / 10;
339: length++;
340: }
341: return length;
342: }
343:
344: public final static String i2s(final int val, final int length) {
345: final StringBuffer sb = new StringBuffer(length);
346: int c = val;
347: for (int i = 0; i < length; i++) {
348: if (c == 0) {
349: sb.insert(0, '0');
350: } else {
351: sb.insert(0, c % 10);
352: c = c / 10;
353: }
354: }
355: return sb.toString();
356: }
357: }
|