[jboss-cvs] JBossAS SVN: r94304 - in projects/jboss-osgi/projects/aQute/trunk: aQute.runtime and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Oct 2 18:56:20 EDT 2009


Author: thomas.diesler at jboss.com
Date: 2009-10-02 18:56:20 -0400 (Fri, 02 Oct 2009)
New Revision: 94304

Modified:
   projects/jboss-osgi/projects/aQute/trunk/aQute.bnd/.project
   projects/jboss-osgi/projects/aQute/trunk/aQute.runtime/.project
   projects/jboss-osgi/projects/aQute/trunk/aQute.runtime/src/main/java/aQute/junit/runtime/GenericFramework.java
   projects/jboss-osgi/projects/aQute/trunk/aQute.runtime/src/main/java/aQute/junit/runtime/Target.java
Log:
Better error handling during framework start and initial bundle install/start.

Modified: projects/jboss-osgi/projects/aQute/trunk/aQute.bnd/.project
===================================================================
--- projects/jboss-osgi/projects/aQute/trunk/aQute.bnd/.project	2009-10-02 22:56:07 UTC (rev 94303)
+++ projects/jboss-osgi/projects/aQute/trunk/aQute.bnd/.project	2009-10-02 22:56:20 UTC (rev 94304)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <projectDescription>
-	<name>bnd</name>
+	<name>aQute.bnd</name>
 	<comment></comment>
 	<projects>
 	</projects>

Modified: projects/jboss-osgi/projects/aQute/trunk/aQute.runtime/.project
===================================================================
--- projects/jboss-osgi/projects/aQute/trunk/aQute.runtime/.project	2009-10-02 22:56:07 UTC (rev 94303)
+++ projects/jboss-osgi/projects/aQute/trunk/aQute.runtime/.project	2009-10-02 22:56:20 UTC (rev 94304)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <projectDescription>
-	<name>runtime</name>
+	<name>aQute.runtime</name>
 	<comment></comment>
 	<projects>
 	</projects>

Modified: projects/jboss-osgi/projects/aQute/trunk/aQute.runtime/src/main/java/aQute/junit/runtime/GenericFramework.java
===================================================================
--- projects/jboss-osgi/projects/aQute/trunk/aQute.runtime/src/main/java/aQute/junit/runtime/GenericFramework.java	2009-10-02 22:56:07 UTC (rev 94303)
+++ projects/jboss-osgi/projects/aQute/trunk/aQute.runtime/src/main/java/aQute/junit/runtime/GenericFramework.java	2009-10-02 22:56:20 UTC (rev 94304)
@@ -47,6 +47,7 @@
     private final Properties       properties;
     private boolean                security;
     private SimplePermissionPolicy policy;
+    private List<Throwable> activateErrors  = new ArrayList<Throwable>();
 
     public GenericFramework(Properties properties) {
         this.properties = properties;
@@ -54,77 +55,81 @@
         systemPackages.add("org.osgi.framework.launch");
     }
 
