5 Replies Latest reply on Mar 19, 2010 8:41 AM by blabno

    How to test an application scoped component?

    cinconnu

      Hi,


      I want to test an application scoped component using SeamTest. I cannot use a plain unit test since I need some values like the current viewID. How can I destroy/remove my component and recreate a new one?


      Here is what I'm trying to achieve:


          @BeforeMethod
          public void setUp()
              throws Exception
          {
              new FacesRequestLocal()
                  {
                      @Override
                      protected void invokeApplication()
                          throws Exception
                      {
                          MyApplicationScopedBean oldBean = (MyApplicationScopedBean) Component.getInstance("bean");
      
                          // ********** I tried that:
                          // oldBean.destroy();
                          // Contexts.removeFromAllContexts("bean");
                          // Component.forName("bean").destroy(oldBean);
      
                          MyApplicationScopedBean newBean = (MyApplicationScopedBean) Component.getInstance("bean");
      
                           // ********* false
                           assertThat(newBean).isNotSameAs(oldBean);
                      }
                  }.run();
          }
      



      The component is stored in the ELContext and I don't know how to remove it.


      Thanks, David

        • 1. Re: How to test an application scoped component?
          blabno

          Something like this should do. I do not have IDE around now, so i'm writting off the top of my head.


          Contexts.getApplicationContext().remove("bean");

          • 2. Re: How to test an application scoped component?
            cinconnu

            Thank you for your answer but I have already tried this, in fact I replaced it with the more general removeFromAllContexts, but no luck so far.

            • 3. Re: How to test an application scoped component?
              cinconnu

              Well, I tried the following (from org.jboss.seam.contexts.Contexts.destroy) which is called when the application is shut down but it does not work:


              Component component = Component.forName("my.full.bean");
              Object object = Contexts.getApplicationContext().get("bean");
              component.destroy(object);
              Contexts.removeFromAllContexts("bean");
              



              So guys how do you test your application scopped components ???

              • 4. Re: How to test an application scoped component?
                cinconnu

                I found this that seems to do the job:


                    /**
                     * Creates a new instance of the specified component and registers it in its scope.
                     *
                     * @param <T> the class of the component.
                     * @param clazz the class of the component.
                     * @param aliases the factory aliases of the component.
                     *
                     * @throws Exception if an error occurs.
                     */
                    @SuppressWarnings("unchecked")
                    protected <T> void newInstance(final Class<T> clazz, final String... aliases)
                        throws Exception
                    {
                        new FacesRequestEx()
                            {
                                @Override
                                protected void invokeApplication()
                                {
                                    final String componentName = Component.getComponentName(clazz);
                                    final Component component = Component.forName(componentName);
                                    final Context componentContext = component.getScope().getContext();
                
                                    final T result = (T) component.newInstance();
                                    componentContext.set(componentName, result);
                
                                    for (String alias : aliases)
                                    {
                                        componentContext.set(alias, result);
                                    }
                                }
                            }.run();
                    }

                • 5. Re: How to test an application scoped component?
                  blabno

                  I just don't believe that removeFromAllContexts does not work. I have no problem with testing application scoped components.


                  What is your problem with viewId ? It is always provided if you use the Faces/NonFacesRequest. Besides, in my opinion, the application scoped components should be designed in such a way that they could have without knowledge about views.