1 19 20 21 package org.apache.james.transport.matchers; 22 23 import java.io.Serializable ; 24 import java.io.UnsupportedEncodingException ; 25 import java.net.InetAddress ; 26 import java.net.UnknownHostException ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Collection ; 30 import java.util.Date ; 31 import java.util.Iterator ; 32 33 import javax.mail.MessagingException ; 34 import javax.mail.internet.MimeMessage ; 35 import javax.mail.internet.ParseException ; 36 37 import junit.framework.TestCase; 38 39 import org.apache.james.Constants; 40 import org.apache.james.services.AbstractDNSServer; 41 import org.apache.james.services.DNSServer; 42 import org.apache.james.test.mock.avalon.MockServiceManager; 43 import org.apache.james.test.mock.mailet.MockMailContext; 44 import org.apache.james.test.mock.mailet.MockMatcherConfig; 45 import org.apache.mailet.Mail; 46 import org.apache.mailet.MailAddress; 47 48 public abstract class AbstractRemoteAddrInNetworkTest extends TestCase { 49 50 protected Mail mockedMail; 51 52 protected AbstractNetworkMatcher matcher; 53 54 private String remoteAddr; 55 56 private DNSServer dnsServer; 57 58 private MockServiceManager m_serviceManager; 59 60 public AbstractRemoteAddrInNetworkTest(String arg0) 61 throws UnsupportedEncodingException { 62 super(arg0); 63 } 64 65 protected void setRemoteAddr(String remoteAddr) { 66 this.remoteAddr = remoteAddr; 67 } 68 69 protected void setupMockedMail() { 70 mockedMail = new Mail() { 71 72 private static final long serialVersionUID = 1L; 73 74 public String getName() { 75 throw new UnsupportedOperationException ( 76 "Unimplemented mock service"); 77 } 78 79 public void setName(String newName) { 80 throw new UnsupportedOperationException ( 81 "Unimplemented mock service"); 82 } 83 84 public MimeMessage getMessage() throws MessagingException { 85 throw new UnsupportedOperationException ( 86 "Unimplemented mock service"); 87 } 88 89 public Collection getRecipients() { 90 ArrayList r = new ArrayList (); 91 try { 92 r = new ArrayList (Arrays 93 .asList(new MailAddress[] { new MailAddress( 94 "test@james.apache.org") })); 95 } catch (ParseException e) { 96 } 97 return r; 98 } 99 100 public void setRecipients(Collection recipients) { 101 throw new UnsupportedOperationException ( 102 "Unimplemented mock service"); 103 } 104 105 public MailAddress getSender() { 106 throw new UnsupportedOperationException ( 107 "Unimplemented mock service"); 108 } 109 110 public String getState() { 111 throw new UnsupportedOperationException ( 112 "Unimplemented mock service"); 113 } 114 115 public String getRemoteHost() { 116 throw new UnsupportedOperationException ( 117 "Unimplemented mock service"); 118 } 119 120 public String getRemoteAddr() { 121 return remoteAddr; 122 } 123 124 public String getErrorMessage() { 125 throw new UnsupportedOperationException ( 126 "Unimplemented mock service"); 127 } 128 129 public void setErrorMessage(String msg) { 130 throw new UnsupportedOperationException ( 131 "Unimplemented mock service"); 132 } 133 134 public void setMessage(MimeMessage message) { 135 throw new UnsupportedOperationException ( 136 "Unimplemented mock service"); 137 } 138 139 public void setState(String state) { 140 throw new UnsupportedOperationException ( 141 "Unimplemented mock service"); 142 } 143 144 public Serializable getAttribute(String name) { 145 throw new UnsupportedOperationException ( 146 "Unimplemented mock service"); 147 } 148 149 public Iterator getAttributeNames() { 150 throw new UnsupportedOperationException ( 151 "Unimplemented mock service"); 152 } 153 154 public boolean hasAttributes() { 155 throw new UnsupportedOperationException ( 156 "Unimplemented mock service"); 157 } 158 159 public Serializable removeAttribute(String name) { 160 throw new UnsupportedOperationException ( 161 "Unimplemented mock service"); 162 } 163 164 public void removeAllAttributes() { 165 throw new UnsupportedOperationException ( 166 "Unimplemented mock service"); 167 } 168 169 public Serializable setAttribute(String name, Serializable object) { 170 throw new UnsupportedOperationException ( 171 "Unimplemented mock service"); 172 } 173 174 public long getMessageSize() throws MessagingException { 175 throw new UnsupportedOperationException ( 176 "Unimplemented mock service"); 177 } 178 179 public Date getLastUpdated() { 180 throw new UnsupportedOperationException ( 181 "Unimplemented mock service"); 182 } 183 184 public void setLastUpdated(Date lastUpdated) { 185 throw new UnsupportedOperationException ( 186 "Unimplemented mock service"); 187 } 188 189 }; 190 191 } 192 193 protected void setupDNSServer() { 194 dnsServer = new AbstractDNSServer() { 195 public InetAddress getByName(String host) 196 throws UnknownHostException { 197 if ("192.168.200.0".equals(host) || "255.255.255.0".equals(host) || "192.168.200.1".equals(host) || "192.168.0.1".equals(host) || "192.168.1.1".equals(host)) { 198 return InetAddress.getByName(host); 200 } 201 throw new UnsupportedOperationException ("getByName("+host+") unimplemented in AbstractRemoteAddrInNetworkTest"); 202 } 203 204 }; 205 } 206 207 protected void setupMatcher() throws MessagingException { 208 m_serviceManager = new MockServiceManager(); 209 m_serviceManager.put(DNSServer.ROLE, dnsServer); 210 211 MockMailContext mmc = new MockMailContext(); 212 mmc.setAttribute(Constants.AVALON_COMPONENT_MANAGER, m_serviceManager); 213 matcher = createMatcher(); 214 MockMatcherConfig mci = new MockMatcherConfig(getConfigOption() 215 + getAllowedNetworks(), mmc); 216 matcher.init(mci); 217 } 218 219 protected void setupAll() throws MessagingException { 220 setupDNSServer(); 221 setupMockedMail(); 222 setupMatcher(); 223 } 224 225 protected abstract String getConfigOption(); 226 227 protected abstract String getAllowedNetworks(); 228 229 protected abstract AbstractNetworkMatcher createMatcher(); 230 } 231 | Popular Tags |