1 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 ; 45 import javax.mail.internet.InternetAddress ; 46 import javax.mail.internet.MimeMessage ; 47 import javax.mail.internet.ParseException ; 48 49 import java.io.IOException ; 50 import java.net.ServerSocket ; 51 import java.util.Arrays ; 52 53 56 public class Util { 57 58 private static final int PORT_RANGE_START = 8000; private static final int PORT_RANGE_END = 11000; private static int PORT_LAST_USED = PORT_RANGE_START; 61 62 66 public static int getNonPrivilegedPort() { 67 return getNextNonPrivilegedPort(); } 69 70 74 protected static int getRandomNonPrivilegedPortInt() { 75 return ((int)( Math.random() * (PORT_RANGE_END - PORT_RANGE_START) + PORT_RANGE_START)); 76 } 77 78 82 protected synchronized static int getNextNonPrivilegedPort() { 83 int nextPortCandidate = PORT_LAST_USED; 85 while (true) { 86 try { 87 nextPortCandidate++; 88 if (PORT_LAST_USED == nextPortCandidate) throw new RuntimeException ("no free port found"); 89 if (nextPortCandidate > PORT_RANGE_END) nextPortCandidate = PORT_RANGE_START; 91 ServerSocket ss; 93 ss = new ServerSocket (nextPortCandidate); 94 ss.setReuseAddress(true); 95 ss.close(); 96 break; 97 } catch (IOException e) { 98 e.printStackTrace(); 99 continue; } 101 } 102 PORT_LAST_USED = nextPortCandidate; 103 return PORT_LAST_USED; 104 } 105 106 public static Configuration getValuedConfiguration(String name, String 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 handlerChainConfig.addChild(createCommandHandlerConfiguration(null, SendMailHandler.class)); 131 return handlerChainConfig; 132 } 133 134 private static DefaultConfiguration createCommandHandlerConfiguration(String command, Class commandClass) { 135 DefaultConfiguration cmdHandlerConfig = new DefaultConfiguration("handler"); 136 if (command != null) { 137 cmdHandlerConfig.setAttribute("command", command); 138 } 139 String classname = commandClass.getName(); 140 cmdHandlerConfig.setAttribute("class", classname); 141 return cmdHandlerConfig; 142 } 143 144 public static MockMail createMockMail2Recipients(MimeMessage m) throws ParseException { 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 { 155 return createMimeMessage(null, null); 156 } 157 158 public static MockMimeMessage createMimeMessageWithSubject(String subject) throws MessagingException { 159 return createMimeMessage(null, null, subject, 0); 160 } 161 162 public static MockMimeMessage createMimeMessage(String subject, int number) throws MessagingException { 163 return createMimeMessage(null, null, subject, number); 164 } 165 166 public static MockMimeMessage createMimeMessage(String headerName, String headerValue) throws MessagingException { 167 return createMimeMessage(headerName, headerValue, "testmail", 0); 168 } 169 170 public static MockMimeMessage createMimeMessage(String headerName, String headerValue, String subject, int number) throws MessagingException { 171 String sender = "test@james.apache.org"; 172 String rcpt = "test2@james.apache.org"; 173 174 MockMimeMessage mockedMimeMessage = new MockMimeMessage(number); 175 mockedMimeMessage.setFrom(new InternetAddress (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 188 public static DefaultDataSourceSelector getDataSourceSelector() throws Exception { 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 |