1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.james.mpt;
21
22 import java.io.BufferedReader;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.Reader;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Properties;
29
30
31
32
33
34
35
36
37
38 public class ProtocolSessionBuilder {
39
40 public static final String SERVER_CONTINUATION_TAG = "S: \\+";
41
42 public static final String CLIENT_TAG = "C:";
43
44 public static final String SERVER_TAG = "S:";
45
46 public static final String OPEN_UNORDERED_BLOCK_TAG = "SUB {";
47
48 public static final String CLOSE_UNORDERED_BLOCK_TAG = "}";
49
50 public static final String COMMENT_TAG = "#";
51
52 public static final String SESSION_TAG = "SESSION:";
53
54 private final Properties variables;
55
56 public ProtocolSessionBuilder() {
57 variables = new Properties();
58 }
59
60
61
62
63
64
65
66
67
68 public void setVariable(final String name, final String value) {
69 variables.put(name, value);
70 }
71
72
73
74
75
76
77
78
79
80 public ProtocolInteractor buildProtocolSession(String fileName)
81 throws Exception {
82 ProtocolInteractor session = new ProtocolSession();
83 addTestFile(fileName, session);
84 return session;
85 }
86
87
88
89
90
91
92
93
94 public ProtocolInteractor buildProtocolSession(final String scriptName, final Reader reader)
95 throws Exception {
96 ProtocolInteractor session = new ProtocolSession();
97 addProtocolLines(scriptName, reader, session);
98 return session;
99 }
100
101
102
103
104
105
106
107
108
109
110
111 public void addTestFile(String fileName, ProtocolInteractor session)
112 throws Exception {
113
114 InputStream is = this.getClass().getResourceAsStream(fileName);
115 if (is == null) {
116 throw new Exception("Test Resource '" + fileName + "' not found.");
117 }
118
119 addProtocolLines(fileName, is, session);
120 }
121
122
123
124
125
126
127
128
129
130
131
132 public void addProtocolLines(String scriptName, InputStream is, ProtocolInteractor session) throws Exception {
133 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
134
135 doAddProtocolLines(session, scriptName, reader);
136 }
137
138
139
140
141
142
143
144
145
146
147
148 public void addProtocolLines(String scriptName, Reader reader, ProtocolInteractor session) throws Exception {
149 final BufferedReader bufferedReader;
150 if (reader instanceof BufferedReader) {
151 bufferedReader = (BufferedReader) reader;
152 } else {
153 bufferedReader = new BufferedReader(reader);
154 }
155 doAddProtocolLines(session, scriptName, bufferedReader);
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169 private void doAddProtocolLines(ProtocolInteractor session, String scriptName, BufferedReader reader) throws Exception {
170 String line;
171 int sessionNumber = -1;
172 int lineNumber = -1;
173 String lastClientMsg = "";
174 while ((line = reader.readLine()) != null) {
175 line = substituteVariables(line);
176 String location = scriptName + ":" + lineNumber;
177 if (SERVER_CONTINUATION_TAG.equals(line)) {
178 session.CONT(sessionNumber);
179 } else if (line.startsWith(CLIENT_TAG)) {
180 String clientMsg = "";
181 if (line.length() > 3) {
182 clientMsg = line.substring(3);
183 }
184 session.CL(sessionNumber, clientMsg);
185 lastClientMsg = clientMsg;
186 } else if (line.startsWith(SERVER_TAG)) {
187 String serverMsg = "";
188 if (line.length() > 3) {
189 serverMsg = line.substring(3);
190 }
191 session.SL(sessionNumber, serverMsg, location, lastClientMsg);
192 } else if (line.startsWith(OPEN_UNORDERED_BLOCK_TAG)) {
193 List<String> unorderedLines = new ArrayList<String>(5);
194 line = reader.readLine();
195
196 while (!line.startsWith(CLOSE_UNORDERED_BLOCK_TAG)) {
197 if (!line.startsWith(SERVER_TAG)) {
198 throw new Exception(
199 "Only 'S: ' lines are permitted inside a 'SUB {' block.");
200 }
201 String serverMsg = line.substring(3);
202 unorderedLines.add(serverMsg);
203 line = reader.readLine();
204 lineNumber++;
205 }
206
207 session.SUB(sessionNumber, unorderedLines, location,
208 lastClientMsg);
209 } else if (line.startsWith(COMMENT_TAG)
210 || line.trim().length() == 0) {
211
212 } else if (line.startsWith(SESSION_TAG)) {
213 String number = line.substring(SESSION_TAG.length()).trim();
214 if (number.length() == 0) {
215 throw new Exception("No session number specified");
216 }
217 sessionNumber = Integer.parseInt(number);
218 } else {
219 String prefix = line;
220 if (line.length() > 3) {
221 prefix = line.substring(0, 3);
222 }
223 throw new Exception("Invalid line prefix: " + prefix);
224 }
225 lineNumber++;
226 }
227 }
228
229
230
231
232
233
234 private String substituteVariables(String line) {
235 if (variables.size() > 0) {
236 final StringBuffer buffer = new StringBuffer(line);
237 int start = 0;
238 int end = 0;
239 while (start >= 0 && end >= 0) {
240 start = buffer.indexOf("${", end);
241 if (start < 0) {
242 break;
243 }
244 end = buffer.indexOf("}", start);
245 if (end < 0) {
246 break;
247 }
248 final String name = buffer.substring(start+2, end);
249 final String value = variables.getProperty(name);
250 if (value != null) {
251 buffer.replace(start, end + 1, value);
252 final int variableLength = (end - start + 2);
253 end = end + (value.length() - variableLength);
254 }
255 }
256 line = buffer.toString();
257 }
258 return line;
259 }
260
261 }