1 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 ; 47 import java.util.Properties ; 48 import java.util.Date ; 49 import java.util.Calendar ; 50 import java.util.Map ; 51 import java.util.HashMap ; 52 import java.util.Set ; 53 import java.io.InputStream ; 54 import java.io.FileInputStream ; 55 56 60 public abstract class TestRuntimeHelper 61 extends TestCase 62 implements PClassMapping.ReferenceConfigurator { 63 64 public final static String LOG_NAME = "org.objectweb.jorm"; 65 public final static String TESTCONFIGFILE = "TESTCONFIG"; 66 67 public PMapper mapper = null; 68 public Logger logger = null; 69 public LoggerFactory loggerFactory = null; 70 71 private Properties configprop = null; 72 private MapperFactory mapperFactory = new MapperFactory(); 73 private Logger spylogger = null; 74 private Map name2pcm = new HashMap (); 75 76 public TestRuntimeHelper(String s) throws Exception { 77 super(s); 78 String fn = System.getProperty(TESTCONFIGFILE); 79 if (fn == null) { 80 throw new Exception ("Cannot read environment variable " 81 + TESTCONFIGFILE + " in order to configure the test case."); 82 } 83 InputStream is = new FileInputStream (fn); 84 if (is == null) { 85 throw new Exception ("Cannot load file " + fn 86 + " in order to configure the test case."); 87 } 88 configprop = new Properties (); 89 configprop.load(is); 90 } 91 92 protected abstract String getLoggerName(); 93 94 protected void setUp() throws Exception { 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 { 104 mapper.stop(); 105 mapper = null; 106 logger = null; 107 P6SpyLogger.logger = spylogger; 108 } 109 110 115 protected void changeLogger(String name) { 116 logger = loggerFactory.getLogger(name); 117 P6SpyLogger.logger = logger; 118 } 119 120 121 protected PClassMapping getMapping(String 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 unresolvedDep = cl.getUnResolvedDependencies(); 143 Iterator it = unresolvedDep.iterator(); 144 if (it.hasNext()) { 148 String className = (String ) 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 e) { 160 Exception 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 className) throws Exception ; 169 170 protected boolean performIO(String cn, PAccessor towrite, 171 PAccessor toread, Object hints) { 172 return performIO(cn, towrite, toread, hints, true); 173 } 174 175 protected boolean performIO(String cn, 176 PAccessor towrite, 177 PAccessor toread, Object hints, 178 boolean doUnexport) { 179 Object 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 e) { 197 Exception 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 e) { 206 } 207 conn = null; 208 } 209 } 210 return res; 211 } 212 213 protected boolean performIO(String cn, 214 PAccessor towrite, 215 PAccessor toread) { 216 return performIO(cn, towrite, toread, null); 217 } 218 219 public Date getTime() { 220 return new Date ((Calendar.getInstance().getTime().getTime() / 1000) * 1000); 221 } 222 223 protected PBinding export(String cn, Object hints) { 224 Object 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 scn = cn.substring(0, fi); 232 String 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 e) { 257 Exception 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 e) { 265 } 266 conn = null; 267 } 268 } 269 return pb; 270 } 271 272 protected PBinding bind(String cn, PName pn) { 273 Object 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 scn = cn.substring(0, fi); 281 String 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 e) { 304 Exception 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 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 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 e) { 329 Exception 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 e) { 338 } 339 conn = null; 340 } 341 } 342 return res; 343 } 344 345 protected boolean unexport(PBinding pb, PAccessor pa) { 346 Object 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 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 e) { 362 } 363 conn = null; 364 } 365 } 366 return res; 367 } 368 369 public void assertEqual(String 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 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 396 public boolean isCodingOnly(String className, String refFieldName) { 397 return true; 398 } 399 400 public PNameCoder getPNameCoder(String sourceclassName, 401 String refFieldName, 402 String destclassName) { 403 return getMapping(destclassName).getPBinder(); 404 } 405 406 public PNameCoder getPNameCoder(String sourceclassName, 407 String refFieldName, 408 String [] genClassNames) { 409 String 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 e) { 416 return null; 417 } 418 } 419 420 public PClassMapping getGenClassMapping(String sourceclassName, 421 String refFieldName, 422 String [] genClassNames) { 423 String 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 e) { 432 return null; 433 } 434 } 435 436 public PClassMapping getGenClassMapping(String sourceclassName, 437 String refFieldName, 438 String [] genClassNames, 439 String destclassName) { 440 PClassMapping pcm = getGenClassMapping(sourceclassName, refFieldName, genClassNames); 441 try { 442 ((PClassMappingCtrl) pcm).setPNameCoder(getBinder(destclassName)); 443 return pcm; 444 } catch (Exception e) { 445 return null; 446 } 447 } 448 } 449 | Popular Tags |