12 Replies Latest reply on Jul 11, 2009 4:54 PM by ssilvert

    ServletTestRunner parameters

    practica

      Hi all.

      I would like to pass unit test parameters thru to my tests via the ServletTestRunner servlet.

      For example:
      http://host:port/context/ServletTestRunner?suite=MyJSFUnitTestCase&xsl=cactus-report.xsl&MYPARAMNAME=MYPARAMVALUE

      Inside my test case class which extends ServletTestCase, I can get access to MYPARAMNAME=MYPARAMVALUE, even though I am exploring this field and the magic behind it:

      HttpServletRequestWrapper requestWrapper = super.request;

      So, is this by design, flaw, neither...or am I missing something ?

      regards
      julian

        • 1. Re: ServletTestRunner parameters
          practica

          My intial post was meant to state 'I CANNOT get access to' ...

          First post nerves I guess !

          • 2. Re: ServletTestRunner parameters
            ssilvert

            This is a limitation of Cactus. See "How do I parametrize my cactus tests?"
            http://jakarta.apache.org/cactus/faq.html

            Also see this:
            http://jakarta.apache.org/cactus/integration/manual/howto_config.html

            So Cactus suggests two options:
            1) Use system properties
            2) Add the properties to cactus.properties. cactus.properties needs to be on the classpath of the container and each property will be exported to a system property.

            In general, you should not use the instance variables of the ServletTestCase in a JSFUnit test. These are dummy instances and JSFUnit likes to use real instances.

            There is one more option I can think of.

            Since the ServletTestRunner and your JSFUnit tests share the same HttpSession instance, you can use the HttpSession as a conduit for your init params. You can extend the ServletRunner to recognize your params and then stuff them in the HttpSession. Then you can use FacesContext.getExternalContext() to get the params from the HttpSession.

            Stan

            • 3. Re: ServletTestRunner parameters
              practica

              Hi Stan.
              Thanks for the response.
              I kinda figured as much, but thought (hoped !) there might be a better way.

              The last option is clearly the best option, but with the most work of course. If we get around to doing that, we'll make it generic and commit it back to the jsfunit tree.

              Thanks for creating/managing this great tool in any case !

              • 4. Re: ServletTestRunner parameters
                practica

                Ok. Created a new Servlet with the following code:

                @Override
                public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                 captureParams(req);
                 super.doGet(req, resp);
                }
                
                private void captureParams(HttpServletRequest req) {
                 HttpSession session = req.getSession();
                 Map parameterMap = req.getParameterMap();
                 Set keySet = parameterMap.keySet();
                 for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();) {
                 String name = iterator.next();
                 Object value = parameterMap.get(name);
                 session.setAttribute(name, value);
                 }
                }
                


                Sitting on the debugger I can see that the attributes are pushed into Session as expected. All good.

                Then, after creating a JSFSession in my TestCase
                _jsfSession = new JSFSession(getWebClientSpec(properties));
                _server = _jsfSession.getJSFServerSession();
                
                ExternalContext ctx = _server.getFacesContext().getCurrentInstance().getExternalContext();
                debugParameters(ctx);
                


                I can see clearly that the external context session (wrapped in the facade) is a completely DIFFERENT INSTANCE to that present in the Servlet class.

                Stan, does this sound correct ?

                • 5. Re: ServletTestRunner parameters
                  practica

                  Forgot to mention my environment:

                  jdk 1.5.0.16
                  apache-tomcat-6.0.18
                  JSF impl : Oracle ADF Faces 10.1.3.4
                  windows xp sp3

                  • 6. Re: ServletTestRunner parameters
                    ssilvert

                    I forgot about something.

                    I do wrap the HttpSession with a JSFUnitHttpSession. And also, that session gets cleared when you call new JSFSession(). You are not the first person who wanted to pass params to the ServletRedirector. I can create a new JSFUnit feature that allows you to do that so you can just call JSFSession.getRedirectorParams().

                    Let me implement that and get back to you. I'll post a snapshot when I'm done.

                    Stan

                    • 7. Re: ServletTestRunner parameters
                      practica

                      Thanks dude :)
                      Great stuff

                      • 8. Re: ServletTestRunner parameters
                        ssilvert

                        This is implemented now.
                        https://jira.jboss.org/jira/browse/JSFUNIT-206

                        I added an API for this. You call:
                        JSFSession.getRedirectorRequestParams()

                        This returns the same map returned by ServletRequest.getParameterMap(). Be careful. This Map is defined as Map<String, String[]> and not Map<String, String>.

                        Let me know what you think. You can get the latest snapshot here:
                        http://snapshots.jboss.org/maven2/org/jboss/jsfunit/jboss-jsfunit-core/1.1.0.GA-SNAPSHOT/

                        Stan

                        • 9. Re: ServletTestRunner parameters
                          ssilvert

                          Have you had a chance to try this out? I could really use some feedback.

                          Thanks,

                          Stan

                          • 10. Re: ServletTestRunner parameters
                            practica

                            Hi Stan :) Just got back from a short break. Will try it tomorrow. Thanks for following up !

                            cheers
                            j

                            • 11. Re: ServletTestRunner parameters
                              practica

                              I just got back from a short break.
                              I will test drive your enhancements today or tomorrow.
                              Thanks for following up :)

                              J

                              • 12. Re: ServletTestRunner parameters
                                ssilvert

                                I created a wiki page for this:
                                http://www.jboss.org/community/wiki/ParameterizingJSFUnitTests

                                Did you ever have a chance to test it out?

                                Stan