[webbeans-commits] Webbeans SVN: r2127 - in extensions/trunk/se: src/main/java/org/jboss/webbeans/environment/se and 3 other directories.

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Sat Mar 21 06:50:32 EDT 2009


Author: peteroyle
Date: 2009-03-21 06:50:31 -0400 (Sat, 21 Mar 2009)
New Revision: 2127

Modified:
   extensions/trunk/se/
   extensions/trunk/se/src/main/java/org/jboss/webbeans/environment/se/StartMain.java
   extensions/trunk/se/src/main/java/org/jboss/webbeans/environment/se/beans/ParametersFactory.java
   extensions/trunk/se/src/test/java/org/jboss/webbeans/environment/se/test/StartMainTest.java
   extensions/trunk/se/src/test/java/org/jboss/webbeans/environment/se/test/beans/MainTestBean.java
   extensions/trunk/se/src/test/java/org/jboss/webbeans/environment/se/test/beans/ParametersTestBean.java
Log:
WebBeans was booting before the command line args were being set, which meant that the args were not available for injection when they needed to be. Args are now set into the ParametersFactory on @Initialized using an observer. Added test.


Property changes on: extensions/trunk/se
___________________________________________________________________
Name: svn:ignore
   - nbactions.xml
target

.classpath

.project

.settings

temp-testng-customsuite.xml

test-output

   + nb-configuration.xml
test-output
.classpath
.project
target
temp-testng-customsuite.xml
.settings
nbactions.xml


Modified: extensions/trunk/se/src/main/java/org/jboss/webbeans/environment/se/StartMain.java
===================================================================
--- extensions/trunk/se/src/main/java/org/jboss/webbeans/environment/se/StartMain.java	2009-03-20 22:19:04 UTC (rev 2126)
+++ extensions/trunk/se/src/main/java/org/jboss/webbeans/environment/se/StartMain.java	2009-03-21 10:50:31 UTC (rev 2127)
@@ -47,14 +47,14 @@
    
    private final Bootstrap bootstrap;
    private final BeanStore applicationBeanStore;
-   private String[] args;
+   public static String[] ARGS;
    
    private WebBeansManager manager;
    
    
    public StartMain(String[] commandLineArgs)
    {
-      this.args = commandLineArgs;
+      ARGS = commandLineArgs;
       try
       {
          bootstrap = Reflections.newInstance(BOOTSTRAP_IMPL_CLASS_NAME, Bootstrap.class);
@@ -73,9 +73,8 @@
       bootstrap.getServices().add(NamingContext.class, new NoNamingContext() {});
       bootstrap.setApplicationContext(applicationBeanStore);
       bootstrap.initialize();
-      bootstrap.boot();
       this.manager = bootstrap.getManager();
-      bootstrap.getManager().getInstanceByType(ParametersFactory.class).setArgs(args);
+      bootstrap.boot();
       DependentContext.INSTANCE.setActive(true);
       bootstrap.getManager().getInstanceByType(ShutdownManager.class).setBootstrap(bootstrap);
    }

Modified: extensions/trunk/se/src/main/java/org/jboss/webbeans/environment/se/beans/ParametersFactory.java
===================================================================
--- extensions/trunk/se/src/main/java/org/jboss/webbeans/environment/se/beans/ParametersFactory.java	2009-03-20 22:19:04 UTC (rev 2126)
+++ extensions/trunk/se/src/main/java/org/jboss/webbeans/environment/se/beans/ParametersFactory.java	2009-03-21 10:50:31 UTC (rev 2127)
@@ -22,8 +22,12 @@
 import java.util.List;
 
 import javax.context.ApplicationScoped;
+import javax.event.Observes;
 import javax.inject.Produces;
 
+import javax.inject.manager.Initialized;
+import javax.inject.manager.Manager;
+import org.jboss.webbeans.environment.se.StartMain;
 import org.jboss.webbeans.environment.se.bindings.Parameters;
 
 /**
@@ -36,8 +40,8 @@
 @ApplicationScoped
 public class ParametersFactory
 {
-   private String[] args = new String[]{};
-   private List<String> argsList = new ArrayList<String>(0);
+   private String[] args;
+   private List<String> argsList;
    
    /**
     * Producer method for the injectable command line args.
@@ -64,11 +68,25 @@
     * StartMain passes in the command line args here.
     * 
     * @param args
-    *           The command line arguments.
+    *           The command line arguments. If null is given then an empty array
+    *           will be used instead.
     */
    public void setArgs(String[] args)
    {
+      if (args == null) {
+         args = new String[] {};
+      }
       this.args = args;
       this.argsList = Collections.unmodifiableList( new ArrayList<String>( Arrays.asList( args ) ) );
    }
+
+   /**
+    * On WebBeans initialisation, retrieve the command line args that were given
+    * to StartMain.
+    *
+    * @param manager The Manager which has been initialized.
+    */
+   public void initArgs(@Observes @Initialized Manager manager) {
+      this.setArgs( StartMain.ARGS );
+   }
 }

