KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > test > util > Util


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.test.util;
21
22 import org.apache.avalon.cornerstone.blocks.datasources.DefaultDataSourceSelector;
23 import org.apache.avalon.framework.configuration.Configuration;
24 import org.apache.avalon.framework.configuration.DefaultConfiguration;
25 import org.apache.james.smtpserver.core.AuthCmdHandler;
26 import org.apache.james.smtpserver.core.DataCmdHandler;
27 import org.apache.james.smtpserver.core.EhloCmdHandler;
28 import org.apache.james.smtpserver.core.ExpnCmdHandler;
29 import org.apache.james.smtpserver.core.HeloCmdHandler;
30 import org.apache.james.smtpserver.core.HelpCmdHandler;
31 import org.apache.james.smtpserver.core.MailCmdHandler;
32 import org.apache.james.smtpserver.core.QuitCmdHandler;
33 import org.apache.james.smtpserver.core.RcptCmdHandler;
34 import org.apache.james.smtpserver.core.RsetCmdHandler;
35 import org.apache.james.smtpserver.core.SendMailHandler;
36 import org.apache.james.smtpserver.core.VrfyCmdHandler;
37 import org.apache.james.test.mock.avalon.MockLogger;
38 import org.apache.james.test.mock.james.MockMailServer;
39 import org.apache.james.test.mock.javaxmail.MockMimeMessage;
40 import org.apache.james.test.mock.mailet.MockMail;
41 import org.apache.james.test.mock.util.AttrValConfiguration;
42 import org.apache.mailet.MailAddress;
43
44 import javax.mail.MessagingException JavaDoc;
45 import javax.mail.internet.InternetAddress JavaDoc;
46 import javax.mail.internet.MimeMessage JavaDoc;
47 import javax.mail.internet.ParseException JavaDoc;
48
49 import java.io.IOException JavaDoc;
50 import java.net.ServerSocket JavaDoc;
51 import java.util.Arrays JavaDoc;
52
53 /**
54  * some utilities for James unit testing
55  */

