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 org.apache.james.mpt.HostSystem.Continuation;
23  import org.jmock.Mock;
24  import org.jmock.MockObjectTestCase;
25  
26  public class TestExternalHostSystem extends MockObjectTestCase {
27  
28      
29      private static final String USER = "USER NAME";
30  
31      private static final String PASSWORD = "SOME PASSWORD";
32  
33      private static final String SHABANG = "This Is The Shabang";
34  
35      private static final int PORT = 10001;
36      
37      private DiscardProtocol protocol;
38      
39      private DiscardProtocol.Record record;
40  
41      private Continuation continuation;
42  
43      private UserAdder userAdder;
44  
45      private Mock mockUserAdder;
46      
47      @Override
48      protected void setUp() throws Exception {
49          super.setUp();
50          protocol = new DiscardProtocol(PORT);
51          protocol.start();
52          record = protocol.recordNext();
53          continuation = (Continuation) mock(Continuation.class).proxy();
54          mockUserAdder = mock(UserAdder.class);
55          userAdder = (UserAdder) mockUserAdder.proxy();
56      }
57  
58      @Override
59      protected void tearDown() throws Exception {
60          protocol.stop();
61          super.tearDown();
62      }
63      
64      public void testWrite() throws Exception {
65          Session session = newSession(SHABANG);
66          final String in = "Hello, World";
67          session.writeLine(in);
68          session.stop();
69          assertEquals(in + "\r\n", record.complete());
70      }
71      
72      public void testAddUser() throws Exception {
73          mockUserAdder.expects(once()).method("addUser").with(eq(USER), eq(PASSWORD));
74          ExternalHostSystem system = buildSystem(SHABANG);
75          system.addUser(USER, PASSWORD);
76      }
77  
78      private Session newSession(final String shabang) throws Exception {
79          ExternalSessionFactory system = buildSystem(shabang);
80          Session session = system.newSession(continuation);
81          return session;
82      }
83  
84      private ExternalHostSystem buildSystem(final String shabang) {
85          ExternalHostSystem system = new ExternalHostSystem("localhost", PORT ,
86                  new NullMonitor(), shabang, userAdder);
87          return system;
88      }
89  }