Revision: 9504
Author: [email protected]
Date: Thu Jan 6 11:29:20 2011
Log: Test the type of a value before casting it.
[JSR 303 TCK Result] 57 of 258 (22.09%) Pass with 32 Failures and 5 Errors.
Review at http://gwt-code-reviews.appspot.com/1261801
Review by: [email protected]
http://code.google.com/p/google-web-toolkit/source/detail?r=9504
Modified:
/trunk/samples/validationtck/test/org/hibernate/jsr303/tck/tests/ValidationGwtSuite.java
/trunk/samples/validationtck/test/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/ValidatorResolutionGwtTest.java
/trunk/samples/validationtck/test/org/hibernate/jsr303/tck/tests/validation/PropertyPathGwtTest.java
/trunk/samples/validationtck/test/org/hibernate/jsr303/tck/tests/validation/TckTestValidatorFactory.java
/trunk/samples/validationtck/test/org/hibernate/jsr303/tck/tests/validation/ValidateValueGwtTest.java
/trunk/user/src/com/google/gwt/validation/rebind/GwtSpecificValidatorCreator.java
=======================================
---
/trunk/samples/validationtck/test/org/hibernate/jsr303/tck/tests/ValidationGwtSuite.java
Wed Jan 5 06:41:18 2011
+++
/trunk/samples/validationtck/test/org/hibernate/jsr303/tck/tests/ValidationGwtSuite.java
Thu Jan 6 11:29:20 2011
@@ -15,8 +15,6 @@
*/
package org.hibernate.jsr303.tck.tests;
-
-
import junit.framework.Test;
import org.hibernate.jsr303.tck.tests.validation.PropertyPathGwtTest;
=======================================
---
/trunk/samples/validationtck/test/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/ValidatorResolutionGwtTest.java
Wed Jan 5 06:41:18 2011
+++
/trunk/samples/validationtck/test/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/ValidatorResolutionGwtTest.java
Thu Jan 6 11:29:20 2011
@@ -44,12 +44,10 @@
delegate.testResolutionOfMultipleSizeValidators();
}
- @Failing(issue = 5806)
public void testTargetedTypeIsField() {
delegate.testTargetedTypeIsField();
}
- @Failing(issue = 5806)
public void testTargetedTypeIsGetter() {
delegate.testTargetedTypeIsGetter();
}
=======================================
---
/trunk/samples/validationtck/test/org/hibernate/jsr303/tck/tests/validation/PropertyPathGwtTest.java
Wed Jan 5 06:41:18 2011
+++
/trunk/samples/validationtck/test/org/hibernate/jsr303/tck/tests/validation/PropertyPathGwtTest.java
Thu Jan 6 11:29:20 2011
@@ -16,7 +16,6 @@
package org.hibernate.jsr303.tck.tests.validation;
import org.hibernate.jsr303.tck.util.Failing;
-import org.hibernate.jsr303.tck.util.NonTckTest;
/**
* Test wrapper for {...@link PropertyPathTest}.
@@ -24,10 +23,6 @@
public class PropertyPathGwtTest extends AbstractValidationTest {
private final PropertyPathTest delegate = new PropertyPathTest();
-
- @NonTckTest
- public void testPlaceHolder() {
- }
@Failing(issue = 5803)
public void testPropertyPathSet() {
@@ -44,7 +39,6 @@
delegate.testPropertyPathWithArray();
}
- @Failing(issue = 5803)
public void testPropertyPathWithConstraintViolationForRootObject() {
delegate.testPropertyPathWithConstraintViolationForRootObject();
}
@@ -58,5 +52,4 @@
public void testPropertyPathWithMap() {
delegate.testPropertyPathWithMap();
}
-
-}
+}
=======================================
---
/trunk/samples/validationtck/test/org/hibernate/jsr303/tck/tests/validation/TckTestValidatorFactory.java
Wed Jan 5 06:41:18 2011
+++
/trunk/samples/validationtck/test/org/hibernate/jsr303/tck/tests/validation/TckTestValidatorFactory.java
Thu Jan 6 11:29:20 2011
@@ -20,6 +20,9 @@
import com.google.gwt.validation.client.GwtValidation;
import com.google.gwt.validation.client.impl.AbstractGwtValidator;
+import org.hibernate.jsr303.tck.tests.validation.PropertyPathTest.ActorDB;
+import
org.hibernate.jsr303.tck.tests.validation.PropertyPathTest.VerySpecialClass;
+
import javax.validation.Validator;
/**
@@ -31,8 +34,9 @@
* Marker Interface for {...@link GWT#create(Class)}.
*/
@GwtValidation(value = {
- Actor.class, ActorArrayBased.class, ActorListBased.class,
Address.class,
- BadlyBehavedEntity.class, Customer.class, Engine.class, Order.class})
+ Actor.class, ActorDB.class, ActorArrayBased.class,
ActorListBased.class,
+ Address.class, BadlyBehavedEntity.class, Customer.class,
Engine.class,
+ Order.class, VerySpecialClass.class})
public static interface GwtValidator extends Validator {
}
=======================================
---
/trunk/samples/validationtck/test/org/hibernate/jsr303/tck/tests/validation/ValidateValueGwtTest.java
Wed Jan 5 06:41:18 2011
+++
/trunk/samples/validationtck/test/org/hibernate/jsr303/tck/tests/validation/ValidateValueGwtTest.java
Thu Jan 6 11:29:20 2011
@@ -63,7 +63,7 @@
delegate.testValidateValueWithNullPropertyName();
}
- @Failing(issue = 5804)
+ // This passes but the desired behavior is still not working
public void testValidIsNotHonoredValidateValue() {
delegate.testValidIsNotHonoredValidateValue();
}
=======================================
---
/trunk/user/src/com/google/gwt/validation/rebind/GwtSpecificValidatorCreator.java
Tue Dec 21 06:50:31 2010
+++
/trunk/user/src/com/google/gwt/validation/rebind/GwtSpecificValidatorCreator.java
Thu Jan 6 11:29:20 2011
@@ -34,7 +34,6 @@
import com.google.gwt.thirdparty.guava.common.collect.Iterables;
import com.google.gwt.thirdparty.guava.common.collect.Maps;
import com.google.gwt.thirdparty.guava.common.collect.Sets;
-import com.google.gwt.thirdparty.guava.common.primitives.Primitives;
import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
import com.google.gwt.user.rebind.SourceWriter;
import com.google.gwt.validation.client.impl.AbstractGwtSpecificValidator;
@@ -56,6 +55,7 @@
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintViolation;
import javax.validation.Payload;
+import javax.validation.ValidationException;
import javax.validation.metadata.BeanDescriptor;
import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.metadata.PropertyDescriptor;
@@ -197,6 +197,14 @@
this.beanType = beanType;
this.beanHelper = beanHelper;
}
+
+ public JType getElementType(PropertyDescriptor p, boolean useField) {
+ if (useField) {
+ return beanType.findField(p.getPropertyName()).getType();
+ } else {
+ return beanType.findMethod(this.asGetter(p),
NO_ARGS).getReturnType();
+ }
+ }
@Override
protected void compose(ClassSourceFileComposerFactory composerFactory) {
@@ -273,7 +281,7 @@
private Annotation getAnnotation(PropertyDescriptor p, boolean useField,
ConstraintDescriptor<?> constraint) {
- Class<? extends Annotation> expectedAnnotaionClass =
+ Class<? extends Annotation> expectedAnnotaionClass =
((Annotation) constraint.getAnnotation()).annotationType();
Annotation annotation = null;
if (useField) {
@@ -289,6 +297,16 @@
}
return annotation;
}
+
+ /**
+ * @param elementType
+ * @return
+ */
+ private String getQualifiedSourceNonPrimitiveType(JType elementType) {
+ JPrimitiveType primitive = elementType.isPrimitive();
+ return primitive == null ? elementType.getQualifiedSourceName()
+ : primitive.getQualifiedBoxedSourceName();
+ }
private boolean hasField(PropertyDescriptor p) {
JField field = beanType.findField(p.getPropertyName());
@@ -310,10 +328,11 @@
}
}
- private boolean isIterable(Class<?> elementClass) {
+ private boolean isIterableOrMap(Class<?> elementClass) {
// TODO(nchalko) handle iterables everywhere this is called.
return elementClass.isArray()
- || Iterable.class.isAssignableFrom(elementClass);
+ || Iterable.class.isAssignableFrom(elementClass)
+ || Map.class.isAssignableFrom(elementClass);
}
private boolean isPropertyConstrained(BeanHelper helper,
PropertyDescriptor p) {
@@ -495,7 +514,7 @@
count++; // index starts at zero.
}
writePropertyDescriptor(sw, p);
- if (p.isCascaded() && !isIterable(p.getElementClass())) {
+ if (p.isCascaded() && !isIterableOrMap(p.getElementClass())) {
beansToValidate.add(createBeanHelper(p.getElementClass()));
}
}
@@ -816,7 +835,7 @@
if (useValue) {
sw.print("null, ");
sw.print("(");
- sw.print(Primitives.wrap(p.getElementClass()).getCanonicalName());
+ sw.print(getQualifiedSourceNonPrimitiveType(getElementType(p,
true)));
sw.print(") value");
} else {
sw.print("object, ");
@@ -848,7 +867,7 @@
if (useValue) {
sw.print("null, ");
sw.print("(");
- sw.print(Primitives.wrap(p.getElementClass()).getCanonicalName());
+ sw.print(getQualifiedSourceNonPrimitiveType(getElementType(p,
false)));
sw.print(") value");
} else {
sw.print("object, ");
@@ -950,18 +969,63 @@
private void writeValidatePropertyCall(SourceWriter sw,
PropertyDescriptor property, boolean useValue) {
if (hasGetter(property)) {
+ if (useValue) {
+ // if ( value == null || value instanceof propertyType) {
+ sw.print("if ( value == null || value instanceof ");
+
sw.print(getQualifiedSourceNonPrimitiveType(getElementType(property,
+ false)));
+ sw.println(") {");
+ sw.indent();
+ }
// validate_getMyProperty
writeValidateGetterCall(sw, property, useValue);
- }
+ if (useValue) {
+ // } else
+ sw.outdent();
+ sw.print("} else ");
+ }
+ }
+
+
if (hasField(property)) {
+ if (useValue) {
+ // if ( value == null || value instanceof propertyType) {
+ sw.print("if ( value == null || value instanceof ");
+
sw.print(getQualifiedSourceNonPrimitiveType(getElementType(property,
+ true)));
+ sw.println(") {");
+ sw.indent();
+ }
// validate_myProperty
writeValidateFieldCall(sw, property, useValue);
+ if (useValue) {
+ // } else
+ sw.outdent();
+ sw.print("} else ");
+ }
+ }
+
+ if (useValue & (hasGetter(property) || hasField(property))) {
+ // {
+ sw.println(" {");
+ sw.indent();
+
+ // throw new ValidationException(value.getClass +
+ // " is not a valid type for " + propertyName);
+ sw.print("throw new ");
+ sw.print(ValidationException.class.getCanonicalName());
+ sw.println("(value.getClass() +\" is not a valid type for \"+
propertyName);");
+
+ // }
+ sw.outdent();
+ sw.println("}");
}
}
private void writeValidatePropertyMethod(SourceWriter sw,
PropertyDescriptor p, boolean useField) {
Class<?> elementClass = p.getElementClass();
+ JType elementType = getElementType(p, useField);
// private final <T> void validateProperty_{get}<p>(
sw.print("private final <T> void ");
@@ -978,7 +1042,7 @@
sw.println("GwtValidationContext<T> context,");
sw.println("Set<ConstraintViolation<T>> violations,");
sw.println(beanHelper.getTypeCanonicalName() + " object,");
- sw.print(elementClass.getCanonicalName());
+ sw.print(elementType.getQualifiedSourceName());
sw.println(" value,");
sw.println("Class<?>... groups) {");
sw.outdent();
@@ -990,7 +1054,7 @@
sw.println("\");");
// TODO(nchalko) move this out of here to the Validate method
- if (p.isCascaded() && !isIterable(elementClass)) {
+ if (p.isCascaded() && !isIterableOrMap(elementClass)) {
BeanHelper helper = createBeanHelper(elementClass);
@@ -1053,7 +1117,9 @@
sw.println("\")) {");
sw.indent();
- writeValidatePropertyCall(sw, property, true);
+ if (!isIterableOrMap(property.getElementClass())) {
+ writeValidatePropertyCall(sw, property, true);
+ }
// validate all super classes and interfaces
writeValidateInheritance(sw, beanHelper.getClazz(),
@@ -1092,7 +1158,7 @@
private void writeValidatorCall(SourceWriter sw, Class<?> type, Stage
stage,
PropertyDescriptor p) {
- if (BeanHelper.isClassConstrained(type) && !isIterable(type)) {
+ if (BeanHelper.isClassConstrained(type) && !isIterableOrMap(type)) {
BeanHelper helper = createBeanHelper(type);
beansToValidate.add(helper);
switch (stage) {
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors