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 /**
23 * Runs protocol scripts.
24 */
25 public class Runner {
26
27 /** The Protocol session which is run before the testElements */
28 private final ProtocolSession preElements = new ProtocolSession();
29
30 /** The Protocol session which contains the tests elements */
31 private final ProtocolSession testElements = new ProtocolSession();
32
33 /** The Protocol session which is run after the testElements. */
34 private final ProtocolSession postElements = new ProtocolSession();
35
36 public void continueAfterFailure() {
37 preElements.setContinueAfterFailure(true);
38 testElements.setContinueAfterFailure(true);
39 postElements.setContinueAfterFailure(true);
40 }
41
42 /**
43 * Gets protocol session run after test.
44 * @return not null
45 */
46 public ProtocolInteractor getPostElements() {
47 return postElements;
48 }
49
50 /**
51 * Gets protocol session run before test.
52 * @return not null
53 */
54 public ProtocolInteractor getPreElements() {
55 return preElements;
56 }
57 /**
58 * Gets protocol session run on test.
59 * @return not null
60 */
61 public ProtocolInteractor getTestElements() {
62 return testElements;
63 }
64
65
66
67 /**
68 * <p>Runs the pre,test and post protocol sessions against a local copy of the
69 * server. This does not require that James be running, and is useful
70 * for rapid development and debugging.
71 * </p><p>
72 * Instead of sending requests to a socket connected to a running instance
73 * of James, this method uses the {@link HostSystem} to simplify
74 * testing. One mock instance is required per protocol session/connection.
75 */
76 public void runSessions(final SessionFactory factory) throws Exception {
77 class SessionContinuation implements HostSystem.Continuation {
78
79 public ProtocolSession session;
80
81 public void doContinue() {
82 if (session != null) {
83 session.doContinue();
84 }
85 }
86
87 }
88 SessionContinuation continuation = new SessionContinuation();
89
90 Session[] sessions = new Session[testElements
91 .getSessionCount()];
92
93 for (int i = 0; i < sessions.length; i++) {
94 sessions[i] = factory.newSession(continuation);
95 sessions[i].start();
96 }
97 try {
98 continuation.session = preElements;
99 preElements.runSessions(sessions);
100 continuation.session = testElements;
101 testElements.runSessions(sessions);
102 continuation.session = postElements;
103 postElements.runSessions(sessions);
104 } finally {
105 for (int i = 0; i < sessions.length; i++) {
106 sessions[i].stop();
107 }
108 }
109 }
110
111 /**
112 * Constructs a <code>String</code> with all attributes
113 * in name = value format.
114 *
115 * @return a <code>String</code> representation
116 * of this object.
117 */
118 public String toString()
119 {
120 final String TAB = " ";
121
122 String result = "Runner ( "
123 + "preElements = " + this.preElements + TAB
124 + "testElements = " + this.testElements + TAB
125 + "postElements = " + this.postElements + TAB
126 + " )";
127
128 return result;
129 }
130
131
132 }