aboutsummaryrefslogtreecommitdiff
path: root/java/test/com/android/i18n/addressinput/FieldVerifierTest.java
blob: 2988691214ef141efbd9589e8f103e31da146493 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Copyright (C) 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.android.i18n.addressinput;

import com.android.i18n.addressinput.testing.AddressDataMapLoader;

import junit.framework.TestCase;

import java.util.Arrays;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Map;
import java.util.Set;

/**
 * Spot check the standard data set for various cases of interest. This is not an exhaustive test.
 */
public class FieldVerifierTest extends TestCase {

  private static final StandardAddressVerifier VERIFIER =
      new StandardAddressVerifier(new FieldVerifier(
          new AddressVerificationData(AddressDataMapLoader.DATA)));

  private AddressProblems problems = new AddressProblems();

  @Override
  protected void setUp() {
    problems.clear();
  }

  public void testUnitedStatesOk() {
    AddressData addr = new AddressData.Builder().setCountry("US")
        .setAdminArea("CA")
        .setLocality("Mountain View")
        .setAddress("1234 Somewhere")
        .setPostalCode("94025").build();
    VERIFIER.verify(addr, problems);
    assertTrue(problems.toString(), problems.isEmpty());  // no mismatch
  }

  public void testUnitedStatesZipMismatch() {
    AddressData addr = new AddressData.Builder().setCountry("US")
        .setAdminArea("CA")
        .setLocality("Mountain View")
        .setPostalCode("12345").build();
    VERIFIER.verify(addr, problems);

    assertEquals(AddressProblemType.MISMATCHING_VALUE,
        problems.getProblem(AddressField.POSTAL_CODE));
  }

  public void testUnitedStatesNotOk() {
    AddressData addr = new AddressData.Builder().setCountry("US")
        .setAdminArea("CA")
        .setLocality(null)
        .setDependentLocality("Foo Bar")
        .setPostalCode("12345").build();
    VERIFIER.verify(addr, problems);

    assertEquals(AddressProblemType.MISMATCHING_VALUE,
        problems.getProblem(AddressField.POSTAL_CODE));
    assertEquals(AddressProblemType.MISSING_REQUIRED_FIELD,
        problems.getProblem(AddressField.LOCALITY));
  }

  public void testChinaOk() {
    AddressData addr = new AddressData.Builder().setCountry("CN")
        .setAdminArea("Beijing Shi")
        .setLocality("Xicheng Qu")
        .setAddress("Yitiao Lu")
        .setPostalCode("123456").build();
    VERIFIER.verify(addr, problems);
    assertTrue(problems.isEmpty());
  }

  public void testGermanAddress() {
    AddressData addr = new AddressData.Builder().setCountry("DE")
        .setLocality("Berlin")
        .setAddress("Huttenstr. 50")
        .setPostalCode("10553")
        .setOrganization("BMW AG Niederkassung Berlin")
        .setRecipient("Herr Diefendorf").build();

    VERIFIER.verify(addr, problems);
    assertTrue(problems.isEmpty());

    // Clones address but leave city empty.
    addr = new AddressData.Builder().set(addr).setLocality(null).build();

    VERIFIER.verify(addr, problems);
    assertEquals(AddressProblemType.MISSING_REQUIRED_FIELD,
        problems.getProblem(AddressField.LOCALITY));
  }

  public void testIrishAddress() {
    AddressData addr = new AddressData.Builder().setCountry("IE")
        .setLocality("Dublin")
        .setAdminArea("Co. Dublin")
        .setAddress("7424 118 Avenue NW")
        .setRecipient("Conan O'Brien").build();

    VERIFIER.verify(addr, problems);
    assertTrue(problems.toString(), problems.isEmpty());

    // Clones address but leave county empty. This address should be valid
    // since county is not required.
    addr = new AddressData.Builder().set(addr).setAdminArea(null).build();

    VERIFIER.verify(addr, problems);
    assertTrue(problems.toString(), problems.isEmpty());
  }

