/**
 * 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.component.mina;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import junit.framework.TestCase;

import org.apache.camel.CamelContext;
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.impl.DefaultCamelContext;

/**
 * @version $Revision$
 */
public class MinaVmTest extends TestCase {
    protected CamelContext container = new DefaultCamelContext();
    protected CountDownLatch latch = new CountDownLatch(1);
    protected Exchange receivedExchange;
    protected String uri = "mina:vm://localhost:8080";
    protected Producer<MinaExchange> producer;

    public void testMinaRoute() throws Exception {

        // now lets fire in a message
        Endpoint<MinaExchange> endpoint = container.getEndpoint(uri);
        MinaExchange exchange = endpoint.createExchange();
        Message message = exchange.getIn();
        message.setBody("Hello there!");
        message.setHeader("cheese", 123);

        producer = endpoint.createProducer();
        producer.start();
        producer.process(exchange);

        // now lets sleep for a while
        boolean received = latch.await(5, TimeUnit.SECONDS);
        assertTrue("Did not receive the message!", received);
    }

    @Override
    protected void setUp() throws Exception {
        container.addRoutes(createRouteBuilder());
        container.start();
    }


    @Override
    protected void tearDown() throws Exception {
        if (producer != null) {
            producer.stop();
        }
        container.stop();
    }

    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {
                from(uri).process(new Processor() {
                    public void process(Exchange e) {
                        System.out.println("Received exchange: " + e.getIn());
                        receivedExchange = e;
                        latch.countDown();
                    }
                });
            }
        };
    }
}

@@ -843,125 +843,8 @@
 a;%0A%0A
-import java.util.concurrent.CountDownLatch;%0Aimport java.util.concurrent.TimeUnit;%0A%0Aimport junit.framework.TestCase;%0A%0A
 impo
@@ -867,190 +867,26 @@
 mel.
-Camel
 Context
-;%0Aimport org.apache.camel.Endpoint;%0Aimport org.apache.camel.Exchange;%0Aimport org.apache.camel.Message;%0Aimport org.apache.camel.Processor;%0Aimport org.apache.camel.Producer
+TestSupport
 ;%0Aim
@@ -957,31 +957,34 @@
 mel.
-impl.DefaultCamelContex
+component.mock.MockEndpoin
 t;%0A%0A
@@ -1050,185 +1050,28 @@
 nds 
-TestCase %7B%0A    protected CamelContext container = new DefaultCamelContext();%0A    protected CountDownLatch latch = new CountDownLatch(1);%0A    protected Exchange receivedExchange;
+ContextTestSupport %7B
 %0A   
@@ -1125,55 +1125,8 @@
 80%22;
-%0A    protected Producer%3CMinaExchange%3E producer;
 %0A%0A  
@@ -1174,25 +1174,24 @@
 ption %7B%0A
-%0A
         
 // now l
@@ -1186,235 +1186,86 @@
     
-// now lets fire in a message%0A        Endpoint%3CMinaExchange%3E endpoint = container.getEndpoint(uri);%0A        MinaExchange exchange = endpoint.createExchange();%0A        Message message = exchange.getIn();%0A        message.setBody(
+MockEndpoint endpoint = getMockEndpoint(%22mock:result%22);%0A        Object body = 
 %22Hel
@@ -1274,17 +1274,16 @@
  there!%22
-)
 ;%0A      
@@ -1288,1070 +1288,324 @@
     
-message.setHeader(%22cheese%22, 123);%0A%0A        producer = endpoint.createProducer();%0A        producer.start();%0A        producer.process(exchange);%0A%0A        // now lets sleep for a while%0A        boolean received = latch.await(5, TimeUnit.SECONDS);%0A        assertTrue(%22Did not receive the message!%22, received);%0A    %7D%0A%0A    @Override%0A    protected void setUp() throws Exception %7B%0A        container.addRoutes(createRouteBuilder());%0A        container.start();%0A    %7D%0A%0A%0A    @Override%0A    protected void tearDown() throws Exception %7B%0A        if (producer != null) %7B%0A            producer.stop();%0A        %7D%0A        container.stop();%0A    %7D%0A%0A    protected RouteBuilder createRouteBuilder() %7B%0A        return new RouteBuilder() %7B%0A            public void configure() %7B%0A                from(uri).process(new Processor() %7B%0A                    public void process(Exchange e) %7B%0A                        System.out.println(%22Received exchange: %22 + e.getIn());%0A                        receivedExchange = e;%0A                        latch.countDown();%0A                    %7D%0A                %7D
+endpoint.expectedBodiesReceived(body);%0A%0A        template.sendBodyAndHeader(uri, body, %22cheese%22, 123);%0A%0A        assertMockEndpointsSatisifed();%0A    %7D%0A%0A    protected RouteBuilder createRouteBuilder() %7B%0A        return new RouteBuilder() %7B%0A            public void configure() %7B%0A                from(uri).to(%22mock:result%22
 );%0A 

refactored the test case to be easier to read