Public: Only process requested groups.

Review at http://gwt-code-reviews.appspot.com/1131801

Review by: rchandia@google.com

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@9268 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/samples/validation/src/com/google/gwt/sample/validation/client/ValidationView.java b/samples/validation/src/com/google/gwt/sample/validation/client/ValidationView.java
index 1383761..090ecf8 100644
--- a/samples/validation/src/com/google/gwt/sample/validation/client/ValidationView.java
+++ b/samples/validation/src/com/google/gwt/sample/validation/client/ValidationView.java
@@ -21,6 +21,7 @@
 import com.google.gwt.event.dom.client.KeyUpEvent;
 import com.google.gwt.resources.client.CssResource;
 import com.google.gwt.safehtml.shared.SafeHtml;
+import com.google.gwt.sample.validation.shared.ClientGroup;
 import com.google.gwt.sample.validation.shared.Person;
 import com.google.gwt.uibinder.client.UiBinder;
 import com.google.gwt.uibinder.client.UiField;
@@ -39,6 +40,7 @@
 import javax.validation.ConstraintViolation;
 import javax.validation.ConstraintViolationException;
 import javax.validation.Validator;
+import javax.validation.groups.Default;
 
 /**
  * Display the Validation sample.
@@ -126,7 +128,8 @@
     person.setName(nameField.getText());
 
     Validator validator = GWT.create(SampleValidator.class);
-    Set<ConstraintViolation<Person>> violations = validator.validate(person);
+    Set<ConstraintViolation<Person>> violations = validator.validate(person,
+        Default.class, ClientGroup.class);
     if (!violations.isEmpty()) {
       StringBuffer errorMessage = new StringBuffer();
       for (ConstraintViolation<Person> constraintViolation : violations) {
diff --git a/samples/validation/src/com/google/gwt/sample/validation/super/com/google/gwt/sample/validation/shared/ServerValidator.java b/samples/validation/src/com/google/gwt/sample/validation/super/com/google/gwt/sample/validation/shared/ServerValidator.java
index 330d8ad..c633d58 100644
--- a/samples/validation/src/com/google/gwt/sample/validation/super/com/google/gwt/sample/validation/shared/ServerValidator.java
+++ b/samples/validation/src/com/google/gwt/sample/validation/super/com/google/gwt/sample/validation/shared/ServerValidator.java
@@ -15,23 +15,13 @@
  */
 package com.google.gwt.sample.validation.shared;
 
-import javax.validation.ConstraintValidator;
-import javax.validation.ConstraintValidatorContext;
+import com.google.gwt.validation.client.constraints.NotGwtCompatibleValidator;
 
 /**
- * Always passes.
+ * Always invalid.
  * <p>
- * TODO(nchalko) change this to extend
- * {@link com.google.gwt.validation.client.constraints.NotGwtCompatibleValidator}
- * when groups are properly handled.
+ * Server validator is overriden so it compiles, but it is always invalid.
  */
-public class ServerValidator implements
-    ConstraintValidator<ServerConstraint, Person> {
-
-  public void initialize(ServerConstraint constraintAnnotation) {
-  }
-
-  public boolean isValid(Person person, ConstraintValidatorContext context) {
-    return true;
-  }
+public class ServerValidator extends
+   NotGwtCompatibleValidator<ServerConstraint, Person> {
 }
\ No newline at end of file
diff --git a/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/application/TckTestValidator.java b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/application/TckTestValidator.java
index d9e7e23..f562b34 100644
--- a/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/application/TckTestValidator.java
+++ b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/application/TckTestValidator.java
@@ -21,7 +21,6 @@
 
 import org.hibernate.jsr303.tck.tests.constraints.application.Building;
 import org.hibernate.jsr303.tck.tests.constraints.application.SuperWoman;
-import org.hibernate.jsr303.tck.tests.constraints.application.Visibility;
 import org.hibernate.jsr303.tck.tests.constraints.application.Woman;
 
 import javax.validation.Validator;
@@ -35,7 +34,7 @@
    * Marker Interface for {@link GWT#create(Class)}.
    */
   @GwtValidation(value = {
-      Building.class, SuperWoman.class, Visibility.class, Woman.class})
+      Building.class, SuperWoman.class, Woman.class})
   public static interface GwtValidator extends Validator {
   }
 
