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 fixes.put("()~><@not.work", ""); 53 54 for (Map.Entry<String, String> e : fixes.entrySet()) { 55 assertEquals(e.getValue(), validator.fixText(e.getKey()).toString()); 56 } 57 } 58 59 @SmallTest 60 public void testEmailValidatorNullDomain() { 61 Rfc822Validator validator = new Rfc822Validator(null); 62 63 Map<String, String> fixes = new HashMap<String, String>(); 64 fixes.put("a", "<a>"); 65 fixes.put("a b", "<a b>"); 66 fixes.put("a@b", "<a@b>"); 67 fixes.put("a@b.com", "<a@b.com>"); // this one is correct 68 69 for (Map.Entry<String, String> e : fixes.entrySet()) { 70 assertEquals(e.getValue(), validator.fixText(e.getKey()).toString()); 71 } 72 } 73 74 @SmallTest 75 public void testEmailValidatorRemoveInvalid() { 76 Rfc822Validator validator = new Rfc822Validator("google.com"); 77 validator.setRemoveInvalid(true); 78 79 Map<String, String> fixes = new HashMap<String, String>(); 80 fixes.put("a", ""); 81 fixes.put("a b", ""); 82 fixes.put("a@b", ""); 83 fixes.put("a@b.com", "<a@b.com>"); // this one is correct 84 85 for (Map.Entry<String, String> e : fixes.entrySet()) { 86 assertEquals(e.getValue(), validator.fixText(e.getKey()).toString()); 87 } 88 } 89} 90