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.InputStream;
23 import java.util.Locale;
24
25
26 /**
27 * A Protocol test which reads the test protocol session from a file. The file
28 * read is taken as "<test-name>.test", where <test-name> is the value passed
29 * into the constructor. Subclasses of this test can set up pre-
30 * and post elements for extra elements not defined in the protocol
31 * session file.
32 */
33 public abstract class AbstractSimpleScriptedTestProtocol extends
34 AbstractProtocolTestFramework {
35 private ProtocolSessionBuilder builder = new ProtocolSessionBuilder();
36
37 private static final Locale BASE_DEFAULT_LOCALE = Locale.getDefault();
38
39 /**
40 * Constructs a scripted test.
41 * @param hostSystem not null
42 * @param userName user name
43 * @param password password for user
44 */
45 public AbstractSimpleScriptedTestProtocol(HostSystem hostSystem, String userName, String password) {
46 super(hostSystem, userName, password);
47 }
48
49 protected void tearDown() throws Exception {
50 Locale.setDefault(BASE_DEFAULT_LOCALE);
51 super.tearDown();
52 }
53
54 /**
55 * Reads test elements from the protocol session file and adds them to the
56 * ProtocolSession. Then calls {@link #runSessions()}.
57 *
58 * @param locale test under this default locale, not null
59 */
60 protected void scriptTest(String fileName, Locale locale) throws Exception {
61 Locale.setDefault(locale);
62 addTestFile(fileName + ".test", runner.getTestElements());
63 runSessions();
64 }
65
66 /**
67 * Finds the protocol session file identified by the test name, and builds
68 * protocol elements from it. All elements from the definition file are
69 * added to the supplied ProtocolSession.
70 *
71 * @param fileName
72 * The name of the file to read
73 * @param session
74 * The ProtocolSession to add elements to.
75 */
76 protected void addTestFile(String fileName, ProtocolInteractor session)
77 throws Exception {
78 // Need to find local resource.
79 InputStream is = this.getClass().getResourceAsStream(fileName);
80 if (is == null) {
81 throw new Exception("Test Resource '" + fileName + "' not found.");
82 }
83
84 builder.addProtocolLines(fileName, is, session);
85 }
86 }