Author: tchemit Date: 2013-09-11 17:24:03 +0200 (Wed, 11 Sep 2013) New Revision: 2601 Url: http://nuiton.org/projects/nuiton-validator/repository/revisions/2601 Log: fixes #2837: Can not validate when using a visitor validator Added: trunk/src/main/resources/xwork.xml trunk/src/test/java/org/nuiton/validator/model/Family.java trunk/src/test/resources/org/nuiton/validator/model/Family-error-validation.xml Modified: trunk/src/main/java/org/nuiton/validator/xwork2/XWork2ValidatorUtil.java trunk/src/test/java/org/nuiton/validator/xwork2/XWork2NuitonValidatorTest.java Modified: trunk/src/main/java/org/nuiton/validator/xwork2/XWork2ValidatorUtil.java =================================================================== --- trunk/src/main/java/org/nuiton/validator/xwork2/XWork2ValidatorUtil.java 2013-08-09 09:04:19 UTC (rev 2600) +++ trunk/src/main/java/org/nuiton/validator/xwork2/XWork2ValidatorUtil.java 2013-09-11 15:24:03 UTC (rev 2601) @@ -23,10 +23,22 @@ */ package org.nuiton.validator.xwork2; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionProxy; +import com.opensymphony.xwork2.ActionProxyFactory; +import com.opensymphony.xwork2.DefaultActionInvocation; +import com.opensymphony.xwork2.ObjectFactory; +import com.opensymphony.xwork2.Result; +import com.opensymphony.xwork2.UnknownHandler; +import com.opensymphony.xwork2.XWorkException; import com.opensymphony.xwork2.config.Configuration; import com.opensymphony.xwork2.config.ConfigurationManager; +import com.opensymphony.xwork2.config.entities.ActionConfig; +import com.opensymphony.xwork2.config.entities.UnknownHandlerConfig; import com.opensymphony.xwork2.inject.Container; +import com.opensymphony.xwork2.inject.Inject; import com.opensymphony.xwork2.util.ValueStack; import com.opensymphony.xwork2.util.ValueStackFactory; import com.opensymphony.xwork2.validator.ActionValidatorManager; @@ -38,6 +50,7 @@ import org.nuiton.validator.NuitonValidatorScope; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; @@ -119,8 +132,33 @@ // before using a validator... Must be cleaned... ActionContext.setContext(context); + Container container = context.getContainer(); + + // We need sometimes a ActionInvocation (see http://nuiton.org/issues/2837) + Configuration configuration = container.getInstance(Configuration.class); + + UnknownHandlerConfig unknownHandlerStack = new UnknownHandlerConfig("nuiton"); + List<UnknownHandlerConfig> unknownHandlerStackList = configuration.getUnknownHandlerStack(); + + if (unknownHandlerStackList == null) { + unknownHandlerStackList = Lists.newArrayList(); + configuration.setUnknownHandlerStack(unknownHandlerStackList); + } + unknownHandlerStackList.add(0, unknownHandlerStack); + + Map<String, Object> extraContext = Maps.newHashMap(); + extraContext.put(ActionContext.VALUE_STACK, vs); + DefaultActionInvocation invocation = new DefaultActionInvocation(extraContext, false); + invocation.setObjectFactory(container.getInstance(ObjectFactory.class)); + invocation.setContainer(container); + + ActionProxyFactory actionProxyFactory = context.getInstance(ActionProxyFactory.class); + ActionProxy actionProxy = actionProxyFactory.createActionProxy(invocation, "java.lang", "java.lang.Object", "nuiton-validation", false, false); + invocation.init(actionProxy); + context.setActionInvocation(invocation); + // init validator - Container container = context.getContainer(); + ActionValidatorManager validatorManager = container.getInstance(ActionValidatorManager.class, "no-annotations"); @@ -229,4 +267,45 @@ return fields; } + + /** + * A dummy unknown handler when we want to use for example visitor validators + * which need a invocation handler. + * <p/> + * <strong>Note:</strong> Do not use this for any purpose... + * + * @author tchemit <chemit@codelutin.com> + * @since 3.0 + */ + public static class NuitonDefaultUnknownHandler implements UnknownHandler { + + protected Configuration configuration; + + protected ObjectFactory objectFactory; + + @Inject + public void setConfiguration(Configuration configuration) { + this.configuration = configuration; + } + + @Inject + public void setObjectFactory(ObjectFactory objectFactory) { + this.objectFactory = objectFactory; + } + + @Override + public ActionConfig handleUnknownAction(String namespace, String actionName) throws XWorkException { + return new ActionConfig.Builder(namespace, actionName, Object.class.getName()).build(); + } + + @Override + public Result handleUnknownResult(ActionContext actionContext, String actionName, ActionConfig actionConfig, String resultCode) throws XWorkException { + return null; + } + + @Override + public Object handleUnknownActionMethod(Object action, String methodName) throws NoSuchMethodException { + return null; + } + } } Added: trunk/src/main/resources/xwork.xml =================================================================== --- trunk/src/main/resources/xwork.xml (rev 0) +++ trunk/src/main/resources/xwork.xml 2013-09-11 15:24:03 UTC (rev 2601) @@ -0,0 +1,31 @@ +<!-- + #%L + Nuiton Validator + $Id$ + $HeadURL$ + %% + Copyright (C) 2013 CodeLutin + %% + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Lesser Public License for more details. + + You should have received a copy of the GNU General Lesser Public + License along with this program. If not, see + <http://www.gnu.org/licenses/lgpl-3.0.html>. + #L% + --> +<!DOCTYPE xwork PUBLIC + "-//Apache Struts//XWork 2.3//EN" + "http://struts.apache.org/dtds/xwork-2.3.dtd"> + +<xwork> + <bean type="com.opensymphony.xwork2.UnknownHandler" name="nuiton" class="org.nuiton.validator.xwork2.XWork2ValidatorUtil$NuitonDefaultUnknownHandler"/> + +</xwork> Property changes on: trunk/src/main/resources/xwork.xml ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/src/test/java/org/nuiton/validator/model/Family.java =================================================================== --- trunk/src/test/java/org/nuiton/validator/model/Family.java (rev 0) +++ trunk/src/test/java/org/nuiton/validator/model/Family.java 2013-09-11 15:24:03 UTC (rev 2601) @@ -0,0 +1,46 @@ +package org.nuiton.validator.model; + +/* + * #%L + * Nuiton Validator + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2013 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import java.util.Collection; + +/** + * @author tchemit <chemit@codelutin.com> + * @since 3.0 + */ +public class Family { + + public static final String PROPERTY_MEMBER = "member"; + + protected Collection<Person> member; + + public Collection<Person> getMember() { + return member; + } + + public void setMember(Collection<Person> member) { + this.member = member; + } +} Property changes on: trunk/src/test/java/org/nuiton/validator/model/Family.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/src/test/java/org/nuiton/validator/xwork2/XWork2NuitonValidatorTest.java =================================================================== --- trunk/src/test/java/org/nuiton/validator/xwork2/XWork2NuitonValidatorTest.java 2013-08-09 09:04:19 UTC (rev 2600) +++ trunk/src/test/java/org/nuiton/validator/xwork2/XWork2NuitonValidatorTest.java 2013-09-11 15:24:03 UTC (rev 2601) @@ -23,11 +23,14 @@ */ package org.nuiton.validator.xwork2; +import com.google.common.collect.Sets; import org.junit.Assert; import org.junit.Test; import org.nuiton.validator.NuitonValidator; import org.nuiton.validator.NuitonValidatorModel; +import org.nuiton.validator.NuitonValidatorResult; import org.nuiton.validator.ValidatorTestHelper; +import org.nuiton.validator.model.Family; import org.nuiton.validator.model.Person; /** @@ -56,4 +59,35 @@ } + @Test + public void testNewValidator2() throws Exception { + + XWork2NuitonValidatorProvider provider = + new XWork2NuitonValidatorProvider(); + + NuitonValidatorModel<Family> model = + provider.getModel(Family.class, null); + + NuitonValidator<Family> validator = provider.newValidator(model); + + Assert.assertNotNull(validator); + + Family f = new Family(); + Person father = new Person(); + Person mother = new Person(); + f.setMember(Sets.newHashSet(father, mother)); + + NuitonValidatorResult validate = validator.validate(f); + + Assert.assertFalse(validate.isValid()); + + father.setFirstname("john"); + father.setName("smith"); + mother.setFirstname("jim"); // since 18 mai 2013 possible in France! + mother.setName("smith"); + + validate = validator.validate(f); + + Assert.assertTrue(validate.isValid()); + } } Added: trunk/src/test/resources/org/nuiton/validator/model/Family-error-validation.xml =================================================================== --- trunk/src/test/resources/org/nuiton/validator/model/Family-error-validation.xml (rev 0) +++ trunk/src/test/resources/org/nuiton/validator/model/Family-error-validation.xml 2013-09-11 15:24:03 UTC (rev 2601) @@ -0,0 +1,37 @@ +<!-- + #%L + Nuiton Validator + $Id$ + $HeadURL$ + %% + Copyright (C) 2013 CodeLutin + %% + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Lesser Public License for more details. + + You should have received a copy of the GNU General Lesser Public + License along with this program. If not, see + <http://www.gnu.org/licenses/lgpl-3.0.html>. + #L% + --> +<!DOCTYPE validators PUBLIC + "-//Apache Struts//XWork Validator 1.0.3//EN" + "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> +<validators> + + <field name="member"> + <field-validator type="visitor"> + <param name="appendPrefix">true</param> + <param name="context">error</param> + <message>family.member.notValid</message> + </field-validator> + </field> + +</validators> Property changes on: trunk/src/test/resources/org/nuiton/validator/model/Family-error-validation.xml ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native