1 19 20 21 package org.apache.james.transport.matchers; 22 23 import org.apache.james.test.mock.mailet.MockMail; 24 import org.apache.james.test.mock.mailet.MockMatcherConfig; 25 26 import org.apache.mailet.Mail; 27 import org.apache.mailet.MailAddress; 28 import org.apache.mailet.MailetContext; 29 import org.apache.mailet.Matcher; 30 31 import javax.mail.MessagingException ; 32 import javax.mail.internet.MimeMessage ; 33 34 import java.io.UnsupportedEncodingException ; 35 import java.util.ArrayList ; 36 import java.util.Arrays ; 37 import java.util.Collection ; 38 import java.util.Iterator ; 39 40 import junit.framework.TestCase; 41 42 public class HostIsLocalTest extends TestCase { 43 44 private MockMail mockedMail; 45 46 private Matcher matcher; 47 48 private final String [] LOCALSERVER = new String [] { "james.apache.org" }; 49 50 private MailAddress[] recipients; 51 52 public HostIsLocalTest(String arg0) throws UnsupportedEncodingException { 53 super(arg0); 54 } 55 56 private void setRecipients(MailAddress[] recipients) { 57 this.recipients = recipients; 58 } 59 60 private void setupMockedMail() { 61 mockedMail = new MockMail(); 62 mockedMail.setRecipients(Arrays.asList(recipients)); 63 64 } 65 66 private void setupMatcher() throws MessagingException { 67 68 MailetContext mockMailContext = new MailetContext() { 69 70 Collection localServer = new ArrayList (Arrays.asList(LOCALSERVER)); 71 72 public void bounce(Mail mail, String message) 73 throws MessagingException { 74 throw new UnsupportedOperationException ( 75 "Unimplemented mock service"); 76 77 } 78 79 public void bounce(Mail mail, String message, MailAddress bouncer) 80 throws MessagingException { 81 throw new UnsupportedOperationException ( 82 "Unimplemented mock service"); 83 84 } 85 86 public Collection getMailServers(String host) { 87 throw new UnsupportedOperationException ( 88 "Unimplemented mock service"); 89 } 90 91 public MailAddress getPostmaster() { 92 throw new UnsupportedOperationException ( 93 "Unimplemented mock service"); 94 } 95 96 public Object getAttribute(String name) { 97 throw new UnsupportedOperationException ( 98 "Unimplemented mock service"); 99 } 100 101 public Iterator getAttributeNames() { 102 throw new UnsupportedOperationException ( 103 "Unimplemented mock service"); 104 } 105 106 public int getMajorVersion() { 107 throw new UnsupportedOperationException ( 108 "Unimplemented mock service"); 109 } 110 111 public int getMinorVersion() { 112 throw new UnsupportedOperationException ( 113 "Unimplemented mock service"); 114 } 115 116 public String getServerInfo() { 117 throw new UnsupportedOperationException ( 118 "Unimplemented mock service"); 119 } 120 121 public boolean isLocalServer(String serverName) { 122 return localServer.contains(serverName); 123 } 124 125 public boolean isLocalUser(String userAccount) { 126 throw new UnsupportedOperationException ( 127 "Unimplemented mock service"); 128 } 129 130 public boolean isLocalEmail(MailAddress mailAddress) { 131 throw new UnsupportedOperationException ( 132 "Unimplemented mock service"); 133 } 134 135 public void log(String message) { 136 throw new UnsupportedOperationException ( 137 "Unimplemented mock service"); 138 } 139 140 public void log(String message, Throwable t) { 141 throw new UnsupportedOperationException ( 142 "Unimplemented mock service"); 143 } 144 145 public void removeAttribute(String name) { 146 throw new UnsupportedOperationException ( 147 "Unimplemented mock service"); 148 } 149 150 public void sendMail(MimeMessage msg) throws MessagingException { 151 throw new UnsupportedOperationException ( 152 "Unimplemented mock service"); 153 } 154 155 public void sendMail(MailAddress sender, Collection recipients, 156 MimeMessage msg) throws MessagingException { 157 throw new UnsupportedOperationException ( 158 "Unimplemented mock service"); 159 } 160 161 public void sendMail(MailAddress sender, Collection recipients, 162 MimeMessage msg, String state) throws MessagingException { 163 throw new UnsupportedOperationException ( 164 "Unimplemented mock service"); 165 } 166 167 public void sendMail(Mail mail) throws MessagingException { 168 throw new UnsupportedOperationException ( 169 "Unimplemented mock service"); 170 } 171 172 public void setAttribute(String name, Object object) { 173 throw new UnsupportedOperationException ( 174 "Unimplemented mock service"); 175 } 176 177 public void storeMail(MailAddress sender, MailAddress recipient, 178 MimeMessage msg) throws MessagingException { 179 throw new UnsupportedOperationException ( 180 "Unimplemented mock service"); 181 } 182 183 public Iterator getSMTPHostAddresses(String domainName) { 184 throw new UnsupportedOperationException ( 185 "Unimplemented mock service"); 186 } 187 188 }; 189 190 matcher = new HostIsLocal(); 191 MockMatcherConfig mci = new MockMatcherConfig("HostIsLocal", 192 mockMailContext); 193 matcher.init(mci); 194 } 195 196 public void testHostIsMatchedAllRecipients() throws MessagingException { 198 setRecipients(new MailAddress[] { 199 new MailAddress("test@james.apache.org"), 200 new MailAddress("test2@james.apache.org") }); 201 202 setupMockedMail(); 203 setupMatcher(); 204 205 Collection matchedRecipients = matcher.match(mockedMail); 206 207 assertNotNull(matchedRecipients); 208 assertEquals(matchedRecipients.size(), mockedMail.getRecipients() 209 .size()); 210 } 211 212 public void testHostIsMatchedOneRecipient() throws MessagingException { 214 setRecipients(new MailAddress[] { 215 new MailAddress("test@james2.apache.org"), 216 new MailAddress("test2@james.apache.org") }); 217 218 setupMockedMail(); 219 setupMatcher(); 220 221 Collection matchedRecipients = matcher.match(mockedMail); 222 223 assertNotNull(matchedRecipients); 224 assertEquals(matchedRecipients.size(), 1); 225 } 226 227 public void testHostIsNotMatch() throws MessagingException { 229 setRecipients(new MailAddress[] { 230 new MailAddress("test@james2.apache.org"), 231 new MailAddress("test2@james2.apache.org") }); 232 233 setupMockedMail(); 234 setupMatcher(); 235 236 Collection matchedRecipients = matcher.match(mockedMail); 237 238 assertEquals(matchedRecipients.size(), 0); 239 } 240 } 241 | Popular Tags |