Author: musachy Date: Mon Mar 30 20:02:31 2009 New Revision: 760136 URL: http://svn.apache.org/viewvc?rev=760136&view=rev Log: Add more tests. Improve field name extraction from OVal message
Added: struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/SimpleFieldsXMLChild.java struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/ValidationInMethods.java struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/aa.java struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/org/apache/struts2/interceptor/SimpleFieldsXMLChild-validation.xml Modified: struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/DefaultOValValidationManager.java struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/OValValidationInterceptor.java struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/OValValidationInterceptorTest.java struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/org/apache/struts2/interceptor/SimpleFieldI18n.properties struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/org/apache/struts2/interceptor/SimpleFieldsXML-validation.xml struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/oval-test.xml Modified: struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/DefaultOValValidationManager.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/DefaultOValValidationManager.java?rev=760136&r1=760135&r2=760136&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/DefaultOValValidationManager.java (original) +++ struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/DefaultOValValidationManager.java Mon Mar 30 20:02:31 2009 @@ -19,9 +19,9 @@ public class DefaultOValValidationManager implements OValValidationManager { private static final Logger LOG = LoggerFactory.getLogger(DefaultOValValidationManager.class); - private static final String VALIDATION_CONFIG_SUFFIX = "-validation.xml"; - private final Map<String, List<Configurer>> validatorCache = new HashMap<String, List<Configurer>>(); - private final Map<String, Configurer> validatorFileCache = new HashMap<String, Configurer>(); + protected static final String VALIDATION_CONFIG_SUFFIX = "-validation.xml"; + protected final Map<String, List<Configurer>> validatorCache = new HashMap<String, List<Configurer>>(); + protected final Map<String, Configurer> validatorFileCache = new HashMap<String, Configurer>(); public synchronized List<Configurer> getConfigurers(Class clazz, String context) { final String validatorKey = buildValidatorKey(clazz, context); Modified: struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/OValValidationInterceptor.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/OValValidationInterceptor.java?rev=760136&r1=760135&r2=760136&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/OValValidationInterceptor.java (original) +++ struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/OValValidationInterceptor.java Mon Mar 30 20:02:31 2009 @@ -42,6 +42,7 @@ import java.util.List; import java.lang.reflect.Method; +import java.lang.reflect.Field; import org.apache.struts2.validation.Profiles; import org.apache.commons.lang.xwork.StringUtils; @@ -89,7 +90,7 @@ ActionProxy proxy = invocation.getProxy(); ValueStack valueStack = invocation.getStack(); String methodName = proxy.getMethod(); - String context = proxy.getActionName(); + String context = proxy.getConfig().getName(); if (LOG.isDebugEnabled()) { LOG.debug("Validating [#0/#1] with method [#2]", invocation.getProxy().getNamespace(), invocation.getProxy().getActionName(), methodName); @@ -167,7 +168,7 @@ String key = violation.getMessage(); //push the validator into the stack - valueStack.push(violation); + valueStack.push(violation.getContext()); String message = key; try { message = validatorContext.getText(key); @@ -178,10 +179,8 @@ if (isActionError(violation)) validatorContext.addActionError(message); else { - String className = clazz.getName(); - //the default OVal message shows the field name as ActionClass.fieldName - message = StringUtils.removeStart(message, className + "."); - validatorContext.addFieldError(extractFieldName(violation), message); + ValidationError validationError = buildValidationError(violation, message); + validatorContext.addFieldError(validationError.getFieldName(), validationError.getMessage()); } } } @@ -189,24 +188,41 @@ /** - * Get field name, used to add the validation error to fieldErrors + * Get field name and message, used to add the validation error to fieldErrors */ - protected String extractFieldName(ConstraintViolation violation) { + protected ValidationError buildValidationError(ConstraintViolation violation, String message) { OValContext context = violation.getContext(); if (context instanceof FieldContext) { - return ((FieldContext) context).getField().getName(); + Field field = ((FieldContext) context).getField(); + String className = field.getDeclaringClass().getName(); + + //the default OVal message shows the field name as ActionClass.fieldName + String finalMessage = StringUtils.removeStart(message, className + "."); + + return new ValidationError(field.getName(), finalMessage); } else if (context instanceof MethodReturnValueContext) { - String methodName = ((MethodReturnValueContext) context).getMethod().getName(); + Method method = ((MethodReturnValueContext) context).getMethod(); + String className = method.getDeclaringClass().getName(); + String methodName = method.getName(); + + //the default OVal message shows the field name as ActionClass.fieldName + String finalMessage = StringUtils.removeStart(message, className + "."); + + String fieldName = null; if (methodName.startsWith("get")) { - return StringUtils.uncapitalize(StringUtils.removeStart(methodName, "get")); + fieldName = StringUtils.uncapitalize(StringUtils.removeStart(methodName, "get")); } else if (methodName.startsWith("is")) { - return StringUtils.uncapitalize(StringUtils.removeStart(methodName, "is")); + fieldName = StringUtils.uncapitalize(StringUtils.removeStart(methodName, "is")); } - return methodName; + //the result will have the full method name, like "getName()", replace it by "name" (obnly if it is a field) + if (fieldName != null) + finalMessage = finalMessage.replaceAll(methodName + "\\(.*?\\)", fieldName); + + return new ValidationError(StringUtils.defaultString(fieldName, methodName), finalMessage); } - return violation.getCheckName(); + return new ValidationError(violation.getCheckName(), message); } /** @@ -215,4 +231,22 @@ protected boolean isActionError(ConstraintViolation violation) { return false; } + + class ValidationError { + private String fieldName; + private String message; + + ValidationError(String fieldName, String message) { + this.fieldName = fieldName; + this.message = message; + } + + public String getFieldName() { + return fieldName; + } + + public String getMessage() { + return message; + } + } } Modified: struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/OValValidationInterceptorTest.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/OValValidationInterceptorTest.java?rev=760136&r1=760135&r2=760136&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/OValValidationInterceptorTest.java (original) +++ struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/OValValidationInterceptorTest.java Mon Mar 30 20:02:31 2009 @@ -43,16 +43,52 @@ assertValue(fieldErrors, "lastName", Arrays.asList("lastName cannot be null")); } + public void testValidationInMethods() throws Exception { + ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "validationInMethods", null, null); + baseActionProxy.execute(); + + Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors(); + assertNotNull(fieldErrors); + assertEquals(4, fieldErrors.size()); + assertValue(fieldErrors, "name", Arrays.asList("name cannot be null")); + assertValue(fieldErrors, "SisyphusHasTheAnswer", Arrays.asList("SisyphusHasTheAnswer() cannot be null")); + assertValue(fieldErrors, "thereAnyMeaningInLife", Arrays.asList("thereAnyMeaningInLife cannot be null")); + assertValue(fieldErrors, "theManingOfLife", Arrays.asList("theManingOfLife cannot be null")); + } + + public void testSimpleFieldsInheritedXML() throws Exception { + ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "simpleFieldsXMLChild", null, null); + baseActionProxy.execute(); + + Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors(); + assertNotNull(fieldErrors); + assertEquals(3, fieldErrors.size()); + assertValue(fieldErrors, "firstName", Arrays.asList("firstName cannot be null")); + assertValue(fieldErrors, "lastName", Arrays.asList("lastName cannot be null")); + assertValue(fieldErrors, "middleName", Arrays.asList("middleName cannot be null")); + } + + public void testSlashesInNameWithWildcardsHitsCache() throws Exception { + ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "simpleFieldsXML/test", null, null); + baseActionProxy.execute(); + + ActionProxy baseActionProxy2 = actionProxyFactory.createActionProxy("oval", "simpleFieldsXML/test2", null, null); + baseActionProxy2.execute(); + + DummyDefaultOValValidationManager manager = (DummyDefaultOValValidationManager) container.getInstance(OValValidationManager.class); + assertEquals(1, manager.getCache().size()); + } + public void testXMLLoadCaching() { OValValidationManager manager = container.getInstance(OValValidationManager.class); - List<Configurer> configurers = manager.getConfigurers(SimpleFieldsXML.class, "simpleFieldsXML"); + List<Configurer> configurers = manager.getConfigurers(SimpleFieldsXML.class, "simpleFieldsXMLCaching"); assertNotNull(configurers); - assertEquals(1, configurers.size()); + assertEquals(2, configurers.size()); //load again and check it is the same instance - List<Configurer> configurers2 = manager.getConfigurers(SimpleFieldsXML.class, "simpleFieldsXML"); + List<Configurer> configurers2 = manager.getConfigurers(SimpleFieldsXML.class, "simpleFieldsXMLCaching"); assertNotNull(configurers2); - assertEquals(1, configurers2.size()); + assertEquals(2, configurers2.size()); assertSame(configurers.get(0), configurers2.get(0)); } @@ -210,7 +246,6 @@ assertEquals(expectedValues, values); } - @Override protected void setUp() throws Exception { super.setUp(); Added: struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/SimpleFieldsXMLChild.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/SimpleFieldsXMLChild.java?rev=760136&view=auto ============================================================================== --- struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/SimpleFieldsXMLChild.java (added) +++ struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/SimpleFieldsXMLChild.java Mon Mar 30 20:02:31 2009 @@ -0,0 +1,33 @@ +/* + * $Id$ + * + * 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.struts2.interceptor; + +public class SimpleFieldsXMLChild extends SimpleFieldsXML { + private String middleName; + + public String getMiddleName() { + return middleName; + } + + public void setMiddleName(String middleName) { + this.middleName = middleName; + } +} Added: struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/ValidationInMethods.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/ValidationInMethods.java?rev=760136&view=auto ============================================================================== --- struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/ValidationInMethods.java (added) +++ struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/ValidationInMethods.java Mon Mar 30 20:02:31 2009 @@ -0,0 +1,52 @@ +/* + * $Id$ + * + * 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.struts2.interceptor; + +import com.opensymphony.xwork2.ActionSupport; +import net.sf.oval.constraint.NotNull; +import net.sf.oval.configuration.annotation.IsInvariant; + +public class ValidationInMethods extends ActionSupport { + @IsInvariant + @NotNull + public String getName() { + return null; + } + + @IsInvariant + @NotNull + public Boolean isThereAnyMeaningInLife() { + return null; + } + + @IsInvariant + @NotNull + public String getTheManingOfLife() { + return null; + } + + @IsInvariant + @NotNull + public String SisyphusHasTheAnswer() { + //some method annotated, whose name is not a field + return null; + } +} \ No newline at end of file Added: struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/aa.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/aa.java?rev=760136&view=auto ============================================================================== --- struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/aa.java (added) +++ struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/aa.java Mon Mar 30 20:02:31 2009 @@ -0,0 +1,33 @@ +/* + * $Id$ + * + * 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.struts2.interceptor; + +import net.sf.oval.configuration.Configurer; + +import java.util.List; +import java.util.Map; + +//just to expose the cache +class DummyDefaultOValValidationManager extends DefaultOValValidationManager { + public Map<String, List<Configurer>> getCache() { + return validatorCache; + } +} Modified: struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/org/apache/struts2/interceptor/SimpleFieldI18n.properties URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/org/apache/struts2/interceptor/SimpleFieldI18n.properties?rev=760136&r1=760135&r2=760136&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/org/apache/struts2/interceptor/SimpleFieldI18n.properties (original) +++ struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/org/apache/struts2/interceptor/SimpleFieldI18n.properties Mon Mar 30 20:02:31 2009 @@ -1 +1 @@ -notnull.field=${context.field.name} cannot be null \ No newline at end of file +notnull.field=${field.name} cannot be null \ No newline at end of file Modified: struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/org/apache/struts2/interceptor/SimpleFieldsXML-validation.xml URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/org/apache/struts2/interceptor/SimpleFieldsXML-validation.xml?rev=760136&r1=760135&r2=760136&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/org/apache/struts2/interceptor/SimpleFieldsXML-validation.xml (original) +++ struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/org/apache/struts2/interceptor/SimpleFieldsXML-validation.xml Mon Mar 30 20:02:31 2009 @@ -1,9 +1,4 @@ <?xml version="1.0" encoding="UTF-8"?> -<!-- -<!DOCTYPE oval PUBLIC - "-//OVal/OVal Configuration DTD 1.3//EN" - "http://oval.sourceforge.net/oval-configuration-1.3.dtd"> ---> <oval xmlns="http://oval.sf.net/oval-configuration" xmlns:xsi="http://http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://oval.sf.net/oval-configuration http://oval.sourceforge.net/oval-configuration-1.3.xsd"> <class type="org.apache.struts2.interceptor.SimpleFieldsXML" overwrite="false" Added: struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/org/apache/struts2/interceptor/SimpleFieldsXMLChild-validation.xml URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/org/apache/struts2/interceptor/SimpleFieldsXMLChild-validation.xml?rev=760136&view=auto ============================================================================== --- struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/org/apache/struts2/interceptor/SimpleFieldsXMLChild-validation.xml (added) +++ struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/org/apache/struts2/interceptor/SimpleFieldsXMLChild-validation.xml Mon Mar 30 20:02:31 2009 @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8"?> +<oval xmlns="http://oval.sf.net/oval-configuration" xmlns:xsi="http://http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://oval.sf.net/oval-configuration http://oval.sourceforge.net/oval-configuration-1.3.xsd"> + <class type="org.apache.struts2.interceptor.SimpleFieldsXMLChild" overwrite="false" + applyFieldConstraintsToSetters="true"> + <field name="middleName"> + <notNull/> + </field> + </class> +</oval> \ No newline at end of file Modified: struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/oval-test.xml URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/oval-test.xml?rev=760136&r1=760135&r2=760136&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/oval-test.xml (original) +++ struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/oval-test.xml Mon Mar 30 20:02:31 2009 @@ -4,7 +4,8 @@ > <xwork> - <bean type="org.apache.struts2.interceptor.OValValidationManager" class="org.apache.struts2.interceptor.DefaultOValValidationManager" /> + <bean type="org.apache.struts2.interceptor.OValValidationManager" class="org.apache.struts2.interceptor.DummyDefaultOValValidationManager"/> + <constant name="struts.enable.SlashesInActionNames" value="true"/> <package namespace="oval" name="oval-test"> <result-types> <result-type name="void" class="org.apache.struts2.interceptor.VoidResult"/> @@ -13,10 +14,26 @@ <interceptor name="ovalValidation" class="org.apache.struts2.interceptor.OValValidationInterceptor"/> </interceptors> + <action name="simpleFieldsXMLChild" class="org.apache.struts2.interceptor.SimpleFieldsXMLChild"> + <interceptor-ref name="ovalValidation"/> + <result type="void"></result> + </action> + <action name="validationInMethods" class="org.apache.struts2.interceptor.ValidationInMethods"> + <interceptor-ref name="ovalValidation"/> + <result type="void"></result> + </action> + <action name="simpleFieldsXML/*" class="org.apache.struts2.interceptor.SimpleFieldsXML"> + <interceptor-ref name="ovalValidation"/> + <result type="void"></result> + </action> <action name="simpleFieldsXML" class="org.apache.struts2.interceptor.SimpleFieldsXML"> <interceptor-ref name="ovalValidation"/> <result type="void"></result> </action> + <action name="simpleFieldsXMLCaching" class="org.apache.struts2.interceptor.SimpleFieldsXML"> + <interceptor-ref name="ovalValidation"/> + <result type="void"></result> + </action> <action name="simpleField" class="org.apache.struts2.interceptor.SimpleField"> <interceptor-ref name="ovalValidation"/> <result type="void"></result>