Modified: extensions/trunk/se/src/test/java/org/jboss/webbeans/environment/se/test/StartMainTest.java
===================================================================
--- extensions/trunk/se/src/test/java/org/jboss/webbeans/environment/se/test/StartMainTest.java	2009-03-20 22:19:04 UTC (rev 2126)
+++ extensions/trunk/se/src/test/java/org/jboss/webbeans/environment/se/test/StartMainTest.java	2009-03-21 10:50:31 UTC (rev 2127)
@@ -32,9 +32,9 @@
  */
 public class StartMainTest {
 
-    public static String[] ARGS = new String[] { "arg1", "arg2", "arg 3"};
+    public static String[] ARGS = new String[] { "arg1", "arg2", "arg3"};
+    public static String[] ARGS_EMPTY = new String[] { };
 
-
     /**
      * Test of main method, of class StartMain. Checks that the beans
      * found in the org.jboss.webbeans.environment.se.beans package are
@@ -51,24 +51,57 @@
 
         ParametersTestBean paramsBean = mainTestBean.getParametersTestBean();
         Assert.assertNotNull( paramsBean );
-        Assert.assertNotNull( paramsBean.getParam1() );
-        Assert.assertEquals( ARGS[0], paramsBean.getParam1() );
-        Assert.assertNotNull( paramsBean.getParam2() );
-        Assert.assertEquals( ARGS[1], paramsBean.getParam2() );
-        Assert.assertNotNull( paramsBean.getParam3() );
-        Assert.assertEquals( ARGS[2], paramsBean.getParam3() );
+        Assert.assertNotNull( paramsBean.getParameters() );
+        Assert.assertNotNull( paramsBean.getParameters().get(0) );
+        Assert.assertEquals( ARGS[0], paramsBean.getParameters().get(0) );
+        Assert.assertNotNull( paramsBean.getParameters().get(1) );
+        Assert.assertEquals( ARGS[1], paramsBean.getParameters().get(1) );
+        Assert.assertNotNull( paramsBean.getParameters().get(2) );
+        Assert.assertEquals( ARGS[2], paramsBean.getParameters().get(2) );
 
-        manager.fireEvent( manager, new AnnotationLiteral<Shutdown>() {} );
+        shutdownManager(manager);
         boolean contextNotActive = false;
         try
         {
            assert manager.getInstanceByType(MainTestBean.class) == null;
         }
-        catch (Exception e) 
+        catch (Exception e)
         {
            contextNotActive = true;
         }
         assert contextNotActive;
     }
 
+    /**
+     * Test of main method, of class StartMain when no command-line args are
+     * provided.
+     */
+    @Test
+    public void testMainEmptyArgs()
+    {
+        Manager manager = new StartMain(ARGS_EMPTY).main();
+
+        MainTestBean mainTestBean = manager.getInstanceByType( MainTestBean.class );
+        Assert.assertNotNull( mainTestBean );
+
+        ParametersTestBean paramsBean = mainTestBean.getParametersTestBean();
+        Assert.assertNotNull( paramsBean );
+        Assert.assertNotNull( paramsBean.getParameters() );
+
+        shutdownManager(manager);
+    }
+
+    private void shutdownManager( Manager manager )
+    {
+        manager.fireEvent( manager, new ShutdownAnnotation() );
+    }
+
+    private static class ShutdownAnnotation extends AnnotationLiteral<Shutdown>
+    {
+
+        public ShutdownAnnotation()
+        {
+        }
+    }
+
 }

