You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ma...@apache.org on 2008/06/25 06:24:31 UTC
svn commit: r671414 - in /myfaces/trinidad/trunk/trinidad-api/src:
main/java/org/apache/myfaces/trinidad/validator/
test/java/org/apache/myfaces/trinidad/validator/
Author: matzew
Date: Tue Jun 24 21:24:30 2008
New Revision: 671414
URL: http://svn.apache.org/viewvc?rev=671414&view=rev
Log:
TRINIDAD-1129 - Server-side validation does not work when using Sun JSF implementation
Added:
myfaces/trinidad/trunk/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidatorTest.java
myfaces/trinidad/trunk/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LengthValidatorTest.java
myfaces/trinidad/trunk/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LongRangeValidatorTest.java
Modified:
myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidator.java
myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LengthValidator.java
myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LongRangeValidator.java
Modified: myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidator.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidator.java?rev=671414&r1=671413&r2=671414&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidator.java (original)
+++ myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidator.java Tue Jun 24 21:24:30 2008
@@ -75,6 +75,14 @@
/**
+ * <p>The message identifier of the FacesMessage to be created if
+ * the value cannot be converted
+ */
+ public static final String CONVERT_MESSAGE_ID =
+ "org.apache.myfaces.trinidad.convert.DoubleConverter.CONVERT";
+
+
+ /**
* Construct a {@link Validator} with no preconfigured limits.
*/
public DoubleRangeValidator()
@@ -116,7 +124,7 @@
{
Object maxDouble = _facesBean.getProperty(_MAXIMUM_KEY);
if(maxDouble == null)
- maxDouble = Double.MAX_VALUE;
+ maxDouble = _MAXIMUM_KEY.getDefault();
return ComponentUtils.resolveDouble(maxDouble);
}
@@ -144,7 +152,7 @@
{
Object minDouble = _facesBean.getProperty(_MINIMUM_KEY);
if(minDouble == null)
- minDouble = Double.MIN_VALUE;
+ minDouble = _MINIMUM_KEY.getDefault();
return ComponentUtils.resolveDouble(minDouble);
}
@@ -303,53 +311,53 @@
Object value
) throws ValidatorException
{
- try
+ if ((context == null) || (component == null))
+ {
+ throw new NullPointerException();
+ }
+
+ if (value == null)
{
- super.validate(context, component, value);
+ return;
}
- catch (ValidatorException ve)
+ if (value instanceof Number)
{
-
- if (value != null && value instanceof Number)
+ double doubleValue = ((Number)value).doubleValue();
+ double min = getMinimum();
+ double max = getMaximum();
+
+ if(isMaximumSet() && isMinimumSet())
{
- double doubleValue = ((Number)value).doubleValue();
-
- double min = getMinimum();
- double max = getMaximum();
-
- if (doubleValue > max)
+ if(doubleValue < min || doubleValue > max)
{
- if (min != Double.MIN_VALUE)//the default...
- {
- throw new ValidatorException
- (_getNotInRangeMessage(context, component, value, min, max));
- }
- else
- {
- throw new ValidatorException
- (_getMaximumMessage(context, component, value, max));
- }
+ throw new ValidatorException(
+ _getNotInRangeMessage(context, component, value, min, max));
}
-
- if (doubleValue < min)
+ }
+
+ if(isMaximumSet())
+ {
+ if (doubleValue > max)
{
- if (max != Double.MAX_VALUE)//the default...
- {
- throw new ValidatorException
- (_getNotInRangeMessage(context, component, value, min, max));
- }
- else
- {
- FacesMessage msg = _getMinimumMessage(context, component, value, min);
- throw new ValidatorException(msg);
- }
+ throw new ValidatorException(
+ _getMaximumMessage(context, component, value, max));
}
}
- else
+
+ if(isMinimumSet())
{
- throw ve;
+ if (doubleValue < min)
+ {
+ throw new ValidatorException(
+ _getMinimumMessage(context, component, value, min));
+ }
}
- }
+ }
+ else
+ {
+ FacesMessage msg = _getNotCorrectType(context);
+ throw new ValidatorException(msg);
+ }
}
// StateHolder Methods
@@ -407,7 +415,22 @@
return (_transientValue);
}
+ protected boolean isMaximumSet()
+ {
+ return _facesBean.getProperty(_MAXIMUM_KEY) != null;
+ }
+
+ protected boolean isMinimumSet()
+ {
+ return _facesBean.getProperty(_MINIMUM_KEY) != null;
+ }
+ private FacesMessage _getNotCorrectType(
+ FacesContext context)
+ {
+ return MessageFactory.getMessage(context, CONVERT_MESSAGE_ID);
+ }
+
@Override
public void setTransient(boolean transientValue)
{
@@ -485,10 +508,14 @@
private static final FacesBean.Type _TYPE = new FacesBean.Type();
private static final PropertyKey _MINIMUM_KEY =
- _TYPE.registerKey("minimum", Double.class);
+ _TYPE.registerKey("minimum", Double.class,
+ // Don't rely on autoboxing: there's a method overload
+ Double.valueOf(Double.MIN_VALUE));
private static final PropertyKey _MAXIMUM_KEY =
- _TYPE.registerKey("maximum", Double.class);
+ _TYPE.registerKey("maximum", Double.class,
+ // Don't rely on autoboxing: there's a method overload
+ Double.valueOf(Double.MAX_VALUE));
private static final PropertyKey _MAXIMUM_MESSAGE_DETAIL_KEY =
_TYPE.registerKey("messageDetailMaximum", String.class);
Modified: myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LengthValidator.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LengthValidator.java?rev=671414&r1=671413&r2=671414&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LengthValidator.java (original)
+++ myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LengthValidator.java Tue Jun 24 21:24:30 2008
@@ -144,7 +144,6 @@
@Override
public void setMaximum(int maximum)
{
- super.setMaximum(maximum);
_facesBean.setProperty(_MAXIMUM_KEY, Integer.valueOf(maximum));
}
@@ -172,7 +171,6 @@
@Override
public void setMinimum(int minimum)
{
- super.setMinimum(minimum);
_facesBean.setProperty(_MINIMUM_KEY, Integer.valueOf(minimum));
}
@@ -368,39 +366,46 @@
Object value
) throws ValidatorException
{
- try
+ if ((context == null) || (component == null))
{
- super.validate(context, component, value);
+ throw new NullPointerException();
}
- catch (ValidatorException ve)
+
+ if(value != null)
{
- // We don't really care about the value. It
- // failed validation in the superclass, so we just
- // care about what sort of message to show
- int min = getMinimum();
int max = getMaximum();
-
- // FIXME: the default of max should be zero, not MAX_VALUE,
- // at least according to the superclass
- if (max != Integer.MAX_VALUE)
+ int min = getMinimum();
+ int length = value instanceof String ?
+ ((String)value).length() : value.toString().length();
+
+ // range validation
+ if(isMaximumSet() && isMinimumSet())
{
- if (min > 0)
+ if(length<min || length>max)
{
throw new ValidatorException(
_getNotInRangeMessage(context, component, value, min, max));
}
- else
+ }
+ // too short
+ if(isMinimumSet())
+ {
+ if (length < min)
{
throw new ValidatorException(
- _getMaximumMessage(context, component, value, max));
+ _getMinimumMessage(context, component, value, max));
}
}
- else
+ // too long
+ if(isMaximumSet())
{
- throw new ValidatorException(
- _getMinimumMessage(context, component, value, min));
+ if (length > max)
+ {
+ throw new ValidatorException(
+ _getMaximumMessage(context, component, value, max));
+ }
}
- }
+ }
}
// StateHolder Methods
@@ -465,6 +470,15 @@
_transientValue = transientValue;
}
+ protected boolean isMaximumSet()
+ {
+ return _facesBean.getProperty(_MAXIMUM_KEY) != null;
+ }
+
+ protected boolean isMinimumSet()
+ {
+ return _facesBean.getProperty(_MINIMUM_KEY) != null;
+ }
private FacesMessage _getNotInRangeMessage(
FacesContext context,
Modified: myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LongRangeValidator.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LongRangeValidator.java?rev=671414&r1=671413&r2=671414&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LongRangeValidator.java (original)
+++ myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/validator/LongRangeValidator.java Tue Jun 24 21:24:30 2008
@@ -77,6 +77,14 @@
/**
+ * <p>The message identifier of the FacesMessage to be created if
+ * the value cannot be converted to an integer
+ */
+ public static final String CONVERT_MESSAGE_ID =
+ "org.apache.myfaces.trinidad.convert.LongConverter.CONVERT";
+
+
+ /**
* Construct a {@link Validator} with no preconfigured limits.
*/
public LongRangeValidator()
@@ -131,7 +139,6 @@
@Override
public void setMaximum(long maximum)
{
- super.setMaximum(maximum);
_facesBean.setProperty(_MAXIMUM_KEY, Long.valueOf(maximum));
}
@@ -159,7 +166,6 @@
@Override
public void setMinimum(long minimum)
{
- super.setMinimum(minimum);
_facesBean.setProperty(_MINIMUM_KEY, Long.valueOf(minimum));
}
@@ -305,53 +311,54 @@
Object value
) throws ValidatorException
{
- try
+ if ((context == null) || (component == null))
{
- super.validate(context, component, value);
+ throw new NullPointerException();
}
- catch (ValidatorException ve)
+
+ if (value == null)
+ {
+ return;
+ }
+
+ if (value instanceof Number)
{
-
- if (value != null && value instanceof Number)
+ long longValue = ((Number)value).longValue();
+ long min = getMinimum();
+ long max = getMaximum();
+
+ if(isMaximumSet() && isMinimumSet())
{
- long longValue = ((Number)value).longValue();
-
- long min = getMinimum();
- long max = getMaximum();
-
- if (longValue > max)
+ if(longValue<min || longValue>max)
{
- if (min != Long.MIN_VALUE)//the default...
- {
- throw new ValidatorException
- (_getNotInRangeMessage(context, component, value, IntegerUtils.getString(min), IntegerUtils.getString(max)));
- }
- else
- {
- throw new ValidatorException
- (_getMaximumMessage(context, component, value, IntegerUtils.getString(max)));
- }
+ throw new ValidatorException(
+ _getNotInRangeMessage(context, component, value, min, max));
}
-
- if (longValue < min)
+ }
+
+ if(isMaximumSet())
+ {
+ if (longValue > max)
{
- if (max != Long.MAX_VALUE)//the default...
- {
- throw new ValidatorException
- (_getNotInRangeMessage(context, component, value, IntegerUtils.getString(min), IntegerUtils.getString(max)));
- }
- else
- {
- FacesMessage msg = _getMinimumMessage(context, component, value, IntegerUtils.getString(min));
- throw new ValidatorException(msg);
- }
+ throw new ValidatorException(
+ _getMaximumMessage(context, component, value, IntegerUtils.getString(max)));
}
}
- else
+
+ if(isMinimumSet())
{
- throw ve;
+ if (longValue < min)
+ {
+ throw new ValidatorException(
+ _getMinimumMessage(context, component, value, IntegerUtils.getString(min)));
+ }
}
- }
+ }
+ else
+ {
+ FacesMessage msg = _getNotCorrectType(context);
+ throw new ValidatorException(msg);
+ }
}
// StateHolder Methods
@@ -416,6 +423,22 @@
_transientValue = transientValue;
}
+ protected boolean isMaximumSet()
+ {
+ return _facesBean.getProperty(_MAXIMUM_KEY) != null;
+ }
+
+ protected boolean isMinimumSet()
+ {
+ return _facesBean.getProperty(_MINIMUM_KEY) != null;
+ }
+
+ private FacesMessage _getNotCorrectType(
+ FacesContext context)
+ {
+ return MessageFactory.getMessage(context, CONVERT_MESSAGE_ID);
+ }
+
private FacesMessage _getNotInRangeMessage(
FacesContext context,
UIComponent component,
Added: myfaces/trinidad/trunk/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidatorTest.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidatorTest.java?rev=671414&view=auto
==============================================================================
--- myfaces/trinidad/trunk/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidatorTest.java (added)
+++ myfaces/trinidad/trunk/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/DoubleRangeValidatorTest.java Tue Jun 24 21:24:30 2008
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.trinidad.validator;
+
+import javax.faces.component.UIComponent;
+import javax.faces.validator.ValidatorException;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
+import org.jmock.Mock;
+
+/**
+ * Unit tests for DoubleRangeValidator
+ *
+ */
+public class DoubleRangeValidatorTest extends ValidatorTestCase
+{
+ public DoubleRangeValidatorTest(String testName)
+ {
+ super(testName);
+ }
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+ }
+
+ @Override
+ protected void tearDown() throws Exception
+ {
+ super.tearDown();
+ }
+
+ public static Test suite()
+ {
+ return new TestSuite(DoubleRangeValidatorTest.class);
+ }
+
+ /**
+ * Tests that null returns immediately.
+ *
+ * @throws ValidatorException when test fails
+ */
+ public void testNull() throws ValidatorException
+ {
+ Mock mock = buildMockUIComponent();
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+ DoubleRangeValidator validator = new DoubleRangeValidator();
+
+ doTestNull(facesContext, wrapper, validator);
+ }
+
+ /**
+ * Test when context is set to null
+ */
+ public void testNullContext()
+ {
+ Mock mock = buildMockUIComponent();
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+ DoubleRangeValidator validator = new DoubleRangeValidator();
+
+ doTestNullContext(wrapper, validator);
+ }
+
+ public void testNullComponent()
+ {
+ DoubleRangeValidator validator = new DoubleRangeValidator();
+
+ doTestNullComponent(facesContext, validator);
+ }
+
+ public void testTooLarge()
+ {
+ // since the pattern has not been set it will be null
+ // let us push some arbitary value
+ Mock mock = mock(UIComponent.class);
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+ setMockLabelForComponent(wrapper);
+
+ try
+ {
+ DoubleRangeValidator validator = new DoubleRangeValidator();
+ validator.setMaximum(100);
+ validator.validate(facesContext, component, 1000);
+ // test fails if it is here
+
+ fail("Expected Null pointer exception");
+ }
+ catch (ValidatorException ve)
+ {
+ // suppress it - this is as expected
+ }
+ mock.verify();
+ }
+
+ public void testWrongType()
+ {
+ // since the pattern has not been set it will be null
+ // let us push some arbitary value
+ Mock mock = mock(UIComponent.class);
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+ setMockLabelForComponent(wrapper);
+
+ try
+ {
+ DoubleRangeValidator validator = new DoubleRangeValidator();
+ validator.setMaximum(2);
+ validator.validate(facesContext, component, "thisShouldFail");
+ // test fails if it is here
+
+ fail("Expected Null pointer exception");
+ }
+ catch (ValidatorException ve)
+ {
+ // suppress it - this is as expected
+ }
+ mock.verify();
+ }
+
+ public void testExactFailure()
+ {
+ // some very basic sanity test
+ Mock mock = buildMockUIComponent();
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+ setMockLabelForComponent(wrapper);
+
+ try
+ {
+ DoubleRangeValidator validator = new DoubleRangeValidator();
+ double value = 20d;
+ validator.setMinimum(2);
+ validator.setMaximum(2);
+ validator.validate(facesContext, component, value);
+ fail("Expected ValidatorException for exact");
+ }
+ catch (ValidatorException ve)
+ {
+ // if exception then fine.
+ }
+
+ mock.verify();
+ }
+
+ public void testSanitySuccess()
+ {
+ //some very basic sanity test
+ //
+ DoubleRangeValidator validator = new DoubleRangeValidator();
+ Mock mock = buildMockUIComponent();
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+
+ Double values[] = {200d,500d};
+ validator.setMinimum(2);
+ for (int i = 0; i < values.length ; i++)
+ {
+ doTestValidate(validator, facesContext, wrapper, values[i]);
+ }
+ }
+
+}
+ //////////////////////////////////////////////////////////////////////////////
+ // MOCK OBJECTS
+ // 1. Get a MockControl for the interface we would like to simulate
+ // 2. get the MockObject from MockControl
+ // 3. specify the behaviour of the Mock Object (record state)
+ // 4. activate the MockObject via the control (replay state)
+ //
+ //////////////////////////////////////////////////////////////////////////////
\ No newline at end of file
Added: myfaces/trinidad/trunk/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LengthValidatorTest.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LengthValidatorTest.java?rev=671414&view=auto
==============================================================================
--- myfaces/trinidad/trunk/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LengthValidatorTest.java (added)
+++ myfaces/trinidad/trunk/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LengthValidatorTest.java Tue Jun 24 21:24:30 2008
@@ -0,0 +1,182 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.trinidad.validator;
+
+import javax.faces.component.UIComponent;
+import javax.faces.validator.ValidatorException;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
+import org.jmock.Mock;
+
+/**
+ * Unit tests for LengthValidator
+ *
+ */
+public class LengthValidatorTest extends ValidatorTestCase
+{
+ public LengthValidatorTest(String testName)
+ {
+ super(testName);
+ }
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+ }
+
+ @Override
+ protected void tearDown() throws Exception
+ {
+ super.tearDown();
+ }
+
+ public static Test suite()
+ {
+ return new TestSuite(LengthValidatorTest.class);
+ }
+
+ /**
+ * Tests that null returns immediately.
+ *
+ * @throws ValidatorException when test fails
+ */
+ public void testNull() throws ValidatorException
+ {
+ Mock mock = buildMockUIComponent();
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+ LengthValidator validator = new LengthValidator();
+
+ doTestNull(facesContext, wrapper, validator);
+ }
+
+ /**
+ * Test when context is set to null
+ */
+ public void testNullContext()
+ {
+ Mock mock = buildMockUIComponent();
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+ LengthValidator validator = new LengthValidator();
+
+ doTestNullContext(wrapper, validator);
+ }
+
+ public void testNullComponent()
+ {
+ LengthValidator validator = new LengthValidator();
+
+ doTestNullComponent(facesContext, validator);
+ }
+
+ public void testTooLargeLength()
+ {
+ // since the pattern has not been set it will be null
+ // let us push some arbitary value
+ Mock mock = mock(UIComponent.class);
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+ setMockLabelForComponent(wrapper);
+
+ try
+ {
+ LengthValidator validator = new LengthValidator();
+ validator.setMaximum(2);
+ validator.validate(facesContext, component, "someValue");
+ // test fails if it is here
+
+ fail("Expected Null pointer exception");
+ }
+ catch (ValidatorException ve)
+ {
+ // suppress it - this is as expected
+ }
+ mock.verify();
+ }
+
+ public void testExactFailure()
+ {
+ // some very basic sanity test
+ Mock mock = buildMockUIComponent();
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+ setMockLabelForComponent(wrapper);
+
+ try
+ {
+ LengthValidator validator = new LengthValidator();
+ String value = "999999";
+ validator.setMinimum(2);
+ validator.setMaximum(2);
+ validator.validate(facesContext, component, value);
+ fail("Expected ValidatorException for exact");
+ }
+ catch (ValidatorException ve)
+ {
+ // if exception then fine.
+ }
+
+ mock.verify();
+ }
+
+ public void testSanitySuccess()
+ {
+ //some very basic sanity test
+ //
+ LengthValidator validator = new LengthValidator();
+ Mock mock = buildMockUIComponent();
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+
+ String values[] = {"Hallo","world"};
+ validator.setMinimum(2);
+ for (int i = 0; i < values.length ; i++)
+ {
+ doTestValidate(validator, facesContext, wrapper, values[i]);
+ }
+ }
+
+ public void testStateHolderSaveRestore()
+ {
+ LengthValidator validator = new LengthValidator();
+ Mock mock = buildMockUIComponent();
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+
+ validator.setMaximum(5);
+ validator.setMessageDetailMaximum("Validation failed");
+ LengthValidator restoreValidator = new LengthValidator();
+
+ doTestStateHolderSaveRestore(validator, restoreValidator,
+ facesContext, wrapper);
+ }
+}
+ //////////////////////////////////////////////////////////////////////////////
+ // MOCK OBJECTS
+ // 1. Get a MockControl for the interface we would like to simulate
+ // 2. get the MockObject from MockControl
+ // 3. specify the behaviour of the Mock Object (record state)
+ // 4. activate the MockObject via the control (replay state)
+ //
+ //////////////////////////////////////////////////////////////////////////////
\ No newline at end of file
Added: myfaces/trinidad/trunk/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LongRangeValidatorTest.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LongRangeValidatorTest.java?rev=671414&view=auto
==============================================================================
--- myfaces/trinidad/trunk/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LongRangeValidatorTest.java (added)
+++ myfaces/trinidad/trunk/trinidad-api/src/test/java/org/apache/myfaces/trinidad/validator/LongRangeValidatorTest.java Tue Jun 24 21:24:30 2008
@@ -0,0 +1,207 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.trinidad.validator;
+
+import javax.faces.component.UIComponent;
+import javax.faces.validator.ValidatorException;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.myfaces.trinidadbuild.test.MockUIComponentWrapper;
+import org.jmock.Mock;
+
+/**
+ * Unit tests for LongRangeValidator
+ *
+ */
+public class LongRangeValidatorTest extends ValidatorTestCase
+{
+ public LongRangeValidatorTest(String testName)
+ {
+ super(testName);
+ }
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+ }
+
+ @Override
+ protected void tearDown() throws Exception
+ {
+ super.tearDown();
+ }
+
+ public static Test suite()
+ {
+ return new TestSuite(LongRangeValidatorTest.class);
+ }
+
+ /**
+ * Tests that null returns immediately.
+ *
+ * @throws ValidatorException when test fails
+ */
+ public void testNull() throws ValidatorException
+ {
+ Mock mock = buildMockUIComponent();
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+ LongRangeValidator validator = new LongRangeValidator();
+
+ doTestNull(facesContext, wrapper, validator);
+ }
+
+ /**
+ * Test when context is set to null
+ */
+ public void testNullContext()
+ {
+ Mock mock = buildMockUIComponent();
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+ LongRangeValidator validator = new LongRangeValidator();
+
+ doTestNullContext(wrapper, validator);
+ }
+
+ public void testNullComponent()
+ {
+ LongRangeValidator validator = new LongRangeValidator();
+
+ doTestNullComponent(facesContext, validator);
+ }
+
+ public void testTooLarge()
+ {
+ // since the pattern has not been set it will be null
+ // let us push some arbitary value
+ Mock mock = mock(UIComponent.class);
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+ setMockLabelForComponent(wrapper);
+
+ try
+ {
+ LongRangeValidator validator = new LongRangeValidator();
+ validator.setMaximum(100);
+ validator.validate(facesContext, component, 1000);
+ // test fails if it is here
+
+ fail("Expected Null pointer exception");
+ }
+ catch (ValidatorException ve)
+ {
+ // suppress it - this is as expected
+ }
+ mock.verify();
+ }
+
+ public void testWrongType()
+ {
+ // since the pattern has not been set it will be null
+ // let us push some arbitary value
+ Mock mock = mock(UIComponent.class);
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+ setMockLabelForComponent(wrapper);
+
+ try
+ {
+ LongRangeValidator validator = new LongRangeValidator();
+ validator.setMaximum(2);
+ validator.validate(facesContext, component, "thisShouldFail");
+ // test fails if it is here
+
+ fail("Expected Null pointer exception");
+ }
+ catch (ValidatorException ve)
+ {
+ // suppress it - this is as expected
+ }
+ mock.verify();
+ }
+
+ public void testExactFailure()
+ {
+ // some very basic sanity test
+ Mock mock = buildMockUIComponent();
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+ setMockLabelForComponent(wrapper);
+
+ try
+ {
+ LongRangeValidator validator = new LongRangeValidator();
+ long value = 20;
+ validator.setMinimum(2);
+ validator.setMaximum(2);
+ validator.validate(facesContext, component, value);
+ fail("Expected ValidatorException for exact");
+ }
+ catch (ValidatorException ve)
+ {
+ // if exception then fine.
+ }
+
+ mock.verify();
+ }
+
+ public void testSanitySuccess()
+ {
+ //some very basic sanity test
+ //
+ LongRangeValidator validator = new LongRangeValidator();
+ Mock mock = buildMockUIComponent();
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+
+ Long values[] = {200l,500l};
+ validator.setMinimum(2);
+ for (int i = 0; i < values.length ; i++)
+ {
+ doTestValidate(validator, facesContext, wrapper, values[i]);
+ }
+ }
+
+ public void testStateHolderSaveRestore()
+ {
+ LongRangeValidator validator = new LongRangeValidator();
+ Mock mock = buildMockUIComponent();
+ UIComponent component = (UIComponent) mock.proxy();
+ MockUIComponentWrapper wrapper = new MockUIComponentWrapper(mock, component);
+
+ validator.setMaximum(5);
+ validator.setMessageDetailMaximum("Validation failed");
+ LongRangeValidator restoreValidator = new LongRangeValidator();
+
+ doTestStateHolderSaveRestore(validator, restoreValidator,
+ facesContext, wrapper);
+ }
+}
+ //////////////////////////////////////////////////////////////////////////////
+ // MOCK OBJECTS
+ // 1. Get a MockControl for the interface we would like to simulate
+ // 2. get the MockObject from MockControl
+ // 3. specify the behaviour of the Mock Object (record state)
+ // 4. activate the MockObject via the control (replay state)
+ //
+ //////////////////////////////////////////////////////////////////////////////
\ No newline at end of file