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  package org.apache.james.test.util;
20  
21  import org.apache.avalon.framework.configuration.Configuration;
22  import org.apache.avalon.framework.configuration.DefaultConfiguration;
23  import org.apache.james.smtpserver.*;
24  
25  import java.io.IOException;
26  import java.net.ServerSocket;
27  
28  /***
29   * some utilities for James unit testing
30   */
31  public class Util {
32  
33      private static final int PORT_RANGE_START =  8000; // the lowest possible port number assigned for testing
34      private static final int PORT_RANGE_END   = 11000; // the highest possible port number assigned for testing
35      private static int PORT_LAST_USED = PORT_RANGE_START;
36  
37      /***
38       * assigns a port from the range of test ports
39       * @return port number
40       */
41      public static int getNonPrivilegedPort() {
42          return getNextNonPrivilegedPort(); // uses sequential assignment of ports
43      }
44  
45      /***
46       * assigns a random port from the range of test ports
47       * @return port number
48       */
49      protected static int getRandomNonPrivilegedPortInt() {
50          return ((int)( Math.random() * (PORT_RANGE_END - PORT_RANGE_START) + PORT_RANGE_START));
51      }
52  
53      /***
54       * assigns ports sequentially from the range of test ports
55       * @return port number
56       */
57      protected synchronized static int getNextNonPrivilegedPort() {
58          // Hack to increase probability that the port is bindable
59          while (true) {
60              try {
61          PORT_LAST_USED++;
62          if (PORT_LAST_USED > PORT_RANGE_END) PORT_LAST_USED = PORT_RANGE_START;
63                  ServerSocket ss;
64                  ss = new ServerSocket(PORT_LAST_USED);
65                  ss.setReuseAddress(true);
66                  ss.close();
67                  break;
68              } catch (IOException e) {
69                  // TODO Auto-generated catch block
70                  e.printStackTrace();
71              }
72          }
73          return PORT_LAST_USED;
74      }
75  
76      public static Configuration getValuedConfiguration(String name, String value) {
77          DefaultConfiguration defaultConfiguration = new DefaultConfiguration(name);
78          defaultConfiguration.setValue(value);
79          return defaultConfiguration;
80      }
81  
82      public static DefaultConfiguration createRemoteManagerHandlerChainConfiguration() {
83          DefaultConfiguration handlerChainConfig = new DefaultConfiguration("test");
84          return handlerChainConfig;
85      }
86      public static DefaultConfiguration createSMTPHandlerChainConfiguration() {
87          DefaultConfiguration handlerChainConfig = new DefaultConfiguration("handlerchain");
88          handlerChainConfig.addChild(createCommandHandlerConfiguration("HELO", HeloCmdHandler.class));
89          handlerChainConfig.addChild(createCommandHandlerConfiguration("EHLO", EhloCmdHandler.class));
90          handlerChainConfig.addChild(createCommandHandlerConfiguration("AUTH", AuthCmdHandler.class));
91          handlerChainConfig.addChild(createCommandHandlerConfiguration("VRFY", VrfyCmdHandler.class));
92          handlerChainConfig.addChild(createCommandHandlerConfiguration("EXPN", ExpnCmdHandler.class));
93          handlerChainConfig.addChild(createCommandHandlerConfiguration("MAIL", MailCmdHandler.class));
94          handlerChainConfig.addChild(createCommandHandlerConfiguration("RCPT", RcptCmdHandler.class));
95          handlerChainConfig.addChild(createCommandHandlerConfiguration("DATA", DataCmdHandler.class));
96          handlerChainConfig.addChild(createCommandHandlerConfiguration("RSET", RsetCmdHandler.class));
97          handlerChainConfig.addChild(createCommandHandlerConfiguration("HELP", HelpCmdHandler.class));
98          handlerChainConfig.addChild(createCommandHandlerConfiguration("QUIT", QuitCmdHandler.class));
99          // mail sender
100         handlerChainConfig.addChild(createCommandHandlerConfiguration(null, SendMailHandler.class));
101         return handlerChainConfig;
102     }
103 
104     private static DefaultConfiguration createCommandHandlerConfiguration(String command, Class commandClass) {
105         DefaultConfiguration cmdHandlerConfig = new DefaultConfiguration("handler");
106         if (command != null) {
107             cmdHandlerConfig.setAttribute("command", command);
108         }
109         String classname = commandClass.getName();
110         cmdHandlerConfig.setAttribute("class", classname);
111         return cmdHandlerConfig;
112     }
113 
114 }