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.StringReader;
23
24 import org.jmock.Mock;
25 import org.jmock.MockObjectTestCase;
26
27 public class TestFileProtocolSessionBuilder extends MockObjectTestCase {
28
29 private static final String SCRIPT_WITH_VARIABLES = "HELLO ${not} ${foo} WORLD ${bar}";
30 private static final String SCRIPT_WITH_FOO_REPLACED_BY_WHATEVER = "HELLO ${not} whatever WORLD ${bar}";
31 private static final String SCRIPT_WITH_VARIABLES_INLINED = "HELLO not foo WORLD bar";
32
33 ProtocolSessionBuilder builder;
34 ProtocolInteractor session;
35
36 private Mock mockSession;
37
38 @Override
39 protected void setUp() throws Exception {
40 super.setUp();
41 builder = new ProtocolSessionBuilder();
42 mockSession = mock(ProtocolInteractor.class);
43 session = (ProtocolInteractor) mockSession.proxy();
44 }
45
46 @Override
47 protected void tearDown() throws Exception {
48 super.tearDown();
49 }
50
51 private void addLines() throws Exception {
52 builder.addProtocolLines("A Script", new StringReader(ProtocolSessionBuilder.CLIENT_TAG + " " + SCRIPT_WITH_VARIABLES), session);
53 }
54
55 public void testShouldPreserveContentsWhenNoVariablesSet() throws Exception {
56 mockSession.expects(once()).method("CL").with(eq(-1), eq(SCRIPT_WITH_VARIABLES));
57 addLines();
58 }
59
60 public void testShouldReplaceVariableWhenSet() throws Exception {
61 mockSession.expects(once()).method("CL").with(eq(-1), eq(SCRIPT_WITH_FOO_REPLACED_BY_WHATEVER));
62 builder.setVariable("foo", "whatever");
63 addLines();
64 }
65
66 public void testShouldReplaceAllVariablesWhenSet() throws Exception {
67 mockSession.expects(once()).method("CL").with(eq(-1), eq(SCRIPT_WITH_VARIABLES_INLINED));
68 builder.setVariable("bar", "bar");
69 builder.setVariable("foo", "foo");
70 builder.setVariable("not", "not");
71 addLines();
72 }
73
74 public void testShouldReplaceVariableAtBeginningAndEnd() throws Exception {
75 mockSession.expects(once()).method("CL").with(eq(-1), eq("whatever Some Other Scriptwhateverwhatever"));
76 builder.setVariable("foo", "whatever");
77 builder.addProtocolLines("A Script", new StringReader(ProtocolSessionBuilder.CLIENT_TAG + " " + "${foo} Some Other Script${foo}${foo}"), session);
78 }
79
80 public void testShouldIgnoreNotQuiteVariables() throws Exception {
81 final String NEARLY = "{foo}${}${foo Some Other Script${foo}";
82 mockSession.expects(once()).method("CL").with(eq(-1), eq(NEARLY));
83 builder.setVariable("foo", "whatever");
84 builder.addProtocolLines("A Script", new StringReader(ProtocolSessionBuilder.CLIENT_TAG + " " + NEARLY), session);
85 }
86 }