KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > test > FileProtocolSessionBuilder


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.test;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * A builder which generates a ProtocolSession from a test file.
28  *
29  *
30  * @version $Revision: 1.4.2.3 $
31  */

32 public class FileProtocolSessionBuilder
33 {
34     private static final String JavaDoc CLIENT_TAG = "C:";
35     private static final String JavaDoc SERVER_TAG = "S:";
36     private static final String JavaDoc OPEN_UNORDERED_BLOCK_TAG = "SUB {";
37     private static final String JavaDoc CLOSE_UNORDERED_BLOCK_TAG = "}";
38     private static final String JavaDoc COMMENT_TAG = "#";
39
40     /**
41      * Builds a ProtocolSession by reading lines from the test file
42      * with the supplied name.
43      * @param fileName The name of the protocol session file.
44      * @return The ProtocolSession
45      */

46     public ProtocolSession buildProtocolSession( String JavaDoc fileName )
47             throws Exception JavaDoc
48     {
49         ProtocolSession session = new ProtocolSession();
50         addTestFile( fileName, session );
51         return session;
52     }
53
54     /**
55      * Adds all protocol elements from a test file to the ProtocolSession supplied.
56      * @param fileName The name of the protocol session file.
57      * @param session The ProtocolSession to add the elements to.
58      */

59     public void addTestFile( String JavaDoc fileName, ProtocolSession session )
60             throws Exception JavaDoc
61     {
62         // Need to find local resource.
63
InputStream JavaDoc is = this.getClass().getResourceAsStream( fileName );
64         if ( is == null ) {
65             throw new Exception JavaDoc( "Test Resource '" + fileName + "' not found." );
66         }
67
68         addProtocolLinesFromStream( is, session, fileName );
69     }
70
71     /**
72      * Reads ProtocolElements from the supplied InputStream and adds
73      * them to the ProtocolSession.
74      * @param is The input stream containing the protocol definition.
75      * @param session The ProtocolSession to add elements to.
76      * @param fileName The name of the source file, for error messages.
77      */

78     public void addProtocolLinesFromStream( InputStream JavaDoc is,
79                                              ProtocolSession session,
80                                              String JavaDoc fileName )
81             throws Exception JavaDoc
82     {
83         BufferedReader JavaDoc reader = new BufferedReader JavaDoc( new InputStreamReader JavaDoc( is ) );
84         String JavaDoc next;
85         int lineNumber = 1;
86         while ( ( next = reader.readLine() ) != null ) {
87             String JavaDoc location = fileName + ":" + lineNumber;
88             if ( next.startsWith( CLIENT_TAG ) ) {
89                 String JavaDoc 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 JavaDoc 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 JavaDoc unorderedLines = new ArrayList JavaDoc( 5 );
104                 next = reader.readLine();
105
106                 while ( !next.startsWith( CLOSE_UNORDERED_BLOCK_TAG ) ) {
107                     if (! next.startsWith( SERVER_TAG ) ) {
108                         throw new Exception JavaDoc( "Only 'S: ' lines are permitted inside a 'SUB {' block.");
109                     }
110                     String JavaDoc 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                 // ignore these lines.
121
}
122             else {
123                 String JavaDoc prefix = next;
124                 if ( next.length() > 3 ) {
125                     prefix = next.substring( 0, 3 );
126                 }
127                 throw new Exception JavaDoc( "Invalid line prefix: " + prefix );
128             }
129             lineNumber++;
130         }
131     }
132
133 }
134
Popular Tags