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: ****************************************************************/package org.apache.james.test.util;
019:
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.avalon.framework.configuration.DefaultConfiguration;
022: import org.apache.james.smtpserver.*;
023:
024: import java.io.IOException;
025: import java.net.ServerSocket;
026:
027: /**
028: * some utilities for James unit testing
029: */
030: public class Util {
031:
032: private static final int PORT_RANGE_START = 8000; // the lowest possible port number assigned for testing
033: private static final int PORT_RANGE_END = 11000; // the highest possible port number assigned for testing
034: private static int PORT_LAST_USED = PORT_RANGE_START;
035:
036: /**
037: * assigns a port from the range of test ports
038: * @return port number
039: */
040: public static int getNonPrivilegedPort() {
041: return getNextNonPrivilegedPort(); // uses sequential assignment of ports
042: }
043:
044: /**
045: * assigns a random port from the range of test ports
046: * @return port number
047: */
048: protected static int getRandomNonPrivilegedPortInt() {
049: return ((int) (Math.random()
050: * (PORT_RANGE_END - PORT_RANGE_START) + PORT_RANGE_START));
051: }
052:
053: /**
054: * assigns ports sequentially from the range of test ports
055: * @return port number
056: */
057: protected synchronized static int getNextNonPrivilegedPort() {
058: // Hack to increase probability that the port is bindable
059: while (true) {
060: try {
061: PORT_LAST_USED++;
062: if (PORT_LAST_USED > PORT_RANGE_END)
063: PORT_LAST_USED = PORT_RANGE_START;
064: ServerSocket ss;
065: ss = new ServerSocket(PORT_LAST_USED);
066: ss.setReuseAddress(true);
067: ss.close();
068: break;
069: } catch (IOException e) {
070: // TODO Auto-generated catch block
071: e.printStackTrace();
072: }
073: }
074: return PORT_LAST_USED;
075: }
076:
077: public static Configuration getValuedConfiguration(String name,
078: String value) {
079: DefaultConfiguration defaultConfiguration = new DefaultConfiguration(
080: name);
081: defaultConfiguration.setValue(value);
082: return defaultConfiguration;
083: }
084:
085: public static DefaultConfiguration createRemoteManagerHandlerChainConfiguration() {
086: DefaultConfiguration handlerChainConfig = new DefaultConfiguration(
087: "test");
088: return handlerChainConfig;
089: }
090:
091: public static DefaultConfiguration createSMTPHandlerChainConfiguration() {
092: DefaultConfiguration handlerChainConfig = new DefaultConfiguration(
093: "handlerchain");
094: handlerChainConfig.addChild(createCommandHandlerConfiguration(
095: "HELO", HeloCmdHandler.class));
096: handlerChainConfig.addChild(createCommandHandlerConfiguration(
097: "EHLO", EhloCmdHandler.class));
098: handlerChainConfig.addChild(createCommandHandlerConfiguration(
099: "AUTH", AuthCmdHandler.class));
100: handlerChainConfig.addChild(createCommandHandlerConfiguration(
101: "VRFY", VrfyCmdHandler.class));
102: handlerChainConfig.addChild(createCommandHandlerConfiguration(
103: "EXPN", ExpnCmdHandler.class));
104: handlerChainConfig.addChild(createCommandHandlerConfiguration(
105: "MAIL", MailCmdHandler.class));
106: handlerChainConfig.addChild(createCommandHandlerConfiguration(
107: "RCPT", RcptCmdHandler.class));
108: handlerChainConfig.addChild(createCommandHandlerConfiguration(
109: "DATA", DataCmdHandler.class));
110: handlerChainConfig.addChild(createCommandHandlerConfiguration(
111: "RSET", RsetCmdHandler.class));
112: handlerChainConfig.addChild(createCommandHandlerConfiguration(
113: "HELP", HelpCmdHandler.class));
114: handlerChainConfig.addChild(createCommandHandlerConfiguration(
115: "QUIT", QuitCmdHandler.class));
116: // mail sender
117: handlerChainConfig.addChild(createCommandHandlerConfiguration(
118: null, SendMailHandler.class));
119: return handlerChainConfig;
120: }
121:
122: private static DefaultConfiguration createCommandHandlerConfiguration(
123: String command, Class commandClass) {
124: DefaultConfiguration cmdHandlerConfig = new DefaultConfiguration(
125: "handler");
126: if (command != null) {
127: cmdHandlerConfig.setAttribute("command", command);
128: }
129: String classname = commandClass.getName();
130: cmdHandlerConfig.setAttribute("class", classname);
131: return cmdHandlerConfig;
132: }
133:
134: }
|