Merge pull request #35 from lararennie/label_fixes

Bug-fix in address verifier, plus removal of unused code
diff --git a/java/src/com/android/i18n/addressinput/StandardAddressVerifier.java b/java/src/com/android/i18n/addressinput/StandardAddressVerifier.java
index 832f1aa..c9ee9f2 100644
--- a/java/src/com/android/i18n/addressinput/StandardAddressVerifier.java
+++ b/java/src/com/android/i18n/addressinput/StandardAddressVerifier.java
@@ -43,46 +43,25 @@
 
   protected final FieldVerifier rootVerifier;
 
-  protected final VerifierRefiner refiner;
-
   protected final Map<AddressField, List<AddressProblemType>> problemMap;
 
   /**
-   * Uses the rootVerifier and {@link #DEFAULT_REFINER} to perform the standard checks on the
-   * address fields, as defined in {@link StandardChecks}.
+   * Uses the rootVerifier to perform the standard checks on the address fields, as defined in
+   * {@link StandardChecks}.
    */
   public StandardAddressVerifier(FieldVerifier rootVerifier) {
-    this(rootVerifier, DEFAULT_REFINER, StandardChecks.PROBLEM_MAP);
+    this(rootVerifier, StandardChecks.PROBLEM_MAP);
   }
 
   /**
-   * Uses the rootVerifier and the refiner to perform the standard checks on the address fields,
-   * as defined in {@link StandardChecks}.
-   */
-  public StandardAddressVerifier(FieldVerifier rootVerifier, VerifierRefiner refiner) {
-    this(rootVerifier, refiner, StandardChecks.PROBLEM_MAP);
-  }
-
-  /**
-   * Uses the rootVerifier and {@link #DEFAULT_REFINER} to perform the given checks on the address
-   * fields. A reference to problemMap is maintained. It is not modified by this class, and should
-   * not be modified subsequent to this call.
+   * Uses the rootVerifier to perform the given checks on the address fields. A reference to
+   * problemMap is maintained. It is not modified by this class, and should not be modified
+   * subsequent to this call.
    */
   public StandardAddressVerifier(FieldVerifier rootVerifier,
       Map<AddressField, List<AddressProblemType>> problemMap) {
-    this(rootVerifier, DEFAULT_REFINER, problemMap);
-  }
-
-  /**
-   * Uses the rootVerifier and the refiner to perform the given checks on the address fields. A
-   * reference to problemMap is maintained. It is not modified by this class, and should not be
-   * modified subsequent to this call.
-   */
-  public StandardAddressVerifier(FieldVerifier rootVerifier, VerifierRefiner refiner,
-      Map<AddressField, List<AddressProblemType>> problemMap) {
     this.rootVerifier = rootVerifier;
-    this.refiner = refiner;
-    this.problemMap = StandardChecks.PROBLEM_MAP;
+    this.problemMap = problemMap;
   }
 
   public void verify(AddressData address, AddressProblems problems) {
@@ -211,31 +190,4 @@
       String datum, AddressProblems problems) {
     return verifier.check(script, problem, field, datum, problems);
   }
-
-  /**
-   * This gets called with the hierarchical fields COUNTRY, ADMIN_AREA, LOCALITY,
-   * DEPENDENT_LOCALITY in order, returning the refined verifier at each step.
-   *
-   * <p>The default implementation is stateless, and delegates to the verifier to do the
-   * refinement.
-   */
-  public static class VerifierRefiner {
-
-    /**
-     * Refines the verifier.  This delegates to the verifier to perform the refinement.
-     */
-    public FieldVerifier refineVerifier(FieldVerifier v, AddressField field, String subkey) {
-      return v.refineVerifier(subkey);
-    }
-
-    /**
-     * Returns a clean version of the refiner.  Since this implementation is stateless, returns
-     * this.
-     */
-    public VerifierRefiner newInstance() {
-      return this;
-    }
-  }
-
-  private static final VerifierRefiner DEFAULT_REFINER = new VerifierRefiner();
 }
diff --git a/java/test/com/android/i18n/addressinput/StandardAddressVerifierTest.java b/java/test/com/android/i18n/addressinput/StandardAddressVerifierTest.java
index f47e64f..c5a7558 100644
--- a/java/test/com/android/i18n/addressinput/StandardAddressVerifierTest.java
+++ b/java/test/com/android/i18n/addressinput/StandardAddressVerifierTest.java
@@ -18,6 +18,11 @@
 
 import junit.framework.TestCase;
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * Spot check the standard data set for various cases of interest. This is not an exhaustive test.
  */
@@ -33,6 +38,50 @@
         StandardChecks.PROBLEM_MAP);
   }
 
+  public void testCustomProblemMapRespected() {
+    AddressData usAddress = new AddressData.Builder().setCountry("US")
+        .setAdminArea("Fake")
+        .setAddress("1234 Somewhere")
+        .setPostalCode("9402")
+        .build();
+    verifier.verify(usAddress, problems);
+    assertFalse(problems.toString(), problems.isEmpty());
+
+    assertEquals(AddressProblemType.UNRECOGNIZED_FORMAT,
+        problems.getProblem(AddressField.POSTAL_CODE));
+    assertEquals(AddressProblemType.UNKNOWN_VALUE,
+        problems.getProblem(AddressField.ADMIN_AREA));
+    // TODO: The standard address verifier fails to properly validate lower-level fields if a
+    // higher-level field is of the wrong value. This is incorrect, and needs to be fixed.
+    // assertEquals(AddressProblemType.MISSING_REQUIRED_FIELD,
+    //      problems.getProblem(AddressField.LOCALITY));
+
+    // Now pass in a custom problem map.
+    Map<AddressField, List<AddressProblemType>> customProblems =
+        new HashMap<AddressField, List<AddressProblemType>>();
+    StandardAddressVerifier emptyProblemVerifier = new StandardAddressVerifier(
+        new FieldVerifier(new ClientData(new CacheData())), customProblems);
+    problems.clear();
+    emptyProblemVerifier.verify(usAddress, problems);
+    // We aren't looking for any problems, so shouldn't find any.
+    assertTrue(problems.toString(), problems.isEmpty());
+
+    // Lastly try with a map that only looks for postal code problems.
+    List<AddressProblemType> postalCodeProblems = new ArrayList<AddressProblemType>();
+    postalCodeProblems.add(AddressProblemType.UNRECOGNIZED_FORMAT);
+    postalCodeProblems.add(AddressProblemType.MISSING_REQUIRED_FIELD);
+    customProblems.put(AddressField.POSTAL_CODE, postalCodeProblems);
+
+    StandardAddressVerifier postalCodeProblemVerifier = new StandardAddressVerifier(
+        new FieldVerifier(new ClientData(new CacheData())), customProblems);
+    problems.clear();
+    postalCodeProblemVerifier.verify(usAddress, problems);
+    assertFalse(problems.toString(), problems.isEmpty());
+    assertEquals(AddressProblemType.UNRECOGNIZED_FORMAT,
+        problems.getProblem(AddressField.POSTAL_CODE));
+    assertNull(problems.getProblem(AddressField.ADMIN_AREA));
+  }
+
   public void testUnitedStatesOk() {
     AddressData addr = new AddressData.Builder().setCountry("US")
         .setAdminArea("CA")