KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > internet > HeaderTokenizerTest


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package javax.mail.internet;
19
20 import javax.mail.internet.HeaderTokenizer.Token;
21
22 import junit.framework.TestCase;
23
24 /**
25  * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
26  */

27 public class HeaderTokenizerTest extends TestCase {
28     public void testTokenizer() throws ParseException {
29         Token t;
30         HeaderTokenizer ht;
31         ht =
32             new HeaderTokenizer("To: \"Geronimo List\" <geronimo-dev@apache.org>, \n\r Geronimo User <geronimo-user@apache.org>");
33         assertEquals("To", ht.peek().getValue());
34         assertEquals("To", ht.next().getValue());
35         assertEquals(":", ht.peek().getValue());
36         assertEquals(":", ht.next().getValue());
37         t = ht.next();
38         assertEquals("Geronimo List", t.getValue());
39         assertEquals(Token.QUOTEDSTRING, t.getType());
40         assertEquals("<", ht.next().getValue());
41         assertEquals("geronimo-dev", ht.next().getValue());
42         assertEquals("@", ht.next().getValue());
43         assertEquals("apache", ht.next().getValue());
44         assertEquals(".", ht.next().getValue());
45         assertEquals("org", ht.next().getValue());
46         assertEquals(">", ht.next().getValue());
47         assertEquals(",", ht.next().getValue());
48         assertEquals("Geronimo", ht.next().getValue());
49         assertEquals("User", ht.next().getValue());
50         assertEquals("<", ht.next().getValue());
51         assertEquals("geronimo-user", ht.next().getValue());
52         assertEquals("@", ht.next().getValue());
53         assertEquals("apache", ht.next().getValue());
54         assertEquals(".", ht.next().getValue());
55         assertEquals("org>", ht.getRemainder());
56         assertEquals("org", ht.peek().getValue());
57         assertEquals("org>", ht.getRemainder());
58         assertEquals("org", ht.next().getValue());
59         assertEquals(">", ht.next().getValue());
60         assertEquals(Token.EOF, ht.next().getType());
61         ht = new HeaderTokenizer(" ");
62         assertEquals(Token.EOF, ht.next().getType());
63         ht = new HeaderTokenizer("J2EE");
64         assertEquals("J2EE", ht.next().getValue());
65         assertEquals(Token.EOF, ht.next().getType());
66         // test comments
67
doComment(true);
68         doComment(false);
69     }
70     public void doComment(boolean ignore) throws ParseException {
71         HeaderTokenizer ht;
72         Token t;
73         ht =
74             new HeaderTokenizer(
75                 "Apache(Geronimo)J2EE",
76                 HeaderTokenizer.RFC822,
77                 ignore);
78         t = ht.next();
79         assertEquals("Apache", t.getValue());
80         assertEquals(Token.ATOM, t.getType());
81         if (!ignore) {
82             t = ht.next();
83             assertEquals("Geronimo", t.getValue());
84             assertEquals(Token.COMMENT, t.getType());
85         }
86         t = ht.next();
87         assertEquals("J2EE", t.getValue());
88         assertEquals(Token.ATOM, t.getType());
89         assertEquals(Token.EOF, ht.next().getType());
90     }
91 }
92
Popular Tags