http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java new file mode 100644 index 0000000..e7045c2 --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java @@ -0,0 +1,278 @@ +package com.opensymphony.xwork2.validator; + +import com.opensymphony.xwork2.*; +import com.opensymphony.xwork2.config.providers.MockConfigurationProvider; +import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider; +import com.opensymphony.xwork2.util.ValueStack; +import com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +/** + * Unit test for {@link DoubleRangeFieldValidator}. + * + * @author <a href="mailto:[email protected]">Rainer Hermanns</a> + * @author Claus Ibsen + * @version $Id$ + */ +public class DoubleRangeValidatorTest extends XWorkTestCase { + private DoubleRangeFieldValidator val; + + public void testRangeValidationWithError() throws Exception { + //Explicitly set an out-of-range double for DoubleRangeValidatorTest + Map<String, Object> context = new HashMap<>(); + HashMap<String, Object> params = new HashMap<>(); + params.put("percentage", 100.0123d); + context.put(ActionContext.PARAMETERS, params); + + ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, context); + proxy.execute(); + assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors()); + + Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors(); + + List<String> errorMessages = errors.get("percentage"); + assertNotNull("Expected double range validation error message.", errorMessages); + assertEquals(1, errorMessages.size()); + + String errorMessage = errorMessages.get(0); + assertNotNull("Expecting: percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage); + assertEquals("percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage); + } + + public void testRangeValidationNoError() throws Exception { + Map<String, Object> context = new HashMap<>(); + HashMap<String, Object> params = new HashMap<>(); + params.put("percentage", 1.234567d); + context.put(ActionContext.PARAMETERS, params); + + ActionProxy proxy = actionProxyFactory.createActionProxy("", "percentage", context); + proxy.execute(); + assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors()); + + Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors(); + Iterator it = errors.entrySet().iterator(); + + List<String> errorMessages = errors.get("percentage"); + assertNull("Expected no double range validation error message.", errorMessages); + } + + public void testRangeNoExclusiveAndNoValueInStack() throws Exception { + val.setFieldName("hello"); + val.validate("world"); + } + + public void testRangeSimpleDoubleValueInStack() throws Exception { + MyTestProduct prod = new MyTestProduct(); + prod.setName("coca cola"); + prod.setPrice(5.99); + + ValueStack stack = ActionContext.getContext().getValueStack(); + stack.push(prod); + ActionContext.getContext().setValueStack(stack); + + val.setMinInclusive(0d); + val.setMaxInclusive(10d); + val.setFieldName("price"); + val.validate(prod); + } + + public void testRangeRealDoubleValueInStack() throws Exception { + MyTestProduct prod = new MyTestProduct(); + prod.setName("coca cola"); + prod.setPrice(5.99); + prod.setVolume(12.34d); + + ValueStack stack = ActionContext.getContext().getValueStack(); + stack.push(prod); + ActionContext.getContext().setValueStack(stack); + + val.setMinInclusive(0d); + val.setMaxInclusive(30d); + val.setFieldName("volume"); + val.validate(prod); + } + + public void testRangeNotADoubleObjectValueInStack() throws Exception { + MyTestProduct prod = new MyTestProduct(); + prod.setName("coca cola"); + + ValueStack stack = ActionContext.getContext().getValueStack(); + stack.push(prod); + ActionContext.getContext().setValueStack(stack); + + val.setMinInclusive(0d); + val.setMaxInclusive(10d); + val.setFieldName("name"); + + DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport()); + val.setValidatorContext(context); + + val.validate(prod); + + assertEquals(0d, val.getMinInclusive()); + assertEquals(10d, val.getMaxInclusive()); + } + + public void testEdgeOfMaxRange() throws Exception { + MyTestProduct prod = new MyTestProduct(); + prod.setName("coca cola"); + prod.setPrice(9.95); + + ValueStack stack = ActionContext.getContext().getValueStack(); + stack.push(prod); + ActionContext.getContext().setValueStack(stack); + + val.setFieldName("price"); + + DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport()); + val.setValidatorContext(context); + + val.setMaxInclusive(9.95d); + val.validate(prod); // should pass + assertTrue(!context.hasErrors()); + assertEquals(9.95d, val.getMaxInclusive()); + + val.setMaxExclusive(9.95d); + val.validate(prod); // should not pass + assertTrue(context.hasErrors()); + assertEquals(9.95d, val.getMaxExclusive()); + } + + public void testEdgeOfMinRange() throws Exception { + MyTestProduct prod = new MyTestProduct(); + prod.setName("coca cola"); + prod.setPrice(9.95); + + ValueStack stack = ActionContext.getContext().getValueStack(); + stack.push(prod); + ActionContext.getContext().setValueStack(stack); + + val.setFieldName("price"); + + DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport()); + val.setValidatorContext(context); + + val.setMinInclusive(9.95d); + val.validate(prod); // should pass + assertTrue(!context.hasErrors()); + + val.setMinExclusive(9.95d); + val.validate(prod); // should not pass + assertTrue(context.hasErrors()); + } + + public void testNoValue() throws Exception { + ValueStack stack = ActionContext.getContext().getValueStack(); + ActionContext.getContext().setValueStack(stack); + + val.setFieldName("price"); + + DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport()); + val.setValidatorContext(context); + + val.setMinInclusive(9.95d); + val.validate(null); + assertTrue(!context.hasErrors()); // should pass as null value passed in + } + + public void testRangeValidationWithExpressionsFail() throws Exception { + //Explicitly set an out-of-range double for DoubleRangeValidatorTest + Map<String, Object> context = new HashMap<>(); + HashMap<String, Object> params = new HashMap<>(); + params.put("percentage", 100.0123d); + context.put(ActionContext.PARAMETERS, params); + + ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.EXPRESSION_VALIDATION_ACTION, context); + proxy.execute(); + assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors()); + + Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors(); + List<String> errorMessages = errors.get("percentage"); + assertNotNull("Expected double range validation error message.", errorMessages); + assertEquals(1, errorMessages.size()); + + String errorMessage = errorMessages.get(0); + assertNotNull("Expecting: percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage); + assertEquals("percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage); + } + + public void testExpressionParams() throws Exception { + ValueStack stack = ActionContext.getContext().getValueStack(); + ActionSupport action = new ActionSupport() { + + public Double getMinInclusiveValue() {return 10d;} + public Double getMaxInclusiveValue() {return 11d;} + public Double getMinExclusiveValue() {return 13d;} + public Double getMaxExclusiveValue() {return 14d;} + public Double getPrice() {return 15d;} + }; + + stack.push(action); + + val.setMinInclusiveExpression("${minInclusiveValue}"); + val.setMaxInclusiveExpression("${maxInclusiveValue}"); + val.setMinExclusiveExpression("${minExclusiveValue}"); + val.setMaxExclusiveExpression("${maxExclusiveValue}"); + + val.setFieldName("price"); + val.setDefaultMessage("Price is wrong!"); + + DelegatingValidatorContext context = new DelegatingValidatorContext(action); + val.setValidatorContext(context); + + val.validate(action); + assertTrue(action.getFieldErrors().get("price").size() == 1); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlConfigurationProvider provider = new XmlConfigurationProvider("xwork-default.xml"); + container.inject(provider); + loadConfigurationProviders(provider, new MockConfigurationProvider()); + val = new DoubleRangeFieldValidator(); + val.setValueStack(ActionContext.getContext().getValueStack()); + ActionContext.getContext().setParameters(new HashMap<String, Object>()); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + val = null; + } + + private class MyTestProduct { + private double price; + private Double volume; + private String name; + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Double getVolume() { + return volume; + } + + public void setVolume(Double volume) { + this.volume = volume; + } + } + +}
http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/test/java/com/opensymphony/xwork2/validator/EmailValidatorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/EmailValidatorTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/EmailValidatorTest.java new file mode 100644 index 0000000..ca9a549 --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/validator/EmailValidatorTest.java @@ -0,0 +1,154 @@ +/* + * Copyright 2002-2006,2009 The Apache Software Foundation. + * + * Licensed 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 com.opensymphony.xwork2.validator; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionSupport; +import com.opensymphony.xwork2.XWorkTestCase; +import com.opensymphony.xwork2.util.ValueStack; +import com.opensymphony.xwork2.validator.validators.EmailValidator; + +/** + * Test case for Email Validator + * + * @author tm_jee + * @version $Date$ $Id$ + */ +public class EmailValidatorTest extends XWorkTestCase { + + public void testEmailValidity() throws Exception { + assertTrue(verifyEmailValidity("[email protected]")); + assertTrue(verifyEmailValidityWithExpression("[email protected]", "\\b^[a-z]+@[a-z]+(\\.[a-z]+)*\\.com$\\b")); + assertTrue(verifyEmailValidity("[email protected]")); + assertTrue(verifyEmailValidityWithExpression("[email protected]", "\\b^[a-z_]+@[a-z]+(\\.[a-z]+)*\\.co$\\b")); + assertTrue(verifyEmailValidity("[email protected]")); + assertTrue(verifyEmailValidity("[email protected]")); + assertTrue(verifyEmailValidity("[email protected]")); + assertTrue(verifyEmailValidity("[email protected]")); + assertTrue(verifyEmailValidity(" [email protected] ")); + assertTrue(verifyEmailValidity("tm.j'[email protected]")); + assertTrue(verifyEmailValidity("tm.j'e.e'@yahoo.co.uk")); + assertTrue(verifyEmailValidity("tmj'[email protected]")); + assertTrue(verifyEmailValidity("[email protected]")); + assertTrue(verifyEmailValidity("[email protected]")); + + assertFalse(verifyEmailValidity("tm_jee#[email protected]")); + assertFalse(verifyEmailValidity("tm_jee@ yahoo.co.uk")); + assertFalse(verifyEmailValidity("tm_jee @yahoo.co.uk")); + assertFalse(verifyEmailValidity("tm_j ee @yah oo.co.uk")); + assertFalse(verifyEmailValidity("tm_jee @yah oo.co.uk")); + assertFalse(verifyEmailValidity("tm_jee @ yahoo.com")); + assertFalse(verifyEmailValidity(" [email protected]#ame.co.uk ")); + assertFalse(verifyEmailValidity("[email protected]")); + assertFalse(verifyEmailValidity("[email protected]")); + + assertTrue(verifyEmailValidityWithExpression("[email protected]", "\\b^[a-z]+@[a-z]+(\\.[a-z]+)*\\.com$\\b")); + } + + protected boolean verifyEmailValidity(final String email) throws Exception { + ActionSupport action = new ActionSupport() { + public String getMyEmail() { + return email; + } + }; + + EmailValidator validator = new EmailValidator(); + validator.setValidatorContext(new DelegatingValidatorContext(action)); + validator.setFieldName("myEmail"); + validator.setDefaultMessage("invalid email"); + validator.setValueStack(ActionContext.getContext().getValueStack()); + validator.validate(action); + + return (action.getFieldErrors().size() == 0); + } + + public boolean verifyEmailValidityWithExpression(final String email, final String expression) throws Exception { + ActionSupport action = new ActionSupport() { + public String getMyEmail() { + return email; + } + + public String getEmailExpression() { + return expression; + } + }; + + EmailValidator validator = new EmailValidator(); + ValueStack valueStack = ActionContext.getContext().getValueStack(); + valueStack.push(action); + validator.setValueStack(valueStack); + + validator.setValidatorContext(new DelegatingValidatorContext(action)); + validator.setFieldName("myEmail"); + validator.setDefaultMessage("invalid email"); + validator.setRegexExpression("${emailExpression}"); + + validator.validate(action); + + return (action.getFieldErrors().size() == 0); + } + + public void testCaseSensitiveViaExpression() throws Exception { + EmailValidator validator = verifyCaseSensitive(true); + assertTrue(validator.isCaseSensitive()); + + validator = verifyCaseSensitive(false); + assertFalse(validator.isCaseSensitive()); + } + + private EmailValidator verifyCaseSensitive(final boolean caseSensitive) { + ActionSupport action = new ActionSupport() { + public boolean getEmailCaseSensitive() { + return caseSensitive; + } + }; + + EmailValidator validator = new EmailValidator(); + ValueStack valueStack = ActionContext.getContext().getValueStack(); + valueStack.push(action); + validator.setValueStack(valueStack); + + validator.setCaseSensitiveExpression("${emailCaseSensitive}"); + + return validator; + } + + public void testTrimViaExpression() throws Exception { + EmailValidator validator = verifyTrim(true); + assertTrue(validator.isTrimed()); + + validator = verifyTrim(false); + assertFalse(validator.isTrimed()); + } + + private EmailValidator verifyTrim(final boolean trim) { + ActionSupport action = new ActionSupport() { + public boolean getTrimEmail() { + return trim; + } + }; + + EmailValidator validator = new EmailValidator(); + ValueStack valueStack = ActionContext.getContext().getValueStack(); + valueStack.push(action); + validator.setValueStack(valueStack); + + validator.setTrimExpression("${trimEmail}"); + + return validator; + } + +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/test/java/com/opensymphony/xwork2/validator/ExpressionValidatorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/ExpressionValidatorTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/ExpressionValidatorTest.java new file mode 100644 index 0000000..572b316 --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/validator/ExpressionValidatorTest.java @@ -0,0 +1,139 @@ +/* + * Copyright 2002-2006,2009 The Apache Software Foundation. + * + * Licensed 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 com.opensymphony.xwork2.validator; + +import com.mockobjects.dynamic.C; +import com.mockobjects.dynamic.Mock; +import com.opensymphony.xwork2.*; +import com.opensymphony.xwork2.config.entities.ActionConfig; +import com.opensymphony.xwork2.config.providers.MockConfigurationProvider; +import com.opensymphony.xwork2.validator.validators.ExpressionValidator; +import org.easymock.EasyMock; + +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Unit test for ExpressionValidator. + * + * @author Jason Carreira + * @author Claus Ibsen + */ +public class ExpressionValidatorTest extends XWorkTestCase { + + public void testExpressionValidationOfStringLength() throws ValidationException { + TestBean bean = new TestBean(); + bean.setName("abc"); + ActionContext.getContext().getValueStack().push(bean); + + DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport()); + container.getInstance(ActionValidatorManager.class).validate(bean, "expressionValidation", context); + assertTrue(context.hasFieldErrors()); + + final Map fieldErrors = context.getFieldErrors(); + assertTrue(fieldErrors.containsKey("name")); + + List nameErrors = (List) fieldErrors.get("name"); + assertEquals(1, nameErrors.size()); + assertEquals("Name must be greater than 5 characters, it is currently 'abc'", nameErrors.get(0)); + + bean.setName("abcdefg"); + context = new DelegatingValidatorContext(new ValidationAwareSupport()); + container.getInstance(ActionValidatorManager.class).validate(bean, "expressionValidation", context); + assertFalse(context.hasFieldErrors()); + } + + public void testExpressionValidatorFailure() throws Exception { + HashMap<String, Object> params = new HashMap<>(); + params.put("date", "12/23/2002"); + params.put("foo", "5"); + params.put("bar", "7"); + + HashMap<String, Object> extraContext = new HashMap<>(); + extraContext.put(ActionContext.PARAMETERS, params); + + ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, extraContext); + proxy.execute(); + assertTrue(((ValidationAware) proxy.getAction()).hasActionErrors()); + + Collection errors = ((ValidationAware) proxy.getAction()).getActionErrors(); + assertEquals(1, errors.size()); + + String message = (String) errors.iterator().next(); + assertNotNull(message); + assertEquals("Foo must be greater than Bar. Foo = 5, Bar = 7.", message); + } + + public void testExpressionValidatorSuccess() throws Exception { + HashMap<String, Object> params = new HashMap<String, Object>(); + + //make it not fail + params.put("date", "12/23/2002"); + params.put("foo", "10"); + params.put("bar", "7"); + + HashMap<String, Object> extraContext = new HashMap<>(); + extraContext.put(ActionContext.PARAMETERS, params); + + ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, extraContext); + proxy.execute(); + assertFalse(((ValidationAware) proxy.getAction()).hasActionErrors()); + } + + public void testGetSetExpresion() { + ExpressionValidator ev = new ExpressionValidator(); + ev.setExpression("{top}"); + assertEquals("{top}", ev.getExpression()); + } + + public void testNoBooleanExpression() throws Exception { + Mock mock = new Mock(ValidationAware.class); + mock.expect("addActionError", C.ANY_ARGS); + + ExpressionValidator ev = new ExpressionValidator(); + ev.setValidatorContext(new DelegatingValidatorContext(mock.proxy())); + ev.setExpression("{top}"); + ev.setValueStack(ActionContext.getContext().getValueStack()); + ev.validate("Hello"); // {top} will evalute to Hello that is not a Boolean + mock.verify(); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + + loadConfigurationProviders(new MockConfigurationProvider()); + + ActionConfig config = new ActionConfig.Builder("", "name", "").build(); + ActionInvocation invocation = EasyMock.createNiceMock(ActionInvocation.class); + ActionProxy proxy = EasyMock.createNiceMock(ActionProxy.class); + + EasyMock.expect(invocation.getProxy()).andReturn(proxy).anyTimes(); + EasyMock.expect(invocation.getAction()).andReturn(null).anyTimes(); + EasyMock.expect(invocation.invoke()).andReturn(Action.SUCCESS).anyTimes(); + EasyMock.expect(proxy.getMethod()).andReturn("execute").anyTimes(); + EasyMock.expect(proxy.getConfig()).andReturn(config).anyTimes(); + + + EasyMock.replay(invocation); + EasyMock.replay(proxy); + + ActionContext.getContext().setActionInvocation(invocation); + } + +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/test/java/com/opensymphony/xwork2/validator/GenericValidatorContext.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/GenericValidatorContext.java b/core/src/test/java/com/opensymphony/xwork2/validator/GenericValidatorContext.java new file mode 100644 index 0000000..6273259 --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/validator/GenericValidatorContext.java @@ -0,0 +1,143 @@ +/* + * Copyright 2002-2003,2009 The Apache Software Foundation. + * + * Licensed 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 com.opensymphony.xwork2.validator; + +import java.util.*; + + +/** + * Dummy validator context to use to capture error messages. + * + * @author Mark Woon + * @author Matthew Payne + */ +public class GenericValidatorContext extends DelegatingValidatorContext { + + private Collection<String> actionErrors; + private Collection<String> actionMessages; + private Map<String, List<String>> fieldErrors; + + + public GenericValidatorContext(Object object) { + super(object); + } + + + @Override + public synchronized void setActionErrors(Collection<String> errorMessages) { + this.actionErrors = errorMessages; + } + + @Override + public synchronized Collection<String> getActionErrors() { + return new ArrayList<>(internalGetActionErrors()); + } + + @Override + public synchronized void setActionMessages(Collection<String> messages) { + this.actionMessages = messages; + } + + @Override + public synchronized Collection<String> getActionMessages() { + return new ArrayList<String>(internalGetActionMessages()); + } + + @Override + public synchronized void setFieldErrors(Map<String, List<String>> errorMap) { + this.fieldErrors = errorMap; + } + + /** + * Get the field specific errors. + * + * @return an unmodifiable Map with errors mapped from fieldname (String) to Collection of String error messages + */ + @Override + public synchronized Map<String, List<String>> getFieldErrors() { + return new HashMap<String, List<String>>(internalGetFieldErrors()); + } + + @Override + public synchronized void addActionError(String anErrorMessage) { + internalGetActionErrors().add(anErrorMessage); + } + + /** + * Add an Action level message to this Action + */ + @Override + public void addActionMessage(String aMessage) { + internalGetActionMessages().add(aMessage); + } + + @Override + public synchronized void addFieldError(String fieldName, String errorMessage) { + final Map<String, List<String>> errors = internalGetFieldErrors(); + List<String> thisFieldErrors = errors.get(fieldName); + + if (thisFieldErrors == null) { + thisFieldErrors = new ArrayList<>(); + errors.put(fieldName, thisFieldErrors); + } + + thisFieldErrors.add(errorMessage); + } + + @Override + public synchronized boolean hasActionErrors() { + return (actionErrors != null) && !actionErrors.isEmpty(); + } + + /** + * Note that this does not have the same meaning as in WW 1.x + * + * @return (hasActionErrors() || hasFieldErrors()) + */ + @Override + public synchronized boolean hasErrors() { + return (hasActionErrors() || hasFieldErrors()); + } + + @Override + public synchronized boolean hasFieldErrors() { + return (fieldErrors != null) && !fieldErrors.isEmpty(); + } + + private Collection<String> internalGetActionErrors() { + if (actionErrors == null) { + actionErrors = new ArrayList<>(); + } + + return actionErrors; + } + + private Collection<String> internalGetActionMessages() { + if (actionMessages == null) { + actionMessages = new ArrayList<>(); + } + + return actionMessages; + } + + private Map<String, List<String>> internalGetFieldErrors() { + if (fieldErrors == null) { + fieldErrors = new HashMap<>(); + } + + return fieldErrors; + } +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/test/java/com/opensymphony/xwork2/validator/IntRangeValidatorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/IntRangeValidatorTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/IntRangeValidatorTest.java new file mode 100644 index 0000000..9c0281b --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/validator/IntRangeValidatorTest.java @@ -0,0 +1,70 @@ +/* + * Copyright 2002-2003,2009 The Apache Software Foundation. + * + * Licensed 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 com.opensymphony.xwork2.validator; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionProxy; +import com.opensymphony.xwork2.ValidationAware; +import com.opensymphony.xwork2.XWorkTestCase; +import com.opensymphony.xwork2.config.providers.MockConfigurationProvider; +import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +/** + * IntRangeValidatorTest + * <p/> + * Created : Jan 21, 2003 12:16:01 AM + * + * @author Jason Carreira + */ +public class IntRangeValidatorTest extends XWorkTestCase { + + public void testRangeValidation() { + HashMap<String, String> params = new HashMap<>(); + params.put("bar", "5"); + + HashMap<String, Object> extraContext = new HashMap<>(); + extraContext.put(ActionContext.PARAMETERS, params); + + try { + ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, extraContext); + proxy.execute(); + assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors()); + + Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors(); + List<String> errorMessages = errors.get("bar"); + assertEquals(1, errorMessages.size()); + + String errorMessage = errorMessages.get(0); + assertNotNull(errorMessage); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlConfigurationProvider provider = new XmlConfigurationProvider("xwork-test-beans.xml"); + container.inject(provider); + loadConfigurationProviders(provider, new MockConfigurationProvider()); + } +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/test/java/com/opensymphony/xwork2/validator/LongRangeValidatorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/LongRangeValidatorTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/LongRangeValidatorTest.java new file mode 100644 index 0000000..dfed43e --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/validator/LongRangeValidatorTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2002-2003,2009 The Apache Software Foundation. + * + * Licensed 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 com.opensymphony.xwork2.validator; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionProxy; +import com.opensymphony.xwork2.ValidationAware; +import com.opensymphony.xwork2.XWorkTestCase; +import com.opensymphony.xwork2.config.providers.MockConfigurationProvider; +import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +/** + * LongRangeValidatorTest + * <p/> + * + */ +public class LongRangeValidatorTest extends XWorkTestCase { + + public void testRangeValidation() { + HashMap<String, String> params = new HashMap<>(); + params.put("longFoo", "200"); + + HashMap<String, Object> extraContext = new HashMap<>(); + extraContext.put(ActionContext.PARAMETERS, params); + + try { + ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, extraContext); + proxy.execute(); + assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors()); + + Map errors = ((ValidationAware) proxy.getAction()).getFieldErrors(); + List errorMessages = (List) errors.get("longFoo"); + assertEquals(1, errorMessages.size()); + + String errorMessage = (String) errorMessages.get(0); + assertNotNull(errorMessage); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlConfigurationProvider provider = new XmlConfigurationProvider("xwork-test-beans.xml"); + container.inject(provider); + loadConfigurationProviders(provider, new MockConfigurationProvider()); + } +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/test/java/com/opensymphony/xwork2/validator/ModelDrivenValidationTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/ModelDrivenValidationTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/ModelDrivenValidationTest.java new file mode 100644 index 0000000..1ced1f5 --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/validator/ModelDrivenValidationTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2002-2003,2009 The Apache Software Foundation. + * + * Licensed 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 com.opensymphony.xwork2.validator; + +import com.opensymphony.xwork2.*; +import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +/** + * ModelDrivenValidationTest + * + * @author Jason Carreira + * Created Oct 1, 2003 10:08:25 AM + */ +public class ModelDrivenValidationTest extends XWorkTestCase { + + public void testModelDrivenValidation() throws Exception { + Map<String, Object> params = new HashMap<>(); + params.put("count", new String[]{"11"}); + + Map<String, Object> context = new HashMap<>(); + context.put(ActionContext.PARAMETERS, params); + + XmlConfigurationProvider provider = new XmlConfigurationProvider("xwork-sample.xml"); + container.inject(provider); + loadConfigurationProviders(provider); + ActionProxy proxy = actionProxyFactory.createActionProxy(null, "TestModelDrivenValidation", context); + assertEquals(Action.SUCCESS, proxy.execute()); + + ModelDrivenAction action = (ModelDrivenAction) proxy.getAction(); + assertTrue(action.hasFieldErrors()); + assertTrue(action.getFieldErrors().containsKey("count")); + assertEquals("count must be between 1 and 10, current value is 11.", ((List) action.getFieldErrors().get("count")).get(0)); + } + +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/test/java/com/opensymphony/xwork2/validator/MyValidator.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/MyValidator.java b/core/src/test/java/com/opensymphony/xwork2/validator/MyValidator.java new file mode 100644 index 0000000..82f1f2b --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/validator/MyValidator.java @@ -0,0 +1,96 @@ +package com.opensymphony.xwork2.validator; + +import com.opensymphony.xwork2.util.ValueStack; + +/** + * TODO lukaszlenart: write a JavaDoc + */ +public class MyValidator implements FieldValidator, ShortCircuitableValidator { + + private String message; + private String fieldName; + private String key; + private String[] messageParameters; + private ValidatorContext validatorContext; + private String type; + private ValueStack stack; + private boolean shortcircuit; + + private int value; + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } + + public void setFieldName(String fieldName) { + this.fieldName = fieldName; + } + + public String getFieldName() { + return fieldName; + } + + public void setDefaultMessage(String message) { + this.message = message; + } + + public String getDefaultMessage() { + return message; + } + + public String getMessage(Object object) { + return "Message"; + } + + public void setMessageKey(String key) { + this.key = key; + } + + public String getMessageKey() { + return key; + } + + public void setMessageParameters(String[] messageParameters) { + this.messageParameters = messageParameters; + } + + public String[] getMessageParameters() { + return messageParameters; + } + + public void setValidatorContext(ValidatorContext validatorContext) { + this.validatorContext = validatorContext; + } + + public ValidatorContext getValidatorContext() { + return validatorContext; + } + + public void validate(Object object) throws ValidationException { + // pass + } + + public void setValidatorType(String type) { + this.type = type; + } + + public String getValidatorType() { + return type; + } + + public void setValueStack(ValueStack stack) { + this.stack = stack; + } + + public void setShortCircuit(boolean shortcircuit) { + this.shortcircuit = shortcircuit; + } + + public boolean isShortCircuit() { + return shortcircuit; + } +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/test/java/com/opensymphony/xwork2/validator/RegexFieldValidatorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/RegexFieldValidatorTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/RegexFieldValidatorTest.java new file mode 100644 index 0000000..bebf255 --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/validator/RegexFieldValidatorTest.java @@ -0,0 +1,195 @@ +/* + * Copyright 2002-2006,2009 The Apache Software Foundation. + * + * Licensed 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 com.opensymphony.xwork2.validator; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.XWorkTestCase; +import com.opensymphony.xwork2.util.ValueStack; +import com.opensymphony.xwork2.validator.validators.RegexFieldValidator; + +import java.util.List; + +/** + * Unit test for RegexFieldValidator. + * <p/> + * This unit test is only to test that the regex field validator works, not to + * unit test the build in reg.exp from JDK. That is why the expressions are so simple. + * + * @author Claus Ibsen + */ +public class RegexFieldValidatorTest extends XWorkTestCase { + + public void testMatch() throws Exception { + MyTestPerson testPerson = new MyTestPerson(); + testPerson.setUsername("Secret"); + + ValueStack stack = ActionContext.getContext().getValueStack(); + ActionContext.getContext().setValueStack(stack); + + RegexFieldValidator validator = new RegexFieldValidator(); + validator.setRegex("^Sec.*"); + validator.setValidatorContext(new GenericValidatorContext(new Object())); + validator.setFieldName("username"); + validator.setValueStack(ActionContext.getContext().getValueStack()); + validator.validate(testPerson); + + assertFalse(validator.getValidatorContext().hasErrors()); + assertFalse(validator.getValidatorContext().hasActionErrors()); + assertFalse(validator.getValidatorContext().hasActionMessages()); + assertFalse(validator.getValidatorContext().hasFieldErrors()); + } + + public void testMatchNoTrim() throws Exception { + MyTestPerson testPerson = new MyTestPerson(); + testPerson.setUsername("Secret "); // must end with one whitespace + + ValueStack stack = ActionContext.getContext().getValueStack(); + ActionContext.getContext().setValueStack(stack); + + RegexFieldValidator validator = new RegexFieldValidator(); + validator.setTrim(false); + validator.setRegex("^Sec.*\\s"); + validator.setValidatorContext(new GenericValidatorContext(new Object())); + validator.setFieldName("username"); + validator.setValueStack(ActionContext.getContext().getValueStack()); + validator.validate(testPerson); + + assertFalse(validator.getValidatorContext().hasErrors()); + assertFalse(validator.getValidatorContext().hasActionErrors()); + assertFalse(validator.getValidatorContext().hasActionMessages()); + assertFalse(validator.getValidatorContext().hasFieldErrors()); + } + + public void testFail() throws Exception { + MyTestPerson testPerson = new MyTestPerson(); + testPerson.setUsername("Superman"); + + ValueStack stack = ActionContext.getContext().getValueStack(); + ActionContext.getContext().setValueStack(stack); + + RegexFieldValidator validator = new RegexFieldValidator(); + validator.setRegex("^Sec.*"); + validator.setValidatorContext(new GenericValidatorContext(new Object())); + validator.setFieldName("username"); + validator.setValueStack(ActionContext.getContext().getValueStack()); + validator.validate(testPerson); + + assertTrue(validator.getValidatorContext().hasErrors()); + assertTrue(validator.getValidatorContext().hasFieldErrors()); + List<String> msgs = validator.getValidatorContext().getFieldErrors().get("username"); + assertNotNull(msgs); + assertTrue(msgs.size() == 1); // should contain 1 error message + + // when failing the validator will not add action errors/msg + assertFalse(validator.getValidatorContext().hasActionErrors()); + assertFalse(validator.getValidatorContext().hasActionMessages()); + } + + public void testNoFieldName() throws Exception { + MyTestPerson testPerson = new MyTestPerson(); + testPerson.setUsername("NoExpression"); + + ValueStack stack = ActionContext.getContext().getValueStack(); + ActionContext.getContext().setValueStack(stack); + + RegexFieldValidator validator = new RegexFieldValidator(); + validator.setRegex("^Sec.*"); + validator.setValidatorContext(new GenericValidatorContext(new Object())); + validator.setFieldName(null); + validator.setValueStack(ActionContext.getContext().getValueStack()); + validator.validate(testPerson); + + assertFalse(validator.getValidatorContext().hasErrors()); + assertFalse(validator.getValidatorContext().hasActionErrors()); + assertFalse(validator.getValidatorContext().hasActionMessages()); + assertFalse(validator.getValidatorContext().hasFieldErrors()); + } + + public void testGetExpression() throws Exception { + RegexFieldValidator validator = new RegexFieldValidator(); + validator.setRegex("^Hello.*"); + assertEquals("^Hello.*", validator.getRegex()); + } + + public void testIsTrimmed() throws Exception { + RegexFieldValidator validator = new RegexFieldValidator(); + assertEquals(true, validator.isTrimed()); + validator.setTrim(false); + assertEquals(false, validator.isTrimed()); + } + + public void testEmptyName() throws Exception { + MyTestPerson testPerson = new MyTestPerson(); + testPerson.setUsername(""); + + ValueStack stack = ActionContext.getContext().getValueStack(); + ActionContext.getContext().setValueStack(stack); + + RegexFieldValidator validator = new RegexFieldValidator(); + validator.setRegex("^Sec.*"); + validator.setValidatorContext(new GenericValidatorContext(new Object())); + validator.setFieldName("username"); + validator.setValueStack(ActionContext.getContext().getValueStack()); + validator.validate(testPerson); + + assertFalse(validator.getValidatorContext().hasErrors()); + assertFalse(validator.getValidatorContext().hasActionErrors()); + assertFalse(validator.getValidatorContext().hasActionMessages()); + assertFalse(validator.getValidatorContext().hasFieldErrors()); + } + + public void testNoStringField() throws Exception { + MyTestPerson testPerson = new MyTestPerson(); + testPerson.setAge(33); + + ValueStack stack = ActionContext.getContext().getValueStack(); + ActionContext.getContext().setValueStack(stack); + + RegexFieldValidator validator = new RegexFieldValidator(); + validator.setRegex("[0-9][0-9]"); + validator.setValidatorContext(new GenericValidatorContext(new Object())); + validator.setFieldName("age"); + validator.setValueStack(ActionContext.getContext().getValueStack()); + validator.validate(testPerson); + + assertFalse(validator.getValidatorContext().hasErrors()); + assertFalse(validator.getValidatorContext().hasActionErrors()); + assertFalse(validator.getValidatorContext().hasActionMessages()); + assertFalse(validator.getValidatorContext().hasFieldErrors()); + } + + private class MyTestPerson { + private String username; + private int age; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + } + +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/test/java/com/opensymphony/xwork2/validator/RepopulateConversionErrorFieldValidatorSupportTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/RepopulateConversionErrorFieldValidatorSupportTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/RepopulateConversionErrorFieldValidatorSupportTest.java new file mode 100644 index 0000000..308ed2a --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/validator/RepopulateConversionErrorFieldValidatorSupportTest.java @@ -0,0 +1,129 @@ +/* + * Copyright 2002-2006,2009 The Apache Software Foundation. + * + * Licensed 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 com.opensymphony.xwork2.validator; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionSupport; +import com.opensymphony.xwork2.XWorkTestCase; +import com.opensymphony.xwork2.mock.MockActionInvocation; +import com.opensymphony.xwork2.util.ValueStack; +import com.opensymphony.xwork2.validator.validators.RepopulateConversionErrorFieldValidatorSupport; + +import java.util.Map; + +/** + * Test RepopulateConversionErrorFieldValidatorSupport. + * + * @author tm_jee + * @version $Date$ $Id$ + */ +public class RepopulateConversionErrorFieldValidatorSupportTest extends XWorkTestCase { + + + InternalRepopulateConversionErrorFieldValidatorSupport validator1; + InternalRepopulateConversionErrorFieldValidatorSupport validator2; + ActionSupport action; + + public void testUseFullFieldName() throws Exception { + validator2.setRepopulateField(true); + validator2.validate(action); + + ActionContext.getContext().getActionInvocation().invoke(); + Object valueFromStack1 = ActionContext.getContext().getValueStack().findValue("someFieldName", String.class); + Object valueFromStack2 = ActionContext.getContext().getValueStack().findValue("xxxsomeFieldName", String.class); + + assertNull(valueFromStack1); + assertEquals(valueFromStack2, "some value"); + } + + public void testGetterSetterGetsCalledApropriately1() throws Exception { + + validator1.setRepopulateField(true); + validator1.validate(action); + + + ActionContext.getContext().getActionInvocation().invoke(); + + Object valueFromStack = ActionContext.getContext().getValueStack().findValue("someFieldName", String.class); + + assertEquals(valueFromStack, "some value"); + } + + + public void testGetterSetterGetsCalledApropriately2() throws Exception { + + validator1.setRepopulateField(false); + validator1.validate(action); + + + ActionContext.getContext().getActionInvocation().invoke(); + + Object valueFromStack = ActionContext.getContext().getValueStack().findValue("someFieldName", String.class); + + assertEquals(valueFromStack, null); + } + + + @Override + protected void setUp() throws Exception { + super.setUp(); + ValueStack stack = ActionContext.getContext().getValueStack(); + MockActionInvocation invocation = new MockActionInvocation(); + invocation.setStack(stack); + ActionContext.getContext().setValueStack(stack); + ActionContext.getContext().setActionInvocation(invocation); + + String[] conversionErrorValue = new String[] { "some value" }; + Map<String, Object> conversionErrors = ActionContext.getContext().getConversionErrors(); + conversionErrors.put("someFieldName", conversionErrorValue); + conversionErrors.put("xxxsomeFieldName", conversionErrorValue); + + action = new ActionSupport(); + validator1 = + new InternalRepopulateConversionErrorFieldValidatorSupport(); + validator1.setFieldName("someFieldName"); + validator1.setValidatorContext(new DelegatingValidatorContext(action)); + + validator2 = + new InternalRepopulateConversionErrorFieldValidatorSupport(); + validator2.setFieldName("someFieldName"); + validator2.setValidatorContext(new DelegatingValidatorContext(action) { + @Override + public String getFullFieldName(String fieldName) { + return "xxx"+fieldName; + } + }); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + validator1 = null; + action = null; + } + + + // === inner class ============ + + class InternalRepopulateConversionErrorFieldValidatorSupport extends RepopulateConversionErrorFieldValidatorSupport { + public boolean doValidateGetsCalled = false; + + @Override + protected void doValidate(Object object) throws ValidationException { + doValidateGetsCalled = true; + } + } +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/test/java/com/opensymphony/xwork2/validator/ShortRangeValidatorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/ShortRangeValidatorTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/ShortRangeValidatorTest.java new file mode 100644 index 0000000..22909df --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/validator/ShortRangeValidatorTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2002-2003,2009 The Apache Software Foundation. + * + * Licensed 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 com.opensymphony.xwork2.validator; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionProxy; +import com.opensymphony.xwork2.ValidationAware; +import com.opensymphony.xwork2.XWorkTestCase; +import com.opensymphony.xwork2.config.providers.MockConfigurationProvider; +import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +/** + * ShortRangeValidatorTest + * <p/> + * + */ +public class ShortRangeValidatorTest extends XWorkTestCase { + + public void testRangeValidation() { + HashMap<String, Object> params = new HashMap<>(); + params.put("shortFoo", "200"); + + HashMap<String, Object> extraContext = new HashMap<>(); + extraContext.put(ActionContext.PARAMETERS, params); + + try { + ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, extraContext); + proxy.execute(); + assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors()); + + Map errors = ((ValidationAware) proxy.getAction()).getFieldErrors(); + List errorMessages = (List) errors.get("shortFoo"); + assertEquals(1, errorMessages.size()); + + String errorMessage = (String) errorMessages.get(0); + assertNotNull(errorMessage); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlConfigurationProvider provider = new XmlConfigurationProvider("xwork-test-beans.xml"); + container.inject(provider); + loadConfigurationProviders(provider, new MockConfigurationProvider()); + } +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/test/java/com/opensymphony/xwork2/validator/SimpleActionValidationTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/SimpleActionValidationTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/SimpleActionValidationTest.java new file mode 100644 index 0000000..dc1fad8 --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/validator/SimpleActionValidationTest.java @@ -0,0 +1,231 @@ +/* + * Copyright 2002-2003,2009 The Apache Software Foundation. + * + * Licensed 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 com.opensymphony.xwork2.validator; + +import com.opensymphony.xwork2.*; +import com.opensymphony.xwork2.config.providers.MockConfigurationProvider; +import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider; +import com.opensymphony.xwork2.util.ValueStack; +import com.opensymphony.xwork2.validator.validators.ValidatorSupport; + +import java.util.*; + + +/** + * SimpleActionValidationTest + * <p/> + * Created : Jan 20, 2003 11:04:25 PM + * + * @author Jason Carreira + */ +public class SimpleActionValidationTest extends XWorkTestCase { + + public void testAliasValidation() { + HashMap<String, Object> params = new HashMap<>(); + params.put("baz", "10"); + + //valid values + params.put("bar", "7"); + params.put("date", "12/23/2002"); + params.put("percentage", "1.23456789"); + + HashMap<String, Object> extraContext = new HashMap<String, Object>(); + extraContext.put(ActionContext.PARAMETERS, params); + + try { + ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, extraContext); + proxy.execute(); + + ValidationAware validationAware = (ValidationAware) proxy.getAction(); + assertFalse(validationAware.hasFieldErrors()); + + // put in an out-of-range value to see if the old validators still work + ActionContext.setContext(new ActionContext(new HashMap<String, Object>())); + params.put("bar", "42"); + proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ALIAS_NAME, extraContext); + proxy.execute(); + validationAware = (ValidationAware) proxy.getAction(); + assertTrue(validationAware.hasFieldErrors()); + + Map<String, List<String>> errors = validationAware.getFieldErrors(); + assertTrue(errors.containsKey("baz")); + + List<String> bazErrors = errors.get("baz"); + assertEquals(1, bazErrors.size()); + + String message = bazErrors.get(0); + assertEquals("baz out of range.", message); + assertTrue(errors.containsKey("bar")); + + List<String> barErrors = errors.get("bar"); + assertEquals(1, barErrors.size()); + message = barErrors.get(0); + assertEquals("bar must be between 6 and 10, current value is 42.", message); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } + + public void testLookingUpFieldNameAsTextKey() { + HashMap<String, Object> params = new HashMap<>(); + + // should cause a message + params.put("baz", "-1"); + + //valid values + params.put("bar", "7"); + + HashMap<String, Object> extraContext = new HashMap<>(); + extraContext.put(ActionContext.PARAMETERS, params); + + try { + ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, extraContext); + proxy.execute(); + assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors()); + + Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors(); + List<String> bazErrors = errors.get("baz"); + assertEquals(1, bazErrors.size()); + + String errorMessage = bazErrors.get(0); + assertNotNull(errorMessage); + assertEquals("Baz Field must be greater than 0", errorMessage); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } + + public void testMessageKey() { + HashMap<String, Object> params = new HashMap<>(); + params.put("foo", "200"); + + HashMap<String, Object> extraContext = new HashMap<>(); + extraContext.put(ActionContext.PARAMETERS, params); + + try { + ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, extraContext); + ValueStack stack = ActionContext.getContext().getValueStack(); + ActionContext.setContext(new ActionContext(stack.getContext())); + ActionContext.getContext().setLocale(Locale.US); + proxy.execute(); + assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors()); + + Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors(); + List<String> fooErrors = errors.get("foo"); + assertEquals(1, fooErrors.size()); + + String errorMessage = fooErrors.get(0); + assertNotNull(errorMessage); + assertEquals("Foo Range Message", errorMessage); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } + + public void testMessageKeyIsReturnedIfNoOtherDefault() throws ValidationException { + Validator validator = new ValidatorSupport() { + public void validate(Object object) throws ValidationException { + addActionError(object); + } + }; + validator.setValueStack(ActionContext.getContext().getValueStack()); + + String messageKey = "does.not.exist"; + validator.setMessageKey(messageKey); + + ValidatorContext validatorContext = new DelegatingValidatorContext(new SimpleAction()); + validator.setValidatorContext(validatorContext); + validator.validate(this); + assertTrue(validatorContext.hasActionErrors()); + + Collection<String> errors = validatorContext.getActionErrors(); + assertEquals(1, errors.size()); + assertEquals(messageKey, errors.toArray()[0]); + } + + public void testParamterizedMessage() { + HashMap<String, Object> params = new HashMap<>(); + params.put("bar", "42"); + + HashMap<String, Object> extraContext = new HashMap<>(); + extraContext.put(ActionContext.PARAMETERS, params); + + try { + ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, extraContext); + proxy.execute(); + assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors()); + + Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors(); + List<String> barErrors = errors.get("bar"); + assertEquals(1, barErrors.size()); + + String errorMessage = barErrors.get(0); + assertNotNull(errorMessage); + assertEquals("bar must be between 6 and 10, current value is 42.", errorMessage); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } + + public void testSubPropertiesAreValidated() { + HashMap<String, Object> params = new HashMap<>(); + params.put("baz", "10"); + + //valid values + params.put("foo", "8"); + params.put("bar", "7"); + params.put("date", "12/23/2002"); + + params.put("bean.name", "Name should be valid"); + + // this should cause a message + params.put("bean.count", "100"); + + HashMap<String, Object> extraContext = new HashMap<>(); + extraContext.put(ActionContext.PARAMETERS, params); + + try { + ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_SUBPROPERTY_NAME, extraContext); + proxy.execute(); + assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors()); + + Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors(); + List<String> beanCountErrors = errors.get("bean.count"); + assertEquals(1, beanCountErrors.size()); + + String errorMessage = beanCountErrors.get(0); + assertNotNull(errorMessage); + assertEquals("bean.count out of range.", errorMessage); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + + XmlConfigurationProvider provider = new XmlConfigurationProvider("xwork-test-beans.xml"); + container.inject(provider); + loadConfigurationProviders(provider, new MockConfigurationProvider()); + } + +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/test/java/com/opensymphony/xwork2/validator/StringLengthFieldValidatorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/StringLengthFieldValidatorTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/StringLengthFieldValidatorTest.java new file mode 100644 index 0000000..1fae0a9 --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/validator/StringLengthFieldValidatorTest.java @@ -0,0 +1,220 @@ +/* + * Copyright 2002-2006,2009 The Apache Software Foundation. + * + * Licensed 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 com.opensymphony.xwork2.validator; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionSupport; +import com.opensymphony.xwork2.XWorkTestCase; +import com.opensymphony.xwork2.util.ValueStack; +import com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator; + +/** + * + * @author tm_jee + * @version $Date$ $Id$ + */ +public class StringLengthFieldValidatorTest extends XWorkTestCase { + + protected InternalActionSupport action; + protected StringLengthFieldValidator validator; + + public void testStringLengthEmptyNoTrim1() throws Exception { + action.setMyField(""); + + validator.setTrim(false); + validator.validate(action); + + assertEquals(action.getMyField(), ""); + assertFalse(action.hasFieldErrors()); + } + + public void testStringLengthNullNoTrim() throws Exception { + action.setMyField(null); + + validator.setTrim(false); + validator.validate(action); + + assertEquals(action.getMyField(), null); + assertFalse(action.hasFieldErrors()); + } + + public void testStringLengthEmptyTrim1() throws Exception { + action.setMyField(" "); + + validator.setTrim(true); + validator.validate(action); + + assertEquals(action.getMyField(), " "); + assertFalse(action.hasFieldErrors()); + } + + public void testStringLengthEmptyNoTrim2() throws Exception { + action.setMyField(" "); + + validator.setTrim(false); + validator.validate(action); + + assertEquals(action.getMyField(), " "); + assertTrue(action.hasFieldErrors()); + } + + + public void testStringLengthNullTrim() throws Exception { + action.setMyField(null); + + validator.setTrim(true); + validator.validate(action); + + assertEquals(action.getMyField(), null); + assertFalse(action.hasFieldErrors()); + } + + public void testInvalidStringLengthNoTrim() throws Exception { + action.setMyField("abcdefghijklmn"); + + validator.setTrim(false); + validator.validate(action); + + assertEquals(action.getMyField(), "abcdefghijklmn"); + assertTrue(action.hasFieldErrors()); + } + + public void testInvalidStringLengthTrim() throws Exception { + action.setMyField("abcdefghijklmn "); + + validator.setTrim(true); + validator.validate(action); + + assertEquals(action.getMyField(), "abcdefghijklmn "); + assertTrue(action.hasFieldErrors()); + } + + public void testValidStringLengthNoTrim() throws Exception { + action.setMyField(" "); + + validator.setTrim(false); + validator.validate(action); + + assertEquals(action.getMyField(), " "); + assertFalse(action.hasFieldErrors()); + } + + public void testValidStringLengthTrim() throws Exception { + action.setMyField("asd "); + + validator.setTrim(true); + validator.validate(action); + + assertEquals(action.getMyField(), "asd "); + assertFalse(action.hasFieldErrors()); + } + + public void testMinLengthViaExpression() throws Exception { + assertEquals(2, validator.getMinLength()); + action.setMinLengthValue(10); + + validator.setMinLengthExpression("${minLengthValue}"); + + assertEquals(10, validator.getMinLength()); + } + + public void testMaxLengthViaExpression() throws Exception { + assertEquals(5, validator.getMaxLength()); + action.setMaxLengthValue(100); + + validator.setMaxLengthExpression("${maxLengthValue}"); + + assertEquals(100, validator.getMaxLength()); + } + + public void testTrimViaExpression() throws Exception { + assertTrue(validator.isTrim()); + action.setTrimValue(false); + + validator.setTrimExpression("${trimValue}"); + + assertFalse(validator.isTrim()); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + + action = new InternalActionSupport(); + ValueStack valueStack = ActionContext.getContext().getValueStack(); + valueStack.push(action); + + validator = new StringLengthFieldValidator(); + validator.setFieldName("myField"); + validator.setMessageKey("error"); + validator.setValidatorContext(new DelegatingValidatorContext(action)); + validator.setMaxLength(5); + validator.setMinLength(2); + validator.setValueStack(valueStack); + } + + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + action = null; + validator = null; + } + + public static class InternalActionSupport extends ActionSupport { + + private static final long serialVersionUID = 1L; + + private String myField; + private boolean trimValue; + private int minLengthValue; + private int maxLengthValue; + + public String getMyField() { + return this.myField; + } + + public void setMyField(String myField) { + this.myField = myField; + } + + public boolean isTrimValue() { + return trimValue; + } + + public void setTrimValue(boolean trimValue) { + this.trimValue = trimValue; + } + + public int getMinLengthValue() { + return minLengthValue; + } + + public void setMinLengthValue(int minLengthValue) { + this.minLengthValue = minLengthValue; + } + + public int getMaxLengthValue() { + return maxLengthValue; + } + + public void setMaxLengthValue(int maxLengthValue) { + this.maxLengthValue = maxLengthValue; + } + + } + +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/test/java/com/opensymphony/xwork2/validator/StringValidatorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/StringValidatorTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/StringValidatorTest.java new file mode 100644 index 0000000..9742b5a --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/validator/StringValidatorTest.java @@ -0,0 +1,217 @@ +/* + * Copyright 2002-2006,2009 The Apache Software Foundation. + * + * Licensed 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 com.opensymphony.xwork2.validator; + +import com.opensymphony.xwork2.Action; +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.ActionProxy; +import com.opensymphony.xwork2.ValidationAwareSupport; +import com.opensymphony.xwork2.XWorkTestCase; +import com.opensymphony.xwork2.config.entities.ActionConfig; +import com.opensymphony.xwork2.config.providers.MockConfigurationProvider; +import com.opensymphony.xwork2.test.Equidae; +import com.opensymphony.xwork2.validator.validators.RequiredStringValidator; +import org.easymock.EasyMock; + +import java.util.List; +import java.util.Map; + +/** + * @author Mark Woon + * @author tm_jee (tm_jee (at) yahoo.co.uk ) + */ +public class StringValidatorTest extends XWorkTestCase { + + public void testRequiredStringWithNullValue() throws Exception { + Equidae equidae = new Equidae(); + equidae.setHorse(null); + + DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport()); + container.getInstance(ActionValidatorManager.class).validate(equidae, null, context); + + assertTrue(context.hasFieldErrors()); + } + + + public void testRequiredString() throws Exception { + Equidae equidae = new Equidae(); + + // everything should fail + equidae.setHorse(""); + ActionContext.getContext().getValueStack().push(equidae); + + DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport()); + container.getInstance(ActionValidatorManager.class).validate(equidae, null, context); + + assertTrue(context.hasFieldErrors()); + + Map fieldErrors = context.getFieldErrors(); + assertTrue(fieldErrors.containsKey("horse")); + assertEquals(2, ((List) fieldErrors.get("horse")).size()); + + // trim = false should fail + equidae.setHorse(" "); + ActionContext.getContext().getValueStack().push(equidae); + context = new DelegatingValidatorContext(new ValidationAwareSupport()); + container.getInstance(ActionValidatorManager.class).validate(equidae, null, context); + + assertTrue(context.hasFieldErrors()); + fieldErrors = context.getFieldErrors(); + assertTrue(fieldErrors.containsKey("horse")); + + List errors = (List) fieldErrors.get("horse"); + assertEquals(1, errors.size()); + assertEquals("trim", (String) errors.get(0)); + } + + public void testStringLength() throws Exception { + Equidae equidae = new Equidae(); + + equidae.setCow("asdf"); + equidae.setDonkey("asdf"); + ActionContext.getContext().getValueStack().push(equidae); + + DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport()); + container.getInstance(ActionValidatorManager.class).validate(equidae, null, context); + assertTrue(context.hasFieldErrors()); + + Map fieldErrors = context.getFieldErrors(); + + // cow + assertTrue(fieldErrors.containsKey("cow")); + + List errors = (List) fieldErrors.get("cow"); + assertEquals(2, errors.size()); + assertEquals("noTrim-min5", errors.get(0)); + assertEquals("noTrim-min5-max10", errors.get(1)); + + // donkey + assertTrue(fieldErrors.containsKey("donkey")); + errors = (List) fieldErrors.get("donkey"); + assertEquals(2, errors.size()); + assertEquals("trim-min5", errors.get(0)); + assertEquals("trim-min5-max10", errors.get(1)); + + equidae.setCow("asdf "); + equidae.setDonkey("asdf "); + ActionContext.getContext().getValueStack().push(equidae); + context = new DelegatingValidatorContext(new ValidationAwareSupport()); + container.getInstance(ActionValidatorManager.class).validate(equidae, null, context); + assertTrue(context.hasFieldErrors()); + + fieldErrors = context.getFieldErrors(); + + // cow + assertFalse(fieldErrors.containsKey("cow")); + + // donkey + assertTrue(fieldErrors.containsKey("donkey")); + errors = (List) fieldErrors.get("donkey"); + assertEquals(2, errors.size()); + assertEquals("trim-min5", errors.get(0)); + assertEquals("trim-min5-max10", errors.get(1)); + + equidae.setCow("asdfasdf"); + equidae.setDonkey("asdfasdf"); + ActionContext.getContext().getValueStack().push(equidae); + context = new DelegatingValidatorContext(new ValidationAwareSupport()); + container.getInstance(ActionValidatorManager.class).validate(equidae, null, context); + assertTrue(context.hasFieldErrors()); + + fieldErrors = context.getFieldErrors(); + + // cow + assertFalse(fieldErrors.containsKey("cow")); + + // donkey + assertFalse(fieldErrors.containsKey("donkey")); + + equidae.setCow("asdfasdf "); + equidae.setDonkey("asdfasdf "); + ActionContext.getContext().getValueStack().push(equidae); + context = new DelegatingValidatorContext(new ValidationAwareSupport()); + container.getInstance(ActionValidatorManager.class).validate(equidae, null, context); + assertTrue(context.hasFieldErrors()); + + fieldErrors = context.getFieldErrors(); + + // cow + assertTrue(fieldErrors.containsKey("cow")); + errors = (List) fieldErrors.get("cow"); + assertEquals(2, errors.size()); + assertEquals("noTrim-min5-max10", errors.get(0)); + assertEquals("noTrim-max10", errors.get(1)); + + // donkey + assertFalse(fieldErrors.containsKey("donkey")); + + equidae.setCow("asdfasdfasdf"); + equidae.setDonkey("asdfasdfasdf"); + ActionContext.getContext().getValueStack().push(equidae); + context = new DelegatingValidatorContext(new ValidationAwareSupport()); + container.getInstance(ActionValidatorManager.class).validate(equidae, null, context); + assertTrue(context.hasFieldErrors()); + + fieldErrors = context.getFieldErrors(); + + // cow + assertTrue(fieldErrors.containsKey("cow")); + errors = (List) fieldErrors.get("cow"); + assertEquals(2, errors.size()); + assertEquals("noTrim-min5-max10", errors.get(0)); + assertEquals("noTrim-max10", errors.get(1)); + + // donkey + assertTrue(fieldErrors.containsKey("donkey")); + errors = (List) fieldErrors.get("donkey"); + assertEquals(2, errors.size()); + assertEquals("trim-min5-max10", errors.get(0)); + assertEquals("trim-max10", errors.get(1)); + } + + public void testGetSetTrim() { + RequiredStringValidator val = new RequiredStringValidator(); + + val.setTrim(true); + assertEquals(true, val.isTrim()); + + val.setTrim(false); + assertEquals(false, val.isTrim()); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + loadConfigurationProviders(new MockConfigurationProvider()); + + ActionConfig config = new ActionConfig.Builder("", "name", "").build(); + ActionInvocation invocation = EasyMock.createNiceMock(ActionInvocation.class); + ActionProxy proxy = EasyMock.createNiceMock(ActionProxy.class); + + EasyMock.expect(invocation.getProxy()).andReturn(proxy).anyTimes(); + EasyMock.expect(invocation.getAction()).andReturn(null).anyTimes(); + EasyMock.expect(invocation.invoke()).andReturn(Action.SUCCESS).anyTimes(); + EasyMock.expect(proxy.getMethod()).andReturn("execute").anyTimes(); + EasyMock.expect(proxy.getConfig()).andReturn(config).anyTimes(); + + + EasyMock.replay(invocation); + EasyMock.replay(proxy); + + ActionContext.getContext().setActionInvocation(invocation); + } +}
