001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.component.test; 018 019 import java.util.ArrayList; 020 import java.util.List; 021 022 import org.apache.camel.Component; 023 import org.apache.camel.Endpoint; 024 import org.apache.camel.Exchange; 025 import org.apache.camel.Processor; 026 import org.apache.camel.Service; 027 import org.apache.camel.component.mock.MockEndpoint; 028 import org.apache.camel.util.EndpointHelper; 029 import org.apache.commons.logging.Log; 030 import org.apache.commons.logging.LogFactory; 031 032 /** 033 * A <a href="http://camel.apache.org/test.html">Test Endpoint</a> is a 034 * <a href="http://camel.apache.org/mock.html">Mock Endpoint</a> for testing but it will 035 * pull all messages from the nested endpoint and use those as expected message body assertions. 036 * 037 * @version $Revision: 747759 $ 038 */ 039 public class TestEndpoint extends MockEndpoint implements Service { 040 private static final transient Log LOG = LogFactory.getLog(TestEndpoint.class); 041 private final Endpoint expectedMessageEndpoint; 042 private long timeout = 2000L; 043 044 public TestEndpoint(String endpointUri, Component component, Endpoint expectedMessageEndpoint) { 045 super(endpointUri, component); 046 this.expectedMessageEndpoint = expectedMessageEndpoint; 047 } 048 049 public TestEndpoint(String endpointUri, Endpoint expectedMessageEndpoint) { 050 super(endpointUri); 051 this.expectedMessageEndpoint = expectedMessageEndpoint; 052 } 053 054 @SuppressWarnings("unchecked") 055 public void start() throws Exception { 056 if (LOG.isDebugEnabled()) { 057 LOG.debug("Consuming expected messages from: " + expectedMessageEndpoint); 058 } 059 final List expectedBodies = new ArrayList(); 060 EndpointHelper.pollEndpoint(expectedMessageEndpoint, new Processor() { 061 public void process(Exchange exchange) throws Exception { 062 Object body = getInBody(exchange); 063 expectedBodies.add(body); 064 } 065 }, timeout); 066 067 if (LOG.isDebugEnabled()) { 068 LOG.debug("Received: " + expectedBodies.size() + " expected message(s) from: " + expectedMessageEndpoint); 069 } 070 expectedBodiesReceived(expectedBodies); 071 } 072 073 public void stop() throws Exception { 074 } 075 076 /** 077 * This method allows us to convert or coerce the expected message body into some other type 078 */ 079 protected Object getInBody(Exchange exchange) { 080 return exchange.getIn().getBody(); 081 } 082 }