1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.wallace.antlr;
20
21 import java.io.StringReader;
22
23 import junit.framework.Assert;
24 import junit.framework.TestCase;
25 import antlr.RecognitionException;
26 import antlr.Token;
27 import antlr.TokenStreamException;
28
29 /***
30 * Verify the Antlr-generated parser code.
31 *
32 * @author rnewson
33 */
34 public class IMAP4CommandParserTest extends TestCase {
35
36 public void testParseLoginCommand() throws Exception {
37 getParser("a1 LOGIN username password\r\n").command();
38 }
39
40 public void testParseLowerCaseLoginCommand() throws Exception {
41 getParser("1 login testuser password\r\n").command();
42 }
43
44 public void testParseLoginWithUsernameWithSpacesCommand() throws Exception {
45 getParser("1 login \"test user\" password\r\n").command();
46 }
47
48 public void testParseLoginWithUsernameAndPasswordWithSpacesCommand() throws Exception {
49 getParser("1 login \"test user\" \"longer password\"\r\n").command();
50 }
51
52 public void testParseLoginWithUsernameThatLooksLikeABitLikeALiteralCommand() throws Exception {
53 getParser("1 login 009} password\r\n").command();
54 }
55
56 public void testParseCapabilityCommand() throws Exception {
57 getParser("99 CAPABILITY\r\n").command();
58 }
59
60 public void testParseLogoutCommand() throws Exception {
61 getParser("a1 LOGOUT\r\n").command();
62 }
63
64 public void testParseNoopCommand() throws Exception {
65 getParser("001 NOOP\r\n").command();
66 }
67
68 public void testParseCreateCommand() throws Exception {
69 getParser("001 CREATE INBOX\r\n").command();
70 }
71
72 public void testParseDeleteCommand() throws Exception {
73 getParser("001 DELETE INBOX\r\n").command();
74 }
75
76 public void testParseSubscribeCommand() throws Exception {
77 getParser("001 SUBSCRIBE INBOX\r\n").command();
78 }
79
80 public void testParseUnsubscribeCommand() throws Exception {
81 getParser("001 UNSUBSCRIBE INBOX\r\n").command();
82 }
83
84 public void testParseExamineCommand() throws Exception {
85 getParser("001 EXAMINE INBOX\r\n").command();
86 }
87
88 public void testParseSelectCommand() throws Exception {
89 getParser("001 SELECT INBOX\r\n").command();
90 }
91
92 public void testParseAppendCommand() throws Exception {
93 getParser("001 APPEND INBOX {10}\r\n0123456789\r\n").command();
94 }
95
96 public void testParseBadCreateCommand() throws Exception {
97 try {
98 getParser("CREATE INBOX\r\n").command();
99 } catch (RecognitionException e) {
100 return;
101 }
102 fail("Expected parse failure.");
103 }
104
105 public void testAstringLexer() throws Exception {
106 Assert.assertEquals(IMAP4TokenTypes.ASTRING, getToken("st").getType());
107 }
108
109 public void testLiteralLexer() throws Exception {
110 Assert.assertEquals(IMAP4TokenTypes.LITERAL, getToken("{1}\r\n0").getType());
111 }
112
113 public void testQuotedLexer() throws Exception {
114 Assert.assertEquals(IMAP4TokenTypes.ASTRING, getToken("\"*\"").getType());
115 }
116
117 private IMAP4CommandParser getParser(final String stringToParse) {
118 final IMAP4CommandLexer lexer = new IMAP4CommandLexer(new StringReader(stringToParse));
119 final IMAP4CommandParser parser = new IMAP4CommandParser(lexer);
120 return parser;
121 }
122
123 private Token getToken(final String stringToParse) throws TokenStreamException {
124 final IMAP4CommandLexer lexer = new IMAP4CommandLexer(new StringReader(stringToParse));
125 return lexer.nextToken();
126 }
127
128 }