  public void testChinaPostalCodeBadFormat() {
    AddressData addr = new AddressData.Builder().setCountry("CN")
        .setAdminArea("Beijing Shi")
        .setLocality("Xicheng Qu")
        .setPostalCode("12345").build();
    VERIFIER.verify(addr, problems);

    assertEquals(AddressProblemType.UNRECOGNIZED_FORMAT,
        problems.getProblem(AddressField.POSTAL_CODE));
  }

  /**
   * If there is a postal code pattern for a certain country, and the input postal code is empty,
   * it should not be reported as bad postal code format. Whether empty postal code is ok should
   * be determined by checks for required fields.
   */
  public void testEmptyPostalCodeReportedAsGoodFormat() {
    // Chilean address has a postal code format pattern, but does not require
    // postal code. The following address is valid.
    AddressData addr = new AddressData.Builder().setCountry("CL")
        .setAddressLine1("GUSTAVO LE PAIGE ST #159")
        .setAdminArea("Atacama")
        .setLocality("San Pedro")
        .setPostalCode("")
        .build();
    VERIFIER.verify(addr, problems);
    assertTrue(problems.toString(), problems.isEmpty());

    problems.clear();

    // Now check for US addresses, which require a postal code. The following
    // address's postal code is wrong because it is missing a required field, not
    // because it doesn't match the expected postal code pattern.
    addr = new AddressData.Builder().setCountry("US").setPostalCode("").build();
    problems.clear();
    VERIFIER.verify(addr, problems);

    assertEquals(AddressProblemType.MISSING_REQUIRED_FIELD,
        problems.getProblem(AddressField.POSTAL_CODE));
  }

  public void testChinaTaiwanOk() {
    AddressData addr = new AddressData.Builder().setCountry("CN")
        .setAdminArea("Taiwan")
        .setLocality("Taichung City")
        .setDependentLocality("Situn District")
        .setAddress("12345 Yitiao Lu")
        .setPostalCode("407").build();
    VERIFIER.verify(addr, problems);
    assertTrue(problems.isEmpty());
  }

  public void testChinaTaiwanUnknownDistrict() {
    AddressData addr = new AddressData.Builder().setCountry("CN")
        .setAdminArea("Taiwan")
        .setLocality("Taichung City")
        .setDependentLocality("Foo Bar")
        .setPostalCode("400").build();
    VERIFIER.verify(addr, problems);

    assertEquals(AddressProblemType.UNKNOWN_VALUE,
        problems.getProblem(AddressField.DEPENDENT_LOCALITY));
  }

  public void testStreetVerification() {
    // Missing street address
    AddressData addr = new AddressData.Builder().setCountry("US")
        .setAdminArea("CA")
        .setLocality("Mountain View")
        .setPostalCode("94025").build();

    assertNull(addr.getAddressLine1());
    assertNull(addr.getAddressLine2());

    VERIFIER.verify(addr, problems);

    assertEquals(AddressProblemType.MISSING_REQUIRED_FIELD,
        problems.getProblem(AddressField.STREET_ADDRESS));
  }

  // Tests The Bahamas' address
  public void failingtestBahamas() {
    final AddressData address =
        new AddressData.Builder().setAddress("Abaco Beach Resort & Boat Habour")
        .setLocality("Treasure Cay")
        .setAdminArea("Abaco")
        .setCountry("BS").build();
    VERIFIER.verify(address, problems);
    assertTrue(problems.isEmpty());
  }

  public void testJapan() {
    // Added AdminArea since address verification can't infer it from Kyoto City
    // Commented out dependent locality since we don't have the data for this and in fact say
    // that it shouldn't be used for Japan.
    // TODO: support inference of higher levels from lower ones
    final AddressData address = new AddressData.Builder()
        .setRecipient("\u5BAE\u672C \u8302")  // SHIGERU_MIYAMOTO
        .setAddress("\u4E0A\u9CE5\u7FBD\u927E\u7ACB\u753A11\u756A\u5730")
        .setAdminArea("\u4eac\u90fd\u5e9c")  // Kyoto prefecture, added
        .setLocality("\u4EAC\u90FD\u5E02")  // Kyoto city
        // .setDependentLocality("\u5357\u533A")
        .setCountry("JP")
        .setPostalCode("601-8501").build();
    VERIFIER.verify(address, problems);
    assertTrue(problems.toString(), problems.isEmpty());
  }

