Author: davsclaus
Date: Wed Apr 11 09:46:21 2012
New Revision: 1324675
URL: http://svn.apache.org/viewvc?rev=1324675&view=rev
Log:
CAMEL-5157: Fixed camel-mail issue with attachments may not appear when doing
redelivery. Fixed by ensuring MailMessage always populate attachments, to
preserve them when error handler perfroms defensive copy of the MailMessage.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/issues/MessageWithAttachmentRedeliveryIssueTest.java
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentRedeliveryTest.java
- copied, changed from r1324642,
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentTest.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java?rev=1324675&r1=1324674&r2=1324675&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
Wed Apr 11 09:46:21 2012
@@ -281,6 +281,9 @@ public class DefaultMessage extends Mess
}
public boolean hasAttachments() {
+ // optimized to avoid calling createAttachments as that creates a new
empty map
+ // that we 99% do not need (only camel-mail supports attachments), and
we have
+ // then ensure camel-mail always creates attachments to remedy for this
return this.attachments != null && this.attachments.size() > 0;
}
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/issues/MessageWithAttachmentRedeliveryIssueTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/issues/MessageWithAttachmentRedeliveryIssueTest.java?rev=1324675&view=auto
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/issues/MessageWithAttachmentRedeliveryIssueTest.java
(added)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/issues/MessageWithAttachmentRedeliveryIssueTest.java
Wed Apr 11 09:46:21 2012
@@ -0,0 +1,76 @@
+/**
+ * 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.camel.issues;
+
+import java.io.File;
+import javax.activation.DataHandler;
+import javax.activation.FileDataSource;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ *
+ */
+public class MessageWithAttachmentRedeliveryIssueTest extends
ContextTestSupport {
+
+ public void testMessageWithAttachmentRedeliveryIssue() throws Exception {
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+
+ template.send("direct:start", new Processor() {
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ exchange.getIn().setBody("Hello World");
+ exchange.getIn().addAttachment("message1.xml", new
DataHandler(new FileDataSource(new File("src/test/data/message1.xml"))));
+ exchange.getIn().addAttachment("message2.xml", new
DataHandler(new FileDataSource(new File("src/test/data/message2.xml"))));
+ }
+ });
+
+ assertMockEndpointsSatisfied();
+
+ Message msg =
getMockEndpoint("mock:result").getReceivedExchanges().get(0).getIn();
+ assertNotNull(msg);
+
+ assertEquals("Hello World", msg.getBody());
+ assertTrue(msg.hasAttachments());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+
onException(Exception.class).maximumRedeliveries(3).redeliveryDelay(0);
+
+ from("direct:start")
+ .process(new Processor() {
+ private int counter;
+ @Override
+ public void process(Exchange exchange) throws
Exception {
+ if (counter++ < 2) {
+ throw new IllegalArgumentException("Forced");
+ }
+ }
+ }).to("mock:result");
+
+ }
+ };
+ }
+}
Modified:
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java?rev=1324675&r1=1324674&r2=1324675&view=diff
==============================================================================
---
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java
(original)
+++
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java
Wed Apr 11 09:46:21 2012
@@ -56,6 +56,10 @@ public class MailMessage extends Default
MailMessage answer = (MailMessage)super.copy();
answer.originalMailMessage = originalMailMessage;
answer.mailMessage = mailMessage;
+ // force attachments to be created (by getting attachments) to ensure
they are always available due Camel error handler
+ // makes defensive copies, and we have optimized it to avoid
populating initial attachments, when not needed,
+ // as all other Camel components do not use attachments
+ getAttachments();
return answer;
}
Copied:
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentRedeliveryTest.java
(from r1324642,
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentTest.java)
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentRedeliveryTest.java?p2=camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentRedeliveryTest.java&p1=camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentTest.java&r1=1324642&r2=1324675&rev=1324675&view=diff
==============================================================================
---
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentTest.java
(original)
+++
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentRedeliveryTest.java
Wed Apr 11 09:46:21 2012
@@ -16,14 +16,16 @@
*/
package org.apache.camel.component.mail;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Map;
-
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
+import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
@@ -34,15 +36,15 @@ import org.jvnet.mock_javamail.Mailbox;
/**
* Unit test for Camel attachments and Mail attachments.
*/
-public class MailAttachmentTest extends CamelTestSupport {
+public class MailAttachmentRedeliveryTest extends CamelTestSupport {
+
+ private final List names = new ArrayList();
@Test
- public void testSendAndReceiveMailWithAttachments() throws Exception {
+ public void testSendAndReceiveMailWithAttachmentsRedelivery() throws
Exception {
// clear mailbox
Mailbox.clearAll();
- // START SNIPPET: e1
-
// create an exchange with a normal body and attachment to be produced
as email
Endpoint endpoint =
context.getEndpoint("smtp://[email protected]?password=secret");
@@ -59,8 +61,6 @@ public class MailAttachmentTest extends
// and let it go (processes the exchange by sending the email)
producer.process(exchange);
- // END SNIPPET: e1
-
// need some time for the mail to arrive on the inbox (consumed and
sent to the mock)
Thread.sleep(2000);
@@ -84,12 +84,35 @@ public class MailAttachmentTest extends
assertEquals("Handler name should be the file name", "logo.jpeg",
handler.getName());
producer.stop();
+
+ assertEquals(3, names.size());
+ assertEquals("logo.jpeg", names.get(0));
+ assertEquals("logo.jpeg", names.get(1));
+ assertEquals("logo.jpeg", names.get(2));
}
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
public void configure() throws Exception {
-
from("pop3://[email protected]?password=secret&consumer.delay=1000").to("mock:result");
+
onException(IllegalArgumentException.class).maximumRedeliveries(3).redeliveryDelay(0);
+
+
from("pop3://[email protected]?password=secret&consumer.delay=1000")
+ .process(new Processor() {
+ private int counter;
+ @Override
+ @SuppressWarnings("unchecked")
+ public void process(Exchange exchange) throws
Exception {
+ Map map = exchange.getIn().getAttachments();
+ assertNotNull(map);
+ assertEquals(1, map.size());
+ names.add(map.keySet().iterator().next());
+
+ if (counter++ < 2) {
+ throw new
IllegalArgumentException("Forced");
+ }
+
+ }
+ }).to("mock:result");
}
};
}