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;
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.jdo.Extent;
028: import javax.jdo.JDOHelper;
029: import javax.jdo.PersistenceManager;
030: import javax.jdo.PersistenceManagerFactory;
031:
032: import junit.framework.Assert;
033: import junit.framework.TestCase;
034:
035: import org.objectweb.fractal.api.Component;
036: import org.objectweb.fractal.api.Interface;
037: import org.objectweb.fractal.util.Fractal;
038: import org.objectweb.jorm.api.PMapper;
039: import org.objectweb.perseus.cache.api.CacheAttributeController;
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: *
048: * @author S.Chassande-Barrioz
049: */
050: public abstract class SpeedoTestHelper extends TestCase {
051:
052: public final static String LOG_NAME = "org.objectweb.speedo.test";
053:
054: protected static PersistenceManagerFactory pmf = null;
055: protected static boolean pmfProblem = false;
056: public Logger logger = null;
057: protected LoggerFactory loggerFactory = null;
058: protected Properties pmfProp;
059:
060: public SpeedoTestHelper(String s) {
061: super (s);
062: if (!pmfProblem) {
063: getPMF();
064: }
065: loggerFactory = Monolog.monologFactory;
066: logger = loggerFactory.getLogger(getLoggerName());
067: }
068:
069: protected abstract String getLoggerName();
070:
071: public Logger getLogger() {
072: return logger;
073: }
074:
075: public PersistenceManagerFactory getPMF() {
076: if (pmf == null) {
077: synchronized (SpeedoTestHelper.class) {
078: if (!pmfProblem && pmf == null) {
079: try {
080: pmf = JDOHelper
081: .getPersistenceManagerFactory(getPMFProperties());
082: } catch (Error e) {
083: pmfProblem = true;
084: e.printStackTrace();
085: throw e;
086: }
087: }
088: }
089: }
090: return pmf;
091: }
092:
093: public Properties getPMFPropertiesFromFile() {
094: pmfProp = new Properties();
095: InputStream is = getClass().getClassLoader()
096: .getResourceAsStream("speedo-jdo.properties");
097: if (is == null) {
098: return null;
099: }
100: try {
101: pmfProp.load(is);
102: } catch (IOException e) {
103: return null;
104: }
105: return pmfProp;
106: }
107:
108: public String getConnectionDriverNameProperty(Properties p) {
109: String dcn = p
110: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME);
111: if (dcn == null) {
112: dcn = p
113: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD);
114: if (dcn != null) {
115: System.err
116: .println("The property '"
117: + SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD
118: + "' is deprecated, you have to use '"
119: + SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME
120: + "'.");
121: } else {
122: dcn = p
123: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD2);
124: if (dcn == null) {
125: System.out.println(p);
126: fail("No driver class name specified");
127: } else {
128: System.err
129: .println("The property '"
130: + SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD2
131: + "' is deprecated, you have to use '"
132: + SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME
133: + "'.");
134:
135: }
136: }
137: }
138: return dcn;
139: }
140:
141: public Properties getPMFProperties() {
142: if (Boolean.getBoolean("org.objectweb.speedo.useFile")) {
143: pmfProp = getPMFPropertiesFromFile();
144: return pmfProp;
145: }
146: String debug = System.getProperty("org.objectweb.speedo.debug",
147: "false");
148: String dcn = getConnectionDriverNameProperty(System
149: .getProperties());
150: String user = System.getProperty(
151: SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, "");
152: String pass = System.getProperty(
153: SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, "");
154: String url = System
155: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL);
156: if (url == null)
157: Assert.fail("No database url specified");
158: pmfProp = new Properties();
159: pmfProp
160: .setProperty(
161: SpeedoProperties.JDO_PERSISTENCE_MANAGER_FACTORY_CLASS,
162: System
163: .getProperty(
164: SpeedoProperties.JDO_PERSISTENCE_MANAGER_FACTORY_CLASS,
165: "org.objectweb.speedo.Speedo"));
166: pmfProp.setProperty("org.objectweb.speedo.debug", debug);
167: pmfProp
168: .setProperty(
169: SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME,
170: dcn);
171: pmfProp.setProperty(
172: SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, user);
173: pmfProp.setProperty(
174: SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, pass);
175: pmfProp.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL,
176: url);
177: pmfProp.setProperty(SpeedoProperties.CACHE_SIZE, "nolimit");
178: return pmfProp;
179: }
180:
181: public void assertSameCollection(String msg, Collection expected,
182: Collection found) {
183: try {
184: if (expected == null) {
185: assertNull(msg + " null collection expected", found);
186: return;
187: }
188: Assert.assertNotNull(msg + " non null collection expected",
189: found);
190: Assert.assertEquals(msg + " not same size",
191: expected.size(), found.size());
192: Iterator it = expected.iterator();
193: while (it.hasNext()) {
194: Object element = it.next();
195: Assert.assertTrue(msg + " the found collection ("
196: + found + ") does not contains the element "
197: + element, found.contains(element));
198: }
199: it = found.iterator();
200: while (it.hasNext()) {
201: Object element = it.next();
202: assertTrue(
203: msg
204: + " the found collection contains an unexpected element "
205: + element, expected.contains(element));
206: }
207: } catch (Error e) {
208: throw e;
209: }
210: }
211:
212: public void assertSameList(String msg, List expected, List found) {
213: if (expected == null) {
214: Assert.assertNull(msg + " null list expected", found);
215: return;
216: }
217: Assert.assertNotNull(msg + " non null list expected", found);
218: Assert.assertEquals(msg + " not same size", expected.size(),
219: found.size());
220: for (int i = 0; i < expected.size(); i++) {
221: Assert.assertEquals(msg + " bad element at the index" + i,
222: expected.get(i), found.get(i));
223: }
224: }
225:
226: public PMapper getMapper(PersistenceManagerFactory _pmf)
227: throws Exception {
228: Interface fcpmf = (Interface) ((AbstractSpeedo) _pmf)
229: .getDelegate();
230: return (PMapper) getSubComponent(fcpmf.getFcItfOwner(),
231: "mapper").getFcInterface("mapper");
232: }
233:
234: protected CacheAttributeController getCacheAttributeController(
235: PersistenceManagerFactory _pmf) throws Exception {
236: Interface fcpmf = (Interface) ((AbstractSpeedo) _pmf)
237: .getDelegate();
238: Component c = getSubComponent(((Interface) fcpmf)
239: .getFcItfOwner(), "tpm.cache-manager.cache-manager");
240: return (CacheAttributeController) Fractal
241: .getAttributeController(c);
242: }
243:
244: private Component getSubComponent(Component parent, String path)
245: throws Exception {
246: //System.out.println("path: " + path);
247:
248: StringTokenizer st = new StringTokenizer(path, ".", false);
249: Component res = parent;
250: while (st.hasMoreTokens()) {
251: String commponenentname = st.nextToken();
252: Component[] children = Fractal.getContentController(res)
253: .getFcSubComponents();
254: int i = 0;
255: //System.out.println("search: " + commponenentname);
256: while (i < children.length
257: && !Fractal.getNameController(children[i])
258: .getFcName().equals(commponenentname)) {
259: //System.out.println("current: " + ((NameController) children[i].getFcInterface("name-controller")).getFcName());
260: i++;
261: }
262: if (i < children.length) {
263: //System.out.println("found: " + ((NameController) children[i].getFcInterface("name-controller")).getFcName());
264: res = children[i];
265: } else {
266: //System.out.println("not found");
267: return null;
268: }
269: }
270: return res;
271: }
272:
273: public Component getSubComponent(Component parent, String path,
274: String s) throws Exception {
275: //System.out.println("path: " + path);
276: StringTokenizer st = new StringTokenizer(path, ".", false);
277: Component res = parent;
278: while (st.hasMoreTokens()) {
279: String commponenentname = st.nextToken();
280: Component[] children = Fractal.getContentController(res)
281: .getFcSubComponents();
282: int i = 0;
283: //System.out.println("search: " + commponenentname);
284: while (i < children.length
285: && !Fractal.getNameController(children[i])
286: .getFcName().equals(commponenentname)) {
287: //System.out.println("current: " + ((NameController) children[i].getFcInterface("name-controller")).getFcName());
288: i++;
289: }
290: if (i < children.length) {
291: //System.out.println("found: " + ((NameController) children[i].getFcInterface("name-controller")).getFcName());
292: res = children[i];
293: } else {
294: //System.out.println("not found");
295: return null;
296: }
297: }
298: return res;
299: }
300:
301: protected int deleteAllInstances(Class clazz) {
302: PersistenceManager pm = pmf.getPersistenceManager();
303: pm.currentTransaction().begin();
304: Extent e = pm.getExtent(clazz, true);
305: Iterator it = e.iterator();
306: int i = 0;
307: while (it.hasNext()) {
308: pm.deletePersistent(it.next());
309: i++;
310: }
311: e.closeAll();
312: pm.currentTransaction().commit();
313: pm.close();
314: logger.log(BasicLevel.DEBUG, "Delete " + i
315: + " instance(s) of the class " + clazz.getName());
316: return i;
317: }
318:
319: public static int getIntProperty(String propertyName,
320: int defaultValue) {
321: String v = System.getProperty(propertyName);
322: if (v == null) {
323: return defaultValue;
324: }
325: try {
326: return Integer.parseInt(v);
327: } catch (NumberFormatException e) {
328: return defaultValue;
329: }
330: }
331:
332: public final static int length(final int val) {
333: int length = 0;
334: int c = val;
335: while (c > 0) {
336: c = c / 10;
337: length++;
338: }
339: return length;
340: }
341:
342: public final static String i2s(final int val, final int length) {
343: final StringBuffer sb = new StringBuffer(length);
344: int c = val;
345: for (int i = 0; i < length; i++) {
346: if (c == 0) {
347: sb.insert(0, '0');
348: } else {
349: sb.insert(0, c % 10);
350: c = c / 10;
351: }
352: }
353: return sb.toString();
354: }
355: }
|