From e5a6acfa56a2c6c1553b2046fefea45d99baf5fa Mon Sep 17 00:00:00 2001 From: rfeng Date: Tue, 8 Jul 2008 23:11:25 +0000 Subject: Simplify the unit test invoker git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@675041 13f79535-47bb-0310-9956-ffa450edef68 --- .../harness/OSGiTuscanyNonOSGiTestHarness.java | 54 +++-- .../test/osgi/harness/OSGiTuscanyTestHarness.java | 219 +++++++-------------- 2 files changed, 93 insertions(+), 180 deletions(-) (limited to 'java/sca/itest/osgi-tuscany') diff --git a/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyNonOSGiTestHarness.java b/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyNonOSGiTestHarness.java index ace762bb47..8bf7a1fb2f 100644 --- a/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyNonOSGiTestHarness.java +++ b/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyNonOSGiTestHarness.java @@ -18,8 +18,6 @@ */ package org.apache.tuscany.sca.test.osgi.harness; - - import java.io.File; import java.lang.reflect.Method; import java.net.URL; @@ -27,7 +25,6 @@ import java.net.URLClassLoader; import java.util.HashSet; import junit.framework.Assert; -import junit.framework.TestResult; import org.apache.tuscany.sca.test.util.TuscanyLoader; @@ -36,14 +33,11 @@ import org.apache.tuscany.sca.test.util.TuscanyLoader; * This harness runs Tuscany samples outside OSGi with Tuscany running in OSGi */ public class OSGiTuscanyNonOSGiTestHarness extends OSGiTuscanyTestHarness { - - - - + public void runTest(String... testDirs) throws Exception { - + String mainTestDir = testDirs[0]; - + File testDir = new File(mainTestDir + "/target/test-classes"); if (!testDir.exists()) { System.err.println("Test directory " + testDir + " does not exist"); @@ -53,8 +47,7 @@ public class OSGiTuscanyNonOSGiTestHarness extends OSGiTuscanyTestHarness { System.out.println("Run tests from : " + mainTestDir); long startTime = System.currentTimeMillis(); - - + String[] dirs = new String[testDirs.length + 2]; int i = 0; dirs[i++] = mainTestDir + "/target/test-classes"; @@ -62,58 +55,55 @@ public class OSGiTuscanyNonOSGiTestHarness extends OSGiTuscanyTestHarness { for (int j = 0; j < testDirs.length; j++) { dirs[i++] = testDirs[j] + "/target/classes"; } - tuscanyRuntime = TuscanyLoader.loadTuscanyIntoOSGi(getBundleContext()); long endTime = System.currentTimeMillis(); - - System.out.println("Loaded Tuscany, time taken = " + (endTime-startTime) + " ms" ); - + + System.out.println("Loaded Tuscany, time taken = " + (endTime - startTime) + " ms"); + URL[] dirURLs = new URL[dirs.length]; for (int j = 0; j < dirs.length; j++) { - dirURLs[j] = new File(dirs[j]).toURI().toURL(); + dirURLs[j] = new File(dirs[j]).toURI().toURL(); } ClassLoader testClassLoader = new URLClassLoader(dirURLs, Thread.currentThread().getContextClassLoader()); Thread.currentThread().setContextClassLoader(testClassLoader); - + Class testClass = testClassLoader.loadClass(this.getClass().getName()); Method testMethod = testClass.getMethod("runAllTestsFromDirs", ClassLoader.class, String[].class); Object testObject = testClass.newInstance(); testMethod.invoke(testObject, testClassLoader, dirs); - + } - + public void getTestCases(File dir, String prefix, HashSet testCaseSet) { File[] files = dir.listFiles(); for (File file : files) { if (file.isDirectory()) { - String newPrefix = prefix == null?file.getName() : prefix + "." + file.getName(); + String newPrefix = prefix == null ? file.getName() : prefix + "." + file.getName(); getTestCases(file, newPrefix, testCaseSet); - } - else if (file.getName().endsWith("TestCase.class")) { + } else if (file.getName().endsWith("TestCase.class")) { String name = file.getName(); - name = name.substring(0, name.length()-6); // remove .class - name = (prefix == null)?name : prefix + "." + name; - + name = name.substring(0, name.length() - 6); // remove .class + name = (prefix == null) ? name : prefix + "." + name; + testCaseSet.add(name); } } } - public void runAllTestsFromDirs(ClassLoader testClassLoader, String[] testDirs) throws Exception { - - TestResult testResult = new TestResult(); + + int failures = 0; HashSet testCaseSet = new HashSet(); for (String testDir : testDirs) { getTestCases(new File(testDir), null, testCaseSet); } for (String className : testCaseSet) { Class testClass = testClassLoader.loadClass(className); - runTestCase(testClass, testResult); - } - - Assert.assertEquals(0, testResult.errorCount()); + failures += runTestCase(testClass).getFailureCount(); + } + + Assert.assertEquals(0, failures); } } diff --git a/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyTestHarness.java b/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyTestHarness.java index 3f3774899d..ba9b7007b4 100644 --- a/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyTestHarness.java +++ b/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyTestHarness.java @@ -18,33 +18,27 @@ */ package org.apache.tuscany.sca.test.osgi.harness; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.lang.reflect.Method; import java.net.URL; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.util.ArrayList; import java.util.Enumeration; import java.util.jar.JarOutputStream; import java.util.jar.Manifest; import java.util.zip.ZipEntry; import junit.framework.Assert; -import junit.framework.TestCase; -import junit.framework.TestResult; +import junit.framework.AssertionFailedError; import org.apache.tuscany.sca.test.osgi.runtime.impl.OSGiTestRuntime; import org.apache.tuscany.sca.test.util.OSGiRuntimeLoader; import org.apache.tuscany.sca.test.util.TuscanyLoader; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.runner.JUnitCore; +import org.junit.runner.Request; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; @@ -53,7 +47,6 @@ import org.osgi.framework.BundleContext; * Harness can be used to run Tuscany samples with Tuscany running in OSGi */ public class OSGiTuscanyTestHarness { - private OSGiTestRuntime osgiRuntime; protected Bundle tuscanyRuntime; @@ -61,10 +54,10 @@ public class OSGiTuscanyTestHarness { private Bundle testBundle; public void setUp() throws Exception { - + osgiRuntime = OSGiRuntimeLoader.startOSGiTestRuntime(); bundleContext = osgiRuntime.getBundleContext(); - + // Uninstall any previously installed test bundles for (Bundle bundle : bundleContext.getBundles()) { if ("org.apache.tuscany.sca.test.samples".equals(bundle.getSymbolicName())) { @@ -72,7 +65,6 @@ public class OSGiTuscanyTestHarness { } } } - public void tearDown() throws Exception { if (tuscanyRuntime != null) { @@ -80,15 +72,15 @@ public class OSGiTuscanyTestHarness { } OSGiRuntimeLoader.shutdownOSGiRuntime(); } - + public BundleContext getBundleContext() { return bundleContext; } - + public void runTest(String... testDirs) throws Exception { - + String mainTestDir = testDirs[0]; - + File testDir = new File(mainTestDir + "/target/test-classes"); if (!testDir.exists()) { System.err.println("Test directory " + testDir + " does not exist"); @@ -98,9 +90,9 @@ public class OSGiTuscanyTestHarness { System.out.println("Run tests from : " + mainTestDir); long startTime = System.currentTimeMillis(); - + tuscanyRuntime = TuscanyLoader.loadTuscanyIntoOSGi(bundleContext); - + String[] dirs = new String[testDirs.length + 2]; int i = 0; dirs[i++] = mainTestDir + "/target/test-classes"; @@ -108,46 +100,44 @@ public class OSGiTuscanyTestHarness { for (int j = 0; j < testDirs.length; j++) { dirs[i++] = testDirs[j] + "/target/classes"; } - + String manifestFile = "target/test-classes/META-INF/MANIFEST.MF"; - - testBundle = createAndInstallBundle( - "file:" + mainTestDir + "/target/classes", // Bundle location: used to get File URLs for DefaultSCADomain - manifestFile, // Test bundle manifest file - dirs // Directory entries to be added to bundle - ); - - + + testBundle = createAndInstallBundle("file:" + mainTestDir + "/target/classes", // Bundle location: used to get File URLs for DefaultSCADomain + manifestFile, // Test bundle manifest file + dirs // Directory entries to be added to bundle + ); long endTime = System.currentTimeMillis(); - - System.out.println("Loaded Tuscany, time taken = " + (endTime-startTime) + " ms" ); - + + System.out.println("Loaded Tuscany, time taken = " + (endTime - startTime) + " ms"); + testBundle.start(); - + Class testClass = testBundle.loadClass(this.getClass().getName()); Method testMethod = testClass.getMethod("runAllTestsFromBundle", Bundle.class); Object testObject = testClass.newInstance(); testMethod.invoke(testObject, testBundle); - + testBundle.stop(); testBundle.uninstall(); } - + // Create and install a bundle with the specified manifest file // The bundle contains all files from the list of directories specified - public Bundle createAndInstallBundle(String bundleLocation, String manifestFileName, - String[] dirNames) throws Exception { + public Bundle createAndInstallBundle(String bundleLocation, String manifestFileName, String[] dirNames) + throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); File manifestFile = new File(manifestFileName); Manifest manifest = new Manifest(); manifest.read(new FileInputStream(manifestFile)); - manifest.getMainAttributes().putValue("Bundle-Version", (String)tuscanyRuntime.getHeaders().get("Bundle-Version")); + manifest.getMainAttributes().putValue("Bundle-Version", + (String)tuscanyRuntime.getHeaders().get("Bundle-Version")); JarOutputStream jarOut = new JarOutputStream(out, manifest); - + for (int i = 0; i < dirNames.length; i++) { File dir = new File(dirNames[i]); addFilesToJar(dir, dirNames[i], jarOut); @@ -160,16 +150,16 @@ public class OSGiTuscanyTestHarness { return bundleContext.installBundle(bundleLocation, inStream); } - + // Add all the files from a build directory into a jar file // This method is used to create bundles on the fly private void addFilesToJar(File dir, String rootDirName, JarOutputStream jarOut) throws Exception { - + if (dir.getName().equals(".svn")) return; - + File[] files = dir.listFiles(); - + if (files == null) return; @@ -181,7 +171,7 @@ public class OSGiTuscanyTestHarness { if (files[i].getName().endsWith("MANIFEST.MF")) continue; - String entryName = files[i].getPath().substring(rootDirName.length()+1); + String entryName = files[i].getPath().substring(rootDirName.length() + 1); entryName = entryName.replaceAll("\\\\", "/"); if (files[i].isDirectory()) { entryName += "/"; @@ -199,122 +189,55 @@ public class OSGiTuscanyTestHarness { } } } - public void runAllTestsFromBundle(Bundle bundle) throws Exception { - - TestResult testResult = new TestResult(); + int failures = 0; Enumeration entries = bundle.findEntries("/", "*TestCase.class", true); while (entries.hasMoreElements()) { URL entry = (URL)entries.nextElement(); String className = entry.getFile(); - className = className.substring(1, className.length()-6); // remove leading / and trailing .class + className = className.substring(1, className.length() - 6); // remove leading / and trailing .class className = className.replaceAll("/", "."); Class testClass = bundle.loadClass(className); - runTestCase(testClass, testResult); - } - - Assert.assertEquals(0, testResult.errorCount()); + failures += runTestCase(testClass).getFailureCount(); + } + + Assert.assertEquals(0, failures); } - - - public void runTestCase(Class testClass, TestResult testResult) throws Exception { - - boolean isJunitTest = TestCase.class.isAssignableFrom(testClass); - if (testClass.getName().endsWith("TestCase") && - !testClass.getPackage().getName().startsWith("org.apache.tuscany.sca.test.osgi")) { - Object test = (Object)testClass.newInstance(); - - System.out.println("Running test " + test + " "); - int ran = 0; - int failed = 0; - ArrayList testMethods = new ArrayList(); - Method setupMethod = null; - Method tearDownMethod = null; - Method setupClassMethod = null; - Method tearDownClassMethod = null; - Method[] methods = testClass.getDeclaredMethods(); - for (final Method method : methods) { - if ((isJunitTest && method.getName().startsWith("test")) - || method.getAnnotation(Test.class) != null) { - testMethods.add(method); - - } else if ((isJunitTest && method.getName().equals("setUp")) - || method.getAnnotation(Before.class) != null) { - - setupMethod = method; - AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - method.setAccessible(true); - return null; - } - }); - - } else if ((isJunitTest && method.getName().equals("tearDown")) - || method.getAnnotation(After.class) != null) { - - tearDownMethod = method; - AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - method.setAccessible(true); - return null; - } - }); - - } else if (method.getAnnotation(BeforeClass.class) != null) { - - setupClassMethod = method; - AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - method.setAccessible(true); - return null; - } - }); - - } else if (method.getAnnotation(AfterClass.class) != null) { - - tearDownClassMethod = method; - AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - method.setAccessible(true); - return null; - } - }); + public Result runTestCase(Class testClass) throws Exception { + + if (testClass.getName().endsWith("TestCase") && !testClass.getName() + .startsWith("org.apache.tuscany.sca.test.osgi.")) { + JUnitCore core = new JUnitCore(); + System.out.println("Running test " + testClass.getName() + " "); + Result result = core.run(Request.aClass(testClass)); + // long duration = result.getRunTime(); + int runs = result.getRunCount(); + int failures = 0, errors = 0; + int ignores = result.getIgnoreCount(); + + for (Failure f : result.getFailures()) { + if (f.getException() instanceof AssertionFailedError) { + failures++; + } else { + errors++; } } - try { - if (setupClassMethod != null) - setupClassMethod.invoke(null); - for (Method testMethod : testMethods) { - - ran++; - failed++; - try { - if (setupMethod != null) - setupMethod.invoke(test); - - testMethod.invoke(test); - failed--; - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } finally { - if (tearDownMethod != null) - tearDownMethod.invoke(test); - } - } - } catch (Exception e) { - e.printStackTrace(); - throw e; - } finally { - System.out.println("Ran: " + ran + ", Passed: " + (ran - failed) + ", Failed: " + failed); - if (tearDownClassMethod != null) - tearDownClassMethod.invoke(null); - } - } + System.out.println("Test Runs: " + runs + + ", Failures: " + + failures + + ", Errors: " + + errors + + ", Ignores: " + + ignores); + + return result; + + } + return new Result(); + } } -- cgit v1.2.3