001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.mail.parser;
019:
020: import java.util.List;
021: import java.util.Vector;
022:
023: import junit.framework.TestCase;
024:
025: /**
026: * @author fdietz
027: *
028: */
029: public class ListParserTest extends TestCase {
030:
031: public void testCreateListFromStringNull() {
032:
033: try {
034: ListParser.createListFromString(null);
035: fail();
036: } catch (IllegalArgumentException e) {
037: }
038: }
039:
040: public void testCreateListFromStringEmpty() {
041: String s = "";
042:
043: List<String> l = ListParser.createListFromString(s);
044: assertEquals("list size 0", 0, l.size());
045: }
046:
047: public void testCreateListFromString() {
048: String s = "test@test.de";
049:
050: List<String> l = ListParser.createListFromString(s);
051: assertEquals("list size 1", 1, l.size());
052:
053: assertEquals("test@test.de", l.get(0));
054: }
055:
056: public void testCreateListFromString2() {
057: String s = "test@test.de, test2@test2.de";
058:
059: List<String> l = ListParser.createListFromString(s);
060: assertEquals("list size 2", 2, l.size());
061:
062: assertEquals("test@test.de", l.get(0));
063: assertEquals("test2@test2.de", l.get(1));
064:
065: }
066:
067: /**
068: * test if leading or trailing whitespaces are trimmed correctly
069: *
070: */
071: public void testCreateListFromString3() {
072: String s = "test@test.de,test2@test2.de, MyGroup, My Test Group";
073:
074: List<String> l = ListParser.createListFromString(s);
075: assertEquals("list size 4", 4, l.size());
076:
077: assertEquals("test@test.de", l.get(0));
078: assertEquals("test2@test2.de", l.get(1));
079: assertEquals("MyGroup", l.get(2));
080: assertEquals("My Test Group", l.get(3));
081: }
082:
083: /**
084: * test if a comma doesn't disturb our parser if enclosed in double-quotes
085: *
086: */
087: public void testCreateListFromString4() {
088: String s = "test@test.de, Firstname Lastname, \"Lastname, Firstname\"";
089:
090: List<String> l = ListParser.createListFromString(s);
091: assertEquals("list size 3", 3, l.size());
092:
093: assertEquals("test@test.de", l.get(0));
094: assertEquals("Firstname Lastname", l.get(1));
095: assertEquals("Lastname, Firstname", l.get(2));
096:
097: }
098:
099: /**
100: * test if \" characters are removed in the list, we only need this in the
101: * String representation
102: */
103: public void testCreateListFromString5() {
104: String s = "test@test.de, \"Firstname Lastname\", \"Lastname, Firstname\"";
105:
106: List<String> l = ListParser.createListFromString(s);
107: assertEquals("list size 3", 3, l.size());
108:
109: assertEquals("test@test.de", l.get(0));
110: assertEquals("Firstname Lastname", l.get(1));
111: assertEquals("Lastname, Firstname", l.get(2));
112: }
113:
114: /**
115: * Test displayname and address with and without comma
116: */
117: public void testCreateListFromString6() {
118: String s = "test@test.de, \"Firstname Lastname\" <mail@mail.org>, \"Lastname, Firstname\" <mail@mail.org>";
119:
120: List<String> l = ListParser.createListFromString(s);
121: assertEquals("list size 3", 3, l.size());
122:
123: assertEquals("test@test.de", l.get(0));
124: assertEquals("Firstname Lastname <mail@mail.org>", l.get(1));
125: assertEquals("Lastname, Firstname <mail@mail.org>", l.get(2));
126: }
127:
128: public void testCreateStringFromListNull() {
129: try {
130: ListParser.createStringFromList(new Vector<String>(), null);
131: fail();
132: } catch (IllegalArgumentException e) {
133: }
134: }
135:
136: public void testCreateStringFromListNull2() {
137: try {
138: ListParser.createStringFromList(null, ";");
139: fail();
140: } catch (IllegalArgumentException e) {
141: }
142: }
143:
144: public void testCreateStringFromListEmpty() {
145:
146: List<String> list = new Vector<String>();
147:
148: String result = ListParser.createStringFromList(list, ";");
149: assertEquals("", result);
150: }
151:
152: public void testCreateStringFromList() {
153:
154: List<String> list = new Vector<String>();
155: list.add("test@test.de");
156: list.add("test2@test2.de");
157:
158: String result = ListParser.createStringFromList(list, ",");
159: assertEquals("test@test.de, test2@test2.de, ", result);
160: }
161:
162: /**
163: * Test if \" character disturbs our parser
164: */
165: public void testCreateStringFromList2() {
166:
167: List<String> list = new Vector<String>();
168: list.add("test@test.de");
169: list.add("\"My yours and he's list\"");
170:
171: String result = ListParser.createStringFromList(list, ",");
172: assertEquals("test@test.de, My yours and he's list, ", result);
173: }
174:
175: /**
176: * Test if "," inside a contact item is escaped correctly
177: *
178: */
179: public void testCreateStringFromList4() {
180:
181: List<String> list = new Vector<String>();
182: list.add("test@test.de");
183: list.add("Firstname Lastname");
184: list.add("\"Lastname, Firstname\"");
185:
186: String result = ListParser.createStringFromList(list, ",");
187: assertEquals(
188: "test@test.de, Firstname Lastname, \"Lastname, Firstname\", ",
189: result);
190: }
191:
192: /**
193: * Test what parser does if contact item already contains an escaped
194: * representation
195: */
196: public void testCreateStringFromList5() {
197:
198: List<String> list = new Vector<String>();
199: list.add("test@test.de");
200: list.add("\"Firstname Lastname\" <mail@mail.org>");
201: list.add("\"Lastname, Firstname\" <mail@mail.org>");
202:
203: String result = ListParser.createStringFromList(list, ",");
204: assertEquals(
205: "test@test.de, Firstname Lastname <mail@mail.org>, \"Lastname, Firstname\" <mail@mail.org>, ",
206: result);
207: }
208:
209: }
|