- Revision
- 475
- Author
- fabio.kung
- Date
- 2007-12-13 18:36:46 -0600 (Thu, 13 Dec 2007)
Log Message
w:errors tag and acceptance tests
Modified Paths
- trunk/integration/waffle-taglib/src/main/java/org/codehaus/waffle/taglib/acceptance/Product.java
- trunk/integration/waffle-taglib/src/main/webapp/WEB-INF/web.xml
- trunk/integration/waffle-taglib/src/main/webapp/index.jsp
- trunk/integration/waffle-taglib/src/main/webapp/select.jsp
- trunk/waffle-taglib/src/main/resources/META-INF/waffle.tld
Added Paths
- trunk/integration/waffle-taglib/src/main/java/org/codehaus/waffle/taglib/acceptance/ProductsController.java
- trunk/integration/waffle-taglib/src/main/java/org/codehaus/waffle/taglib/acceptance/ProductsControllerValidator.java
- trunk/integration/waffle-taglib/src/main/java/org/codehaus/waffle/taglib/acceptance/TaglibAcceptanceRegistrar.java
- trunk/integration/waffle-taglib/src/main/webapp/products.jsp
- trunk/integration/waffle-taglib/src/test/java/org/codehaus/waffle/taglib/acceptance/ErrorsTest.java
- trunk/waffle-taglib/src/main/resources/META-INF/tags/
- trunk/waffle-taglib/src/main/resources/META-INF/tags/errors.tag
Diff
Modified: trunk/integration/waffle-taglib/src/main/java/org/codehaus/waffle/taglib/acceptance/Product.java (474 => 475)
--- trunk/integration/waffle-taglib/src/main/java/org/codehaus/waffle/taglib/acceptance/Product.java 2007-12-13 21:58:19 UTC (rev 474) +++ trunk/integration/waffle-taglib/src/main/java/org/codehaus/waffle/taglib/acceptance/Product.java 2007-12-14 00:36:46 UTC (rev 475) @@ -4,7 +4,17 @@ private Integer id; private String name; + private Double price; + public Product() { + } + + public Product(Integer id, String name, Double price) { + this.id = id; + this.name = name; + this.price = price; + } + public Integer getId() { return id; } @@ -21,13 +31,11 @@ this.name = name; } - public Product(Integer id, String name) { - super(); - this.id = id; - this.name = name; + public Double getPrice() { + return price; } - public Product() { + public void setPrice(Double price) { + this.price = price; } - }
Added: trunk/integration/waffle-taglib/src/main/java/org/codehaus/waffle/taglib/acceptance/ProductsController.java (0 => 475)
--- trunk/integration/waffle-taglib/src/main/java/org/codehaus/waffle/taglib/acceptance/ProductsController.java (rev 0) +++ trunk/integration/waffle-taglib/src/main/java/org/codehaus/waffle/taglib/acceptance/ProductsController.java 2007-12-14 00:36:46 UTC (rev 475) @@ -0,0 +1,17 @@ +package org.codehaus.waffle.taglib.acceptance; + +import org.codehaus.waffle.action.annotation.ActionMethod; +import org.codehaus.waffle.validation.ErrorsContext; +import org.codehaus.waffle.validation.FieldErrorMessage; +import org.codehaus.waffle.validation.GlobalErrorMessage; + +/** + * @Author Fabio Kung + */ +public class ProductsController { + + @ActionMethod(parameters = {"name", "price"}) + public void add(String name, Double price) { + // has validation errors... + } +}
Added: trunk/integration/waffle-taglib/src/main/java/org/codehaus/waffle/taglib/acceptance/ProductsControllerValidator.java (0 => 475)
--- trunk/integration/waffle-taglib/src/main/java/org/codehaus/waffle/taglib/acceptance/ProductsControllerValidator.java (rev 0) +++ trunk/integration/waffle-taglib/src/main/java/org/codehaus/waffle/taglib/acceptance/ProductsControllerValidator.java 2007-12-14 00:36:46 UTC (rev 475) @@ -0,0 +1,25 @@ +package org.codehaus.waffle.taglib.acceptance; + +import org.codehaus.waffle.validation.ErrorsContext; +import org.codehaus.waffle.validation.FieldErrorMessage; +import org.codehaus.waffle.validation.GlobalErrorMessage; + +/** + * @Author Fabio Kung + */ +public class ProductsControllerValidator { + public void add(ErrorsContext errors, String name, Double price) { + boolean hasError = false; + if (price == null || price.doubleValue() == 0.0) { + hasError = true; + errors.addErrorMessage(new FieldErrorMessage("price", price.toString(), "invalid_price")); + } + if (name == null || name.trim().length() == 0) { + hasError = true; + errors.addErrorMessage(new FieldErrorMessage("name", name, "invalid_name")); + } + if (hasError) { + errors.addErrorMessage(new GlobalErrorMessage("there are errors...")); + } + } +}
Added: trunk/integration/waffle-taglib/src/main/java/org/codehaus/waffle/taglib/acceptance/TaglibAcceptanceRegistrar.java (0 => 475)
--- trunk/integration/waffle-taglib/src/main/java/org/codehaus/waffle/taglib/acceptance/TaglibAcceptanceRegistrar.java (rev 0) +++ trunk/integration/waffle-taglib/src/main/java/org/codehaus/waffle/taglib/acceptance/TaglibAcceptanceRegistrar.java 2007-12-14 00:36:46 UTC (rev 475) @@ -0,0 +1,19 @@ +package org.codehaus.waffle.taglib.acceptance; + +import org.codehaus.waffle.registrar.AbstractRegistrar; +import org.codehaus.waffle.registrar.Registrar; + +/** + * @Author Fabio Kung + */ +public class TaglibAcceptanceRegistrar extends AbstractRegistrar { + + public TaglibAcceptanceRegistrar(Registrar delegate) { + super(delegate); + } + + public void request() { + register("products", ProductsController.class); + register("productsValidator", ProductsControllerValidator.class); + } +}
Modified: trunk/integration/waffle-taglib/src/main/webapp/WEB-INF/web.xml (474 => 475)
--- trunk/integration/waffle-taglib/src/main/webapp/WEB-INF/web.xml 2007-12-13 21:58:19 UTC (rev 474) +++ trunk/integration/waffle-taglib/src/main/webapp/WEB-INF/web.xml 2007-12-14 00:36:46 UTC (rev 475) @@ -1,10 +1,46 @@ <?xml version="1.0" encoding="ISO-8859-1"?> -<web-app id="WebApp_ID" version="2.4" - xmlns="http://java.sun.com/xml/ns/j2ee" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> +<web-app version="2.4" + xmlns="http://java.sun.com/xml/ns/j2ee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> - <display-name>waffle taglib acceptance tests</display-name> + <display-name>waffle taglib acceptance tests</display-name> + <!-- 1. This is how an application registers its custom Registrar --> + <context-param> + <param-name>org.codehaus.waffle.registrar.Registrar</param-name> + <param-value>org.codehaus.waffle.taglib.acceptance.TaglibAcceptanceRegistrar</param-value> + </context-param> + + <!-- 3. Waffle context listener (ServletContext and HttpSession) --> + <listener> + <listener-class>org.codehaus.waffle.context.pico.PicoWaffleContextListener</listener-class> + </listener> + + <!-- 4. Waffle request filter (responsible for request level context) --> + <filter> + <filter-name>WaffleRequestFilter</filter-name> + <filter-class>org.codehaus.waffle.context.WaffleRequestFilter</filter-class> + </filter> + <filter-mapping> + <filter-name>WaffleRequestFilter</filter-name> + <url-pattern>*.waffle</url-pattern> + </filter-mapping> + + <!-- 5. Register Waffle's FrontController servlet --> + <servlet> + <servlet-name>waffle</servlet-name> + <servlet-class>org.codehaus.waffle.servlet.WaffleServlet</servlet-class> + <init-param> + <param-name>view.suffix</param-name> + <param-value>.jsp</param-value> + </init-param> + <load-on-startup>1</load-on-startup> + </servlet> + <servlet-mapping> + <servlet-name>waffle</servlet-name> + <url-pattern>*.waffle</url-pattern> + </servlet-mapping> + </web-app>
Modified: trunk/integration/waffle-taglib/src/main/webapp/index.jsp (474 => 475)
--- trunk/integration/waffle-taglib/src/main/webapp/index.jsp 2007-12-13 21:58:19 UTC (rev 474) +++ trunk/integration/waffle-taglib/src/main/webapp/index.jsp 2007-12-14 00:36:46 UTC (rev 475) @@ -15,6 +15,7 @@ <a href="" /> <a href="" /> <a href="" /> + <a href="" /> </body > </html>
Added: trunk/integration/waffle-taglib/src/main/webapp/products.jsp (0 => 475)
--- trunk/integration/waffle-taglib/src/main/webapp/products.jsp (rev 0) +++ trunk/integration/waffle-taglib/src/main/webapp/products.jsp 2007-12-14 00:36:46 UTC (rev 475) @@ -0,0 +1,41 @@ +<%@ taglib uri="http://waffle.codehaus.org" prefix="w" %> +<html> +<head><title>Errors tag acceptance test</title></head> +<body> +<h2>All errors:</h2> +<ul id="allErrors"> + <w:errors> + <li class="error">${error.message}</li> + </w:errors> +</ul> + +<w:form id="productForm" type="table" action="" method="post"> + <w:hidden name="method" value="add"/> + <w:text id="nameField" name="name"/> + <w:number id="priceField" name="price"/> + <w:submit/> +</w:form> + +<h2>Field errors:</h2> +<ul id="nameErrors"> + <w:errors field="name"> + <li class="error"> + field: ${error.name}<br/> + value: ${error.value}<br/> + message: ${error.message} + </li> + </w:errors> +</ul> + +<ul id="priceErrors"> + <w:errors field="price"> + <li class="error"> + field: ${error.name}<br/> + value: ${error.value}<br/> + message: ${error.message} + </li> + </w:errors> +</ul> + +</body> +</html> \ No newline at end of file
Modified: trunk/integration/waffle-taglib/src/main/webapp/select.jsp (474 => 475)
--- trunk/integration/waffle-taglib/src/main/webapp/select.jsp 2007-12-13 21:58:19 UTC (rev 474) +++ trunk/integration/waffle-taglib/src/main/webapp/select.jsp 2007-12-14 00:36:46 UTC (rev 475) @@ -12,10 +12,10 @@ <% List<Product> lista = new ArrayList<Product>(); - lista.add(new Product(1,"trabant")); - lista.add(new Product(2,"wartburg")); - lista.add(new Product(3,"skoda")); - lista.add(new Product(null, "nasty")); + lista.add(new Product(1,"trabant", 10.0)); + lista.add(new Product(2,"wartburg", 20.0)); + lista.add(new Product(3,"skoda", 30.0)); + lista.add(new Product(null, "nasty", 40.0)); pageContext.setAttribute("lista",lista); %>
Added: trunk/integration/waffle-taglib/src/test/java/org/codehaus/waffle/taglib/acceptance/ErrorsTest.java (0 => 475)
--- trunk/integration/waffle-taglib/src/test/java/org/codehaus/waffle/taglib/acceptance/ErrorsTest.java (rev 0) +++ trunk/integration/waffle-taglib/src/test/java/org/codehaus/waffle/taglib/acceptance/ErrorsTest.java 2007-12-14 00:36:46 UTC (rev 475) @@ -0,0 +1,67 @@ +package org.codehaus.waffle.taglib.acceptance; + +/** + * @Author Fabio Kung + */ +public class ErrorsTest extends IntegrationTest { + + public void testGlobalErrorMessages() { + open("products.waffle"); + selenium.refresh(); + selenium.submit("productForm"); + + int globalErrorCount = selenium.getXpathCount("//[EMAIL PROTECTED]'allErrors']/li").intValue(); + assertEquals(3, globalErrorCount); + } + + public void testEmptySubmitGeneratesTwoFieldErrorMessages() { + open("products.waffle"); + selenium.refresh(); + selenium.submit("productForm"); + + int nameErrorsCount = selenium.getXpathCount("//[EMAIL PROTECTED]'nameErrors']/li").intValue(); + assertEquals(1, nameErrorsCount); + int priceErrorsCount = selenium.getXpathCount("//[EMAIL PROTECTED]'priceErrors']/li").intValue(); + assertEquals(1, priceErrorsCount); + + } + + public void testPriceEmptyGeneratesOneFieldErrorMessage() { + open("products.waffle"); + selenium.refresh(); + selenium.type("nameField", "John Travolta"); + selenium.submit("productForm"); + + int nameErrorsCount = selenium.getXpathCount("//[EMAIL PROTECTED]'nameErrors']/li").intValue(); + assertEquals(0, nameErrorsCount); + int priceErrorsCount = selenium.getXpathCount("//[EMAIL PROTECTED]'priceErrors']/li").intValue(); + assertEquals(1, priceErrorsCount); + + } + + public void testNameEmptyGeneratesOneFieldErrorMessages() { + open("products.waffle"); + selenium.refresh(); + selenium.type("priceField", "10"); + selenium.submit("productForm"); + + int nameErrorsCount = selenium.getXpathCount("//[EMAIL PROTECTED]'nameErrors']/li").intValue(); + assertEquals(1, nameErrorsCount); + int priceErrorsCount = selenium.getXpathCount("//[EMAIL PROTECTED]'priceErrors']/li").intValue(); + assertEquals(0, priceErrorsCount); + + } + + public void testFormFilledGeneratesNoErrorMessages() { + open("products.waffle"); + selenium.refresh(); + selenium.type("nameField", "Luke"); + selenium.type("priceField", "10"); + selenium.submit("productForm"); + + int nameErrorsCount = selenium.getXpathCount("//[EMAIL PROTECTED]'nameErrors']/li").intValue(); + assertEquals(0, nameErrorsCount); + int priceErrorsCount = selenium.getXpathCount("//[EMAIL PROTECTED]'priceErrors']/li").intValue(); + assertEquals(0, priceErrorsCount); + } +}
Added: trunk/waffle-taglib/src/main/resources/META-INF/tags/errors.tag (0 => 475)
--- trunk/waffle-taglib/src/main/resources/META-INF/tags/errors.tag (rev 0) +++ trunk/waffle-taglib/src/main/resources/META-INF/tags/errors.tag 2007-12-14 00:36:46 UTC (rev 475) @@ -0,0 +1,27 @@ +<%@ taglib uri="http://waffle.codehaus.org" prefix="w" %> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> +<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> +<%@ attribute name="errors" type="org.codehaus.waffle.validation.ErrorsContext" required="false" %> +<%@ attribute name="field" required="false" %> +<%@ variable name-given="error" scope="NESTED" %> + +<c:if test="${empty errors }"> + <c:set var="errors" value="${requestScope.errors}"/> +</c:if> +<c:if test="${not empty errors}"> + <c:choose> + <c:when test="${empty field}"> + <c:set var="errorMessages" + value="${errors.allErrorMessages }"/> + </c:when> + <c:otherwise> + <c:set var="errorMessages" + value="${w:findErrorsForField(errors, 'FIELD', field) }"/> + </c:otherwise> + </c:choose> + <c:if test="${fn:length(errorMessages) ne 0 }"> + <c:forEach var="error" items="${errorMessages}"> + <jsp:doBody/> + </c:forEach> + </c:if> +</c:if>
Modified: trunk/waffle-taglib/src/main/resources/META-INF/waffle.tld (474 => 475)
--- trunk/waffle-taglib/src/main/resources/META-INF/waffle.tld 2007-12-13 21:58:19 UTC (rev 474) +++ trunk/waffle-taglib/src/main/resources/META-INF/waffle.tld 2007-12-14 00:36:46 UTC (rev 475) @@ -49,6 +49,11 @@ </function-signature> </function> + <tag-file> + <name>errors</name> + <path>/META-INF/tags/errors.tag</path> + </tag-file> + <!-- checkbox tag --> <tag> <name>checkbox</name>
To unsubscribe from this list please visit:
