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