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.component.mock.MockEndpoint; 027 import org.apache.camel.component.seda.SedaConsumer; 028 import org.apache.camel.spi.UriEndpoint; 029 import org.apache.camel.spi.UriParam; 030 import org.apache.camel.util.EndpointHelper; 031 import org.slf4j.Logger; 032 import org.slf4j.LoggerFactory; 033 034 /** 035 * A <a href="http://camel.apache.org/test.html">Test Endpoint</a> is a 036 * <a href="http://camel.apache.org/mock.html">Mock Endpoint</a> for testing but it will 037 * pull all messages from the nested endpoint and use those as expected message body assertions. 038 * 039 * @version 040 */ 041 @UriEndpoint(scheme = "test") 042 public class TestEndpoint extends MockEndpoint { 043 private static final Logger LOG = LoggerFactory.getLogger(TestEndpoint.class); 044 private final Endpoint expectedMessageEndpoint; 045 @UriParam 046 private long timeout = 2000L; 047 048 public TestEndpoint(String endpointUri, Component component, Endpoint expectedMessageEndpoint) { 049 super(endpointUri, component); 050 this.expectedMessageEndpoint = expectedMessageEndpoint; 051 } 052 053 @Override 054 protected void doStart() throws Exception { 055 LOG.debug("Consuming expected messages from: {}", expectedMessageEndpoint); 056 057 final List<Object> expectedBodies = new ArrayList<Object>(); 058 EndpointHelper.pollEndpoint(expectedMessageEndpoint, new Processor() { 059 public void process(Exchange exchange) throws Exception { 060 Object body = getInBody(exchange); 061 LOG.trace("Received message body {}", body); 062 expectedBodies.add(body); 063 } 064 }, timeout); 065 066 LOG.debug("Received: {} expected message(s) from: {}", expectedBodies.size(), expectedMessageEndpoint); 067 expectedBodiesReceived(expectedBodies); 068 } 069 070 /** 071 * This method allows us to convert or coerce the expected message body into some other type 072 */ 073 protected Object getInBody(Exchange exchange) { 074 return exchange.getIn().getBody(); 075 } 076 077 public long getTimeout() { 078 return timeout; 079 } 080 081 public void setTimeout(long timeout) { 082 this.timeout = timeout; 083 } 084 }