1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
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  }