-    public boolean activate() throws Exception {
-        boolean error = false;
+    public List<Throwable> activate() {
         Policy.setPolicy(new AllPolicy());
 
-        systemBundle = createFramework();
-        if (systemBundle == null)
-            return false;
-
         try {
-            PermissionInfo         allPermissions[]          = new PermissionInfo[] { new PermissionInfo(
-                    AllPermission.class
-                            .getName(),
-                    null, null) };
-            policy = new SimplePermissionPolicy(systemBundle.getBundleContext());
+			systemBundle = createFramework();
+	        if (systemBundle == null) 
+	        	throw new IllegalStateException("No system bundle");
+	        
+	        
+	        try {
+	            PermissionInfo allPermissions[] = new PermissionInfo[] { 
+	            		new PermissionInfo(AllPermission.class.getName(),null, null) };
+	            policy = new SimplePermissionPolicy(systemBundle.getBundleContext());
 
-            // All bundles installed from the script are getting AllPermission
-            // for now.
-            policy.setDefaultPermissions(allPermissions);
-            security = true;
-        } catch (Throwable t) {
-            // This can throw a linkage error when the framework
-            // does not carry the PermissionAdmin class
-            security = false;
-        }
+	            // All bundles installed from the script are getting AllPermission
+	            // for now.
+	            policy.setDefaultPermissions(allPermissions);
+	            security = true;
+	        } catch (Throwable t) {
+	            // This can throw a linkage error when the framework
+	            // does not carry the PermissionAdmin class
+	            security = false;
+	        }
 
-        systemBundle.start();
-        // Initialize this framework so it becomes STARTING
-        BundleContext systemContext = getFrameworkContext();
+	        systemBundle.start();
+	        // Initialize this framework so it becomes STARTING
+	        BundleContext systemContext = getFrameworkContext();
 
-        // Install the set of bundles
-        List/* <Bundle> */installed = new ArrayList/* <Bundle> */();
+	        // Install the set of bundles
+	        List/* <Bundle> */installed = new ArrayList/* <Bundle> */();
 
-        for (Iterator/* <File> */i = bundles.iterator(); i.hasNext();) {
-            File path = ((File) i.next()).getAbsoluteFile();
+	        for (Iterator/* <File> */i = bundles.iterator(); i.hasNext();) {
+	            File path = ((File) i.next()).getAbsoluteFile();
 
-            InputStream in = new FileInputStream(path);
-            try {
-                Bundle bundle = systemContext
-                        .installBundle(path.toString(), in);
-                installed.add(bundle);
-            } catch (BundleException e) {
-                System.out.println("Install: " + path + " ");
-                report(e, System.out);
-                error = true;
-            } finally {
-                in.close();
-            }
-        }
+	            InputStream in = new FileInputStream(path);
+	            try {
+	                Bundle bundle = systemContext
+	                        .installBundle(path.toString(), in);
+	                installed.add(bundle);
+	            } catch (BundleException e) {
+	                System.out.println("Install error: " + path + " ");
+	                report(e, System.out);
+	                activateErrors.add(e);
+	            } finally {
+	                in.close();
+	            }
+	        }
 
-        // From now on, the bundles are on their own. They have
-        // by default AllPermission, but if they install bundles
-        // they will not automatically get AllPermission anymore
-        
-        if (security)
-            policy.setDefaultPermissions(null);
+	        // From now on, the bundles are on their own. They have
+	        // by default AllPermission, but if they install bundles
+	        // they will not automatically get AllPermission anymore
+	        
+	        if (security)
+	            policy.setDefaultPermissions(null);
 
-        // Now start all the installed bundles in the same order
-        // (unless they're a fragment)
-        
-        for (Iterator/* <Bundle> */i = installed.iterator(); i.hasNext();) {
-            Bundle b = (Bundle) i.next();
-            try {
-                if (!isFragment(b))
-                    b.start();
-            } catch (BundleException e) {
-                System.out.println("Start: " + b.getBundleId() + " ");
-                report(e, System.out);
-                error = true;
-            }
-        }
-        return !error;
+	        // Now start all the installed bundles in the same order
+	        // (unless they're a fragment)
+	        
+	        for (Iterator/* <Bundle> */i = installed.iterator(); i.hasNext();) {
+	            Bundle b = (Bundle) i.next();
+	            try {
+	                if (!isFragment(b))
+	                    b.start();
+	            } catch (BundleException e) {
+	                System.out.println("Start error: " + b + " ");
+	                report(e, System.out);
+	                activateErrors.add(e);
+	            }
+	        }
+	        
+		} catch (Exception ex) {
+        	activateErrors.add(ex);
+		}
+
+        return activateErrors;
     }
 
     private boolean isFragment(Bundle b) {
@@ -338,12 +343,10 @@
 
     public void report(PrintStream out) {
         try {
-            System.out
-                    .println("------------------------------- REPORT --------------------------");
+            out.println("------------------------------- REPORT --------------------------");
             out.println();
             out.println("Framework             " + framework);
-            out.println("SystemBundle          "
-                    + (systemBundle == null ? "<>" : systemBundle.getClass().getName()));
+            out.println("SystemBundle          " + (systemBundle == null ? "<>" : systemBundle.getClass().getName()));
             out.println("Storage               " + storage);
             out.println("Keep                  " + keep);
             out.println("Security              " + security);
@@ -464,7 +467,7 @@
         case BundleException.UNSUPPORTED_OPERATION:
         case BundleException.UNSPECIFIED:
         case BundleException.RESOLVE_ERROR:
-            System.out.println(e.getMessage());
+            e.printStackTrace(System.out);
             break;
         }
     }

Modified: projects/jboss-osgi/projects/aQute/trunk/aQute.runtime/src/main/java/aQute/junit/runtime/Target.java
===================================================================
--- projects/jboss-osgi/projects/aQute/trunk/aQute.runtime/src/main/java/aQute/junit/runtime/Target.java	2009-10-02 22:56:07 UTC (rev 94303)
+++ projects/jboss-osgi/projects/aQute/trunk/aQute.runtime/src/main/java/aQute/junit/runtime/Target.java	2009-10-02 22:56:20 UTC (rev 94304)
@@ -2,11 +2,13 @@
 
 import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.PrintStream;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Enumeration;
 import java.util.Iterator;
 import java.util.List;
@@ -130,20 +132,24 @@
     /**
      * Main test routine.
      * 
-     * @param testSuiteReport
+     * @param testReporter
      * @param framework
      * @param targetBundle
      * @param testNames
      * @return
      * @throws Throwable
      */
-    private int doTesting(TestReporter testSuiteReport) throws Throwable {
+    private int doTesting(TestReporter testReporter) throws Throwable {
         
     	Bundle targetBundle;
         
+        long startTime = System.currentTimeMillis();
 		try {
-			if (framework.activate() == false)
-			    throw new IllegalStateException("Framework does not activate");
+			List<Throwable> activateErrors = framework.activate();
+			if (activateErrors.size() > 0) {
+				handleSetupErrors(activateErrors, startTime);
+				return activateErrors.size();
+			}
 				
 			boolean report = properties.containsKey("report");
 			if (report)
@@ -151,7 +157,7 @@
 
 			targetBundle = framework.getBundle(target);
 			if (targetBundle == null)
-			    throw new IllegalArgumentException("No target specified");
+			    throw new IllegalArgumentException("Cannot obtain target bundle: " + target);
 
 			// Verify if we have any test names set
 			if (testNames.size() == 0)
@@ -164,16 +170,14 @@
 			    return 0;
 			}
 		} catch (Throwable th) {
-			FileOutputStream fos = new FileOutputStream(reportName);
-			th.printStackTrace(new PrintStream(fos));
-			fos.close();
+			handleSetupError(th, startTime);
 			throw th;
 		}
 
         int allErrors = 0;
         
         TestSuite suite = createSuite(targetBundle, testNames);
-        testSuiteReport.begin(framework, targetBundle, null, suite.countTestCases());
+        testReporter.begin(framework, targetBundle, null, suite.countTestCases());
         
         try {
             Enumeration tests = suite.tests();
@@ -186,7 +190,7 @@
                 // Note, this also sets the BundleContext
                 BasicTestReport basicReport = new BasicTestReport();
                 result.addListener(basicReport);
-                result.addListener(testSuiteReport);
+                result.addListener(testReporter);
                 
                 // Get the test name
             	String testName = null;
@@ -195,11 +199,7 @@
             	else if (test instanceof TestCase)
             		testName = ((TestCase)test).getName();
                 
-                // Add the standard plain and xml formatters 
-                String reportDir = new File(reportName).getParentFile() + "/test-reports";
-                List<JUnitResultFormatter> formatters = new ArrayList<JUnitResultFormatter>();
-                formatters.add(new XMLResultFormatter(reportDir, testName));
-                formatters.add(new PlainResultFormatter(reportDir, testName));
+                List<JUnitResultFormatter> formatters = getResultFormatters(testName);
                 for (JUnitResultFormatter formatter : formatters) 
                     result.addListener(formatter);
                 
@@ -207,7 +207,7 @@
                 junitTest.setProperties(System.getProperties());
                 
                 // Start the report formatters
-                long startTime = System.currentTimeMillis();
+                startTime = System.currentTimeMillis();
                 fireStartTestSuite(formatters, junitTest);
                 basicReport.begin(framework, targetBundle, null, test.countTestCases());
                 
@@ -231,7 +231,7 @@
             return allErrors;
             
         } finally {
-            testSuiteReport.end();
+            testReporter.end();
             
             if (properties.containsKey("wait")) {
                 framework.waitForStop(10000000);
@@ -240,6 +240,67 @@
         }
     }
 
+	/**
+	 *  Add the standard plain and xml formatters 
+	 */
+	private List<JUnitResultFormatter> getResultFormatters(String testName) throws IOException {
+		String reportDir = new File(reportName).getParentFile() + "/test-reports";
+		List<JUnitResultFormatter> formatters = new ArrayList<JUnitResultFormatter>();
+		formatters.add(new XMLResultFormatter(reportDir, testName));
+		formatters.add(new PlainResultFormatter(reportDir, testName));
+		return formatters;
+	}
+
+	private void handleSetupError(Throwable th, long startTime) throws IOException {
+		List<Throwable> errors = Arrays.asList(new Throwable[] { th } );
+		handleSetupErrors(errors, startTime);
+	}
+
+    /**
+     * Handles any error that might occur during
+     *  
+     *  #1 framework startup
+     *  #2 list of test bundle install
+     *  #3 list of test bundle start
+     *  
+     *  [TODO] 
+     *  The above steps should probably be done as part of normal
+     *  JUnit test setup. Special handling should not be necessary.  
+     */
+	private void handleSetupErrors(List<Throwable> errors, long startTime) throws IOException {
+
+		// Print the stack traces to the legacy report file 
+		PrintStream ps = new PrintStream(new FileOutputStream(reportName));
+		for (Throwable ex : errors) {
+			ex.printStackTrace(ps);
+			ps.println();
+		}
+		ps.close();
+		
+        TestResult result = new TestResult();
+        
+        String targetName = new File (target).getName();
+        if (targetName.endsWith(".jar"));
+           	targetName = targetName.substring(0, targetName.length() - 4);
+        
+        List<JUnitResultFormatter> formatters = getResultFormatters(targetName);
+        for (JUnitResultFormatter formatter : formatters) 
+            result.addListener(formatter);
+        
+		JUnitTest junitTest = new JUnitTest(targetName);
+        junitTest.setProperties(System.getProperties());
+        
+        // Start the report formatters
+        fireStartTestSuite(formatters, junitTest);
+		for (Throwable ex : errors) 
+			result.addError(null, ex);
+        
+        // End the report formatters
+        junitTest.setCounts(0, result.failureCount(), result.errorCount());
+        junitTest.setRunTime(System.currentTimeMillis() - startTime);
+        fireEndTestSuite(formatters, junitTest);
+	}
+
     private void fireStartTestSuite(List<JUnitResultFormatter> formatters, JUnitTest junitTest) {
         for (JUnitResultFormatter formatter : formatters) {
             formatter.startTestSuite(junitTest);




More information about the jboss-cvs-commits mailing list