diff --git a/samples/validationtck/test/com/google/gwt/sample/validationtck/validatorfactory/ConstraintApplicationGwtSuite.java b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/groups/ConstraintsGroupsGwtSuite.java
similarity index 72%
copy from samples/validationtck/test/com/google/gwt/sample/validationtck/validatorfactory/ConstraintApplicationGwtSuite.java
copy to samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/groups/ConstraintsGroupsGwtSuite.java
index 590a1bb..7f55804 100644
--- a/samples/validationtck/test/com/google/gwt/sample/validationtck/validatorfactory/ConstraintApplicationGwtSuite.java
+++ b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/groups/ConstraintsGroupsGwtSuite.java
@@ -13,20 +13,20 @@
  * License for the specific language governing permissions and limitations under
  * the License.
  */
-package com.google.gwt.sample.validationtck.validatorfactory;
+package com.google.gwt.sample.validationtck.constraints.groups;
 
 import com.google.gwt.junit.tools.GWTTestSuite;
 
 import junit.framework.Test;
 
 /**
- * Tck Tests for the {@code validatorfactory} package.
+ * Tck Tests for the {@code constraints groups} package.
  */
-public class ConstraintApplicationGwtSuite {
+public class ConstraintsGroupsGwtSuite {
   public static Test suite() {
     GWTTestSuite suite = new GWTTestSuite(
-        "TCK for GWT Validation, validatorfactory package");
-    suite.addTestSuite(CustomConstraintValidatorTest.class);
+        "TCK for GWT Validation, constraints groups package");
+    suite.addTestSuite(GroupTest.class);
     return suite;
   }
 }
