KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > runtime > TestRuntimeHelper


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
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  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.runtime;
25
26 import junit.framework.TestCase;
27 import org.objectweb.jorm.api.PMapper;
28 import org.objectweb.jorm.api.PClassMapping;
29 import org.objectweb.jorm.api.PException;
30 import org.objectweb.jorm.api.PMapCluster;
31 import org.objectweb.jorm.api.PAccessor;
32 import org.objectweb.jorm.api.PBinding;
33 import org.objectweb.jorm.api.PClassMappingCtrl;
34 import org.objectweb.jorm.MapperFactory;
35 import org.objectweb.jorm.ExceptionHelper;
36 import org.objectweb.jorm.util.api.Loggable;
37 import org.objectweb.jorm.naming.api.PBinder;
38 import org.objectweb.jorm.naming.api.PNameCoder;
39 import org.objectweb.jorm.naming.api.PName;
40 import org.objectweb.util.monolog.api.Logger;
41 import org.objectweb.util.monolog.api.LoggerFactory;
42 import org.objectweb.util.monolog.api.BasicLevel;
43 import org.objectweb.util.monolog.wrapper.p6spy.P6SpyLogger;
44 import org.objectweb.medor.eval.prefetch.lib.PrefetchCacheImpl;
45
46 import java.util.Iterator JavaDoc;
47 import java.util.Properties JavaDoc;
48 import java.util.Date JavaDoc;
49 import java.util.Calendar JavaDoc;
50 import java.util.Map JavaDoc;
51 import java.util.HashMap JavaDoc;
52 import java.util.Set JavaDoc;
53 import java.io.InputStream JavaDoc;
54 import java.io.FileInputStream JavaDoc;
55
56 /**
57  *
58  * @author S.Chassande-Barrioz
59  */

