KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > commands > ArgumentTest


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * 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 *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.imapserver.commands;
19
20 import junit.framework.TestCase;
21
22 import java.util.StringTokenizer JavaDoc;
23
24 public final class ArgumentTest
25         extends TestCase
26 {
27     public ArgumentTest( String JavaDoc s )
28     {
29         super( s );
30     }
31
32     public void testAstring() throws Exception JavaDoc
33     {
34         AstringArgument arg = new AstringArgument( "test" );
35         ParseChecker parser = new ParseChecker( arg );
36
37         parser.check( "straightup", "straightup" );
38         parser.check( "quoted", "\"quoted\"" );
39         parser.check( "with space", "\"with space\"" );
40
41         // Test currently fails - don't see whitespace.
42
//parser.check( "multiple spaces", "\"multiple spaces\"" );
43

44         parser.checkFail( "Missing argument <test>", "" );
45         parser.checkFail( "Missing closing quote for <test>", "\"something" );
46         parser.checkFail( "Missing closing quote for <test>", "\"" );
47         parser.checkFail( "Missing closing quote for <test>", "\"something special" );
48     }
49
50     private static class ParseChecker
51     {
52         private ImapArgument arg;
53
54         ParseChecker( ImapArgument arg )
55         {
56             this.arg = arg;
57         }
58
59         public void check( Object JavaDoc expected, String JavaDoc input )
60         {
61             StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc( input );
62             Object JavaDoc result = null;
63             try {
64                 result = this.arg.parse( tokens );
65             }
66             catch ( Exception JavaDoc e ) {
67                 fail( "Error encountered: " + e.getMessage() );
68             }
69
70             assertEquals( expected, result );
71         }
72
73         public void checkFail( String JavaDoc expectedError, String JavaDoc input )
74         {
75             StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc( input );
76             try {
77                 Object JavaDoc result = this.arg.parse( tokens );
78             }
79             catch ( Exception JavaDoc e ) {
80                 assertEquals( expectedError, e.getMessage() );
81                 return;
82             }
83
84             fail( "Expected error" );
85         }
86     }
87 }
88
Popular Tags