6 Replies Latest reply on Dec 12, 2008 12:49 AM by Steven Z

    JSFServerSession getManagedBeanValue() and Bean Constructor

    Steven Z Newbie

      Hello,

      Regarding the JSFServerSession getManagedBeanValue() method, I have noticed a very weird behavior.

      Say I had a bean called MyBean

      package com.example;
      
      public class MyBean{
       public int random = -1;
      
       public MyBean(){
       // guaranteed to generate a number greater than 0
       random = SomeClass.generateRandom();
       System.out.println("MyBean constructed, random="+random);
       }
      }


      Defined properly in faces-config.xml
      <managed-bean>
       <description>My Perfect Bean</description>
       <managed-bean-name>myBean</managed-bean-name>
       <managed-bean-class>com.example.MyBean</managed-bean-class>
       <managed-bean-scope>request</managed-bean-scope>
      </managed-bean>


      And a J-Unit called UserBeanTest with two tests
      package com.example;
      
      public class UserBeanTest extends org.apache.cactus.ServletTestCase{
       public static junit.framework.Test suite(){
       return new junit.framework.TestSuite( UserBeanTest.class );
       }
      
       // SUCCESS
       public void test__DirectConstructorInvocation(){
       MyBean bean = new MyBean();
       assertNotNull( bean );
       assertTrue( bean.random != -1 ); // <<<<<<<<<<<< PASS !!
       }
      
       // FAIL
       public void test__WithJSFServer(){
       JSFSession jsfSession = new JSFSession("/index.faces");
       JSFClientSession jsfClient = jsfSession.getJSFClientSession();
       JSFServerSession jsfServer = jsfSession.getJSFServerSession();
      
       MyBean bean = (MyBean)jsfServer.getManagedBeanValue("myBean");
      
       assertNotNull( bean ); //pass
       assertTrue( bean.random != -1 ); // <<<<<<<<<<<< FAIL !!
       }
      }


      As expected when the test__DirectConstructorInvocation test, the bean is constructed and the a random number is generated. This allows the test the pass.

      With the test__WithJSFServer test, the bean is successfully created and a random number is generated and assigned to the variable random. (i know this code has ran because of my System.out statement) However, once the JSFServerSessions returns back to me the value of "myBean" from getManagedBeanValue, the value of random is still -1? I don't understand how the object could have been created, the constructor invoked, but somehow none of the code in the constructor retained?

      Any help?

      Thanks,
      Steven