You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2008/07/09 01:11:25 UTC

svn commit: r675041 - in /tuscany/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness: OSGiTuscanyNonOSGiTestHarness.java OSGiTuscanyTestHarness.java

Author: rfeng
Date: Tue Jul  8 16:11:25 2008
New Revision: 675041

URL: http://svn.apache.org/viewvc?rev=675041&view=rev
Log:
Simplify the unit test invoker

Modified:
    tuscany/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyNonOSGiTestHarness.java
    tuscany/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyTestHarness.java

Modified: tuscany/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyNonOSGiTestHarness.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyNonOSGiTestHarness.java?rev=675041&r1=675040&r2=675041&view=diff
==============================================================================
--- tuscany/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyNonOSGiTestHarness.java (original)
+++ tuscany/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyNonOSGiTestHarness.java Tue Jul  8 16:11:25 2008
@@ -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.util.HashSet;
 
 import junit.framework.Assert;
-import junit.framework.TestResult;
 
 import org.apache.tuscany.sca.test.util.TuscanyLoader;
 
@@ -36,14 +33,11 @@
  * 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 @@
         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 @@
         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<String> 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<String> testCaseSet = new HashSet<String>();
         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);
 
     }
 }

Modified: tuscany/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyTestHarness.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyTestHarness.java?rev=675041&r1=675040&r2=675041&view=diff
==============================================================================
--- tuscany/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyTestHarness.java (original)
+++ tuscany/java/sca/itest/osgi-tuscany/osgi-tuscany-test/src/test/java/org/apache/tuscany/sca/test/osgi/harness/OSGiTuscanyTestHarness.java Tue Jul  8 16:11:25 2008
@@ -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 @@
  * 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 @@
     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 void tearDown() throws Exception {
         if (tuscanyRuntime != null) {
@@ -80,15 +72,15 @@
         }
         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 @@
         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 @@
         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 @@
         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 @@
             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 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<Method> testMethods = new ArrayList<Method>();
-            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<Object>() {
-                        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<Object>() {
-                        public Object run() {
-                            method.setAccessible(true);
-                            return null;
-                        }
-                    });
-
-                } else if (method.getAnnotation(BeforeClass.class) != null) {
-
-                    setupClassMethod = method;
-                    AccessController.doPrivileged(new PrivilegedAction<Object>() {
-                        public Object run() {
-                            method.setAccessible(true);
-                            return null;
-                        }
-                    });
-
-                } else if (method.getAnnotation(AfterClass.class) != null) {
-
-                    tearDownClassMethod = method;
-                    AccessController.doPrivileged(new PrivilegedAction<Object>() {
-                        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();
+
     }
 }