Author: dan.j.allen
Date: 2009-04-09 01:52:28 -0400 (Thu, 09 Apr 2009)
New Revision: 10371
Modified:
trunk/src/main/org/jboss/seam/navigation/Pages.java
trunk/src/main/org/jboss/seam/navigation/Param.java
trunk/src/main/org/jboss/seam/pages-2.1.xsd
trunk/src/test/integration/resources/WEB-INF/pages.xml
trunk/src/test/integration/src/org/jboss/seam/test/integration/PageParamTest.java
trunk/src/test/integration/src/org/jboss/seam/test/integration/Person.java
trunk/src/test/unit/org/jboss/seam/test/unit/AbstractPageTest.java
trunk/src/test/unit/org/jboss/seam/test/unit/PageParamTest.java
Log:
JBSEAM-4056
JBSEAM-4091
Modified: trunk/src/main/org/jboss/seam/navigation/Pages.java
===================================================================
--- trunk/src/main/org/jboss/seam/navigation/Pages.java 2009-04-09 05:48:05 UTC (rev
10370)
+++ trunk/src/main/org/jboss/seam/navigation/Pages.java 2009-04-09 05:52:28 UTC (rev
10371)
@@ -1485,6 +1485,12 @@
param.setValidatorValueExpression(Expressions.instance().createValueExpression(validatorExpression));
}
param.setRequired( Boolean.parseBoolean(
element.attributeValue("required") ) );
+ String validateModelStr = element.attributeValue("validateModel");
+ if (validateModelStr != null)
+ {
+ param.setValidateModel(Boolean.parseBoolean(validateModelStr));
+ }
+
return param;
}
Modified: trunk/src/main/org/jboss/seam/navigation/Param.java
===================================================================
--- trunk/src/main/org/jboss/seam/navigation/Param.java 2009-04-09 05:48:05 UTC (rev
10370)
+++ trunk/src/main/org/jboss/seam/navigation/Param.java 2009-04-09 05:52:28 UTC (rev
10371)
@@ -1,7 +1,6 @@
package org.jboss.seam.navigation;
import java.util.Map;
-import java.util.ResourceBundle;
import javax.el.ELContext;
import javax.el.ELException;
@@ -13,7 +12,6 @@
import org.hibernate.validator.InvalidValue;
import org.jboss.seam.contexts.Contexts;
-import org.jboss.seam.core.SeamResourceBundle;
import org.jboss.seam.core.Validators;
import org.jboss.seam.core.Expressions.ValueExpression;
import org.jboss.seam.faces.FacesMessages;
@@ -30,10 +28,14 @@
{
private static final LogProvider log = Logging.getLogProvider(Param.class);
+ public static final String INVALID_MESSAGE_ID =
"org.jboss.seam.param.Invalid";
+ public static final String REQUIRED_MESSAGE_ID =
"org.jboss.seam.param.Required";
+
private final String name;
private ValueExpression valueExpression;
private boolean required;
+ private boolean validateModel = true;
private ValueExpression converterValueExpression;
private String converterId;
@@ -233,7 +235,7 @@
validator.validate( facesContext, facesContext.getViewRoot(), value );
}
- if (valueExpression!=null)
+ if (valueExpression!=null && validateModel)
{
//TODO: note that this code is duplicated from ModelValidator!!
ELContext elContext = facesContext.getELContext();
@@ -258,22 +260,33 @@
private FacesMessage createMessage(InvalidValue[] invalidValues)
{
- return FacesMessages.createFacesMessage( FacesMessage.SEVERITY_ERROR,
invalidValues[0].getMessage() );
+ return FacesMessages.createFacesMessage(
+ FacesMessage.SEVERITY_ERROR,
+ INVALID_MESSAGE_ID,
+ "'" + name + "' parameter is invalid: " +
invalidValues[0].getMessage(),
+ new Object[] { invalidValues[0], name}
+ );
}
private FacesMessage createMessage(Throwable cause)
{
- return new FacesMessage(FacesMessage.SEVERITY_ERROR, "model validation
failed:" + cause, null);
+ return FacesMessages.createFacesMessage(
+ FacesMessage.SEVERITY_ERROR,
+ INVALID_MESSAGE_ID,
+ "'" + name + "' parameter is invalid: " +
cause.getMessage(),
+ new Object[] { cause.getMessage(), name }
+ );
}
private void addRequiredMessage(FacesContext facesContext)
{
- ResourceBundle resourceBundle = SeamResourceBundle.getBundle();
- throw new ValidatorException( new FacesMessage(
- FacesMessage.SEVERITY_ERROR,
-
resourceBundle.getString("javax.faces.component.UIInput.REQUIRED"),
- null
- ) );
+ throw new ValidatorException(
+ FacesMessages.createFacesMessage(
+ FacesMessage.SEVERITY_ERROR,
+ REQUIRED_MESSAGE_ID,
+ "'" + name + "' parameter is required",
+ name)
+ );
}
public String getValidatorId()
@@ -305,5 +318,15 @@
{
this.required = required;
}
+
+ public boolean isValidateModel()
+ {
+ return validateModel;
+ }
+
+ public void setValidateModel(boolean validateModel)
+ {
+ this.validateModel = validateModel;
+ }
}
Modified: trunk/src/main/org/jboss/seam/pages-2.1.xsd
===================================================================
--- trunk/src/main/org/jboss/seam/pages-2.1.xsd 2009-04-09 05:48:05 UTC (rev 10370)
+++ trunk/src/main/org/jboss/seam/pages-2.1.xsd 2009-04-09 05:52:28 UTC (rev 10371)
@@ -143,6 +143,7 @@
<xs:attribute name="validator" type="xs:token" />
<xs:attribute name="validatorId" type="xs:token" />
<xs:attribute name="required"
type="pages:tf-boolean"/>
+ <xs:attribute name="validateModel" default="true"
type="pages:tf-boolean"/>
</xs:attributeGroup>
<xs:element name="header">
Modified: trunk/src/test/integration/resources/WEB-INF/pages.xml
===================================================================
--- trunk/src/test/integration/resources/WEB-INF/pages.xml 2009-04-09 05:48:05 UTC (rev
10370)
+++ trunk/src/test/integration/resources/WEB-INF/pages.xml 2009-04-09 05:52:28 UTC (rev
10371)
@@ -21,6 +21,10 @@
<param name="personName" value="#{person.name}"
required="true"/>
</page>
+ <page view-id="/pageWithValidateModelDisabledParameter.xhtml">
+ <param name="personName" value="#{person.name}"
validateModel="false"/>
+ </page>
+
<exception class="org.jboss.seam.framework.EntityNotFoundException">
<redirect view-id="/error.xhtml">
<message>Not found</message>
Modified:
trunk/src/test/integration/src/org/jboss/seam/test/integration/PageParamTest.java
===================================================================
---
trunk/src/test/integration/src/org/jboss/seam/test/integration/PageParamTest.java 2009-04-09
05:48:05 UTC (rev 10370)
+++
trunk/src/test/integration/src/org/jboss/seam/test/integration/PageParamTest.java 2009-04-09
05:52:28 UTC (rev 10371)
@@ -2,10 +2,13 @@
import org.jboss.seam.mock.SeamTest;
import org.testng.annotations.Test;
+import javax.faces.validator.ValidatorException;
+import javax.faces.application.FacesMessage;
+import java.util.List;
/**
* @author Pete Muir
- *
+ * @author Dan Allen
*/
public class PageParamTest extends SeamTest
{
@@ -43,6 +46,47 @@
}
}.run();
}
+
+ @Test
+ public void testPageParameterFailsModelValidation() throws Exception
+ {
+ new FacesRequest("/pageWithParameter.xhtml")
+ {
+ @Override
+ protected void beforeRequest()
+ {
+ setParameter("personName", "pe");
+ }
+
+ @Override
+ protected void invokeApplication() throws Exception
+ {
+ List<FacesMessage> messages = (List<FacesMessage>)
getValue("#{facesMessages.currentMessages}");
+ assert messages.size() == 1;
+ assert messages.get(0).getDetail().startsWith("'personName'
parameter is invalid");
+ assert getValue("#{person.name}") == null;
+ }
+ }.run();
+ }
+
+ @Test
+ public void testPageParameterModelValidationDisabled() throws Exception
+ {
+ new FacesRequest("/pageWithValidateModelDisabledParameter.xhtml")
+ {
+ @Override
+ protected void beforeRequest()
+ {
+ setParameter("personName", "pe");
+ }
+
+ @Override
+ protected void invokeApplication() throws Exception
+ {
+ assert "pe".equals(getValue("#{person.name}"));
+ }
+ }.run();
+ }
@Test
public void testRequiredPageParameter() throws Exception
Modified: trunk/src/test/integration/src/org/jboss/seam/test/integration/Person.java
===================================================================
--- trunk/src/test/integration/src/org/jboss/seam/test/integration/Person.java 2009-04-09
05:48:05 UTC (rev 10370)
+++ trunk/src/test/integration/src/org/jboss/seam/test/integration/Person.java 2009-04-09
05:52:28 UTC (rev 10371)
@@ -1,6 +1,7 @@
package org.jboss.seam.test.integration;
import java.io.Serializable;
+import org.hibernate.validator.Length;
import org.jboss.seam.annotations.Name;
@@ -18,6 +19,7 @@
private String name;
+ @Length(min = 3, max = 10)
public String getName()
{
return name;
Modified: trunk/src/test/unit/org/jboss/seam/test/unit/AbstractPageTest.java
===================================================================
--- trunk/src/test/unit/org/jboss/seam/test/unit/AbstractPageTest.java 2009-04-09 05:48:05
UTC (rev 10370)
+++ trunk/src/test/unit/org/jboss/seam/test/unit/AbstractPageTest.java 2009-04-09 05:52:28
UTC (rev 10371)
@@ -12,6 +12,7 @@
import org.jboss.seam.core.Expressions;
import org.jboss.seam.core.Init;
import org.jboss.seam.core.Interpolator;
+import org.jboss.seam.core.Locale;
import org.jboss.seam.core.ResourceLoader;
import org.jboss.seam.mock.MockApplication;
import org.jboss.seam.mock.MockExternalContext;
@@ -74,6 +75,7 @@
installComponent(appContext, Expressions.class);
installComponent(appContext, Pages.class);
installComponent(appContext, Interpolator.class);
+ installComponent(appContext, Locale.class);
}
/**
Modified: trunk/src/test/unit/org/jboss/seam/test/unit/PageParamTest.java
===================================================================
--- trunk/src/test/unit/org/jboss/seam/test/unit/PageParamTest.java 2009-04-09 05:48:05
UTC (rev 10370)
+++ trunk/src/test/unit/org/jboss/seam/test/unit/PageParamTest.java 2009-04-09 05:52:28
UTC (rev 10371)
@@ -1,19 +1,15 @@
package org.jboss.seam.test.unit;
-import java.util.HashMap;
-
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.IntegerConverter;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
-import org.jboss.seam.contexts.Lifecycle;
+import org.hibernate.validator.Length;
+import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.core.Expressions;
-import org.jboss.seam.jsf.SeamApplication;
-import org.jboss.seam.mock.MockApplication;
-import org.jboss.seam.mock.MockExternalContext;
-import org.jboss.seam.mock.MockFacesContext;
+import org.jboss.seam.core.Validators;
import org.jboss.seam.navigation.Param;
import org.testng.annotations.Test;
@@ -23,14 +19,13 @@
*
* @author Dan Allen
*/
-public class PageParamTest
+public class PageParamTest extends AbstractPageTest
{
@Test
public void testGetConverterById()
{
String converterId = "javax.faces.Integer";
String converterClass = "javax.faces.convert.IntegerConverter";
- setupMockFacesContextWithSeamApplication();
Param param = new Param("param");
param.setConverterId(converterId);
assert param.getConverter() instanceof IntegerConverter : "expecting: " +
converterClass + "; got: " + param.getConverter();
@@ -44,13 +39,9 @@
@Test
public void testConverterIsNullForNonFacesValueExpression()
{
- setupMockFacesContextWithSeamApplication();
Param param = new Param("param");
param.setValueExpression(Expressions.instance().createValueExpression("#{variable}"));
- Lifecycle.beginApplication(new HashMap<String, Object>());
- Lifecycle.setupApplication();
assert param.getConverter() == null;
- Lifecycle.endApplication();
}
@Test
@@ -58,23 +49,57 @@
{
String validatorId = "TestValidator";
String validatorClass=
"org.jboss.seam.test.unit.PageParamTest$TestValidator";
- FacesContext facesContext = setupMockFacesContextWithSeamApplication();
- facesContext.getApplication().addValidator(validatorId, validatorClass);
+ FacesContext.getCurrentInstance().getApplication().addValidator(validatorId,
validatorClass);
Param param = new Param("param");
param.setValidatorId(validatorId);
assert param.getValidator() instanceof TestValidator : "expecting: " +
validatorClass + "; got: " + param.getValidator();
}
- protected FacesContext setupMockFacesContextWithSeamApplication()
+ @Test(expectedExceptions = ValidatorException.class)
+ public void testValidateModelWithInvalidValue()
{
- // MockApplication is wrapped with SeamApplication to validate the behavior Seam
introduces
- return new MockFacesContext(new MockExternalContext(), new SeamApplication(new
MockApplication())).setCurrent();
+
setupParamToValidate(false).validateConvertedValue(FacesContext.getCurrentInstance(),
"a");
}
+ @Test
+ public void testValidateModelWithValidValue()
+ {
+
setupParamToValidate(false).validateConvertedValue(FacesContext.getCurrentInstance(),
"aaa");
+ }
+
+ @Test
+ public void testValidateModelDisabled()
+ {
+
setupParamToValidate(true).validateConvertedValue(FacesContext.getCurrentInstance(),
"a");
+ }
+
+ protected Param setupParamToValidate(boolean disableModelValidator)
+ {
+ installComponent(Contexts.getApplicationContext(), Validators.class);
+ Param param = new Param("value");
+
param.setValueExpression(Expressions.instance().createValueExpression("#{bean.value}"));
+ Contexts.getEventContext().set("bean", new Bean());
+ if (disableModelValidator) {
+ param.setValidateModel(false);
+ }
+ return param;
+ }
+
public static class TestValidator implements Validator
{
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException
{
}
}
+
+ class Bean {
+ private String value;
+ @Length(min = 3, max = 10)
+ public String getValue() {
+ return value;
+ }
+ public void setValue(String value) {
+ this.value = value;
+ }
+ }
}