60 public abstract class TestRuntimeHelper
61         extends TestCase
62         implements PClassMapping.ReferenceConfigurator {
63
64     public final static String JavaDoc LOG_NAME = "org.objectweb.jorm";
65     public final static String JavaDoc TESTCONFIGFILE = "TESTCONFIG";
66
67     public PMapper mapper = null;
68     public Logger logger = null;
69     public LoggerFactory loggerFactory = null;
70
71     private Properties JavaDoc configprop = null;
72     private MapperFactory mapperFactory = new MapperFactory();
73     private Logger spylogger = null;
74     private Map JavaDoc name2pcm = new HashMap JavaDoc();
75
76     public TestRuntimeHelper(String JavaDoc s) throws Exception JavaDoc {
77         super(s);
78         String JavaDoc fn = System.getProperty(TESTCONFIGFILE);
79         if (fn == null) {
80             throw new Exception JavaDoc("Cannot read environment variable "
81                                 + TESTCONFIGFILE + " in order to configure the test case.");
82         }
83         InputStream JavaDoc is = new FileInputStream JavaDoc(fn);
84         if (is == null) {
85             throw new Exception JavaDoc("Cannot load file " + fn
86                                 + " in order to configure the test case.");
87         }
88         configprop = new Properties JavaDoc();
89         configprop.load(is);
90     }
91
92     protected abstract String JavaDoc getLoggerName();
93
94     protected void setUp() throws Exception JavaDoc {
95         mapper = mapperFactory.createMapper(configprop);
96         mapper.start();
97         loggerFactory = ((Loggable) mapper).getLoggerFactory();
98         logger = loggerFactory.getLogger(getLoggerName());
99         P6SpyLogger.logger = logger;
100         mapper.setPrefetchCache(new PrefetchCacheImpl(logger));
101     }
102
103     protected void tearDown() throws Exception JavaDoc {
104         mapper.stop();
105         mapper = null;
106         logger = null;
107         P6SpyLogger.logger = spylogger;
108     }
109
110     /**
111      * It changes the current logger to a new Logger with the name specified in
112      * parameter.
113      * @param name the name of the new Logger
114      */

115     protected void changeLogger(String JavaDoc name) {
116         logger = loggerFactory.getLogger(name);
117         P6SpyLogger.logger = logger;
118     }
119
120
121     protected PClassMapping getMapping(String JavaDoc cn) {
122         PClassMapping pcm = mapper.lookup(cn);
123         if (pcm == null) {
124             pcm = (PClassMapping) name2pcm.get(cn);
125         }
126         if (pcm == null) {
127             try {
128                 logger.log(BasicLevel.DEBUG, "Initializing the PCM of the class " + cn);
129                 pcm = (PClassMapping) Class.forName(
130                         mapper.cn2mn(cn) + PMapper.PCLASSMAPPINGAPPENDER)
131                         .newInstance();
132                 PBinder binder = getBinder(cn);
133                 binder.setPClassMapping(pcm);
134                 pcm.setPBinder(binder);
135                 name2pcm.put(cn, pcm);
136                 pcm.configureRefFields(this);
137                 mapper.map(pcm);
138                 name2pcm.remove(cn);
139                 PMapCluster cl = mapper.getPMapCluster(cn);
140                 logger.log(BasicLevel.DEBUG, "cluster(" + cn + "):" + cl);
141                 if (!cl.isDefined()) {
142                     Set JavaDoc unresolvedDep = cl.getUnResolvedDependencies();
143                     Iterator JavaDoc it = unresolvedDep.iterator();
144                     // if there is a dependency left, treat it
145
// the algo stops eventually because at least one element of unresolvedDep is removed
146
// at each recursion
147
if (it.hasNext()) {
148                         String JavaDoc className = (String JavaDoc) it.next();
149                         logger.log(BasicLevel.DEBUG, "Resolving next dependency: " + className);
150                         getMapping(className);
151                         logger.log(BasicLevel.DEBUG, "Dependency " + className + " resolved");
152                     }
153                 } else {
154                     logger.log(BasicLevel.DEBUG, "Deleting the hosting structure");
155                     cl.deleteMappingStructures();
156                     logger.log(BasicLevel.DEBUG, "Creating a hosting structure");
157                     cl.createMappingStructures(true);
158                 }
159             } catch (Exception JavaDoc e) {
160                 Exception JavaDoc inner = ExceptionHelper.getFirstException(e);
161                 logger.log(BasicLevel.ERROR, logger.getName(), inner);
162                 fail(logger.getName() + ": " + inner.getMessage());
163             }
164         }
165         return pcm;
166     }
167
168     protected abstract PBinder getBinder(String JavaDoc className) throws Exception JavaDoc;
169
170     protected boolean performIO(String JavaDoc cn, PAccessor towrite,
171                                 PAccessor toread, Object JavaDoc hints) {
172         return performIO(cn, towrite, toread, hints, true);
173     }
174     
175     protected boolean performIO(String JavaDoc cn,
176                                 PAccessor towrite,
177                                 PAccessor toread, Object JavaDoc hints,
178                                 boolean doUnexport) {
179         Object JavaDoc conn = null;
180         boolean res = true;
181         try {
182             PClassMapping pcm = getMapping(cn);
183             conn = mapper.getConnection();
184             PBinding pb1 = pcm.createPBinding();
185             if (hints == null) {
186                 pb1.export(conn);
187             } else {
188                 pb1.export(conn, hints);
189             }
190             pb1.write(conn, towrite);
191             pb1.read(conn, toread);
192             if (doUnexport) {
193                 pb1.unexport(conn);
194                 pb1.write(conn, towrite);
195             }
196         } catch (Exception JavaDoc e) {
197             Exception JavaDoc inner = ExceptionHelper.getFirstException(e);
198             logger.log(BasicLevel.ERROR, logger.getName(), inner);
199             fail(logger.getName() + ": " + inner.getMessage());
200             res = false;
201         } finally {
202             if (conn != null) {
203                 try {
204                     mapper.closeConnection(conn);
205                 } catch (Exception JavaDoc e) {
206                 }
207                 conn = null;
208             }
209         }
210         return res;
211     }
212
213     protected boolean performIO(String JavaDoc cn,
214                                 PAccessor towrite,
215                                 PAccessor toread) {
216         return performIO(cn, towrite, toread, null);
217     }
218
219     public Date JavaDoc getTime() {
220         return new Date JavaDoc((Calendar.getInstance().getTime().getTime() / 1000) * 1000);
221     }
222
223     protected PBinding export(String JavaDoc cn, Object JavaDoc hints) {
224         Object JavaDoc conn = null;
225         PBinding pb = null;
226         PClassMapping pcm;
227         try {
228             int fi = cn.indexOf("/");
229             if (fi != -1) {
230                 int li, nbgc = 0;
231                 String JavaDoc scn = cn.substring(0, fi);
232                 String JavaDoc fn;
233                 li = cn.indexOf("/", fi + 1);
234                 if (li == -1) {
235                     fn = cn.substring(fi + 1);
236                 } else {
237                     fn = cn.substring(fi + 1, li);
238                     do {
239                         nbgc++;
240                         li = cn.indexOf("/", li + 1);
241                     } while (li != -1);
242                 }
243                 
244                 pcm = getMapping(scn);
245                 pcm = pcm.getGenClassMapping(fn);
246             } else {
247                pcm = getMapping(cn);
248             }
249             conn = mapper.getConnection();
250             pb = pcm.createPBinding();
251             if (hints == null) {
252                 pb.export(conn);
253             } else {
254                 pb.export(conn, hints);
255             }
256         } catch (Exception JavaDoc e) {
257             Exception JavaDoc inner = ExceptionHelper.getFirstException(e);
258             logger.log(BasicLevel.ERROR, logger.getName(), inner);
259             fail(logger.getName() + ": " + inner.getMessage());
260         } finally {
261             if (conn != null) {
262                 try {
263                     mapper.closeConnection(conn);
264                 } catch (Exception JavaDoc e) {
265                 }
266                 conn = null;
267             }
268         }
269         return pb;
270     }
271
272     protected PBinding bind(String JavaDoc cn, PName pn) {
273         Object JavaDoc conn = null;
274         PBinding pb = null;
275         PClassMapping pcm;
276         try {
277             int fi = cn.indexOf("/");
278             if (fi != -1) {
279                 int li, nbgc = 0;
280                 String JavaDoc scn = cn.substring(0, fi);
281                 String JavaDoc fn;
282                 li = cn.indexOf("/", fi + 1);
283                 if (li == -1) {
284                     fn = cn.substring(fi + 1);
285                 } else {
286                     fn = cn.substring(fi + 1, li);
287                     do {
288                         nbgc++;
289                         li = cn.indexOf("/", li + 1);
290                     } while (li != -1);
291                 }
292                 pcm = getMapping(scn);
293                 pcm = pcm.getGenClassMapping(fn);
294                 for (int i = 0; i < nbgc; i++) {
295                     pcm = pcm.getGenClassMapping();
296                 }
297             } else {
298                 pcm = getMapping(cn);
299             }
300             conn = mapper.getConnection();
301             pb = pcm.createPBinding();
302             pb.bind(pn);
303         } catch (Exception JavaDoc e) {
304             Exception JavaDoc inner = ExceptionHelper.getFirstException(e);
305             logger.log(BasicLevel.ERROR, logger.getName(), inner);
306             fail(logger.getName() + ": " + inner.getMessage());
307         } finally {
308             if (conn != null) {
309                 try {
310                     mapper.closeConnection(conn);
311                 } catch (Exception JavaDoc e) {
312                 }
313                 conn = null;
314             }
315         }
316         return pb;
317     }
318
319     protected boolean writeRead(PBinding pb, PAccessor towrite,
320                                 PAccessor toread) {
321
322         Object JavaDoc conn = null;
323         boolean res = true;
324         try {
325             conn = mapper.getConnection();
326             pb.write(conn, towrite);
327             pb.read(conn, toread);
328         } catch (Exception JavaDoc e) {
329             Exception JavaDoc inner = ExceptionHelper.getFirstException(e);
330             logger.log(BasicLevel.ERROR, logger.getName(), inner);
331             fail(logger.getName() + ": " + inner.getMessage());
332             res = false;
333         } finally {
334             if (conn != null) {
335                 try {
336                     mapper.closeConnection(conn);
337                 } catch (Exception JavaDoc e) {
338                 }
339                 conn = null;
340             }
341         }
342         return res;
343     }
344
345     protected boolean unexport(PBinding pb, PAccessor pa) {
346         Object JavaDoc conn = null;
347         boolean res = true;
348         try {
349             conn = mapper.getConnection();
350             pb.unexport(conn);
351             pb.write(conn, pa);
352         } catch (PException e) {
353             Exception JavaDoc inner = ExceptionHelper.getFirstException(e);
354             logger.log(BasicLevel.ERROR, logger.getName(), inner);
355             fail(logger.getName() + ": " + inner.getMessage());
356             res = false;
357         } finally {
358             if (conn != null) {
359                 try {
360                     mapper.closeConnection(conn);
361                 } catch (Exception JavaDoc e) {
362                 }
363                 conn = null;
364             }
365         }
366         return res;
367     }
368
369     public void assertEqual(String JavaDoc msg, byte[] ba1, byte[] ba2) {
370         if (ba1 == null) {
371             assertNull(msg + "Must be null, found: " + ba2, ba2);
372         } else {
373             assertNotNull(msg + "Must not be null, expected " + ba1, ba2);
374             assertEquals(msg + "Bad length", ba1.length, ba2.length);
375             for (int i = 0; i < ba1.length; i++) {
376                 assertEquals(msg + "Bad " + i + " character", ba1[i], ba2[i]);
377             }
378         }
379     }
380
381     public void assertEqual(String JavaDoc msg, char[] ba1, char[] ba2) {
382         if (ba1 == null) {
383             assertNull(msg + "Must be null, found: " + ba2.toString(), ba2);
384         } else {
385             assertNotNull(msg + "Must not be null, expected " + ba1.toString(), ba2);
386             assertEquals(msg + "Bad length", ba1.length, ba2.length);
387             for (int i = 0; i < ba1.length; i++) {
388                 assertEquals(msg + "Bad " + i + " character", ba1[i], ba2[i]);
389             }
390         }
391     }
392
393     // IMPLEMENTATION OF THE ReferenceConfigurator INTERFACE //
394
//-------------------------------------------------------//
395

396     public boolean isCodingOnly(String JavaDoc className, String JavaDoc refFieldName) {
397         return true;
398     }
399
400     public PNameCoder getPNameCoder(String JavaDoc sourceclassName,
401                                     String JavaDoc refFieldName,
402                                     String JavaDoc destclassName) {
403         return getMapping(destclassName).getPBinder();
404     }
405
406     public PNameCoder getPNameCoder(String JavaDoc sourceclassName,
407                                     String JavaDoc refFieldName,
408                                     String JavaDoc[] genClassNames) {
409         String JavaDoc gcname = sourceclassName + "/" + refFieldName;
410         for (int i = 0; i < genClassNames.length - 1; i++) {
411             gcname += genClassNames[i];
412         }
413         try {
414             return getBinder(gcname);
415         } catch (Exception JavaDoc e) {
416             return null;
417         }
418     }
419
420     public PClassMapping getGenClassMapping(String JavaDoc sourceclassName,
421                                             String JavaDoc refFieldName,
422                                             String JavaDoc[] genClassNames) {
423         String JavaDoc gcname = sourceclassName + "/" + refFieldName;
424         for (int i = 0; i < genClassNames.length - 1; i++) {
425             gcname += genClassNames[i];
426         }
427         try {
428             PClassMapping pcm = mapper.createGenClassMapping();
429             pcm.setPBinder(getBinder(gcname));
430             return pcm;
431         } catch (Exception JavaDoc e) {
432             return null;
433         }
434     }
435
436     public PClassMapping getGenClassMapping(String JavaDoc sourceclassName,
437                                             String JavaDoc refFieldName,
438                                             String JavaDoc[] genClassNames,
439                                             String JavaDoc destclassName) {
440         PClassMapping pcm = getGenClassMapping(sourceclassName, refFieldName, genClassNames);
441         try {
442             ((PClassMappingCtrl) pcm).setPNameCoder(getBinder(destclassName));
443             return pcm;
444         } catch (Exception JavaDoc e) {
445             return null;
446         }
447     }
448 }
449
Popular Tags