Author: norman Date: Mon May 29 11:23:03 2006 New Revision: 410136 URL: http://svn.apache.org/viewvc?rev=410136&view=rev Log: -Add MockMatcherConfig -Add HasHeader matcher junit test
Added: james/server/trunk/src/test/org/apache/james/test/mock/mailet/MockMatcherConfig.java james/server/trunk/src/test/org/apache/james/transport/matchers/ james/server/trunk/src/test/org/apache/james/transport/matchers/HasHeaderTest.java Added: james/server/trunk/src/test/org/apache/james/test/mock/mailet/MockMatcherConfig.java URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/test/mock/mailet/MockMatcherConfig.java?rev=410136&view=auto ============================================================================== --- james/server/trunk/src/test/org/apache/james/test/mock/mailet/MockMatcherConfig.java (added) +++ james/server/trunk/src/test/org/apache/james/test/mock/mailet/MockMatcherConfig.java Mon May 29 11:23:03 2006 @@ -0,0 +1,49 @@ +/*********************************************************************** + * Copyright (c) 2006 The Apache Software Foundation. * + * All rights reserved. * + * ------------------------------------------------------------------- * + * Licensed under the Apache License, Version 2.0 (the "License"); you * + * may not use this file except in compliance with the License. You * + * may obtain a copy of the License at: * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * + * implied. See the License for the specific language governing * + * permissions and limitations under the License. * + ***********************************************************************/ +package org.apache.james.test.mock.mailet; + +import org.apache.mailet.MailetContext; +import org.apache.mailet.MatcherConfig; + +/** + * MailetConfig over Properties + */ +public class MockMatcherConfig implements MatcherConfig { + + private String matcherName; + + private MailetContext mc; + + public MockMatcherConfig(String matcherName, MailetContext mc) { + super(); + this.matcherName = matcherName; + this.mc = mc; + } + + public String getCondition() { + return matcherName.split("=")[1]; + } + + public MailetContext getMailetContext() { + return mc; + } + + public String getMatcherName() { + return matcherName; + } + +} Added: james/server/trunk/src/test/org/apache/james/transport/matchers/HasHeaderTest.java URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/transport/matchers/HasHeaderTest.java?rev=410136&view=auto ============================================================================== --- james/server/trunk/src/test/org/apache/james/transport/matchers/HasHeaderTest.java (added) +++ james/server/trunk/src/test/org/apache/james/transport/matchers/HasHeaderTest.java Mon May 29 11:23:03 2006 @@ -0,0 +1,226 @@ +/*********************************************************************** + * Copyright (c) 2006 The Apache Software Foundation. * + * All rights reserved. * + * ------------------------------------------------------------------- * + * Licensed under the Apache License, Version 2.0 (the "License"); you * + * may not use this file except in compliance with the License. You * + * may obtain a copy of the License at: * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * + * implied. See the License for the specific language governing * + * permissions and limitations under the License. * + ***********************************************************************/ + +package org.apache.james.transport.matchers; + +import org.apache.james.test.mock.javaxmail.MockMimeMessage; +import org.apache.james.test.mock.mailet.MockMailContext; +import org.apache.james.test.mock.mailet.MockMatcherConfig; +import org.apache.mailet.Mail; +import org.apache.mailet.MailAddress; +import org.apache.mailet.Matcher; + +import javax.mail.MessagingException; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMessage.RecipientType; + +import java.io.Serializable; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.Iterator; + +import junit.framework.TestCase; + +public class HasHeaderTest extends TestCase { + + private MimeMessage mockedMimeMessage; + + private Mail mockedMail; + + private Matcher matcher; + + private final String HEADER_NAME = "JUNIT"; + + private final String HEADER_VALUE = "test-value"; + + private String headerName = "defaultHeaderName"; + + private String headerValue = "defaultHeaderValue"; + + public HasHeaderTest(String arg0) throws UnsupportedEncodingException { + super(arg0); + } + + private void setHeaderName(String headerName) { + this.headerName = headerName; + } + + private void setHeaderValue(String headerValue) { + this.headerValue = headerValue; + } + + private void setupMockedMimeMessage() throws MessagingException { + String sender = "[EMAIL PROTECTED]"; + String rcpt = "[EMAIL PROTECTED]"; + + mockedMimeMessage = new MockMimeMessage(); + mockedMimeMessage.setFrom(new InternetAddress(sender)); + mockedMimeMessage.setRecipients(RecipientType.TO, rcpt); + mockedMimeMessage.setHeader(headerName, headerValue); + mockedMimeMessage.setSubject("testmail"); + mockedMimeMessage.setText("testtext"); + mockedMimeMessage.saveChanges(); + + } + + private void setupMockedMail() { + mockedMail = new Mail() { + + private static final long serialVersionUID = 1L; + + public String getName() { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public void setName(String newName) { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public MimeMessage getMessage() throws MessagingException { + return mockedMimeMessage; + } + + public Collection getRecipients() { + return new ArrayList(); + } + + public void setRecipients(Collection recipients) { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public MailAddress getSender() { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public String getState() { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public String getRemoteHost() { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public String getRemoteAddr() { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public String getErrorMessage() { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public void setErrorMessage(String msg) { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public void setMessage(MimeMessage message) { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public void setState(String state) { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public Serializable getAttribute(String name) { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public Iterator getAttributeNames() { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public boolean hasAttributes() { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public Serializable removeAttribute(String name) { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public void removeAllAttributes() { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public Serializable setAttribute(String name, Serializable object) { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public long getMessageSize() throws MessagingException { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public Date getLastUpdated() { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + public void setLastUpdated(Date lastUpdated) { + throw new UnsupportedOperationException( + "Unimplemented mock service"); + } + + }; + } + + private void setupMatcher() throws MessagingException { + setupMockedMimeMessage(); + matcher = new HasHeader(); + MockMatcherConfig mci = new MockMatcherConfig("HasHeader=" + + HEADER_NAME, new MockMailContext()); + matcher.init(mci); + } + + // test if the Header was matched + public void testHeaderIsMatched() throws MessagingException { + setHeaderName(HEADER_NAME); + setHeaderValue(HEADER_VALUE); + setupMockedMail(); + setupMockedMimeMessage(); + setupMatcher(); + + assertTrue(matcher.match(mockedMail) != null); + } + + // test if the Header was matched + public void testHeaderIsNotMatched() throws MessagingException { + setupMockedMail(); + setupMockedMimeMessage(); + setupMatcher(); + + assertTrue(matcher.match(mockedMail) == null); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]