1 10 11 package org.mule.tck; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import junit.framework.TestCase; 17 18 import org.apache.commons.lang.StringUtils; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.mule.MuleManager; 22 import org.mule.config.MuleConfiguration; 23 import org.mule.impl.MuleDescriptor; 24 import org.mule.tck.testmodels.mule.TestConnector; 25 import org.mule.umo.UMOComponent; 26 import org.mule.umo.UMOEvent; 27 import org.mule.umo.UMOEventContext; 28 import org.mule.umo.UMOException; 29 import org.mule.umo.UMOSession; 30 import org.mule.umo.endpoint.UMOEndpoint; 31 import org.mule.umo.endpoint.UMOImmutableEndpoint; 32 import org.mule.umo.manager.UMOManager; 33 import org.mule.umo.transformer.UMOTransformer; 34 import org.mule.util.FileUtils; 35 import org.mule.util.StringMessageUtils; 36 37 41 public abstract class AbstractMuleTestCase extends TestCase 42 { 43 protected transient final Log logger = LogFactory.getLog(getClass()); 44 45 protected String prereqs = null; 47 private boolean offline = System.getProperty("org.mule.offline", "false").equalsIgnoreCase("true"); 48 private boolean testLogging = System.getProperty("org.mule.test.logging", "false").equalsIgnoreCase( 49 "true"); 50 51 private static Map testCounters; 52 53 public AbstractMuleTestCase() 54 { 55 super(); 56 if (testCounters == null) 57 { 58 testCounters = new HashMap (); 59 } 60 addTest(); 61 } 62 63 protected void addTest() 64 { 65 TestInfo info = (TestInfo)testCounters.get(getClass().getName()); 66 if (info == null) 67 { 68 info = new TestInfo(getClass().getName()); 69 testCounters.put(getClass().getName(), info); 70 } 71 info.incTestCount(); 72 } 73 74 protected void setDisposeManagerPerSuite(boolean val) 75 { 76 getTestInfo().setDisposeManagerPerSuite(val); 77 } 78 79 protected TestInfo getTestInfo() 80 { 81 TestInfo info = (TestInfo)testCounters.get(getClass().getName()); 82 if (info == null) 83 { 84 info = new TestInfo(getClass().getName()); 85 testCounters.put(getClass().getName(), info); 86 } 87 return info; 88 } 89 90 private void clearAllCounters() 91 { 92 if (testCounters != null) 93 { 94 testCounters.clear(); 95 } 96 log("Cleared all counters"); 97 } 98 99 private void clearCounter() 100 { 101 if (testCounters != null) 102 { 103 testCounters.remove(getClass().getName()); 104 } 105 log("Cleared counter: " + getClass().getName()); 106 } 107 108 private void log(String s) 109 { 110 if (testLogging) 111 { 112 System.err.println(s); 113 } 114 } 115 116 public String getName() 117 { 118 return super.getName().substring(4).replaceAll("([A-Z])", " $1").toLowerCase() + " "; 119 } 120 121 127 protected String checkPreReqs() 128 { 129 return null; 130 } 131 132 public boolean isOffline(String method) 133 { 134 if (offline) 135 { 136 System.out.println(StringMessageUtils.getBoilerPlate( 137 "Working offline cannot run test: " + method, '=', 80)); 138 } 139 return offline; 140 } 141 142 public boolean isPrereqsMet(String method) 143 { 144 prereqs = checkPreReqs(); 145 if (prereqs != null) 146 { 147 System.out.println(StringMessageUtils.getBoilerPlate( 148 "WARNING\nPrerequisites for test: " + method + " were not met. skipping test: " + prereqs, 149 '=', 80)); 150 } 151 return prereqs == null; 152 } 153 154 protected final void setUp() throws Exception 155 { 156 System.out.println(StringMessageUtils.getBoilerPlate("Testing: " + toString(), '=', 80)); 157 MuleManager.getConfiguration().getDefaultThreadingProfile().setDoThreading(false); 158 MuleManager.getConfiguration().setServerUrl(StringUtils.EMPTY); 159 160 try 161 { 162 if (getTestInfo().getRunCount() == 0) 163 { 164 if (getTestInfo().isDisposeManagerPerSuite()) 165 { 166 disposeManager(); 168 } 169 log("Pre suiteSetup for test: " + getTestInfo()); 170 suitePreSetUp(); 171 } 172 if (!getTestInfo().isDisposeManagerPerSuite()) 173 { 174 disposeManager(); 176 } 177 if (!isPrereqsMet(getClass().getName() + ".setUp()")) 178 { 179 return; 180 } 181 doSetUp(); 182 if (getTestInfo().getRunCount() == 0) 183 { 184 log("Post suiteSetup for test: " + getTestInfo()); 185 suitePostSetUp(); 186 } 187 } 188 catch (Exception e) 189 { 190 getTestInfo().incRunCount(); 191 throw e; 192 } 193 } 194 195 protected void suitePreSetUp() throws Exception 196 { 197 } 199 200 protected void suitePostSetUp() throws Exception 201 { 202 } 204 205 protected void suitePreTearDown() throws Exception 206 { 207 } 209 210 protected void suitePostTearDown() throws Exception 211 { 212 } 214 215 protected final void tearDown() throws Exception 216 { 217 try 218 { 219 if (getTestInfo().getRunCount() == getTestInfo().getTestCount()) 220 { 221 log("Pre suiteTearDown for test: " + getTestInfo()); 222 suitePreTearDown(); 223 } 224 doTearDown(); 225 if (!getTestInfo().isDisposeManagerPerSuite()) 226 { 227 disposeManager(); 228 } 229 } 230 finally 231 { 232 getTestInfo().incRunCount(); 233 if (getTestInfo().getRunCount() == getTestInfo().getTestCount()) 234 { 235 try 236 { 237 log("Post suiteTearDown for test: " + getTestInfo()); 238 suitePostTearDown(); 239 } 240 finally 241 { 242 clearCounter(); 243 disposeManager(); 244 } 245 } 246 } 247 } 248 249 protected void disposeManager() 250 { 251 log("disposing manager. disposeManagerPerSuite=" + getTestInfo().isDisposeManagerPerSuite()); 252 if (MuleManager.isInstanciated()) 253 { 254 MuleManager.getInstance().dispose(); 255 } 256 FileUtils.deleteTree(FileUtils.newFile(MuleManager.getConfiguration().getWorkingDirectory())); 257 FileUtils.deleteTree(FileUtils.newFile("./ActiveMQ")); 258 MuleManager.setConfiguration(new MuleConfiguration()); 259 } 260 261 protected void doSetUp() throws Exception 262 { 263 } 265 266 protected void doTearDown() throws Exception 267 { 268 } 270 271 public static UMOManager getManager(boolean disableAdminAgent) throws Exception 272 { 273 return MuleTestUtils.getManager(disableAdminAgent); 274 } 275 276 public static UMOEndpoint getTestEndpoint(String name, String type) throws Exception 277 { 278 return MuleTestUtils.getTestEndpoint(name, type); 279 } 280 281 public static UMOEvent getTestEvent(Object data) throws Exception 282 { 283 return MuleTestUtils.getTestEvent(data); 284 } 285 286 public static UMOEventContext getTestEventContext(Object data) throws Exception 287 { 288 return MuleTestUtils.getTestEventContext(data); 289 } 290 291 public static UMOTransformer getTestTransformer() 292 { 293 return MuleTestUtils.getTestTransformer(); 294 } 295 296 public static UMOEvent getTestEvent(Object data, MuleDescriptor descriptor) throws Exception 297 { 298 return MuleTestUtils.getTestEvent(data, descriptor); 299 } 300 301 public static UMOEvent getTestEvent(Object data, UMOImmutableEndpoint endpoint) throws Exception 302 { 303 return MuleTestUtils.getTestEvent(data, endpoint); 304 } 305 306 public static UMOEvent getTestEvent(Object data, MuleDescriptor descriptor, UMOImmutableEndpoint endpoint) 307 throws UMOException 308 { 309 return MuleTestUtils.getTestEvent(data, descriptor, endpoint); 310 } 311 312 public static UMOSession getTestSession(UMOComponent component) 313 { 314 return MuleTestUtils.getTestSession(component); 315 } 316 317 public static TestConnector getTestConnector() 318 { 319 return MuleTestUtils.getTestConnector(); 320 } 321 322 public static UMOComponent getTestComponent(MuleDescriptor descriptor) 323 { 324 return MuleTestUtils.getTestComponent(descriptor); 325 } 326 327 public static MuleDescriptor getTestDescriptor(String name, String implementation) throws Exception 328 { 329 return MuleTestUtils.getTestDescriptor(name, implementation); 330 } 331 332 public static UMOManager getTestManager() throws UMOException 333 { 334 return MuleTestUtils.getTestManager(); 335 } 336 337 protected void finalize() throws Throwable 338 { 339 try 340 { 341 clearAllCounters(); 342 } 343 finally 344 { 345 super.finalize(); 346 } 347 } 348 349 protected class TestInfo 350 { 351 355 private boolean disposeManagerPerSuite = false; 356 private int testCount = 0; 357 private int runCount = 0; 358 private String name; 359 360 public TestInfo(String name) 361 { 362 this.name = name; 363 } 364 365 public void clearCounts() 366 { 367 testCount = 0; 368 runCount = 0; 369 log("Cleared counts for: " + name); 370 } 371 372 public void incTestCount() 373 { 374 testCount++; 375 log("Added test: " + name + " " + testCount); 376 } 377 378 public void incRunCount() 379 { 380 runCount++; 381 log("Finished Run: " + toString()); 382 } 383 384 public int getTestCount() 385 { 386 return testCount; 387 } 388 389 public int getRunCount() 390 { 391 return runCount; 392 } 393 394 public String getName() 395 { 396 return name; 397 } 398 399 public boolean isDisposeManagerPerSuite() 400 { 401 return disposeManagerPerSuite; 402 } 403 404 public void setDisposeManagerPerSuite(boolean disposeManagerPerSuite) 405 { 406 this.disposeManagerPerSuite = disposeManagerPerSuite; 407 } 408 409 public String toString() 410 { 411 StringBuffer buf = new StringBuffer (); 412 return buf.append(name).append(", (").append(runCount).append(" / ").append(testCount).append( 413 ") tests run, disposePerSuite=").append(disposeManagerPerSuite).toString(); 414 } 415 } 416 } 417 | Popular Tags |