Author: berndf Date: Fri Jun 2 01:54:54 2006 New Revision: 411096 URL: http://svn.apache.org/viewvc?rev=411096&view=rev Log: moved copy/paste-code into common superclass.
Added: james/server/trunk/src/test/org/apache/james/transport/matchers/AbstractHasMailAttributeTest.java Modified: james/server/trunk/src/test/org/apache/james/transport/matchers/HasMailAttributeTest.java james/server/trunk/src/test/org/apache/james/transport/matchers/HasMailAttributeWithValueRegexTest.java james/server/trunk/src/test/org/apache/james/transport/matchers/HasMailAttributeWithValueTest.java Added: james/server/trunk/src/test/org/apache/james/transport/matchers/AbstractHasMailAttributeTest.java URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/transport/matchers/AbstractHasMailAttributeTest.java?rev=411096&view=auto ============================================================================== --- james/server/trunk/src/test/org/apache/james/transport/matchers/AbstractHasMailAttributeTest.java (added) +++ james/server/trunk/src/test/org/apache/james/transport/matchers/AbstractHasMailAttributeTest.java Fri Jun 2 01:54:54 2006 @@ -0,0 +1,129 @@ +/*********************************************************************** + * 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 junit.framework.TestCase; + +import javax.mail.MessagingException; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.ParseException; + +import org.apache.james.test.mock.javaxmail.MockMimeMessage; +import org.apache.james.test.mock.mailet.MockMail; +import org.apache.james.test.mock.mailet.MockMatcherConfig; +import org.apache.james.test.mock.mailet.MockMailContext; +import org.apache.mailet.MailAddress; +import org.apache.mailet.Matcher; + +import java.util.Arrays; +import java.util.Collection; +import java.io.Serializable; + +public abstract class AbstractHasMailAttributeTest extends TestCase { + protected MimeMessage mockedMimeMessage; + protected MockMail mockedMail; + protected Matcher matcher; + protected final String MAIL_ATTRIBUTE_NAME = "org.apache.james.test.junit"; + protected final String MAIL_ATTRIBUTE_VALUE = "true"; + protected String mailAttributeName = "org.apache.james"; + protected String mailAttributeValue = "false"; + + public AbstractHasMailAttributeTest() { + super(null); + } + + protected void setMailAttributeName(String mailAttributeName) { + this.mailAttributeName = mailAttributeName; + } + + protected void setMailAttributeValue(String mailAttributeValue) { + this.mailAttributeValue = mailAttributeValue; + } + + protected void setupMockedMimeMessage() throws MessagingException { + String sender = "[EMAIL PROTECTED]"; + String rcpt = "[EMAIL PROTECTED]"; + + mockedMimeMessage = new MockMimeMessage(); + mockedMimeMessage.setFrom(new InternetAddress(sender)); + mockedMimeMessage.setRecipients(MimeMessage.RecipientType.TO, rcpt); + mockedMimeMessage.setSubject("testmail"); + mockedMimeMessage.setText("testtext"); + mockedMimeMessage.saveChanges(); + + } + + protected void setupMockedMail(MimeMessage m) throws ParseException { + mockedMail = new MockMail(); + mockedMail.setMessage(m); + mockedMail.setRecipients(Arrays.asList(new MailAddress[] { + new MailAddress("[EMAIL PROTECTED]"), + new MailAddress("[EMAIL PROTECTED]") })); + mockedMail.setAttribute(mailAttributeName, + (Serializable) mailAttributeValue); + + } + + protected void setupMatcher() throws MessagingException { + setupMockedMimeMessage(); + matcher = createMatcher(); + MockMatcherConfig mci = new MockMatcherConfig("HasMailAttribute=" + + getHasMailAttribute(), + new MockMailContext()); + matcher.init(mci); + } + + // test if the mail attribute was matched + public void testAttributeIsMatched() throws MessagingException { + init(); + + setupAll(); + + Collection matchedRecipients = matcher.match(mockedMail); + + assertNotNull(matchedRecipients); + assertEquals(matchedRecipients.size(), mockedMail.getRecipients() + .size()); + } + + protected void init() { + setMailAttributeName(MAIL_ATTRIBUTE_NAME); + setMailAttributeValue(MAIL_ATTRIBUTE_VALUE); + } + + protected void setupAll() throws MessagingException { + setupMockedMimeMessage(); + setupMockedMail(mockedMimeMessage); + setupMatcher(); + } + + // test if the mail attribute was not matched + public void testAttributeIsNotMatched() throws MessagingException { + setupAll(); + + Collection matchedRecipients = matcher.match(mockedMail); + + assertNull(matchedRecipients); + } + + protected abstract String getHasMailAttribute(); + + protected abstract Matcher createMatcher(); +} Modified: james/server/trunk/src/test/org/apache/james/transport/matchers/HasMailAttributeTest.java URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/transport/matchers/HasMailAttributeTest.java?rev=411096&r1=411095&r2=411096&view=diff ============================================================================== --- james/server/trunk/src/test/org/apache/james/transport/matchers/HasMailAttributeTest.java (original) +++ james/server/trunk/src/test/org/apache/james/transport/matchers/HasMailAttributeTest.java Fri Jun 2 01:54:54 2006 @@ -17,112 +17,32 @@ package org.apache.james.transport.matchers; -import org.apache.james.test.mock.javaxmail.MockMimeMessage; -import org.apache.james.test.mock.mailet.MockMail; import org.apache.james.test.mock.mailet.MockMailContext; import org.apache.james.test.mock.mailet.MockMatcherConfig; - -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.ParseException; -import javax.mail.internet.MimeMessage.RecipientType; - -import java.io.Serializable; -import java.io.UnsupportedEncodingException; -import java.util.Arrays; -import java.util.Collection; - -import junit.framework.TestCase; - -public class HasMailAttributeTest extends TestCase { - - private MimeMessage mockedMimeMessage; - - private MockMail mockedMail; - - private Matcher matcher; - - private final String MAIL_ATTRIBUTE_NAME = "org.apache.james.test.junit"; - - private final String MAIL_ATTRIBUTE_VALUE = "true"; - - private String mailAttributeName = "org.apache.james"; - - private String mailAttributeValue = "false"; - - public HasMailAttributeTest(String arg0) - throws UnsupportedEncodingException { - super(arg0); - } - - private void setMailAttributeName(String mailAttributeName) { - this.mailAttributeName = mailAttributeName; - } - - private void setMailAttributeValue(String mailAttributeValue) { - this.mailAttributeValue = mailAttributeValue; - } - 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.setSubject("testmail"); - mockedMimeMessage.setText("testtext"); - mockedMimeMessage.saveChanges(); +public class HasMailAttributeTest extends AbstractHasMailAttributeTest { + public HasMailAttributeTest() { + super(); } - private void setupMockedMail(MimeMessage m) throws ParseException { - mockedMail = new MockMail(); - mockedMail.setMessage(m); - mockedMail.setRecipients(Arrays.asList(new MailAddress[] { - new MailAddress("[EMAIL PROTECTED]"), - new MailAddress("[EMAIL PROTECTED]") })); - mockedMail.setAttribute(mailAttributeName, - (Serializable) mailAttributeValue); - - } - - private void setupMatcher() throws MessagingException { + protected void setupMatcher() throws MessagingException { setupMockedMimeMessage(); - matcher = new HasMailAttribute(); + matcher = createMatcher(); MockMatcherConfig mci = new MockMatcherConfig("HasMailAttribute=" - + MAIL_ATTRIBUTE_NAME, new MockMailContext()); + + getHasMailAttribute(), new MockMailContext()); matcher.init(mci); } - // test if the mail attribute was matched - public void testAttributeIsMatched() throws MessagingException { - setMailAttributeName(MAIL_ATTRIBUTE_NAME); - setMailAttributeValue(MAIL_ATTRIBUTE_VALUE); - - setupMockedMimeMessage(); - setupMockedMail(mockedMimeMessage); - setupMatcher(); - - Collection matchedRecipients = matcher.match(mockedMail); - - assertNotNull(matchedRecipients); - assertEquals(matchedRecipients.size(), mockedMail.getRecipients() - .size()); + protected Matcher createMatcher() { + return new HasMailAttribute(); } - // test if the mail attribute was not matched - public void testAttributeIsNotMatched() throws MessagingException { - setupMockedMimeMessage(); - setupMockedMail(mockedMimeMessage); - setupMatcher(); - - Collection matchedRecipients = matcher.match(mockedMail); - - assertNull(matchedRecipients); + protected String getHasMailAttribute() { + return MAIL_ATTRIBUTE_NAME; } + } Modified: james/server/trunk/src/test/org/apache/james/transport/matchers/HasMailAttributeWithValueRegexTest.java URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/transport/matchers/HasMailAttributeWithValueRegexTest.java?rev=411096&r1=411095&r2=411096&view=diff ============================================================================== --- james/server/trunk/src/test/org/apache/james/transport/matchers/HasMailAttributeWithValueRegexTest.java (original) +++ james/server/trunk/src/test/org/apache/james/transport/matchers/HasMailAttributeWithValueRegexTest.java Fri Jun 2 01:54:54 2006 @@ -17,103 +17,36 @@ package org.apache.james.transport.matchers; -import org.apache.james.test.mock.javaxmail.MockMimeMessage; -import org.apache.james.test.mock.mailet.MockMail; -import org.apache.james.test.mock.mailet.MockMailContext; -import org.apache.james.test.mock.mailet.MockMatcherConfig; - -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.ParseException; -import javax.mail.internet.MimeMessage.RecipientType; - -import java.io.Serializable; -import java.io.UnsupportedEncodingException; -import java.util.Arrays; import java.util.Collection; -import junit.framework.TestCase; - -public class HasMailAttributeWithValueRegexTest extends TestCase { - - private MimeMessage mockedMimeMessage; - - private MockMail mockedMail; - - private Matcher matcher; - - private final String MAIL_ATTRIBUTE_NAME = "org.apache.james.test.junit"; - - private final String MAIL_ATTRIBUTE_VALUE = "true"; - - private String mailAttributeName = "org.apache.james"; - - private String mailAttributeValue = "false"; +public class HasMailAttributeWithValueRegexTest extends AbstractHasMailAttributeTest { private String regex = ".*"; - public HasMailAttributeWithValueRegexTest(String arg0) - throws UnsupportedEncodingException { - super(arg0); - } - - private void setMailAttributeName(String mailAttributeName) { - this.mailAttributeName = mailAttributeName; - } - - private void setMailAttributeValue(String mailAttributeValue) { - this.mailAttributeValue = mailAttributeValue; + public HasMailAttributeWithValueRegexTest() { + super(); } private void setRegex(String regex) { this.regex = regex; } - 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.setSubject("testmail"); - mockedMimeMessage.setText("testtext"); - mockedMimeMessage.saveChanges(); - + protected String getHasMailAttribute() { + return MAIL_ATTRIBUTE_NAME + ", " + regex; } - private void setupMockedMail(MimeMessage m) throws ParseException { - mockedMail = new MockMail(); - mockedMail.setMessage(m); - mockedMail.setRecipients(Arrays.asList(new MailAddress[] { - new MailAddress("[EMAIL PROTECTED]"), - new MailAddress("[EMAIL PROTECTED]") })); - mockedMail.setAttribute(mailAttributeName, - (Serializable) mailAttributeValue); - - } - - private void setupMatcher() throws MessagingException { - setupMockedMimeMessage(); - matcher = new HasMailAttributeWithValueRegex(); - MockMatcherConfig mci = new MockMatcherConfig("HasMailAttribute=" - + MAIL_ATTRIBUTE_NAME + ", " + regex, new MockMailContext()); - matcher.init(mci); + protected Matcher createMatcher() { + return new HasMailAttributeWithValueRegex(); } - // test if the mail attribute was matched +// test if the mail attribute was matched public void testAttributeIsMatched() throws MessagingException { - setMailAttributeName(MAIL_ATTRIBUTE_NAME); - setMailAttributeValue(MAIL_ATTRIBUTE_VALUE); + init(); setRegex(".*"); - - setupMockedMimeMessage(); - setupMockedMail(mockedMimeMessage); - setupMatcher(); + setupAll(); Collection matchedRecipients = matcher.match(mockedMail); @@ -125,9 +58,7 @@ // test if the mail attribute was not matched public void testHeaderIsNotMatched() throws MessagingException { setRegex("\\d"); - setupMockedMimeMessage(); - setupMockedMail(mockedMimeMessage); - setupMatcher(); + setupAll(); Collection matchedRecipients = matcher.match(mockedMail); Modified: james/server/trunk/src/test/org/apache/james/transport/matchers/HasMailAttributeWithValueTest.java URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/transport/matchers/HasMailAttributeWithValueTest.java?rev=411096&r1=411095&r2=411096&view=diff ============================================================================== --- james/server/trunk/src/test/org/apache/james/transport/matchers/HasMailAttributeWithValueTest.java (original) +++ james/server/trunk/src/test/org/apache/james/transport/matchers/HasMailAttributeWithValueTest.java Fri Jun 2 01:54:54 2006 @@ -17,117 +17,27 @@ package org.apache.james.transport.matchers; -import org.apache.james.test.mock.javaxmail.MockMimeMessage; -import org.apache.james.test.mock.mailet.MockMail; -import org.apache.james.test.mock.mailet.MockMailContext; -import org.apache.james.test.mock.mailet.MockMatcherConfig; - -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.ParseException; -import javax.mail.internet.MimeMessage.RecipientType; - -import java.io.Serializable; -import java.io.UnsupportedEncodingException; -import java.util.Arrays; import java.util.Collection; -import junit.framework.TestCase; - -public class HasMailAttributeWithValueTest extends TestCase { - - private MimeMessage mockedMimeMessage; - - private MockMail mockedMail; - - private Matcher matcher; - - private final String MAIL_ATTRIBUTE_NAME = "org.apache.james.test.junit"; +public class HasMailAttributeWithValueTest extends AbstractHasMailAttributeTest { - private final String MAIL_ATTRIBUTE_VALUE = "true"; - - private String mailAttributeName = "org.apache.james"; - - private String mailAttributeValue = "false"; - - public HasMailAttributeWithValueTest(String arg0) - throws UnsupportedEncodingException { - super(arg0); + public HasMailAttributeWithValueTest() { + super(); } - private void setMailAttributeName(String mailAttributeName) { - this.mailAttributeName = mailAttributeName; + protected String getHasMailAttribute() { + return MAIL_ATTRIBUTE_NAME + ", " + MAIL_ATTRIBUTE_VALUE; } - private void setMailAttributeValue(String mailAttributeValue) { - this.mailAttributeValue = mailAttributeValue; - } - - 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.setSubject("testmail"); - mockedMimeMessage.setText("testtext"); - mockedMimeMessage.saveChanges(); - - } - - private void setupMockedMail(MimeMessage m) throws ParseException { - mockedMail = new MockMail(); - mockedMail.setMessage(m); - mockedMail.setRecipients(Arrays.asList(new MailAddress[] { - new MailAddress("[EMAIL PROTECTED]"), new MailAddress("[EMAIL PROTECTED]") })); - mockedMail.setAttribute(mailAttributeName, - (Serializable) mailAttributeValue); - - } - - private void setupMatcher() throws MessagingException { - setupMockedMimeMessage(); - matcher = new HasMailAttributeWithValue(); - MockMatcherConfig mci = new MockMatcherConfig("HasMailAttribute=" - + MAIL_ATTRIBUTE_NAME + ", " + MAIL_ATTRIBUTE_VALUE, - new MockMailContext()); - matcher.init(mci); - } - - // test if the mail attribute was matched - public void testHeaderIsMatched() throws MessagingException { - setMailAttributeName(MAIL_ATTRIBUTE_NAME); - setMailAttributeValue(MAIL_ATTRIBUTE_VALUE); - - setupMockedMimeMessage(); - setupMockedMail(mockedMimeMessage); - setupMatcher(); - - Collection matchedRecipients = matcher.match(mockedMail); - - assertNotNull(matchedRecipients); - assertEquals(matchedRecipients.size(), mockedMail.getRecipients() - .size()); - } - - // test if the mail attribute was not matched - public void testHeaderIsNotMatched() throws MessagingException { - setupMockedMimeMessage(); - setupMockedMail(mockedMimeMessage); - setupMatcher(); - - Collection matchedRecipients = matcher.match(mockedMail); - - assertNull(matchedRecipients); + protected Matcher createMatcher() { + return new HasMailAttributeWithValue(); } // test if the mail attribute was not matched cause diffrent value - public void testHeaderIsNotMatchedCauseValue() throws MessagingException { + public void testAttributeIsNotMatchedCauseValue() throws MessagingException { setMailAttributeName(MAIL_ATTRIBUTE_NAME); setupMockedMimeMessage(); setupMockedMail(mockedMimeMessage); --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]