1 17 18 package org.apache.james.test; 19 20 import java.io.BufferedReader ; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 26 32 public class FileProtocolSessionBuilder 33 { 34 private static final String CLIENT_TAG = "C:"; 35 private static final String SERVER_TAG = "S:"; 36 private static final String OPEN_UNORDERED_BLOCK_TAG = "SUB {"; 37 private static final String CLOSE_UNORDERED_BLOCK_TAG = "}"; 38 private static final String COMMENT_TAG = "#"; 39 40 46 public ProtocolSession buildProtocolSession( String fileName ) 47 throws Exception 48 { 49 ProtocolSession session = new ProtocolSession(); 50 addTestFile( fileName, session ); 51 return session; 52 } 53 54 59 public void addTestFile( String fileName, ProtocolSession session ) 60 throws Exception 61 { 62 InputStream is = this.getClass().getResourceAsStream( fileName ); 64 if ( is == null ) { 65 throw new Exception ( "Test Resource '" + fileName + "' not found." ); 66 } 67 68 addProtocolLinesFromStream( is, session, fileName ); 69 } 70 71 78 public void addProtocolLinesFromStream( InputStream is, 79 ProtocolSession session, 80 String fileName ) 81 throws Exception 82 { 83 BufferedReader reader = new BufferedReader ( new InputStreamReader ( is ) ); 84 String next; 85 int lineNumber = 1; 86 while ( ( next = reader.readLine() ) != null ) { 87 String location = fileName + ":" + lineNumber; 88 if ( next.startsWith( CLIENT_TAG ) ) { 89 String clientMsg = ""; 90 if ( next.length() > 3 ) { 91 clientMsg = next.substring( 3 ); 92 } 93 session.CL( clientMsg ); 94 } 95 else if ( next.startsWith( SERVER_TAG ) ) { 96 String serverMsg = ""; 97 if ( next.length() > 3 ) { 98 serverMsg = next.substring( 3 ); 99 } 100 session.SL( serverMsg, location ); 101 } 102 else if ( next.startsWith( OPEN_UNORDERED_BLOCK_TAG ) ) { 103 List unorderedLines = new ArrayList ( 5 ); 104 next = reader.readLine(); 105 106 while ( !next.startsWith( CLOSE_UNORDERED_BLOCK_TAG ) ) { 107 if (! next.startsWith( SERVER_TAG ) ) { 108 throw new Exception ( "Only 'S: ' lines are permitted inside a 'SUB {' block."); 109 } 110 String serverMsg = next.substring( 3 ); 111 unorderedLines.add( serverMsg ); 112 next = reader.readLine(); 113 lineNumber++; 114 } 115 116 session.SUB( unorderedLines, location ); 117 } 118 else if ( next.startsWith( COMMENT_TAG ) 119 || next.trim().length() == 0 ) { 120 } 122 else { 123 String prefix = next; 124 if ( next.length() > 3 ) { 125 prefix = next.substring( 0, 3 ); 126 } 127 throw new Exception ( "Invalid line prefix: " + prefix ); 128 } 129 lineNumber++; 130 } 131 } 132 133 } 134 | Popular Tags |