56 public class Util {
57
58     private static final int PORT_RANGE_START = 8000; // the lowest possible port number assigned for testing
59
private static final int PORT_RANGE_END = 11000; // the highest possible port number assigned for testing
60
private static int PORT_LAST_USED = PORT_RANGE_START;
61
62     /**
63      * assigns a port from the range of test ports
64      * @return port number
65      */

66     public static int getNonPrivilegedPort() {
67         return getNextNonPrivilegedPort(); // uses sequential assignment of ports
68
}
69
70     /**
71      * assigns a random port from the range of test ports
72      * @return port number
73      */

74     protected static int getRandomNonPrivilegedPortInt() {
75         return ((int)( Math.random() * (PORT_RANGE_END - PORT_RANGE_START) + PORT_RANGE_START));
76     }
77
78     /**
79      * assigns ports sequentially from the range of test ports
80      * @return port number
81      */

82     protected synchronized static int getNextNonPrivilegedPort() {
83         // Hack to increase probability that the port is bindable
84
int nextPortCandidate = PORT_LAST_USED;
85         while (true) {
86             try {
87                 nextPortCandidate++;
88                 if (PORT_LAST_USED == nextPortCandidate) throw new RuntimeException JavaDoc("no free port found");
89                 if (nextPortCandidate > PORT_RANGE_END) nextPortCandidate = PORT_RANGE_START; // start over
90

91                 // test, port is available
92
ServerSocket JavaDoc ss;
93                 ss = new ServerSocket JavaDoc(nextPortCandidate);
94                 ss.setReuseAddress(true);
95                 ss.close();
96                 break;
97             } catch (IOException JavaDoc e) {
98                 e.printStackTrace();
99                 continue; // try next port
100
}
101         }
102         PORT_LAST_USED = nextPortCandidate;
103         return PORT_LAST_USED;
104     }
105
106     public static Configuration getValuedConfiguration(String JavaDoc name, String JavaDoc value) {
107         DefaultConfiguration defaultConfiguration = new DefaultConfiguration(name);
108         defaultConfiguration.setValue(value);
109         return defaultConfiguration;
110     }
111
112     public static DefaultConfiguration createRemoteManagerHandlerChainConfiguration() {
113         DefaultConfiguration handlerChainConfig = new DefaultConfiguration("test");
114         return handlerChainConfig;
115     }
116     public static DefaultConfiguration createSMTPHandlerChainConfiguration() {
117         DefaultConfiguration handlerChainConfig = new DefaultConfiguration("handlerchain");
118         handlerChainConfig.addChild(createCommandHandlerConfiguration("HELO", HeloCmdHandler.class));
119         handlerChainConfig.addChild(createCommandHandlerConfiguration("EHLO", EhloCmdHandler.class));
120         handlerChainConfig.addChild(createCommandHandlerConfiguration("AUTH", AuthCmdHandler.class));
121         handlerChainConfig.addChild(createCommandHandlerConfiguration("VRFY", VrfyCmdHandler.class));
122         handlerChainConfig.addChild(createCommandHandlerConfiguration("EXPN", ExpnCmdHandler.class));
123         handlerChainConfig.addChild(createCommandHandlerConfiguration("MAIL", MailCmdHandler.class));
124         handlerChainConfig.addChild(createCommandHandlerConfiguration("RCPT", RcptCmdHandler.class));
125         handlerChainConfig.addChild(createCommandHandlerConfiguration("DATA", DataCmdHandler.class));
126         handlerChainConfig.addChild(createCommandHandlerConfiguration("RSET", RsetCmdHandler.class));
127         handlerChainConfig.addChild(createCommandHandlerConfiguration("HELP", HelpCmdHandler.class));
128         handlerChainConfig.addChild(createCommandHandlerConfiguration("QUIT", QuitCmdHandler.class));
129         // mail sender
130
handlerChainConfig.addChild(createCommandHandlerConfiguration(null, SendMailHandler.class));
131         return handlerChainConfig;
132     }
133
134     private static DefaultConfiguration createCommandHandlerConfiguration(String JavaDoc command, Class JavaDoc commandClass) {
135         DefaultConfiguration cmdHandlerConfig = new DefaultConfiguration("handler");
136         if (command != null) {
137             cmdHandlerConfig.setAttribute("command", command);
138         }
139         String JavaDoc classname = commandClass.getName();
140         cmdHandlerConfig.setAttribute("class", classname);
141         return cmdHandlerConfig;
142     }
143
144     public static MockMail createMockMail2Recipients(MimeMessage m) throws ParseException JavaDoc {
145         MockMail mockedMail = new MockMail();
146         mockedMail.setName(MockMailServer.newId());
147         mockedMail.setMessage(m);
148         mockedMail.setRecipients(Arrays.asList(new MailAddress[] {
149                 new MailAddress("test@james.apache.org"),
150                 new MailAddress("test2@james.apache.org") }));
151         return mockedMail;
152     }
153
154     public static MockMimeMessage createMimeMessage() throws MessagingException JavaDoc {
155         return createMimeMessage(null, null);
156     }
157     
158     public static MockMimeMessage createMimeMessageWithSubject(String JavaDoc subject) throws MessagingException JavaDoc {
159         return createMimeMessage(null, null, subject, 0);
160     }
161     
162     public static MockMimeMessage createMimeMessage(String JavaDoc subject, int number) throws MessagingException JavaDoc {
163         return createMimeMessage(null, null, subject, number);
164     }
165     
166     public static MockMimeMessage createMimeMessage(String JavaDoc headerName, String JavaDoc headerValue) throws MessagingException JavaDoc {
167         return createMimeMessage(headerName, headerValue, "testmail", 0);
168     }
169     
170     public static MockMimeMessage createMimeMessage(String JavaDoc headerName, String JavaDoc headerValue, String JavaDoc subject, int number) throws MessagingException JavaDoc {
171         String JavaDoc sender = "test@james.apache.org";
172         String JavaDoc rcpt = "test2@james.apache.org";
173
174         MockMimeMessage mockedMimeMessage = new MockMimeMessage(number);
175         mockedMimeMessage.setFrom(new InternetAddress JavaDoc(sender));
176         mockedMimeMessage.setRecipients(MimeMessage.RecipientType.TO, rcpt);
177         if (headerName != null) mockedMimeMessage.setHeader(headerName, headerValue);
178         if (subject != null) mockedMimeMessage.setSubject(subject);
179         mockedMimeMessage.setText("testtext");
180         mockedMimeMessage.saveChanges();
181         return mockedMimeMessage;
182     }
183
184     /**
185      * @return
186      * @throws Exception
187      */

188     public static DefaultDataSourceSelector getDataSourceSelector() throws Exception JavaDoc {
189         DefaultDataSourceSelector dataSourceSelector = new DefaultDataSourceSelector();
190         dataSourceSelector.enableLogging(new MockLogger());
191         DefaultConfiguration dc = new DefaultConfiguration("database-connections");
192         DefaultConfiguration ds = new DefaultConfiguration("data-source");
193         ds.setAttribute("name","maildb");
194         ds.setAttribute("class","org.apache.james.util.dbcp.JdbcDataSource");
195         
196         ds.addChild(new AttrValConfiguration("driver","org.apache.derby.jdbc.EmbeddedDriver"));
197         ds.addChild(new AttrValConfiguration("dburl","jdbc:derby:target/testdb;create=true"));
198         ds.addChild(new AttrValConfiguration("user","james"));
199         ds.addChild(new AttrValConfiguration("password","james"));
200     
201         ds.addChild(new AttrValConfiguration("max","20"));
202         dc.addChild(ds);
203         dataSourceSelector.configure(dc);
204         dataSourceSelector.initialize();
205         return dataSourceSelector;
206     }
207 }
208
Popular Tags