4 Replies Latest reply on Dec 16, 2010 1:36 AM by yy zhang

    how to test c:foreach

    yy zhang Newbie

      i tried test jef with jsfunit but there were a few problems when it came to c,could u help me to figure it out?thanks,my jsp was as followed:

      hi,

      i tried test jsf with jsfunit but there were a few problems when it came to c:foreach,could you help me to figure it out?thanks,

      my jsp was as followed:

      <tbody>

                         <c:forEach items="#{proxyListBean.pagedProxys.entities}" var="proxy" >

                          <tr>

                            <td class="a-center" width="3%">

                              <input type="checkbox" name="checkbox" value="${proxy.id}" />

                            </td>

                          <td><a href="#"><h:outputText value="#{proxy.name}"></h:outputText></a></td>

                          <td>

                            <c:if test="${fn:length(proxy.desc) > 12}">

                              ${fn:substring(proxy.desc, 0, 10)}...

                            </c:if>

                            <c:if test="${fn:length(proxy.desc) <= 12}">

                              ${proxy.desc}

                            </c:if>

                          </td>

      .................

        • 1. Re: how to test c:foreach
          Stan Silvert Master

          I'll be glad to help however I can.  I see your JSP.  What is your problem?

           

          Stan

          • 2. Re: how to test c:foreach
            yy zhang Newbie

            I'll be glad to help however I can.  I see your JSP.  What is your problem?
            Stan
            i don't know that how to test c:foreach
            Could you  show me a example?

            i don't know how to test c:foreach

            Could you  show me an example?

             

            thanks

            • 3. Re: how to test c:foreach
              Stan Silvert Master

              This might help:

               

              marathons.jsp

              <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
              <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
              <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
              
              <HTML>
              
              <f:view>  
                 <h1><h:outputText value="Major Marathons" id="title"/></h1>
                 
                 <h:form id="form1">    
              
                    <table>
                       <tr><th>Marathon</th> <th>Location</th> <th>Select</th></tr>
                       <c:forEach var="marathon" items="#{marathons.list}">
                          <tr>
                            <td><h:outputText value="#{marathon.name}" /></td>
                            <td><h:outputText value="#{marathon.location}" /></td>
                            <td>
                               <h:commandLink id="marathonSelect" onclick="var foo='bar';" action="#{marathons.select}">
                                  <f:param name="selectedName" value="#{marathon.name}"/>
                                  <h:outputText id="selectText" value="Select"/>
                               </h:commandLink>
                            </td>
                          </tr>
                       </c:forEach>
                    </table>
                    <br/><b>
                       <h:outputText value="Selected Marathon: #{marathons.selectedMarathon}"/>
                    </b>
                 </h:form>      
              </f:view>
              

               

              FacadeAPITest.java

              public void testCommandLinkWithParamFromLoopVariable() throws IOException
                 {
                    // test should not run for JSF 1.1 - it uses a loop variable from <c:forEach>
                    if ((Environment.getJSFMajorVersion() == 1) &&
                        (Environment.getJSFMinorVersion() < 2)) return;
                    
                    JSFSession jsfSession = new JSFSession("/marathons.faces");
                    JSFClientSession client = jsfSession.getJSFClientSession();
                    
                    client.click("marathonSelect");
                    assertTrue(client.getPageAsText().contains("Selected Marathon: BAA Boston Marathon"));
                    
                    client.click("marathonSelectj_id_3");
                    assertTrue(client.getPageAsText().contains("Selected Marathon: Flora London Marathon"));
                    
                    client.click("marathonSelectj_id_5");
                    assertTrue(client.getPageAsText().contains("Selected Marathon: Olympic Marathon"));
                 }

               

              This test is taken from the example here:

              http://anonsvn.jboss.org/repos/jsfunit/trunk/jboss-jsfunit-examples/jboss-jsfunit-examples-hellojsf/jboss-jsfunit-examples-hellojsf-jsfunit/

               

              Note that the above webapp is dependent on another webapp.  So the JSP actually lives in this submodule:

              http://anonsvn.jboss.org/repos/jsfunit/trunk/jboss-jsfunit-examples/jboss-jsfunit-examples-hellojsf/jboss-jsfunit-examples-hellojsf-webapp/

               

              Stan

              • 4. Re: how to test c:foreach
                yy zhang Newbie

                Sorry for late reply, very grateful for your help

                Sorry for late reply, very grateful for your help