  public void testJapanLatin() {
    // added AdminArea since address verification can't infer it from Kyoto City
    // commented out dependent locality since address verification doesn't use it
    final AddressData address = new AddressData.Builder()
        .setRecipient("Shigeru Miyamoto")  // SHIGERU_MIYAMOTO_ENGLISH
        .setAddress("11-1 Kamitoba-hokotate-cho")
        .setAdminArea("KYOTO")  // added
        .setLocality("Kyoto")
        // .setDependentLocality("Minami-ku")
        .setLanguageCode("ja_Latn")
        .setCountry("JP")
        .setPostalCode("601-8501").build();
    VERIFIER.verify(address, problems);
    assertTrue(problems.isEmpty());
  }

  public void testJapanLatinInvalidAdmin() {
    final AddressData address = new AddressData.Builder()
        .setRecipient("Shigeru Miyamoto")  // SHIGERU_MIYAMOTO_ENGLISH
        .setAddress("11-1 Kamitoba-hokotate-cho")
        .setAdminArea("Fake Admin")
        .setLocality("Kyoto")
        .setLanguageCode("ja_Latn")
        .setCountry("JP")
        .setPostalCode("601-8501").build();
    VERIFIER.verify(address, problems);
    assertFalse(problems.isEmpty());
    assertEquals(AddressProblemType.UNKNOWN_VALUE,
        problems.getProblem(AddressField.ADMIN_AREA));
  }

  public void testCanadaMixedCasePostcode() {
    final AddressData address = new AddressData.Builder()
        .setRecipient("Joe Bloggs")
        .setAddress("11 East St")
        .setLocality("Montreal")
        .setAdminArea("Quebec")
        .setCountry("CA")
        .setPostalCode("H2b 2y5").build();
    VERIFIER.verify(address, problems);
    assertTrue(problems.isEmpty());
  }

  public void testMultipleAddressLines() {
    final AddressData address = new AddressData.Builder()
        .setCountry("US")
        .setAdminArea("CA")
        .setLocality("Mountain View")
        .setAddressLine1("Somewhere")
        .setAddressLine2("1234")
        .setPostalCode("94025").build();
    VERIFIER.verify(address, problems);
    assertTrue(problems.isEmpty());
  }

  public void testFieldVerifierUsesRegionDataConstantsForFmtAndRequire() {
    Map<AddressDataKey, String> map = new EnumMap<AddressDataKey, String>(AddressDataKey.class);
    // Values for format and require are deliberately different from RegionDataConstants so that
    // we can test that the RDC's version is preferred.
    map.put(AddressDataKey.FMT, "%N%n%O");
    map.put(AddressDataKey.REQUIRE, "A");
    map.put(AddressDataKey.SUB_KEYS, "Test");
    map.put(AddressDataKey.ID, "data/FM");
    AddressVerificationNodeData testNode = new AddressVerificationNodeData(map);
    FieldVerifier fieldVerifier = new FieldVerifier(VERIFIER.rootVerifier, testNode);

    // Used and required obtained from RegionDataConstants for FM.
    Set<AddressField> expectedPossibleFields = EnumSet.of(AddressField.RECIPIENT,
        AddressField.ORGANIZATION, AddressField.STREET_ADDRESS, AddressField.LOCALITY,
        AddressField.ADMIN_AREA, AddressField.POSTAL_CODE, AddressField.COUNTRY);
    Set<AddressField> expectedRequiredField = EnumSet.of(AddressField.STREET_ADDRESS,
        AddressField.LOCALITY, AddressField.ADMIN_AREA, AddressField.POSTAL_CODE,
        AddressField.COUNTRY);
    assertEquals(expectedPossibleFields, fieldVerifier.possiblyUsedFields);
    assertEquals(expectedRequiredField, fieldVerifier.required);
    assertEquals("data/FM", fieldVerifier.id);
    // Keys should be populated from the test node.
    assertEquals("[Test]", Arrays.toString(fieldVerifier.keys));
  }
}