1 17 18 package org.apache.james.imapserver.commands; 19 20 import junit.framework.TestCase; 21 22 import java.util.StringTokenizer ; 23 24 public final class ArgumentTest 25 extends TestCase 26 { 27 public ArgumentTest( String s ) 28 { 29 super( s ); 30 } 31 32 public void testAstring() throws Exception 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 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 expected, String input ) 60 { 61 StringTokenizer tokens = new StringTokenizer ( input ); 62 Object result = null; 63 try { 64 result = this.arg.parse( tokens ); 65 } 66 catch ( Exception e ) { 67 fail( "Error encountered: " + e.getMessage() ); 68 } 69 70 assertEquals( expected, result ); 71 } 72 73 public void checkFail( String expectedError, String input ) 74 { 75 StringTokenizer tokens = new StringTokenizer ( input ); 76 try { 77 Object result = this.arg.parse( tokens ); 78 } 79 catch ( Exception e ) { 80 assertEquals( expectedError, e.getMessage() ); 81 return; 82 } 83 84 fail( "Expected error" ); 85 } 86 } 87 } 88 | Popular Tags |