001/****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one   *
003 * or more contributor license agreements.  See the NOTICE file *
004 * distributed with this work for additional information        *
005 * regarding copyright ownership.  The ASF licenses this file   *
006 * to you under the Apache License, Version 2.0 (the            *
007 * "License"); you may not use this file except in compliance   *
008 * with the License.  You may obtain a copy of the License at   *
009 *                                                              *
010 *   http://www.apache.org/licenses/LICENSE-2.0                 *
011 *                                                              *
012 * Unless required by applicable law or agreed to in writing,   *
013 * software distributed under the License is distributed on an  *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015 * KIND, either express or implied.  See the License for the    *
016 * specific language governing permissions and limitations      *
017 * under the License.                                           *
018 ****************************************************************/
019
020package org.apache.james.mpt.host;
021
022import org.apache.commons.lang.NotImplementedException;
023import org.apache.james.mailbox.model.MailboxPath;
024import org.apache.james.mpt.api.ImapFeatures;
025import org.apache.james.mpt.api.ImapFeatures.Feature;
026import org.apache.james.mpt.api.ImapHostSystem;
027import org.apache.james.mpt.api.Monitor;
028import org.apache.james.mpt.api.UserAdder;
029import org.apache.james.mpt.session.ExternalSessionFactory;
030
031/**
032 * <p>
033 * Connects to a host system serving on an open port.
034 * </p>
035 * <p>
036 * This is typically used for functional integration testing of a complete
037 * server system (including sockets). Apache James MPT AntLib provides an <a
038 * href='http://ant.apache.org' rel='tag'>Ant</a> task suitable for this use
039 * case.
040 * </p>
041 */
042public class ExternalHostSystem extends ExternalSessionFactory implements ImapHostSystem {
043
044    private final UserAdder userAdder;
045    private final ImapFeatures features;
046
047    /**
048     * Constructs a host system suitable for connection to an open port.
049     * 
050     * @param features
051     *            set of features supported by the system
052     * @param host
053     *            host name that will be connected to, not null
054     * @param port
055     *            port on host that will be connected to, not null
056     * @param monitor
057     *            monitors the conduct of the connection
058     * @param shabang
059     *            protocol shabang will be sent to the script test in the place
060     *            of the first line received from the server. Many protocols
061     *            pass server specific information in the first line. When not
062     *            null, this line will be replaced. Or null when the first line
063     *            should be passed without replacement
064     * @param userAdder
065     *            null when test system has appropriate users already set
066     */
067    public ExternalHostSystem(ImapFeatures features, String host, int port, 
068            Monitor monitor, String shabang, UserAdder userAdder) {
069        super(host, port, monitor, shabang);
070        this.features = features;
071        this.userAdder = userAdder;
072    }
073    
074    public ExternalHostSystem(ImapFeatures features, Monitor monitor, String shabang, UserAdder userAdder) {
075        super(monitor, shabang);
076        this.features = features;
077        this.userAdder = userAdder;
078    }
079    public boolean addUser(String user, String password) throws Exception {
080        if (userAdder == null) {
081            monitor.note("Please ensure user '" + user + "' with password '" + password + "' exists.");
082            return false;
083        }
084        else {
085            userAdder.addUser(user, password);
086        }
087        return true;
088    }
089
090    public void createMailbox(MailboxPath mailboxPath) throws Exception {
091        throw new NotImplementedException();
092    }
093    
094    public void beforeTests() throws Exception {
095    }
096
097    public void afterTests() throws Exception {
098    }
099
100    public void beforeTest() throws Exception {
101    }
102    
103    public void afterTest() throws Exception {
104    }
105
106    @Override
107    public boolean supports(Feature... features) {
108        return this.features.supports(features);
109    }
110
111    @Override
112    public void setQuotaLimits(long maxMessageQuota, long maxStorageQuota) throws Exception {
113        throw new NotImplementedException();
114    }
115    
116}