Modified: extensions/trunk/se/src/test/java/org/jboss/webbeans/environment/se/test/beans/MainTestBean.java
===================================================================
--- extensions/trunk/se/src/test/java/org/jboss/webbeans/environment/se/test/beans/MainTestBean.java	2009-03-20 22:19:04 UTC (rev 2126)
+++ extensions/trunk/se/src/test/java/org/jboss/webbeans/environment/se/test/beans/MainTestBean.java	2009-03-21 10:50:31 UTC (rev 2127)
@@ -16,8 +16,11 @@
  */
 package org.jboss.webbeans.environment.se.test.beans;
 
+import javax.event.Observes;
 import javax.inject.Current;
 import javax.inject.Initializer;
+import javax.inject.manager.Deployed;
+import javax.inject.manager.Manager;
 
 /**
  *
@@ -34,8 +37,14 @@
     {
         this.initialised = true;
         this.parametersTestBean = paramsTestBean;
+        // this call is important. It invokes initialiser on the proxy
+        paramsTestBean.getParameters();
     }
 
+    public void mainMethod(@Observes @Deployed Manager manager) {
+        System.out.println( "Starting main test app" );
+    }
+
     public ParametersTestBean getParametersTestBean()
     {
         return parametersTestBean;
@@ -45,6 +54,5 @@
     {
         return initialised;
     }
-
     
 }

Modified: extensions/trunk/se/src/test/java/org/jboss/webbeans/environment/se/test/beans/ParametersTestBean.java
===================================================================
--- extensions/trunk/se/src/test/java/org/jboss/webbeans/environment/se/test/beans/ParametersTestBean.java	2009-03-20 22:19:04 UTC (rev 2126)
+++ extensions/trunk/se/src/test/java/org/jboss/webbeans/environment/se/test/beans/ParametersTestBean.java	2009-03-21 10:50:31 UTC (rev 2127)
@@ -20,44 +20,31 @@
 
 import java.util.List;
 
+import javax.context.ApplicationScoped;
 import javax.inject.Initializer;
 
 import org.jboss.webbeans.environment.se.bindings.Parameters;
-import org.jboss.webbeans.environment.se.test.StartMainTest;
 import org.testng.Assert;
 
 /**
  *
  * @author Peter Royle
  */
+ at ApplicationScoped
 public class ParametersTestBean {
 
-    String param1;
-    String param2;
-    String param3;
+    List<String> parameters;
 
     @Initializer
     public void init(@Parameters List<String> params) {
+        this.parameters = params;
+        // even if no args are given, it should will always at least be an empty array
         Assert.assertNotNull( params );
-        Assert.assertEquals( StartMainTest.ARGS.length, params.size(), "Unexpected number of arguments");
-        param1 = params.get( 0 );
-        param2 = params.get( 1 );
-        param3 = params.get( 2 );
     }
 
-    public String getParam1()
+    public List<String> getParameters()
     {
-        return param1;
+        return parameters;
     }
 
-    public String getParam2()
-    {
-        return param2;
-    }
-
-    public String getParam3()
-    {
-        return param3;
-    }
-
 }




More information about the weld-commits mailing list