diff --git a/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/groups/GroupTest.java b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/groups/GroupTest.java
new file mode 100644
index 0000000..c40ceeb
--- /dev/null
+++ b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/groups/GroupTest.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * 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.google.gwt.sample.validationtck.constraints.groups;
+
+import com.google.gwt.junit.client.GWTTestCase;
+
+/**
+ * Test wrapper for
+ * {@link org.hibernate.jsr303.tck.tests.constraints.groups.GroupTest}.
+ */
+public class GroupTest extends GWTTestCase {
+  private final org.hibernate.jsr303.tck.tests.constraints.groups.GroupTest delegate = 
+      new org.hibernate.jsr303.tck.tests.constraints.groups.GroupTest();
+
+  @Override
+  public String getModuleName() {
+    return "com.google.gwt.sample.validationtck.constraints.groups.TckTest";
+  }
+
+  public void testConstraintCanBelongToMoreThanOneGroup() {
+    delegate.testConstraintCanBelongToMoreThanOneGroup();
+  }
+
+  public void testConstraintWithNoExplicitlySpecifiedGroupBelongsToDefault() {
+    delegate.testConstraintWithNoExplicitlySpecifiedGroupBelongsToDefault();
+  }
+
+  public void testCyclicGroupSequence() {
+    delegate.testCyclicGroupSequence();
+  }
+
+  public void testGroups() {
+    delegate.testGroups();
+  }
+
+  public void testGroupSequence() {
+    delegate.testGroupSequence();
+  }
+
+  public void testGroupSequenceFollowedByGroup() {
+    delegate.testGroupSequenceFollowedByGroup();
+  }
+
+  public void testImplicitGrouping() {
+    delegate.testImplicitGrouping();
+  }
+
+  public void testValidateAgainstDifferentGroups() {
+    delegate.testValidateAgainstDifferentGroups();
+  }
+
+  public void testValidationFailureInMultipleGroups() {
+    delegate.testValidationFailureInMultipleGroups();
+  }
+
+}
diff --git a/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/groups/TckTest.gwt.xml b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/groups/TckTest.gwt.xml
new file mode 100644
index 0000000..c12157b
--- /dev/null
+++ b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/groups/TckTest.gwt.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.0.1//EN" "http://google-web-toolkit.googlecode.com/svn/tags/2.0.1/distro-source/core/src/gwt-module.dtd">
+<!--
+  Copyright 2010 Google Inc.
+
+  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.
+-->
+<module>
+  <inherits name="com.google.gwt.sample.validationtck.ValidationTck" />
+  <source path="">
+    <include name="*.java" />
+    <exclude name="super" />
+  </source>
+  <replace-with class="com.google.gwt.sample.validationtck.constraints.groups.TckTestValidator">
+    <when-type-is class="javax.validation.Validator"/>
+  </replace-with>
+</module>
\ No newline at end of file
diff --git a/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/groups/TckTestValidator.java b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/groups/TckTestValidator.java
new file mode 100644
index 0000000..c1f014a
--- /dev/null
+++ b/samples/validationtck/test/com/google/gwt/sample/validationtck/constraints/groups/TckTestValidator.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * 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.google.gwt.sample.validationtck.constraints.groups;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.validation.client.AbstractValidator;
+import com.google.gwt.validation.client.GwtValidation;
+
+import org.hibernate.jsr303.tck.tests.constraints.groups.Animal;
+import org.hibernate.jsr303.tck.tests.constraints.groups.Book;
+import org.hibernate.jsr303.tck.tests.constraints.groups.Order;
+import org.hibernate.jsr303.tck.tests.constraints.groups.User;
+
+import javax.validation.Validator;
+
+/**
+ * Validator implementation that uses
+ * {@link com.google.gwt.validation.client.GwtValidation GwtValidation}.
+ */
+public final class TckTestValidator extends AbstractValidator {
+  /**
+   * Marker Interface to {@link GWT#create(Class)}.
+   */
+  @GwtValidation(value = {Animal.class, Book.class, Order.class, User.class})
+  public static interface GwtValidator extends Validator {
+  }
+
+  public TckTestValidator() {
+    super((Validator) GWT.create(GwtValidator.class));
+  }
+}
diff --git a/samples/validationtck/test/com/google/gwt/sample/validationtck/validatorfactory/ConstraintApplicationGwtSuite.java b/samples/validationtck/test/com/google/gwt/sample/validationtck/validatorfactory/ValidatorFactoryGwtSuite.java
similarity index 95%
rename from samples/validationtck/test/com/google/gwt/sample/validationtck/validatorfactory/ConstraintApplicationGwtSuite.java
rename to samples/validationtck/test/com/google/gwt/sample/validationtck/validatorfactory/ValidatorFactoryGwtSuite.java
index 590a1bb..ddb8abf 100644
--- a/samples/validationtck/test/com/google/gwt/sample/validationtck/validatorfactory/ConstraintApplicationGwtSuite.java
+++ b/samples/validationtck/test/com/google/gwt/sample/validationtck/validatorfactory/ValidatorFactoryGwtSuite.java
@@ -22,7 +22,7 @@
 /**
  * Tck Tests for the {@code validatorfactory} package.
  */
