Rfc822ValidatorTest.java revision b4f5e0e5755938f2be9a2f1a1a6609b018c530ad
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.common;
18
19import android.test.suitebuilder.annotation.SmallTest;
20
21import java.util.HashMap;
22import java.util.Map;
23
24import junit.framework.TestCase;
25
26public class Rfc822ValidatorTest extends TestCase {
27
28    @SmallTest
29    public void testEmailValidator() {
30        Rfc822Validator validator = new Rfc822Validator("gmail.com");
31        String[] validEmails = new String[] {
32            "a@b.com", "a@b.fr", "a+b@c.com", "a@b.info", "john@example.com", "john@example.fr",
33            "john@corp.example.com",
34        };
35
36        for (String email : validEmails) {
37            assertTrue(email + " should be a valid email address", validator.isValid(email));
38        }
39
40        String[] invalidEmails = new String[] {
41            "a", "a@b", "a b", "a@b.12", "john@example..com", "johnexample.com", "john.example.com"
42        };
43
44        for (String email : invalidEmails) {
45            assertFalse(email + " should not be a valid email address", validator.isValid(email));
46        }
47
48        Map<String, String> fixes = new HashMap<String, String>();
49        fixes.put("a", "<a@gmail.com>");
50        fixes.put("a b", "<ab@gmail.com>");
51        fixes.put("a@b", "<a@b>");
52
53        for (Map.Entry<String, String> e : fixes.entrySet()) {
54            assertEquals(e.getValue(), validator.fixText(e.getKey()).toString());
55        }
56    }
57}
58