aboutsummaryrefslogtreecommitdiff
path: root/java/test/com/android/i18n/addressinput/StandardAddressVerifierTest.java
blob: f2c86f6737466cb8a3d0762fe0af9c4b88a06cdb (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
/*
 * 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 junit.framework.TestCase;

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

    private AddressProblems problems = new AddressProblems();
    private StandardAddressVerifier verifier;

    @Override
    protected void setUp() {
        problems.clear();
        verifier = new StandardAddressVerifier(new FieldVerifier(new ClientData(new CacheData())),
                                               StandardChecks.PROBLEM_MAP);
    }

    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.toString(), 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.toString(), 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);

        // ensure problem is unrecognized format and problem is in POSTAL_CODE
        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 checks for US addresses, which requires postal code. The following
        // address's postal code is wrong because it misses required field, not
        // because it mismatches 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.toString(), 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.toString(), problems.isEmpty());
    }

    public void testJapan() {
        // added AdminArea since address verification can't infer it from Kyoto City
        // commented out dependent locality since address verification doesn't use it
        // TODO: support inference of higher levels from lower ones
        // TODO: add dependent locality support for japan addresses
        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")  // Kyoto prefecture, added
                        .setLocality("Kyoto")  // Kyoto city
                        // .setDependentLocality("Minami-ku")
                        .setLanguageCode("ja_Latn")
                        .setCountry("JP")
                        .setPostalCode("601-8501")
                        .build();
        verifier.verify(address, problems);
        assertTrue(problems.toString(), problems.isEmpty());
    }
}