-public class ConstraintApplicationGwtSuite {
+public class ValidatorFactoryGwtSuite {
   public static Test suite() {
     GWTTestSuite suite = new GWTTestSuite(
         "TCK for GWT Validation, validatorfactory package");
diff --git a/user/src/com/google/gwt/validation/client/impl/AbstractGwtSpecificValidator.java b/user/src/com/google/gwt/validation/client/impl/AbstractGwtSpecificValidator.java
index 34a1a5e..05a949b 100644
--- a/user/src/com/google/gwt/validation/client/impl/AbstractGwtSpecificValidator.java
+++ b/user/src/com/google/gwt/validation/client/impl/AbstractGwtSpecificValidator.java
@@ -16,13 +16,16 @@
 package com.google.gwt.validation.client.impl;
 
 import java.lang.annotation.Annotation;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
 import javax.validation.ConstraintValidator;
 import javax.validation.ConstraintViolation;
+import javax.validation.groups.Default;
 
 /**
  * Base methods for implementing a {@link GwtSpecificValidator}.
@@ -59,18 +62,6 @@
     return new AttributeBuilder();
   }
 
-  /**
-   * @param <A>
-   * @param <T>
-   * @param <V>
-   * @param context
-   * @param violations
-   * @param object
-   * @param value
-   * @param validator
-   * @param constraintDescriptor
-   * @param groups
-   */
   protected <A extends Annotation, T, V> void validate(
       GwtValidationContext<T> context, Set<ConstraintViolation<T>> violations,
       G object, V value, ConstraintValidator<A, ? super V> validator,
@@ -78,6 +69,22 @@
     validator.initialize(constraintDescriptor.getAnnotation());
     ConstraintValidatorContextImpl<A, V> constraintValidatorContext =
         context.createConstraintValidatorContext(constraintDescriptor);
+
+    // TODO(nchalko) set empties to Default earlier.
+    Set<Class<?>> constraintGroup = constraintDescriptor.getGroups();
+    if (groups.length == 0) {
+      groups = new Class<?>[]{Default.class};
+    }
+    if (constraintGroup.isEmpty()) {
+      constraintGroup = new HashSet<Class<?>>();
+      constraintGroup.add(Default.class);
+    }
+
+    // check group
+    if (!containsAny(groups, constraintGroup)) {
+      return;
+    }
+
     if (!validator.isValid(value, constraintValidatorContext)) {
       addViolations(//
           context, //
@@ -105,6 +112,15 @@
     }
   }
 
+  private <T> boolean containsAny(T[] left, Collection<T> right) {
+    for (T t : left) {
+      if (right.contains(t)) {
+        return true;
+      }
+    }
+    return false;
+  }
+
   private <T, V, A extends Annotation> ConstraintViolation<T> createConstraintViolation(
       GwtValidationContext<T> context, G object, V value,
       ConstraintDescriptorImpl<A> constraintDescriptor,
diff --git a/user/src/com/google/gwt/validation/rebind/ValidatorCreator.java b/user/src/com/google/gwt/validation/rebind/ValidatorCreator.java
index e8e8e83..968c5a7 100644
--- a/user/src/com/google/gwt/validation/rebind/ValidatorCreator.java
+++ b/user/src/com/google/gwt/validation/rebind/ValidatorCreator.java
@@ -190,11 +190,25 @@
     }
   }
 
-  private void writeThrowIllegalArgumnet(SourceWriter sourceWriter) {
-    sourceWriter.print("throw new IllegalArgumentException(\""
-        + this.validatorType.getName() + " can only validate ");
-    sourceWriter.print(beansToValidate.toString());
+  private void writeThrowIllegalArgumnet(SourceWriter sourceWriter,
+      String getClassName) {
+    // throw new IllegalArgumentException("MyValidator can not validate ",
+    sourceWriter.print("throw new IllegalArgumentException(\"");
+    sourceWriter.print(this.validatorType.getName() + " can not  validate \"");
+    sourceWriter.indent();
+    sourceWriter.indent();
+
+    // + object.getClass().getName() +". "
+    sourceWriter.print("+ ");
+    sourceWriter.print(getClassName);
+    sourceWriter.println("+ \". \"");
+
+    // + "Valid values are {Foo.clas, Bar.class}
+    sourceWriter.print("+ \"Valid types are ");
+    sourceWriter.print(beansToValidate.entrySet().toString());
     sourceWriter.println("\");");
+    sourceWriter.outdent();
+    sourceWriter.outdent();
   }
 
   private void writeTypeSupport(SourceWriter sw) {
@@ -229,7 +243,7 @@
       writeValidate(sw, bean);
     }
 
-    writeThrowIllegalArgumnet(sw);
+    writeThrowIllegalArgumnet(sw, "object.getClass().getName()");
 
     sw.outdent();
     sw.println("}");
@@ -265,7 +279,7 @@
       writeValidateProperty(sw, bean);
     }
 
-    writeThrowIllegalArgumnet(sw);
+    writeThrowIllegalArgumnet(sw, "object.getClass().getName()");
 
     sw.outdent();
     sw.println("}");
@@ -296,7 +310,7 @@
       writeValidateValue(sw, bean);
     }
 
-    writeThrowIllegalArgumnet(sw);
+    writeThrowIllegalArgumnet(sw, "beanType.getName()");
 
     sw.outdent();
     sw.println("}");