1 /* 2 * Copyright (C) 2008 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 17 package android.text.util.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.fail; 21 22 import android.support.test.filters.SmallTest; 23 import android.support.test.runner.AndroidJUnit4; 24 import android.text.util.Rfc822Token; 25 import android.text.util.Rfc822Tokenizer; 26 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 33 /** 34 * Test {@link Rfc822Tokenizer}. 35 */ 36 @SmallTest 37 @RunWith(AndroidJUnit4.class) 38 public class Rfc822TokenizerTest { 39 @Test 40 public void testConstructor() { 41 new Rfc822Tokenizer(); 42 } 43 44 @Test 45 public void testFindTokenStart() { 46 Rfc822Tokenizer rfc822Tokenizer = new Rfc822Tokenizer(); 47 48 String token1 = "John Doe <janed (at) example.net> (work)"; 49 String token2 = "Ann Lee <annlee (at) example.com> (secret)"; 50 String token3 = "Charles Hanson (work)"; 51 String token4 = "Lucia Smith <lsmith (at) example.com>"; 52 String text = ", " + token1 + "\",\"" + token2 + ";" + token3 + " <;>" + token4; 53 final int TOKEN_START_POS_1 = text.indexOf(token1); 54 final int TOKEN_START_POS_2 = text.indexOf(token2); 55 final int TOKEN_START_POS_3 = text.indexOf(token3); 56 final int TOKEN_START_POS_4 = text.indexOf(token4); 57 assertEquals(0, rfc822Tokenizer.findTokenStart(text, 0)); 58 assertEquals(0, rfc822Tokenizer.findTokenStart(text, TOKEN_START_POS_1)); 59 assertEquals(TOKEN_START_POS_1, rfc822Tokenizer.findTokenStart(text, TOKEN_START_POS_2)); 60 // token 2 is ignored because ',' between token1 and token2 is in a pair of \". 61 assertEquals(TOKEN_START_POS_1, rfc822Tokenizer.findTokenStart(text, TOKEN_START_POS_3)); 62 assertEquals(TOKEN_START_POS_3, rfc822Tokenizer.findTokenStart(text, TOKEN_START_POS_4)); 63 // token 4 is ignored because ',' between token3 and token4 is in <>. 64 assertEquals(TOKEN_START_POS_3, rfc822Tokenizer.findTokenStart(text, text.length())); 65 66 assertEquals(0, rfc822Tokenizer.findTokenStart(text, -1)); 67 assertEquals(TOKEN_START_POS_3, rfc822Tokenizer.findTokenStart(text, text.length() + 1)); 68 69 try { 70 rfc822Tokenizer.findTokenStart(null, TOKEN_START_POS_1); 71 fail("Should throw NullPointerException!"); 72 } catch (NullPointerException e) { 73 // issue 1695243, not clear what is supposed to happen if text is null. 74 } 75 } 76 77 @Test 78 public void testFindTokenEnd() { 79 Rfc822Tokenizer rfc822Tokenizer = new Rfc822Tokenizer(); 80 81 String token1 = "John Doe <janed (at) example.net> (work)"; 82 String text1 = ", " + token1; 83 assertEquals(0, rfc822Tokenizer.findTokenEnd(text1, 0)); 84 assertEquals(text1.length(), rfc822Tokenizer.findTokenEnd(text1, 1)); 85 // issue 1695243 86 // not clear whether it is supposed result if cursor is exceptional value. 87 // bigger than the text length. 88 assertEquals(Integer.MAX_VALUE, rfc822Tokenizer.findTokenEnd(text1, Integer.MAX_VALUE)); 89 // smaller than 0 90 try { 91 rfc822Tokenizer.findTokenEnd(text1, -1); 92 fail("Should throw IndexOutOfBoundsException!"); 93 } catch (IndexOutOfBoundsException e) { 94 } 95 96 String token2 = "Ann Lee <annlee (at) example.com> (secret)"; 97 String token3 = "Charles Hanson (work)"; 98 String token4 = "Lucia Smith <lsmith (at) example.com>"; 99 String text2 = token1 + "\",\"" + token2 + ";" + token3 + " <;>" + token4 + ","; 100 final int TOKEN_END_POS_2 = text2.indexOf(token2) + token2.length(); 101 final int TOKEN_END_POS_4 = text2.indexOf(token4) + token4.length(); 102 assertEquals(TOKEN_END_POS_2, rfc822Tokenizer.findTokenEnd(text2, 0)); 103 assertEquals(TOKEN_END_POS_4, rfc822Tokenizer.findTokenEnd(text2, TOKEN_END_POS_2 + 1)); 104 } 105 106 @Test(expected=NullPointerException.class) 107 public void testFindTokenEndNull() { 108 Rfc822Tokenizer rfc822Tokenizer = new Rfc822Tokenizer(); 109 110 rfc822Tokenizer.findTokenEnd(null, 0); 111 } 112 113 @Test 114 public void testTerminateToken() { 115 Rfc822Tokenizer rfc822Tokenizer = new Rfc822Tokenizer(); 116 117 String comma = ","; 118 String space = " "; 119 120 String text = "text"; 121 assertEquals(text + comma + space, rfc822Tokenizer.terminateToken(text)); 122 123 text = null; 124 // issue 1695243, not clear what is supposed result if text is null. 125 assertEquals(text + comma + space, rfc822Tokenizer.terminateToken(null)); 126 } 127 128 @Test 129 public void testTokenize() { 130 Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(""); 131 assertEquals(0, tokens.length); 132 133 String text = "\"Berg\" (home) <berg\\@example.com>, tom\\@example.com (work)"; 134 tokens = Rfc822Tokenizer.tokenize(text); 135 assertEquals(2, tokens.length); 136 verifyLocalAssertEquals(tokens[0], "Berg", "berg\\@example.com", "home"); 137 verifyLocalAssertEquals(tokens[1], null, "tom\\@example.com", "work"); 138 139 text = "Foo Bar (something) <foo\\@example.com>, blah\\@example.com (something)"; 140 tokens = Rfc822Tokenizer.tokenize(text); 141 assertEquals(2, tokens.length); 142 verifyLocalAssertEquals(tokens[0], "Foo Bar", "foo\\@example.com", "something"); 143 verifyLocalAssertEquals(tokens[1], null, "blah\\@example.com", "something"); 144 } 145 146 @Test(expected=NullPointerException.class) 147 public void testTokenizeNull() { 148 Rfc822Tokenizer.tokenize(null); 149 } 150 151 @Test 152 public void testTokenize_withListParam() { 153 final List<Rfc822Token> list = new ArrayList<>(); 154 Rfc822Tokenizer.tokenize("", list); 155 assertEquals(0, list.size()); 156 157 String text = "\"Berg\" (home) <berg\\@example.com>, tom\\@example.com (work)"; 158 Rfc822Tokenizer.tokenize(text, list); 159 assertEquals(2, list.size()); 160 verifyLocalAssertEquals(list.get(0), "Berg", "berg\\@example.com", "home"); 161 verifyLocalAssertEquals(list.get(1), null, "tom\\@example.com", "work"); 162 163 text = "Foo Bar (something) <foo\\@example.com>, blah\\@example.com (something)"; 164 list.clear(); 165 Rfc822Tokenizer.tokenize(text, list); 166 assertEquals(2, list.size()); 167 verifyLocalAssertEquals(list.get(0), "Foo Bar", "foo\\@example.com", "something"); 168 verifyLocalAssertEquals(list.get(1), null, "blah\\@example.com", "something"); 169 } 170 171 172 @Test(expected=NullPointerException.class) 173 public void testTokenize_withListParamNull() { 174 Rfc822Tokenizer.tokenize(null); 175 } 176 177 /** 178 * Assert the specified token's name, address and comment all equal specified ones. 179 * @param token the Rfc822Token to be asserted. 180 * @param name expected name. 181 * @param address expected address. 182 * @param comment expected comment. 183 */ 184 private void verifyLocalAssertEquals(Rfc822Token token, String name, 185 String address, String comment) { 186 assertEquals(name, token.getName()); 187 assertEquals(address, token.getAddress()); 188 assertEquals(comment, token.getComment()); 189 } 190 } 191