1 18 package org.objectweb.speedo; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Properties ; 26 import java.util.StringTokenizer ; 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 51 public abstract class SpeedoTestHelper extends TestCase { 52 53 public final static String 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 s) { 60 super(s); 61 getPMF(); 62 loggerFactory = Monolog.monologFactory; 63 logger = loggerFactory.getLogger(getLoggerName()); 64 } 65 66 protected abstract String 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 e) { 79 e.printStackTrace(); 80 throw e; 81 } 82 } 83 } 84 } 85 return pmf; 86 } 87 88 public Properties getPMFPropertiesFromFile() { 89 Properties p = new Properties (); 90 InputStream is = getClass().getClassLoader() 91 .getResourceAsStream("speedo.properties"); 92 if (is == null) { 93 return null; 94 } 95 try { 96 p.load(is); 97 } catch (IOException e) { 98 return null; 99 } 100 return p; 101 } 102 103 public String getConnectionDriverNameProperty(Properties p) { 104 String 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 getPMFProperties() { 132 if (Boolean.getBoolean("org.objectweb.speedo.useFile")) { 133 return getPMFPropertiesFromFile(); 134 } 135 String debug = System.getProperty("org.objectweb.speedo.debug", "false"); 136 String dcn = getConnectionDriverNameProperty(System.getProperties()); 137 String user = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, ""); 138 String pass = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, ""); 139 String url = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL); 140 if (url == null) 141 Assert.fail("No database url specified"); 142 Properties p = new Properties (); 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 msg, Collection expected, Collection 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 it = expected.iterator(); 165 while(it.hasNext()) { 166 Object 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 element = it.next(); 174 assertTrue(msg + " the found collection contains an unexpected element " 175 + element , expected.contains(element)); 176 } 177 } catch (Error e) { 178 throw e; 179 } 180 } 181 182 public void assertSameList(String msg, List expected, List 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 { 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 { 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 path) throws Exception { 213 215 StringTokenizer st = new StringTokenizer (path, ".", false); 216 Component res = parent; 217 while(st.hasMoreTokens()) { 218 String commponenentname = st.nextToken(); 219 Component[] children = Fractal.getContentController(res) 220 .getFcSubComponents(); 221 int i = 0; 222 while(i<children.length && 224 !Fractal.getNameController(children[i]) 225 .getFcName().equals(commponenentname)) { 226 i++; 228 } 229 if (i<children.length) { 230 res = children[i]; 232 } else { 233 return null; 235 } 236 } 237 return res; 238 } 239 240 public Component getSubComponent(Component parent, 241 String path, String s) throws Exception { 242 StringTokenizer st = new StringTokenizer (path, ".", false); 244 Component res = parent; 245 while(st.hasMoreTokens()) { 246 String commponenentname = st.nextToken(); 247 Component[] children = Fractal.getContentController(res) 248 .getFcSubComponents(); 249 int i = 0; 250 while(i<children.length && 252 !Fractal.getNameController(children[i]) 253 .getFcName().equals(commponenentname)) { 254 i++; 256 } 257 if (i<children.length) { 258 res = children[i]; 260 } else { 261 return null; 263 } 264 } 265 return res; 266 } 267 268 protected int deleteAllInstances(Class clazz) { 269 PersistenceManager pm = pmf.getPersistenceManager(); 270 pm.currentTransaction().begin(); 271 Extent e = pm.getExtent(clazz, true); 272 Iterator 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 propertyName, int defaultValue) { 286 String v = System.getProperty(propertyName); 287 if (v == null) { 288 return defaultValue; 289 } 290 try { 291 return Integer.parseInt(v); 292 } catch (NumberFormatException e) { 293 return defaultValue; 294 } 295 } 296 } 297 | Popular Tags |