KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tests > jfun > parsec > Rfc822TestCase


1 /*
2  * Created on Dec 8, 2004
3  *
4  * Author Ben Yu
5  */

6 package tests.jfun.parsec;
7
8 import java.io.FileReader JavaDoc;
9 import java.io.IOException JavaDoc;
10
11 import tests.jfun.parsec.rfc822.AddressParser;
12 import tests.jfun.parsec.rfc822.DateTimeParser;
13 import tests.jfun.parsec.rfc822.LexicalRules;
14 import tests.jfun.parsec.rfc822.MessageParser;
15 import tests.jfun.parsec.rfc822.ToString;
16
17
18
19 import jfun.parsec.ParserException;
20 import jfun.parsec.Scanners;
21 import jfun.parsec.Parser;
22 import jfun.parsec.Parsers;
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26
27 /**
28  * @author Ben Yu
29  *
30  * Dec 8, 2004
31  */

32 public class Rfc822TestCase extends TestCase {
33   public static Test suite(){
34     return new TestSuite(Rfc822TestCase.class);
35   }
36   public static void main(String JavaDoc[] args){
37     junit.textui.TestRunner.run(suite());
38   }
39   public void testChr(){
40     final Parser chr = LexicalRules.chr;
41     testGood("1", chr);
42     testGood("a", chr);
43     testGood(""+(char)0, chr);
44     testGood(""+(char)1, chr);
45     testGood(""+(char)127, chr);
46     System.out.println(testBad(""+(char)128, chr));
47     System.out.println(testBad(""+(char)255, chr));
48     //System.out.println(testBad("?", chr));
49
}
50   public void testLwsp(){
51     final Parser lwsp = LexicalRules.lwsp;
52     testGood(" ", lwsp);
53     testGood(" \t\t", lwsp);
54     testGood(" \r\n\t", lwsp);
55     testGood(" \r\n\t \r\n ", lwsp);
56     System.out.println(testBad("", lwsp));
57     System.out.println(testBad("\r", lwsp));
58     System.out.println(testBad("\n", lwsp));
59     System.out.println(testBad("\n ", lwsp));
60     System.out.println(testBad(" \r\n", lwsp));
61     System.out.println(testBad("a", lwsp));
62     
63   }
64   public void testComment(){
65     final Parser comm = LexicalRules.comment;
66     testGood("()", comm);
67     testGood("(())", comm);
68     testGood("((\\\"))", comm);
69     testGood("(abc)", comm);
70     testGood("( ab \r\n )", comm);
71     testGood("( ab \n )", comm);
72     testGood("(\r\n ab \n )", comm);
73     testGood("(\r\n ab \\)\n )", comm);
74     testError("", comm);
75     testError("a", comm);
76     testError("(()", comm);
77     testError("())", comm);
78     testError("(\r)", comm);
79     testError("(\\)", comm);
80     testError("(abc", comm);
81   }
82   public void testQuotedString(){
83     final Parser qstr = LexicalRules.quoted_string;
84     testGood("\"\"", qstr);
85     testGood("\"\\\"\"", qstr);
86     testGood("\"\\\"\n \"", qstr);
87     testGood("\"\\\"\r\n \"", qstr);
88     testGood("\"\\\"\\\r\n\"", qstr);
89     testGood("\"\\\"\\\r\r\n\t\"", qstr);
90     testError("\"", qstr);
91     testError("\"\\\"", qstr);
92     testError("\"\r\"", qstr);
93     testError("\"\r\n\"", qstr);
94   }
95   public void testText(){
96     final Parser text = LexicalRules.text;
97     testGood("a", text);
98     testGood("\r", text);
99     testGood("\n", text);
100     testGood("\\", text);
101     testGood("\"", text);
102     testError("\r\n", text.repeat(2));
103   }
104   private final AddressParser aparser = new AddressParser();
105   private final DateTimeParser dparser = new DateTimeParser();
106   private final MessageParser mparser = new MessageParser();
107   public void testAtom(){
108     final Parser atom = aparser.atom;
109     testAddressParser("abc", atom);
110     testAddressParser("1", atom);
111     testAddressParser("123", atom);
112   }
113   public void testDot(){
114     final Parser op = aparser.dot;
115     testAddressParser(".", op);
116   }
117   /*
118   public void testCommas(){
119     final Parser commas = aparser.commas;
120     testAddressParser(",,,", commas);
121     testAddressParser(",", commas);
122   }*/

123   public void testDomain(){
124     final Parser domain = aparser.domain;
125     System.out.println(ToString.printDomain((String JavaDoc[])testAddressParser("abc.x.y", domain)));
126     System.out.println(ToString.printDomain((String JavaDoc[])testAddressParser("abc.x.[y]", domain)));
127     testAddressParserErr("a.", domain);
128     testAddressParserErr("a.\"b\"", domain);
129   }
130   public void testPhrase(){
131     final Parser phrase = aparser.phrase;
132     System.out.println(ToString.printPhrase((String JavaDoc[])testAddressParser("hello world", phrase)));
133   }
134   public void testLocalpart(){
135     final Parser local = aparser.local_part;
136     System.out.println(ToString.printLocalPart((String JavaDoc[])testAddressParser("hello.world", local)));
137   }
138   public void testAddrSpec(){
139     final Parser addr = aparser.addr_spec;
140     System.out.println(testAddressParser("hello.world@TOM.COM", addr));
141   }
142   public void testRouteAddr(){
143     final Parser ra = aparser.route_addr;
144     System.out.println(testAddressParser("<hello.world@TOM.COM>", ra));
145     System.out.println(testAddressParser("<@a:hello.world@TOM.COM>", ra));
146     System.out.println(testAddressParser("<@a,,@b:hello.world@TOM.COM>", ra));
147   }
148   public void testMailbox(){
149     final Parser mb = aparser.mailbox;
150     System.out.println(testAddressParser("hello.world@TOM.COM", mb));
151     System.out.println(testAddressParser("hello \" \r\n \"<@a:hello.world@TOM.COM>", mb));
152     System.out.println(testAddressParser(" this is tom <@a,,@b:hello.world@TOM.COM>", mb));
153   }
154   public void testMailboxes(){
155     final Parser mb = aparser.mailboxes;
156     System.out.println(testAddressParser("this is tom<@a,,@b:hello.world@TOM.COM>,,this is tom<@a,,@b:hello.world@TOM.COM>;", mb));
157      
158   }
159   public void testAddress(){
160     final Parser mb = aparser.address;
161     System.out.println(testAddressParser("hello.world@TOM.COM", mb));
162     System.out.println(testAddressParser("hello<@a:hello.world@TOM.COM>", mb));
163     System.out.println(testAddressParser("this is tom<@a,,@b:hello.world@TOM.COM>", mb));
164     System.out.println(testAddressParser("this is a phrase:this is tom<@a,,@b:hello.world@TOM.COM>;", mb));
165     System.out.println(testAddressParser("this is a phrase:;", mb));
166     System.out.println(testAddressParser("this is a phrase:this is tom<@a,,@b:hello.world@TOM.COM>,hello.world@TOM.COM,this is tom<@a,,@b:hello.world@TOM.COM>;", mb));
167   }
168   public void testAddresses(){
169     testFileAddresses("test/addresses.txt");
170   }
171   
172   public void testDigit2(){
173     final Parser p = dparser.pdigit2;
174     System.out.println(testDateTimeParser("11", p));
175   }
176   public void testDate(){
177     final Parser p = dparser.pdate;
178     System.out.println(testDateTimeParser("10 Nov 03", p));
179   }
180
181   public void testDateTime(){
182     final Parser p = dparser.pdatetime;
183     System.out.println(testDateTimeParser("Wed, 10 Nov 03 10:20:10 ut", p));
184     System.out.println(testDateTimeParser("10 Nov 03 10:20:10 ut", p));
185     System.out.println(testDateTimeParser("Wed, 10 Nov 03 10:20:10 +1000", p));
186     System.out.println(testDateTimeParser("Wed, 10 Nov 03 10:20 a", p));
187     System.out.println(testDateTimeParser("7 Nov 03 10:20 a", p));
188     System.out.println(testDateTimeParser("7 Nov 03 10:20 a", p));
189     testDateTimeParserErr("7 Nov 03 10:20 ab", p);
190     testDateTimeParserErr("7 Nov 03 10:20:351 a", p);
191     testDateTimeParserErr("7 Nov 03 10:20: a", p);
192     testDateTimeParserErr("11 Nov 03 10:20:2a a", p);
193   }
194   
195   public void testMsgId(){
196     testGoodMsgId("<hello.world@TOM.COM>");
197     testGoodMsgId("<hello.world@TOM.COM \r\n > ");
198     testGoodMsgId(" <hello.world@TOM.COM \r\n > ");
199   }
200   public void testOptionalField(){
201     testGoodOptionalField("Message-id: <hellow@tom.com>\r\n");
202     testGoodOptionalField("in-reply-to: this is a phrase for in-reply-to <tom.google@gmail.com> right?\r\n");
203     testGoodOptionalField("keywords: \r\n phrase 1, second phrase, , , 3rd phrase\r\n");
204     testGoodOptionalField(" comments: \r\n a text is any character excluding crlf but including \r and \n\r\n");
205     testGoodOptionalField(" ENCRYPTED: \r\n WORD1 word2\r\n");
206   }
207   public void testDestination(){
208     testGoodDestination("to:tom@tom.com, tom2@tom.com\r\n");
209   }
210   public void testOrigDate(){
211     testGoodOrigDate("date: Wed, 10 Nov 03 10:20:10 +1000\r\n");
212   }
213   public void testResentDate(){
214     testGoodResentDate("resent-date: 10 Nov 03 10:20:10 ut\r\n");
215   }
216   public void testDates(){
217     testGoodDates("date: Wed, 10 Nov 03 10:20:10 +1000 \r\nresent-date: 10 Nov 03 10:20:10 ut\r\n");
218   }
219   public void testAuthentic(){
220     testGoodAuthentic("from : this is tom <@a,,@b:hello.world@TOM.COM>\r\n");
221     testGoodAuthentic("sender : this is tom <@a,,@b:hello.world@TOM.COM> \r\nFROM : this is tom <@a,,@b:hello.world@TOM.COM>,, hello \" \r\n \"<@a:hello.world@TOM.COM>\r\n");
222   }
223   public void testResent(){
224     testGoodResent("Resent-from : this is tom <@a,,@b:hello.world@TOM.COM> \r\nresent-reply-to:address@email.com, this is a phrase:;\r\n");
225     testGoodResent("Resent-sender : this is tom <@a,,@b:hello.world@TOM.COM> \r\nresent-from: tom <@a,,@b:hello.world@TOM.COM>,hello \" \r\n \"<@a:hello.world@TOM.COM> \r\nresent-reply-to:address@email.com, this is a phrase:;\r\n");
226     //testGoodAuthentic("sender : this is tom <@a,,@b:hello.world@TOM.COM> \r\n FROM : this is tom <@a,,@b:hello.world@TOM.COM>,, hello \" \r\n \"<@a:hello.world@TOM.COM>");
227
}
228   public void testReceived(){
229     testGoodReceived("received:;Wed, 10 Nov 03 10:20:10 +1000\r\n");
230     testGoodReceived("received:from tom.[com] ;10 Nov 03 10:20:10 UT\r\n");
231     testGoodReceived("received:from tom.[com] with zhang with wang with li with zhao id<tom@tom.com> ;10 Nov 03 10:20:10 UT\r\n");
232   }
233   public void testReturn(){
234     testGoodReturn("return-path:<@a,,@b:hello.world@TOM.COM>\r\n");
235   }
236   public void testTrace(){
237     testGoodTrace("return-path:<@a,,@b:hello.world@TOM.COM> \r\nreceived:from tom.[com] with zhang with wang with li with zhao id<tom@tom.com> ;10 Nov 03 10:20:10 UT\r\n");
238     testGoodTrace("return-path:<@a,,@b:hello.world@TOM.COM> \r\nreceived:from tom.[com] with zhang with wang with li with zhao id<tom@tom.com> ;10 Nov 03 10:20:10 UT \r\n \r\nreceived:;Wed, 10 Nov 03 10:20:10 +1000\r\n");
239   }
240   public void testSource(){
241     testGoodSource("return-path:<@a,,@b:hello.world@TOM.COM> \r\nreceived:from tom.[com] with zhang with wang with li with zhao id<tom@tom.com> ;10 Nov 03 10:20:10 UT \r\nsender : this is tom <@a,,@b:hello.world@TOM.COM> \r\nfrom: tom <@a,,@b:hello.world@TOM.COM>,hello \" \r\n \"<@a:hello.world@TOM.COM> \r\nreply-to:address@email.com, this is a phrase:;\r\n");
242     testGoodSource("return-path:<@a,,@b:hello.world@TOM.COM> \r\nreceived:from tom.[com] with zhang with wang with li with zhao id<tom@tom.com> ;10 Nov 03 10:20:10 UT \r\nsender : this is tom <@a,,@b:hello.world@TOM.COM> \r\nfrom: tom <@a,,@b:hello.world@TOM.COM>,hello \" \r\n \"<@a:hello.world@TOM.COM> \r\nreply-to:address@email.com, this is a phrase:;\r\nResent-sender : this is tom <@a,,@b:hello.world@TOM.COM> \r\nresent-from: tom <@a,,@b:hello.world@TOM.COM>,hello \" \r\n \"<@a:hello.world@TOM.COM> \r\nresent-reply-to:address@email.com, this is a phrase:;\r\n");
243     testGoodSource("sender : this is tom <@a,,@b:hello.world@TOM.COM> \r\nfrom: tom <@a,,@b:hello.world@TOM.COM>,hello \" \r\n \"<@a:hello.world@TOM.COM> \r\nreply-to:address@email.com, this is a phrase:;\r\nResent-sender : this is tom <@a,,@b:hello.world@TOM.COM> \r\nresent-from: tom <@a,,@b:hello.world@TOM.COM>,hello \" \r\n \"<@a:hello.world@TOM.COM> \r\nresent-reply-to:address@email.com, this is a phrase:;\r\n");
244     //testGoodSource("return-path:<@a,,@b:hello.world@TOM.COM> received:from tom.[com] with zhang with wang with li with zhao id<tom@tom.com> ;10 Nov 03 10:20:10 UT \r\n received:;Wed, 10 Nov 03 10:20:10 +1000");
245
}
246   public void testFields(){
247     testGoodFields("date: Wed, 10 Nov 03 10:20:10 +1000 \r\nresent-date: 10 Nov 03 10:20:10 ut \r\n \r\nreturn-path:<@a,,@b:hello.world@TOM.COM> \r\nreceived:from tom.[com] with zhang with wang with li with zhao id<tom@tom.com> ;10 Nov 03 10:20:10 UT \r\nsender : this is tom <@a,,@b:hello.world@TOM.COM> \r\nfrom: tom <@a,,@b:hello.world@TOM.COM>,hello \" \r\n \"<@a:hello.world@TOM.COM> \r\nreply-to:address@email.com, this is a phrase:;\r\nResent-sender : this is tom <@a,,@b:hello.world@TOM.COM> \r\nresent-from: tom <@a,,@b:hello.world@TOM.COM>,hello \" \r\n \"<@a:hello.world@TOM.COM> \r\nresent-reply-to:address@email.com, this is a phrase:;\r\nto:tom@tom.com, tom2@tom.com \r\nto:tom@tom.com, tom2@tom.com \r\nin-reply-to: this is a phrase for in-reply-to <tom.google@gmail.com> right? \r\nkeywords: \r\n phrase 1, second phrase, , , 3rd phrase\r\n");
248   }
249   public void testMessage(){
250     testGoodMessage("date: Wed, 10 Nov 03 10:20:10 +1000 \r\nresent-date: 10 Nov 03 10:20:10 ut \r\n \r\nreturn-path:<@a,,@b:hello.world@TOM.COM> \r\nreceived:from tom.[com] with zhang with wang with li with zhao id<tom@tom.com> ;10 Nov 03 10:20:10 UT \r\nsender : this is tom <@a,,@b:hello.world@TOM.COM> \r\nfrom: tom <@a,,@b:hello.world@TOM.COM>,hello \" \r\n \"<@a:hello.world@TOM.COM> \r\nreply-to:address@email.com, this is a phrase:;\r\nResent-sender : this is tom <@a,,@b:hello.world@TOM.COM> \r\nresent-from: tom <@a,,@b:hello.world@TOM.COM>,hello \" \r\n \"<@a:hello.world@TOM.COM> \r\nresent-reply-to:address@email.com, this is a phrase:;\r\nto:tom@tom.com, tom2@tom.com \r\nto:tom@tom.com, tom2@tom.com \r\nin-reply-to: this is a phrase for in-reply-to <tom.google@gmail.com> right? \r\nkeywords: \r\n phrase 1, second phrase, , , 3rd phrase\r\n\r\nnow this is the body even if I have \r\n or \r\n\r\n\r\n, it does not matter!");
251   }
252   public void testMessageFile1(){
253     _testMessageFile("test/message1.txt");
254   }
255   private void testGood(final CharSequence JavaDoc src, final Parser s){
256     s.followedBy(Parsers.eof()).parse(src, "testGood");
257   }
258   private String JavaDoc testBad(final CharSequence JavaDoc src, final Parser s){
259     try{
260       s.followedBy(Parsers.eof()).parse(src, "testBad");
261       throw new IllegalStateException JavaDoc("should have failed!");
262     }
263     catch(ParserException e){
264       return e.getMessage();
265     }
266   }
267   private Object JavaDoc testDateTimeParser(final CharSequence JavaDoc src, final Parser p){
268     return dparser.runParser(src, p);
269   }
270   private Object JavaDoc testAddressParser(final CharSequence JavaDoc src, final Parser p){
271     return aparser.runParser(src, p);
272   }
273   private Object JavaDoc testMessageParser(final CharSequence JavaDoc src, final Parser p){
274     return mparser.runParser(src, p);
275   }
276   private String JavaDoc testBadAddressParser(final CharSequence JavaDoc src, final Parser p){
277     try{
278       aparser.runParser(src, p);
279       throw new IllegalStateException JavaDoc("should have failed");
280     }
281     catch(ParserException e){
282       return e.getMessage();
283     }
284   }
285   private String JavaDoc testBadDateTimeParser(final CharSequence JavaDoc src, final Parser p){
286     try{
287       dparser.runParser(src, p);
288       throw new IllegalStateException JavaDoc("should have failed");
289     }
290     catch(ParserException e){
291       return e.getMessage();
292     }
293   }
294   private void test_address(final String JavaDoc s){
295     final Parser p = aparser.address;
296     System.out.println(aparser.runParser(s, p));
297   }
298   private void test_addresses(final String JavaDoc[] srcs){
299     for(int i=0; i<srcs.length; i++){
300       test_address(srcs[i]);
301     }
302   }
303   private void testFileAddresses(final String JavaDoc name){
304     final String JavaDoc[] lines = loadLines(name);
305     test_addresses(lines);
306   }
307   private static String JavaDoc[] loadLines(final String JavaDoc name){
308     try{
309       final FileReader JavaDoc fr = new FileReader JavaDoc(name);
310       try{
311         final java.io.BufferedReader JavaDoc br = new java.io.BufferedReader JavaDoc(fr);
312         final java.util.ArrayList JavaDoc buf = new java.util.ArrayList JavaDoc();
313         for(String JavaDoc line=null;;){
314           line = br.readLine();
315           if(line==null) break;
316           buf.add(line);
317         }
318         final String JavaDoc[] ret = new String JavaDoc[buf.size()];
319         buf.toArray(ret);
320         return ret;
321       }
322       finally{
323         fr.close();
324       }
325     }
326     catch(IOException JavaDoc e){
327       throw new IllegalStateException JavaDoc(e.getMessage());
328     }
329   }
330   private void testAddressParserErr(final CharSequence JavaDoc src, final Parser p){
331     System.out.println(testBadAddressParser(src, p));
332   }
333   private void testDateTimeParserErr(final CharSequence JavaDoc src, final Parser p){
334     System.out.println(testBadDateTimeParser(src, p));
335   }
336   private void testError(final CharSequence JavaDoc src, final Parser s){
337     System.out.println(testBad(src, s));
338   }
339   private void testGoodMsgId(final String JavaDoc src){
340     System.out.println(testMessageParser(src, mparser.msg_id));
341   }
342   private void testGoodOptionalField(final String JavaDoc src){
343     System.out.println(testMessageParser(src, mparser.optional_field));
344   }
345   private void testGoodDestination(final String JavaDoc src){
346     System.out.println(testMessageParser(src, mparser.destination));
347   }
348   private void testGoodOrigDate(final String JavaDoc src){
349     System.out.println(testMessageParser(src, mparser.orig_date));
350   }
351   private void testGoodResentDate(final String JavaDoc src){
352     System.out.println(testMessageParser(src, mparser.resent_date));
353   }
354   private void testGoodDates(final String JavaDoc src){
355     System.out.println(testMessageParser(src, mparser.dates));
356   }
357   private void testGoodAuthentic(final String JavaDoc src){
358     System.out.println(testMessageParser(src, mparser.authentic));
359   }
360   private void testGoodResent(final String JavaDoc src){
361     System.out.println(testMessageParser(src, mparser.resent));
362   }
363   private void testGoodReceived(final String JavaDoc src){
364     System.out.println(testMessageParser(src, mparser.received));
365   }
366   private void testGoodReturn(final String JavaDoc src){
367     System.out.println(testMessageParser(src, mparser.retn));
368   }
369   private void testGoodTrace(final String JavaDoc src){
370     System.out.println(testMessageParser(src, mparser.trace));
371   }
372   private void testGoodSource(final String JavaDoc src){
373     System.out.println(testMessageParser(src, mparser.source));
374   }
375   private void testGoodFields(final String JavaDoc src){
376     System.out.println(testMessageParser(src, mparser.fields));
377   }
378   private void testGoodMessage(final String JavaDoc src){
379     System.out.println(testMessageParser(src, mparser.message));
380   }
381   private void _testMessageFile(final String JavaDoc fname){
382     final String JavaDoc txt = readFile(fname);
383     testGoodMessage(txt);
384   }
385   private static String JavaDoc readFile(final String JavaDoc name){
386     try{
387       final FileReader JavaDoc fr = new FileReader JavaDoc(name);
388       try{
389         final StringBuffer JavaDoc dest = new StringBuffer JavaDoc();
390         final char[] buf = new char[4000];
391         for(;;){
392           final int n = fr.read(buf);
393           if(n>0){
394             dest.append(buf, 0, n);
395           }
396           if(n < 0) break;
397         }
398         return dest.toString();
399       }
400       finally{
401         fr.close();
402       }
403     }
404     catch(IOException JavaDoc e){
405       throw new IllegalStateException JavaDoc(e.getMessage());
406     }
407   }
408 }
409
Popular Tags