1 17 18 package org.apache.james.test; 19 20 import org.apache.oro.text.perl.Perl5Util; 21 22 import java.io.BufferedReader ; 23 import java.io.PrintWriter ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 37 public class ProtocolSession 38 { 39 protected List testElements = new ArrayList (); 40 private static final Perl5Util perl = new Perl5Util(); 41 42 50 public void runLiveSession( PrintWriter out, BufferedReader in ) throws Exception 51 { 52 for ( Iterator iter = testElements.iterator(); iter.hasNext(); ) { 53 Object obj = iter.next(); 54 if ( obj instanceof ProtocolElement ) { 55 ProtocolElement test = ( ProtocolElement ) obj; 56 test.testProtocol( out, in ); 57 } 58 } 59 } 60 61 67 public void writeClient( PrintWriter out ) throws Exception 68 { 69 Iterator iterator = testElements.iterator(); 70 while ( iterator.hasNext() ) { 71 ProtocolElement element = (ProtocolElement) iterator.next(); 72 if ( element instanceof ClientRequest ) { 73 element.testProtocol( out, null ); 74 } 75 } 76 } 77 78 85 public void testResponse( BufferedReader in ) throws Exception 86 { 87 Iterator iterator = testElements.iterator(); 88 while ( iterator.hasNext() ) { 89 ProtocolElement element = (ProtocolElement) iterator.next(); 90 if ( element instanceof ServerResponse ) { 91 element.testProtocol( null, in ); 92 } 93 } 94 } 95 96 99 public void CL( String clientLine ) 100 { 101 testElements.add( new ClientRequest( clientLine ) ); 102 } 103 104 107 public void SL( String serverLine, String location ) 108 { 109 testElements.add( new ServerResponse( serverLine, location ) ); 110 } 111 112 115 public void SUB( List serverLines, String location ) 116 { 117 testElements.add( new ServerUnorderedBlockResponse( serverLines, location ) ); 118 } 119 120 123 public void addProtocolElement( ProtocolElement element ) 124 { 125 testElements.add( element ); 126 } 127 128 131 private class ClientRequest implements ProtocolElement 132 { 133 private String message; 134 135 138 public ClientRequest( String message ) 139 { 140 this.message = message; 141 } 142 143 146 public void testProtocol( PrintWriter out, BufferedReader in ) 147 { 148 out.write( message ); 149 out.write( '\r' ); 150 out.write( '\n' ); 151 out.flush(); 152 } 153 } 154 155 160 private class ServerResponse implements ProtocolElement 161 { 162 private String expectedLine; 163 protected String location; 164 165 171 public ServerResponse( String expectedPattern, String location ) 172 { 173 this.expectedLine = expectedPattern; 174 this.location = location; 175 } 176 177 185 public void testProtocol( PrintWriter out, BufferedReader in ) 186 throws InvalidServerResponseException 187 { 188 String testLine = readLine( in ); 189 if ( ! match( expectedLine, testLine ) ) { 190 String errMsg = "\nLocation: " + location + 191 "\nExcpected: " + expectedLine + 192 "\nActual : " + testLine; 193 throw new InvalidServerResponseException( errMsg ); 194 } 195 } 196 197 204 protected boolean match( String expected, String actual ) 205 { 206 String pattern = "m/" + expected + "/"; 207 return perl.match( pattern, actual ); 208 } 209 210 216 protected String readLine( BufferedReader in ) 217 throws InvalidServerResponseException 218 { 219 try { 220 return in.readLine(); 221 } 222 catch ( IOException e ) { 223 String errMsg = "\nLocation: " + location + 224 "\nExpected: " + expectedLine + 225 "\nReason: Server Timeout."; 226 throw new InvalidServerResponseException( errMsg ); 227 } 228 } 229 } 230 231 235 private class ServerUnorderedBlockResponse extends ServerResponse 236 { 237 private List expectedLines = new ArrayList (); 238 239 245 public ServerUnorderedBlockResponse( List expectedLines, String location ) 246 { 247 super( "<Unordered Block>", location ); 248 this.expectedLines = expectedLines; 249 } 250 251 260 public void testProtocol( PrintWriter out, BufferedReader in ) 261 throws InvalidServerResponseException 262 { 263 List testLines = new ArrayList ( expectedLines ); 264 while ( testLines.size() > 0 ) 265 { 266 String actualLine = readLine( in ); 267 boolean foundMatch = false; 268 269 for ( int i = 0; i < testLines.size(); i++ ) 270 { 271 String expected = (String )testLines.get( i ); 272 if ( match( expected, actualLine )) 273 { 274 foundMatch = true; 275 testLines.remove( expected ); 276 break; 277 } 278 } 279 280 if (! foundMatch ) 281 { 282 StringBuffer errMsg = new StringBuffer () 283 .append( "\nLocation: " ) 284 .append( location ) 285 .append( "\nExpected one of: " ); 286 Iterator iter = expectedLines.iterator(); 287 while ( iter.hasNext() ) { 288 errMsg.append( "\n " ); 289 errMsg.append( iter.next() ); 290 } 291 errMsg.append("\nActual: " ) 292 .append( actualLine ); 293 294 throw new InvalidServerResponseException( errMsg.toString() ); 295 } 296 } 297 } 298 } 299 300 305 interface ProtocolElement 306 { 307 314 void testProtocol( PrintWriter out, BufferedReader in ) 315 throws InvalidServerResponseException; 316 } 317 318 322 public class InvalidServerResponseException extends Exception 323 { 324 public InvalidServerResponseException( String message ) 325 { 326 super( message ); 327 } 328 } 329 330 } 331 | Popular Tags |