View Javadoc

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 junit.framework.TestCase;
23  
24  /**
25   * Abstract Protocol Test is the root of all of the scripted test
26   * cases. It provides basic functionality for running a protocol session as a
27   * JUnit test, and failing if exceptions are thrown. To create a test which
28   * reads the entire protocol session from a single protocol definition file, use
29   * the {@link AbstractSimpleScriptedTestProtocol}.
30   * 
31   * @author Darrell DeBoer
32   * @author Andrew C. Oliver
33   */
34  public abstract class AbstractProtocolTestFramework extends TestCase {
35  
36      protected final Runner runner;
37      private final HostSystem hostSystem;
38      
39      private final String userName;
40      private final String password;
41  
42      public AbstractProtocolTestFramework(HostSystem hostSystem, String userName, String password) {
43          this.hostSystem = hostSystem;
44          this.userName = userName;
45          this.password = password;
46          runner = new Runner();
47      }
48  
49      protected void setUp() throws Exception {
50          super.setUp();
51          setUpEnvironment();
52      }
53  
54      protected void continueAfterFailure() {
55          runner.continueAfterFailure();
56      }
57  
58      /**
59       * <p>Runs the pre,test and post protocol sessions against a local copy of the
60       * Server. This is useful for rapid development and debugging.
61       * </p>
62       * Instead of sending requests to a socket connected to a running instance
63       * of James, this method uses the {@link HostSystem} to simplify
64       * testing. One mock instance is required per protocol session/connection.
65       */
66      protected void runSessions() throws Exception {
67          runner.runSessions(hostSystem);
68      }
69  
70      /**
71       * Initialises the host on first call.
72       */
73      private void setUpEnvironment() throws Exception {
74          hostSystem.reset();
75          hostSystem.addUser(userName, password);
76      }
77  }