Author: norman
Date: Sat Jan 8 15:09:37 2011
New Revision: 1056722
URL: http://svn.apache.org/viewvc?rev=1056722&view=rev
Log:
Add tests for exception handling in AbstractStateMailetProcessor and fix a
possible NPE
Added:
james/server/trunk/mailetcontainer-api/src/test/java/org/apache/james/mailetcontainer/api/mock/ExceptionThrowingMailet.java
james/server/trunk/mailetcontainer-api/src/test/java/org/apache/james/mailetcontainer/api/mock/ExceptionThrowingMatcher.java
Modified:
james/server/trunk/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/camel/MatcherSplitter.java
james/server/trunk/mailetcontainer-library/src/test/java/org/apache/james/mailetcontainer/lib/AbstractStateMailetProcessorTest.java
Added:
james/server/trunk/mailetcontainer-api/src/test/java/org/apache/james/mailetcontainer/api/mock/ExceptionThrowingMailet.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/mailetcontainer-api/src/test/java/org/apache/james/mailetcontainer/api/mock/ExceptionThrowingMailet.java?rev=1056722&view=auto
==============================================================================
---
james/server/trunk/mailetcontainer-api/src/test/java/org/apache/james/mailetcontainer/api/mock/ExceptionThrowingMailet.java
(added)
+++
james/server/trunk/mailetcontainer-api/src/test/java/org/apache/james/mailetcontainer/api/mock/ExceptionThrowingMailet.java
Sat Jan 8 15:09:37 2011
@@ -0,0 +1,53 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you 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.mailetcontainer.api.mock;
+
+import javax.mail.MessagingException;
+
+import org.apache.mailet.Mail;
+import org.apache.mailet.Mailet;
+import org.apache.mailet.MailetConfig;
+
+public class ExceptionThrowingMailet implements Mailet{
+
+ private MailetConfig config;
+
+ public void destroy() {
+
+
+ }
+
+ public MailetConfig getMailetConfig() {
+ return config;
+ }
+
+ public String getMailetInfo() {
+ return getClass().getName();
+ }
+
+ public void init(MailetConfig config) throws MessagingException {
+ this.config = config;
+ }
+
+ public void service(Mail arg0) throws MessagingException {
+ throw new MessagingException();
+ }
+
+}
Added:
james/server/trunk/mailetcontainer-api/src/test/java/org/apache/james/mailetcontainer/api/mock/ExceptionThrowingMatcher.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/mailetcontainer-api/src/test/java/org/apache/james/mailetcontainer/api/mock/ExceptionThrowingMatcher.java?rev=1056722&view=auto
==============================================================================
---
james/server/trunk/mailetcontainer-api/src/test/java/org/apache/james/mailetcontainer/api/mock/ExceptionThrowingMatcher.java
(added)
+++
james/server/trunk/mailetcontainer-api/src/test/java/org/apache/james/mailetcontainer/api/mock/ExceptionThrowingMatcher.java
Sat Jan 8 15:09:37 2011
@@ -0,0 +1,53 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you 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.mailetcontainer.api.mock;
+
+import java.util.Collection;
+
+import javax.mail.MessagingException;
+
+import org.apache.mailet.Mail;
+import org.apache.mailet.Matcher;
+import org.apache.mailet.MatcherConfig;
+
+public class ExceptionThrowingMatcher implements Matcher{
+
+ private MatcherConfig config;
+
+ public void destroy() {
+ }
+
+ public MatcherConfig getMatcherConfig() {
+ return config;
+ }
+
+ public String getMatcherInfo() {
+ return getClass().getName();
+ }
+
+ public void init(MatcherConfig config) throws MessagingException {
+ this.config = config;
+ }
+
+ public Collection match(Mail arg0) throws MessagingException {
+ throw new MessagingException();
+ }
+
+}
Modified:
james/server/trunk/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/camel/MatcherSplitter.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/camel/MatcherSplitter.java?rev=1056722&r1=1056721&r2=1056722&view=diff
==============================================================================
---
james/server/trunk/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/camel/MatcherSplitter.java
(original)
+++
james/server/trunk/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/camel/MatcherSplitter.java
Sat Jan 8 15:09:37 2011
@@ -166,7 +166,8 @@ public class MatcherSplitter {
List<MailetProcessorListener> listeners = container.getListeners();
for (int i = 0; i < listeners.size(); i++) {
MailetProcessorListener listener = listeners.get(i);
- if (matchedRcpts.isEmpty()) {
+ // need to check if its null or empty!
+ if (matchedRcpts == null || matchedRcpts.isEmpty()) {
listener.afterMatcher(matcher, mail.getName(), origRcpts,
null, complete, ex);
} else {
listener.afterMatcher(matcher, mail.getName(), origRcpts,
matchedRcpts, complete, ex);
Modified:
james/server/trunk/mailetcontainer-library/src/test/java/org/apache/james/mailetcontainer/lib/AbstractStateMailetProcessorTest.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/mailetcontainer-library/src/test/java/org/apache/james/mailetcontainer/lib/AbstractStateMailetProcessorTest.java?rev=1056722&r1=1056721&r2=1056722&view=diff
==============================================================================
---
james/server/trunk/mailetcontainer-library/src/test/java/org/apache/james/mailetcontainer/lib/AbstractStateMailetProcessorTest.java
(original)
+++
james/server/trunk/mailetcontainer-library/src/test/java/org/apache/james/mailetcontainer/lib/AbstractStateMailetProcessorTest.java
Sat Jan 8 15:09:37 2011
@@ -30,6 +30,8 @@ import org.apache.commons.configuration.
import org.apache.commons.configuration.DefaultConfigurationBuilder;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.james.core.MailImpl;
+import org.apache.james.mailetcontainer.api.mock.ExceptionThrowingMailet;
+import org.apache.james.mailetcontainer.api.mock.ExceptionThrowingMatcher;
import org.apache.james.mailetcontainer.api.mock.MockMailet;
import org.apache.james.mailetcontainer.api.mock.MockMatcher;
import
org.apache.james.mailetcontainer.lib.AbstractStateMailetProcessor.MailetProcessorListener;
@@ -46,10 +48,10 @@ public abstract class AbstractStateMaile
- private HierarchicalConfiguration createConfig(int count) throws
ConfigurationException {
+ private HierarchicalConfiguration createConfig(Class<?> matcherClass,
Class<?> mailetClass, int count) throws ConfigurationException {
StringBuilder sb = new StringBuilder();
sb.append("<processor state=\"" + Mail.DEFAULT + "\">");
- sb.append("<mailet
match=\"").append(MockMatcher.class.getName()).append("=").append(count).append("\"").append("
class=\"").append(MockMailet.class.getName()).append("\">");
+ sb.append("<mailet
match=\"").append(matcherClass.getName()).append("=").append(count).append("\"").append("
class=\"").append(mailetClass.getName()).append("\">");
sb.append("<state>test</state>");
sb.append("</mailet>");
@@ -60,6 +62,7 @@ public abstract class AbstractStateMaile
return builder;
}
+
public void testSimpleRouting() throws ConfigurationException, Exception {
final CountDownLatch latch = new CountDownLatch(2);
final MailImpl mail = new MailImpl();
@@ -67,7 +70,7 @@ public abstract class AbstractStateMaile
mail.setSender(new MailAddress("t...@localhost"));
mail.setRecipients(Arrays.asList(new MailAddress("t...@localhost"),
new MailAddress("te...@localhost")));
- AbstractStateMailetProcessor processor =
createProcessor(createConfig(1));
+ AbstractStateMailetProcessor processor =
createProcessor(createConfig(MockMatcher.class, MockMailet.class, 1));
processor.addListener(new MailetProcessorListener() {
public void afterMatcher(Matcher m, String mailName,
Collection<MailAddress> recipients, Collection<MailAddress> matches, long
processTime, MessagingException e) {
@@ -111,13 +114,13 @@ public abstract class AbstractStateMaile
mail.setSender(new MailAddress("t...@localhost"));
mail.setRecipients(Arrays.asList(new MailAddress("t...@localhost"),
new MailAddress("te...@localhost")));
- AbstractStateMailetProcessor processor =
createProcessor(createConfig(2));
+ AbstractStateMailetProcessor processor =
createProcessor(createConfig(MockMatcher.class, MockMailet.class, 2));
processor.addListener(new MailetProcessorListener() {
public void afterMatcher(Matcher m, String mailName,
Collection<MailAddress> recipients, Collection<MailAddress> matches, long
processTime, MessagingException e) {
if (MockMatcher.class.equals(m.getClass())) {
assertEquals(mail.getName(), mailName);
- // match one recipient
+ // match all recipient
assertEquals(2, matches.size());
assertNull(e);
latch.countDown();
@@ -129,7 +132,8 @@ public abstract class AbstractStateMaile
// check for class name as the terminating mailet will kick
in too
if (MockMailet.class.equals(m.getClass())) {
- //assertEquals(mail.getName(), mailName);
+ // the name should be the same as we have a full match
+ assertEquals(mail.getName(), mailName);
assertEquals("test", state);
assertNull(e);
latch.countDown();
@@ -147,4 +151,95 @@ public abstract class AbstractStateMaile
processor.destroy();
}
+
+
+ public void testMatcherThrowException() throws ConfigurationException,
Exception {
+ final CountDownLatch latch = new CountDownLatch(1);
+ final MailImpl mail = new MailImpl();
+ mail.setName(MailImpl.getId());
+ mail.setSender(new MailAddress("t...@localhost"));
+ mail.setRecipients(Arrays.asList(new MailAddress("t...@localhost"),
new MailAddress("te...@localhost")));
+
+ AbstractStateMailetProcessor processor =
createProcessor(createConfig(ExceptionThrowingMatcher.class, MockMailet.class,
0));
+ processor.addListener(new MailetProcessorListener() {
+
+ public void afterMatcher(Matcher m, String mailName,
Collection<MailAddress> recipients, Collection<MailAddress> matches, long
processTime, MessagingException e) {
+ if (ExceptionThrowingMatcher.class.equals(m.getClass())) {
+ assertEquals(mail.getName(), mailName);
+ // match no recipient because of the error
+ assertNull(matches);
+ assertNotNull(e);
+ latch.countDown();
+ }
+
+ }
+
+ public void afterMailet(Mailet m, String mailName, String state,
long processTime, MessagingException e) {
+ throw new RuntimeException("Should not call any mailet!");
+ }
+ });
+
+ assertEquals(Mail.DEFAULT, mail.getState());
+
+ boolean catched = false;
+ try {
+ processor.service(mail);
+ } catch (MessagingException e) {
+ catched = true;
+ }
+ assertTrue(catched);
+
+ // the source mail should have state error as the exception was thrown
+ assertEquals(Mail.ERROR, mail.getState());
+ latch.await();
+ processor.destroy();
+
+ }
+
+ public void testMailetThrowException() throws ConfigurationException,
Exception {
+ final CountDownLatch latch = new CountDownLatch(2);
+ final MailImpl mail = new MailImpl();
+ mail.setName(MailImpl.getId());
+ mail.setSender(new MailAddress("t...@localhost"));
+ mail.setRecipients(Arrays.asList(new MailAddress("t...@localhost"),
new MailAddress("te...@localhost")));
+
+ AbstractStateMailetProcessor processor =
createProcessor(createConfig(MockMatcher.class, ExceptionThrowingMailet.class,
1));
+ processor.addListener(new MailetProcessorListener() {
+
+ public void afterMatcher(Matcher m, String mailName,
Collection<MailAddress> recipients, Collection<MailAddress> matches, long
processTime, MessagingException e) {
+ if (MockMatcher.class.equals(m.getClass())) {
+ assertEquals(mail.getName(), mailName);
+ // match one recipient
+ assertEquals(1, matches.size());
+ assertNull(e);
+ latch.countDown();
+ }
+
+ }
+
+ public void afterMailet(Mailet m, String mailName, String state,
long processTime, MessagingException e) {
+ if (ExceptionThrowingMailet.class.equals(m.getClass())) {
+ // the name should be not the same as we have a part match
+ assertFalse(mail.getName().equals(mailName));
+ assertNotNull(e);
+ assertEquals(Mail.ERROR, state);
+ latch.countDown();
+ }
+ }
+ });
+
+ assertEquals(Mail.DEFAULT, mail.getState());
+
+ boolean catched = false;
+ try {
+ processor.service(mail);
+ } catch (MessagingException e) {
+ catched = true;
+ }
+ assertTrue(catched);
+
+ latch.await();
+ processor.destroy();
+
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]