package org.camelcookbook.routing.multicast;

import org.apache.camel.*;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class MulticastShallowCopyTest extends CamelTestSupport {

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new MulticastShallowCopyRouteBuilder();
    }

    @Produce(uri = "direct:in")
    protected ProducerTemplate template;

    @EndpointInject(uri = "mock:first")
    private MockEndpoint mockFirst;

    @EndpointInject(uri = "mock:second")
    private MockEndpoint mockSecond;

    @EndpointInject(uri = "mock:afterMulticast")
    private MockEndpoint afterMulticast;

    // TODO this is checking too much stuff, split into seperate tests to clarify
    @Test
    public void testMessageRoutedToMulticastEndpoints() throws InterruptedException {
        String messageBody = "Message to be multicast";
        mockFirst.setExpectedMessageCount(1);
        mockFirst.message(0).equals(messageBody);
        mockFirst.message(0).header("firstModifies").equals("apple");

        mockSecond.setExpectedMessageCount(1);
        mockSecond.message(0).equals(messageBody);
        mockSecond.message(0).header("secondModifies").equals("banana");
        mockSecond.message(0).header("firstModifies").isNull();

        afterMulticast.setExpectedMessageCount(1);
        afterMulticast.message(0).equals(messageBody);
        afterMulticast.message(0).header("modifiedBy").isNull();

        template.sendBody(messageBody);

        assertMockEndpointsSatisfied();

        // check that all of the mock endpoints were reached by the same thread
        String firstThreadName = getExchange(mockFirst).getIn().getHeader("threadName", String.class);
        String secondThreadName = getExchange(mockSecond).getIn().getHeader("threadName", String.class);
        assertEquals(firstThreadName, secondThreadName);

        // check that all of the messages participated in different transactions
        assertNotEquals(getExchange(afterMulticast).getUnitOfWork(), getExchange(mockFirst).getUnitOfWork());
        assertNotEquals(getExchange(afterMulticast).getUnitOfWork(), getExchange(mockSecond).getUnitOfWork());
    }

    private Exchange getExchange(MockEndpoint mock) {
        return mock.getExchanges().get(0);
    }

}

@@ -309,16 +309,90 @@
 port %7B%0A%0A
+    public static final String MESSAGE_BODY = %22Message to be multicast%22;%0A%0A
     @Ove
@@ -851,90 +851,8 @@
 t;%0A%0A
-    // TODO this is checking too much stuff, split into seperate tests to clarify%0A
     
@@ -947,64 +947,8 @@
 n %7B%0A
-        String messageBody = %22Message to be multicast%22;%0A
     
@@ -1021,35 +1021,36 @@
 e(0).equals(
-messageBody
+MESSAGE_BODY
 );%0A        m
@@ -1191,35 +1191,36 @@
 e(0).equals(
-messageBody
+MESSAGE_BODY
 );%0A        m
@@ -1440,27 +1440,28 @@
 .equals(
-messageBody
+MESSAGE_BODY
 );%0A     
@@ -1551,19 +1551,20 @@
 ody(
-messageBody
+MESSAGE_BODY
 );%0A%0A
@@ -1607,297 +1607,310 @@
 ();%0A
-%0A
     
+%7D%0A%0A
     
-// check that all of the mock endpoints were reached by the same thread%0A        String firstThreadName = getExchange(mockFirst).getIn().getHeader(%22threadName%22, String.class);%0A        String secondThreadName = getExchange(mockSecond).getIn().getHeader(%22threadName%22, String.class
+@Test%0A    public void testAllMessagesParticipateInDifferentTransactions() throws InterruptedException %7B%0A        afterMulticast.setExpectedMessageCount(1);%0A        mockFirst.setExpectedMessageCount(1);%0A        mockSecond.setExpectedMessageCount(1);%0A%0A        template.sendBody(MESSAGE_BODY
 );%0A
+%0A
     
@@ -1923,48 +1923,31 @@
 sert
-Equals(firstThreadName, secondThreadName
+MockEndpointsSatisfied(
 );%0A%0A
@@ -2251,24 +2251,699 @@
 ());%0A    %7D%0A%0A
+%0A    @Test%0A    public void testAllEndpointsReachedBySameThread() throws InterruptedException %7B%0A        afterMulticast.setExpectedMessageCount(1);%0A        mockFirst.setExpectedMessageCount(1);%0A        mockSecond.setExpectedMessageCount(1);%0A%0A        template.sendBody(MESSAGE_BODY);%0A%0A        assertMockEndpointsSatisfied();%0A%0A        // check that all of the mock endpoints were reached by the same thread%0A        String firstThreadName = getExchange(mockFirst).getIn().getHeader(%22threadName%22, String.class);%0A        String secondThreadName = getExchange(mockSecond).getIn().getHeader(%22threadName%22, String.class);%0A        assertEquals(firstThreadName, secondThreadName);%0A    %7D%0A%0A
     private 

Split out heavy test into separate smaller checks.