KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > SpeedoTestHelper


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 import javax.jdo.Extent;
29 import javax.jdo.JDOHelper;
30 import javax.jdo.PersistenceManager;
31 import javax.jdo.PersistenceManagerFactory;
32
33 import junit.framework.Assert;
34 import junit.framework.TestCase;
35
36 import org.objectweb.fractal.api.Component;
37 import org.objectweb.fractal.api.Interface;
38 import org.objectweb.fractal.util.Fractal;
39 import org.objectweb.jorm.api.PMapper;
40 import org.objectweb.perseus.cache.api.CacheAttributeController;
41 import org.objectweb.speedo.api.SpeedoProperties;
42 import org.objectweb.util.monolog.Monolog;
43 import org.objectweb.util.monolog.api.BasicLevel;
44 import org.objectweb.util.monolog.api.Logger;
45 import org.objectweb.util.monolog.api.LoggerFactory;
46
47 /**
48  *
49  * @author S.Chassande-Barrioz
50  */

51 public abstract class SpeedoTestHelper extends TestCase {
52
53     public final static String JavaDoc LOG_NAME = "org.objectweb.speedo.test";
54
55     protected static PersistenceManagerFactory pmf = null;
56     protected Logger logger = null;
57     protected LoggerFactory loggerFactory = null;
58
59     public SpeedoTestHelper(String JavaDoc s) {
60         super(s);
61         getPMF();
62         loggerFactory = Monolog.monologFactory;
63         logger = loggerFactory.getLogger(getLoggerName());
64     }
65    
66     protected abstract String JavaDoc getLoggerName();
67     
68     public Logger getLogger() {
69         return logger;
70     }
71
72     public PersistenceManagerFactory getPMF() {
73         if (pmf == null) {
74             synchronized(SpeedoTestHelper.class) {
75                 if (pmf == null) {
76                     try {
77                         pmf = JDOHelper.getPersistenceManagerFactory(getPMFProperties());
78                     } catch (Error JavaDoc e) {
79                         e.printStackTrace();
80                         throw e;
81                     }
82                 }
83             }
84         }
85         return pmf;
86     }
87     
88     public Properties JavaDoc getPMFPropertiesFromFile() {
89         Properties JavaDoc p = new Properties JavaDoc();
90         InputStream JavaDoc is = getClass().getClassLoader()
91                 .getResourceAsStream("speedo.properties");
92         if (is == null) {
93             return null;
94         }
95         try {
96             p.load(is);
97         } catch (IOException JavaDoc e) {
98             return null;
99         }
100         return p;
101     }
102     
103     public String JavaDoc getConnectionDriverNameProperty(Properties JavaDoc p) {
104         String JavaDoc dcn = p.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME);
105         if (dcn == null) {
106             dcn = p.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_old);
107             if (dcn != null) {
108                 System.err.println("The property '" +
109                         SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_old
110                         + "' is deprecated, you have to use '"
111                         + SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME
112                         + "'.");
113             } else {
114                 dcn = p.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_old2);
115                 if (dcn == null) {
116                     System.out.println(p);
117                     fail("No driver class name specified");
118                 } else {
119                     System.err.println("The property '" +
120                             SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_old2
121                             + "' is deprecated, you have to use '"
122                             + SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME
123                             + "'.");
124                     
125                 }
126             }
127         }
128         return dcn;
129     }
130     
131     public Properties JavaDoc getPMFProperties() {
132         if (Boolean.getBoolean("org.objectweb.speedo.useFile")) {
133             return getPMFPropertiesFromFile();
134         }
135         String JavaDoc debug = System.getProperty("org.objectweb.speedo.debug", "false");
136         String JavaDoc dcn = getConnectionDriverNameProperty(System.getProperties());
137         String JavaDoc user = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, "");
138         String JavaDoc pass = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, "");
139         String JavaDoc url = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL);
140         if (url == null)
141             Assert.fail("No database url specified");
142         Properties JavaDoc p = new Properties JavaDoc();
143         p.setProperty(
144                 SpeedoProperties.JDO_PERSISTENCE_MANAGER_FACTORY_CLASS,
145             System.getProperty(SpeedoProperties.JDO_PERSISTENCE_MANAGER_FACTORY_CLASS,
146                 "org.objectweb.speedo.Speedo"));
147         p.setProperty("org.objectweb.speedo.debug", debug);
148         p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME, dcn);
149         p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, user);
150         p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, pass);
151         p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL, url);
152         p.setProperty(SpeedoProperties.CACHE_SIZE, "nolimit");
153         return p;
154     }
155
156     public void assertSameCollection(String JavaDoc msg, Collection JavaDoc expected, Collection JavaDoc found) {
157         try {
158             if(expected == null) {
159                 assertNull(msg + " null collection expected", found);
160                 return;
161             }
162             Assert.assertNotNull(msg + " non null collection expected", found);
163             Assert.assertEquals(msg + " not same size", expected.size(), found.size());
164             Iterator JavaDoc it = expected.iterator();
165             while(it.hasNext()) {
166                 Object JavaDoc element = it.next();
167                 Assert.assertTrue(msg + " the found collection (" + found
168                             + ") does not contains the element "
169                         + element , found.contains(element));
170             }
171             it = found.iterator();
172             while(it.hasNext()) {
173                 Object JavaDoc element = it.next();
174                 assertTrue(msg + " the found collection contains an unexpected element "
175                         + element , expected.contains(element));
176             }
177         } catch (Error JavaDoc e) {
178             throw e;
179         }
180     }
181
182     public void assertSameList(String JavaDoc msg, List JavaDoc expected, List JavaDoc found) {
183         if(expected == null) {
184             Assert.assertNull(msg + " null list expected", found);
185             return;
186         }
187         Assert.assertNotNull(msg + " non null list expected", found);
188         Assert.assertEquals(msg + " not same size", expected.size(), found.size());
189         for(int i=0; i<expected.size(); i++) {
190             Assert.assertEquals(msg + " bad element at the index" + i,
191                     expected.get(i), found.get(i));
192         }
193     }
194
195     public PMapper getMapper(PersistenceManagerFactory _pmf) throws Exception JavaDoc {
196         PersistenceManagerFactory fcpmf = ((Speedo) _pmf).getDelegate();
197         return (PMapper) getSubComponent(
198                 ((Interface)fcpmf).getFcItfOwner(),
199                 "mapper")
200                 .getFcInterface("mapper");
201     }
202
203     protected CacheAttributeController getCacheAttributeController(PersistenceManagerFactory _pmf) throws Exception JavaDoc {
204         PersistenceManagerFactory fcpmf = ((Speedo) _pmf).getDelegate();
205         Component c = getSubComponent(
206                 ((Interface)fcpmf).getFcItfOwner(),
207                 "tpm.cache-manager.cache-manager");
208         return (CacheAttributeController) Fractal.getAttributeController(c);
209     }
210
211     private Component getSubComponent(Component parent,
212                                       String JavaDoc path) throws Exception JavaDoc {
213         //System.out.println("path: " + path);
214

215         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, ".", false);
216         Component res = parent;
217         while(st.hasMoreTokens()) {
218             String JavaDoc commponenentname = st.nextToken();
219             Component[] children = Fractal.getContentController(res)
220                     .getFcSubComponents();
221             int i = 0;
222             //System.out.println("search: " + commponenentname);
223
while(i<children.length &&
224                     !Fractal.getNameController(children[i])
225                         .getFcName().equals(commponenentname)) {
226                 //System.out.println("current: " + ((NameController) children[i].getFcInterface("name-controller")).getFcName());
227
i++;
228             }
229             if (i<children.length) {
230                 //System.out.println("found: " + ((NameController) children[i].getFcInterface("name-controller")).getFcName());
231
res = children[i];
232             } else {
233                 //System.out.println("not found");
234
return null;
235             }
236         }
237         return res;
238     }
239
240     public Component getSubComponent(Component parent,
241             String JavaDoc path, String JavaDoc s) throws Exception JavaDoc {
242         //System.out.println("path: " + path);
243
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, ".", false);
244         Component res = parent;
245         while(st.hasMoreTokens()) {
246             String JavaDoc commponenentname = st.nextToken();
247             Component[] children = Fractal.getContentController(res)
248             .getFcSubComponents();
249             int i = 0;
250             //System.out.println("search: " + commponenentname);
251
while(i<children.length &&
252                     !Fractal.getNameController(children[i])
253                     .getFcName().equals(commponenentname)) {
254                 //System.out.println("current: " + ((NameController) children[i].getFcInterface("name-controller")).getFcName());
255
i++;
256             }
257             if (i<children.length) {
258                 //System.out.println("found: " + ((NameController) children[i].getFcInterface("name-controller")).getFcName());
259
res = children[i];
260             } else {
261                 //System.out.println("not found");
262
return null;
263             }
264         }
265         return res;
266     }
267     
268     protected int deleteAllInstances(Class JavaDoc clazz) {
269         PersistenceManager pm = pmf.getPersistenceManager();
270         pm.currentTransaction().begin();
271         Extent e = pm.getExtent(clazz, true);
272         Iterator JavaDoc it = e.iterator();
273         int i = 0;
274         while(it.hasNext()) {
275             pm.deletePersistent(it.next());
276             i++;
277         }
278         e.closeAll();
279         pm.currentTransaction().commit();
280         pm.close();
281         logger.log(BasicLevel.DEBUG, "Delete " + i + " instance(s) of the class " + clazz.getName());
282         return i;
283     }
284
285     public static int getIntProperty(String JavaDoc propertyName, int defaultValue) {
286         String JavaDoc v = System.getProperty(propertyName);
287         if (v == null) {
288             return defaultValue;
289         }
290         try {
291             return Integer.parseInt(v);
292         } catch (NumberFormatException JavaDoc e) {
293             return defaultValue;
294         }
295     }
296 }
297
Popular Tags