Return-Path: Delivered-To: apmail-activemq-camel-commits-archive@locus.apache.org Received: (qmail 19475 invoked from network); 1 Dec 2008 17:01:19 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 1 Dec 2008 17:01:19 -0000 Received: (qmail 1246 invoked by uid 500); 1 Dec 2008 17:01:31 -0000 Delivered-To: apmail-activemq-camel-commits-archive@activemq.apache.org Received: (qmail 1228 invoked by uid 500); 1 Dec 2008 17:01:31 -0000 Mailing-List: contact camel-commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: camel-dev@activemq.apache.org Delivered-To: mailing list camel-commits@activemq.apache.org Received: (qmail 1219 invoked by uid 99); 1 Dec 2008 17:01:31 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 01 Dec 2008 09:01:31 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 01 Dec 2008 17:00:10 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id EDD88238897B; Mon, 1 Dec 2008 09:00:57 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r722127 - in /activemq/camel/trunk/components/camel-test: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/camel/ src/main/java/org/apache/camel/test/ src/main/resources/ src/main/resour... Date: Mon, 01 Dec 2008 17:00:57 -0000 To: camel-commits@activemq.apache.org From: jstrachan@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20081201170057.EDD88238897B@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jstrachan Date: Mon Dec 1 09:00:56 2008 New Revision: 722127 URL: http://svn.apache.org/viewvc?rev=722127&view=rev Log: Added initial spike for CAMEL-1136 to add a reusable base class, CamelTestSupport for easy unit testing Added: activemq/camel/trunk/components/camel-test/ activemq/camel/trunk/components/camel-test/pom.xml (with props) activemq/camel/trunk/components/camel-test/src/ activemq/camel/trunk/components/camel-test/src/main/ activemq/camel/trunk/components/camel-test/src/main/java/ activemq/camel/trunk/components/camel-test/src/main/java/org/ activemq/camel/trunk/components/camel-test/src/main/java/org/apache/ activemq/camel/trunk/components/camel-test/src/main/java/org/apache/camel/ activemq/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/ activemq/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelTestSupport.java (with props) activemq/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/TestSupport.java (with props) activemq/camel/trunk/components/camel-test/src/main/resources/ activemq/camel/trunk/components/camel-test/src/main/resources/META-INF/ activemq/camel/trunk/components/camel-test/src/main/resources/META-INF/LICENSE.txt (with props) activemq/camel/trunk/components/camel-test/src/main/resources/META-INF/NOTICE.txt (with props) activemq/camel/trunk/components/camel-test/src/test/ activemq/camel/trunk/components/camel-test/src/test/java/ activemq/camel/trunk/components/camel-test/src/test/java/org/ activemq/camel/trunk/components/camel-test/src/test/java/org/apache/ activemq/camel/trunk/components/camel-test/src/test/java/org/apache/camel/ activemq/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/ activemq/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/ activemq/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java (with props) activemq/camel/trunk/components/camel-test/src/test/resources/ activemq/camel/trunk/components/camel-test/src/test/resources/jndi.properties (with props) activemq/camel/trunk/components/camel-test/src/test/resources/log4j.properties (with props) Added: activemq/camel/trunk/components/camel-test/pom.xml URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-test/pom.xml?rev=722127&view=auto ============================================================================== --- activemq/camel/trunk/components/camel-test/pom.xml (added) +++ activemq/camel/trunk/components/camel-test/pom.xml Mon Dec 1 09:00:56 2008 @@ -0,0 +1,69 @@ + + + + + + + 4.0.0 + + + org.apache.camel + camel-parent + 2.0-SNAPSHOT + + + camel-test + bundle + Camel :: Test + Camel Testing Library + + + org.apache.camel.test.* + + + + + + org.apache.camel + camel-core + + + org.apache.camel + camel-spring + + + org.hamcrest + hamcrest-all + + + junit + junit + + + commons-logging + commons-logging + + + log4j + log4j + + + + + Propchange: activemq/camel/trunk/components/camel-test/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Added: activemq/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelTestSupport.java URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelTestSupport.java?rev=722127&view=auto ============================================================================== --- activemq/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelTestSupport.java (added) +++ activemq/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelTestSupport.java Mon Dec 1 09:00:56 2008 @@ -0,0 +1,339 @@ +/** + * 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.test; + +import org.apache.camel.CamelContext; +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Expression; +import org.apache.camel.Message; +import org.apache.camel.Predicate; +import org.apache.camel.Processor; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.Service; +import org.apache.camel.spring.CamelBeanPostProcessor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.impl.JndiRegistry; +import org.apache.camel.management.JmxSystemPropertyKeys; +import org.apache.camel.spi.Language; +import org.apache.camel.util.CamelContextHelper; + +import javax.naming.Context; +import javax.naming.InitialContext; +import java.io.InputStream; +import java.util.Hashtable; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +/** + * A useful base class which creates a {@link org.apache.camel.CamelContext} with some routes + * along with a {@link org.apache.camel.ProducerTemplate} for use in the test case + * + * @version $Revision: 712844 $ + */ +public abstract class CamelTestSupport extends TestSupport { + protected CamelContext context; + protected ProducerTemplate template; + private boolean useRouteBuilder = true; + private Service camelContextService; + + public boolean isUseRouteBuilder() { + return useRouteBuilder; + } + + public void setUseRouteBuilder(boolean useRouteBuilder) { + this.useRouteBuilder = useRouteBuilder; + } + + public Service getCamelContextService() { + return camelContextService; + } + + /** + * Allows a service to be registered a separate lifecycle service to start + * and stop the context; such as for Spring when the ApplicationContext is + * started and stopped, rather than directly stopping the CamelContext + */ + public void setCamelContextService(Service camelContextService) { + this.camelContextService = camelContextService; + } + + @Override + protected void setUp() throws Exception { + context = createCamelContext(); + assertValidContext(context); + + template = context.createProducerTemplate(); + + postProcessTest(); + + if (isUseRouteBuilder()) { + RouteBuilder[] builders = createRouteBuilders(); + for (RouteBuilder builder : builders) { + log.debug("Using created route builder: " + builder); + context.addRoutes(builder); + } + startCamelContext(); + log.debug("Routing Rules are: " + context.getRoutes()); + } else { + log.debug("Using route builder from the created context: " + context); + } + } + + @Override + protected void tearDown() throws Exception { + log.debug("tearDown test: " + getName()); + template.stop(); + stopCamelContext(); + } + + /** + * Lets post process this test instance to process any Camel annotations. + * Note that using Spring Test or Guice is a more powerful approach. + */ + protected void postProcessTest() throws Exception { + CamelBeanPostProcessor processor = new CamelBeanPostProcessor(); + processor.setCamelContext(context); + processor.postProcessBeforeInitialization(this, "this"); + } + + protected void stopCamelContext() throws Exception { + if (camelContextService != null) { + camelContextService.stop(); + } else { + context.stop(); + } + } + + protected void startCamelContext() throws Exception { + if (camelContextService != null) { + camelContextService.start(); + } else { + if (context instanceof DefaultCamelContext) { + DefaultCamelContext defaultCamelContext = (DefaultCamelContext) context; + if (!defaultCamelContext.isStarted()) { + defaultCamelContext.start(); + } + } else { + context.start(); + } + } + } + + protected CamelContext createCamelContext() throws Exception { + return new DefaultCamelContext(createRegistry()); + } + + protected JndiRegistry createRegistry() throws Exception { + return new JndiRegistry(createJndiContext()); + } + + protected Context createJndiContext() throws Exception { + InputStream in = getClass().getClassLoader().getResourceAsStream("jndi.properties"); + assertNotNull("Cannot find jndi.properties on the classpath!", in); + Properties properties = new Properties(); + properties.load(in); + return new InitialContext(new Hashtable(properties)); + } + + /** + * Factory method which derived classes can use to create a {@link org.apache.camel.builder.RouteBuilder} + * to define the routes for testing + */ + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + // no routes added by default + } + }; + } + + /** + * Factory method which derived classes can use to create an array of + * {@link org.apache.camel.builder.RouteBuilder}s to define the routes for testing + * + * @see #createRouteBuilder() + */ + protected RouteBuilder[] createRouteBuilders() throws Exception { + return new RouteBuilder[]{createRouteBuilder()}; + } + + /** + * Resolves a mandatory endpoint for the given URI or an exception is thrown + * + * @param uri the Camel URI to use to create or resolve an endpoint + * @return the endpoint + */ + protected Endpoint resolveMandatoryEndpoint(String uri) { + return resolveMandatoryEndpoint(context, uri); + } + + /** + * Resolves a mandatory endpoint for the given URI and expected type or an exception is thrown + * + * @param uri the Camel URI to use to create or resolve an endpoint + * @return the endpoint + */ + protected T resolveMandatoryEndpoint(String uri, Class endpointType) { + return resolveMandatoryEndpoint(context, uri, endpointType); + } + + /** + * Resolves the mandatory Mock endpoint using a URI of the form mock:someName + * + * @param uri the URI which typically starts with "mock:" and has some name + * @return the mandatory mock endpoint or an exception is thrown if it could not be resolved + */ + protected MockEndpoint getMockEndpoint(String uri) { + return resolveMandatoryEndpoint(uri, MockEndpoint.class); + } + + /** + * Sends a message to the given endpoint URI with the body value + * + * @param endpointUri the URI of the endpoint to send to + * @param body the body for the message + */ + protected void sendBody(String endpointUri, final Object body) { + template.send(endpointUri, new Processor() { + public void process(Exchange exchange) { + Message in = exchange.getIn(); + in.setBody(body); + in.setHeader("testCase", getName()); + } + }); + } + + /** + * Sends a message to the given endpoint URI with the body value and specified headers + * + * @param endpointUri the URI of the endpoint to send to + * @param body the body for the message + * @param headers any headers to set on the message + */ + protected void sendBody(String endpointUri, final Object body, final Map headers) { + template.send(endpointUri, new Processor() { + public void process(Exchange exchange) { + Message in = exchange.getIn(); + in.setBody(body); + in.setHeader("testCase", getName()); + for (Map.Entry entry : headers.entrySet()) { + in.setHeader(entry.getKey(), entry.getValue()); + } + } + }); + } + + /** + * Sends messages to the given endpoint for each of the specified bodies + * + * @param endpointUri the endpoint URI to send to + * @param bodies the bodies to send, one per message + */ + protected void sendBodies(String endpointUri, Object... bodies) { + for (Object body : bodies) { + sendBody(endpointUri, body); + } + } + + /** + * Creates an exchange with the given body + */ + protected Exchange createExchangeWithBody(Object body) { + return createExchangeWithBody(context, body); + } + + /** + * Asserts that the given language name and expression evaluates to the + * given value on a specific exchange + */ + protected void assertExpression(Exchange exchange, String languageName, String expressionText, Object expectedValue) { + Language language = assertResolveLanguage(languageName); + + Expression expression = language.createExpression(expressionText); + assertNotNull("No Expression could be created for text: " + expressionText + " language: " + language, expression); + + assertExpression(expression, exchange, expectedValue); + } + + /** + * Asserts that the given language name and predicate expression evaluates + * to the expected value on the message exchange + */ + protected void assertPredicate(String languageName, String expressionText, Exchange exchange, boolean expected) { + Language language = assertResolveLanguage(languageName); + + Predicate predicate = language.createPredicate(expressionText); + assertNotNull("No Predicate could be created for text: " + expressionText + " language: " + language, predicate); + + assertPredicate(predicate, exchange, expected); + } + + /** + * Asserts that the language name can be resolved + */ + protected Language assertResolveLanguage(String languageName) { + Language language = context.resolveLanguage(languageName); + assertNotNull("No language found for name: " + languageName, language); + return language; + } + + /** + * Asserts that all the expectations of the Mock endpoints are valid + */ + protected void assertMockEndpointsSatisfied() throws InterruptedException { + MockEndpoint.assertIsSatisfied(context); + } + + protected void assertValidContext(CamelContext context) { + assertNotNull("No context found!", context); + } + + protected List getSingletonEndpoints(Class type) { + return CamelContextHelper.getSingletonEndpoints(context, type); + } + + protected T getMandatoryEndpoint(String uri, Class type) { + T endpoint = context.getEndpoint(uri, type); + assertNotNull("No endpoint found for uri: " + uri, endpoint); + return endpoint; + } + + protected Endpoint getMandatoryEndpoint(String uri) { + Endpoint endpoint = context.getEndpoint(uri); + assertNotNull("No endpoint found for uri: " + uri, endpoint); + return endpoint; + } + + /** + * Disables the JMX agent. Must be called before the {@link #setUp()} method. + */ + protected void disableJMX() { + System.setProperty(JmxSystemPropertyKeys.DISABLED, "true"); + } + + /** + * Enables the JMX agent. Must be called before the {@link #setUp()} method. + */ + protected void enableJMX() { + System.setProperty(JmxSystemPropertyKeys.DISABLED, "false"); + } + +} \ No newline at end of file Propchange: activemq/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelTestSupport.java ------------------------------------------------------------------------------ svn:eol-style = native Added: activemq/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/TestSupport.java URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/TestSupport.java?rev=722127&view=auto ============================================================================== --- activemq/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/TestSupport.java (added) +++ activemq/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/TestSupport.java Mon Dec 1 09:00:56 2008 @@ -0,0 +1,373 @@ +/** + * 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.test; + +import java.io.File; +import java.util.List; + +import junit.framework.TestCase; +import org.apache.camel.builder.Builder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.builder.ValueBuilder; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.impl.DefaultExchange; +import org.apache.camel.processor.DelegateAsyncProcessor; +import org.apache.camel.processor.DelegateProcessor; +import org.apache.camel.util.ExchangeHelper; +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.InvalidPayloadException; +import org.apache.camel.Message; +import org.apache.camel.Expression; +import org.apache.camel.Predicate; +import org.apache.camel.CamelContext; +import org.apache.camel.Route; +import org.apache.camel.Processor; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * A bunch of useful testing methods + * + * @version $Revision: 712937 $ + */ +public abstract class TestSupport extends TestCase { + + protected transient Log log = LogFactory.getLog(getClass()); + + // Builder methods for expressions used when testing + // ------------------------------------------------------------------------- + + /** + * Returns a value builder for the given header + */ + public ValueBuilder header(String name) { + return Builder.header(name); + } + + /** + * Returns a predicate and value builder for the inbound body on an exchange + */ + public ValueBuilder body() { + return Builder.body(); + } + + /** + * Returns a predicate and value builder for the inbound message body as a + * specific type + */ + public ValueBuilder bodyAs(Class type) { + return Builder.bodyAs(type); + } + + /** + * Returns a predicate and value builder for the outbound body on an + * exchange + */ + public ValueBuilder outBody() { + return Builder.outBody(); + } + + /** + * Returns a predicate and value builder for the outbound message body as a + * specific type + */ + public ValueBuilder outBodyAs(Class type) { + return Builder.outBodyAs(type); + } + + /** + * Returns a predicate and value builder for the fault body on an + * exchange + */ + public ValueBuilder faultBody() { + return Builder.faultBody(); + } + + /** + * Returns a predicate and value builder for the fault message body as a + * specific type + */ + public ValueBuilder faultBodyAs(Class type) { + return Builder.faultBodyAs(type); + } + + /** + * Returns a value builder for the given system property + */ + public ValueBuilder systemProperty(String name) { + return Builder.systemProperty(name); + } + + /** + * Returns a value builder for the given system property + */ + public ValueBuilder systemProperty(String name, String defaultValue) { + return Builder.systemProperty(name, defaultValue); + } + + // Assertions + // ----------------------------------------------------------------------- + + protected T assertIsInstanceOf(Class expectedType, Object value) { + assertNotNull("Expected an instance of type: " + expectedType.getName() + " but was null", value); + assertTrue("object should be a " + expectedType.getName() + " but was: " + value + " with type: " + + value.getClass().getName(), expectedType.isInstance(value)); + return expectedType.cast(value); + } + + protected void assertEndpointUri(Endpoint endpoint, String uri) { + assertNotNull("Endpoint is null when expecting endpoint for: " + uri, endpoint); + assertEquals("Endoint uri for: " + endpoint, uri, endpoint.getEndpointUri()); + } + + /** + * Asserts the In message on the exchange contains the expected value + */ + protected Object assertInMessageHeader(Exchange exchange, String name, Object expected) { + return assertMessageHeader(exchange.getIn(), name, expected); + } + + /** + * Asserts the Out message on the exchange contains the expected value + */ + protected Object assertOutMessageHeader(Exchange exchange, String name, Object expected) { + return assertMessageHeader(exchange.getOut(), name, expected); + } + + /** + * Asserts that the given exchange has an OUT message of the given body value + * + * @param exchange the exchange which should have an OUT message + * @param expected the expected value of the OUT message + * @throws org.apache.camel.InvalidPayloadException is thrown if the payload is not the expected class type + */ + protected void assertInMessageBodyEquals(Exchange exchange, Object expected) throws InvalidPayloadException { + assertNotNull("Should have a response exchange!", exchange); + + Object actual; + if (expected == null) { + actual = ExchangeHelper.getMandatoryInBody(exchange); + assertEquals("in body of: " + exchange, expected, actual); + } else { + actual = ExchangeHelper.getMandatoryInBody(exchange, expected.getClass()); + } + assertEquals("in body of: " + exchange, expected, actual); + + log.debug("Received response: " + exchange + " with in: " + exchange.getIn()); + } + + /** + * Asserts that the given exchange has an OUT message of the given body value + * + * @param exchange the exchange which should have an OUT message + * @param expected the expected value of the OUT message + * @throws org.apache.camel.InvalidPayloadException is thrown if the payload is not the expected class type + */ + protected void assertOutMessageBodyEquals(Exchange exchange, Object expected) throws InvalidPayloadException { + assertNotNull("Should have a response exchange!", exchange); + + Object actual; + if (expected == null) { + actual = ExchangeHelper.getMandatoryOutBody(exchange); + assertEquals("output body of: " + exchange, expected, actual); + } else { + actual = ExchangeHelper.getMandatoryOutBody(exchange, expected.getClass()); + } + assertEquals("output body of: " + exchange, expected, actual); + + log.debug("Received response: " + exchange + " with out: " + exchange.getOut()); + } + + protected Object assertMessageHeader(Message message, String name, Object expected) { + Object value = message.getHeader(name); + assertEquals("Header: " + name + " on Message: " + message, expected, value); + return value; + } + + /** + * Asserts that the given expression when evaluated returns the given answer + */ + protected Object assertExpression(Expression expression, Exchange exchange, Object expected) { + Object value = expression.evaluate(exchange); + + // lets try convert to the type of the expected + if (expected != null) { + value = ExchangeHelper.convertToType(exchange, expected.getClass(), value); + } + + log.debug("Evaluated expression: " + expression + " on exchange: " + exchange + " result: " + value); + + assertEquals("Expression: " + expression + " on Exchange: " + exchange, expected, value); + return value; + } + + /** + * Asserts that the predicate returns the expected value on the exchange + */ + protected void assertPredicateMatches(Predicate predicate, Exchange exchange) { + assertPredicate(predicate, exchange, true); + } + + /** + * Asserts that the predicate returns the expected value on the exchange + */ + protected void assertPredicateDoesNotMatch(Predicate predicate, Exchange exchange) { + try { + predicate.assertMatches("Predicate should match", exchange); + } catch (AssertionError e) { + log.debug("Caught expected assertion error: " + e); + } + assertPredicate(predicate, exchange, false); + } + + /** + * Asserts that the predicate returns the expected value on the exchange + */ + protected boolean assertPredicate(Predicate predicate, Exchange exchange, boolean expected) { + if (expected) { + predicate.assertMatches("Predicate failed", exchange); + } + boolean value = predicate.matches(exchange); + + log.debug("Evaluated predicate: " + predicate + " on exchange: " + exchange + " result: " + value); + + assertEquals("Predicate: " + predicate + " on Exchange: " + exchange, expected, value); + return value; + } + + /** + * Resolves an endpoint and asserts that it is found + */ + protected Endpoint resolveMandatoryEndpoint(CamelContext context, String uri) { + Endpoint endpoint = context.getEndpoint(uri); + + assertNotNull("No endpoint found for URI: " + uri, endpoint); + + return endpoint; + } + + /** + * Resolves an endpoint and asserts that it is found + */ + protected T resolveMandatoryEndpoint(CamelContext context, String uri, + Class endpointType) { + T endpoint = context.getEndpoint(uri, endpointType); + + assertNotNull("No endpoint found for URI: " + uri, endpoint); + + return endpoint; + } + + /** + * Creates an exchange with the given body + */ + protected Exchange createExchangeWithBody(CamelContext camelContext, Object body) { + Exchange exchange = new DefaultExchange(camelContext); + Message message = exchange.getIn(); + message.setHeader("testName", getName()); + message.setHeader("testClass", getClass().getName()); + message.setBody(body); + return exchange; + } + + protected T assertOneElement(List list) { + assertEquals("Size of list should be 1: " + list, 1, list.size()); + return list.get(0); + } + + /** + * Asserts that a list is of the given size + */ + protected List assertListSize(List list, int size) { + assertEquals("List should be of size: " + size + " but is: " + list, size, list.size()); + return list; + } + + /** + * A helper method to create a list of Route objects for a given route builder + */ + protected List getRouteList(RouteBuilder builder) throws Exception { + CamelContext context = new DefaultCamelContext(); + context.addRoutes(builder); + context.start(); + List answer = context.getRoutes(); + context.stop(); + return answer; + } + + /** + * Asserts that the text contains the given string + * + * @param text the text to compare + * @param containedText the text which must be contained inside the other text parameter + */ + protected void assertStringContains(String text, String containedText) { + assertNotNull("Text should not be null!", text); + assertTrue("Text: " + text + " does not contain: " + containedText, text.contains(containedText)); + } + + /** + * If a processor is wrapped with a bunch of DelegateProcessor or DelegateAsyncProcessor objects + * this call will drill through them and return the wrapped Processor. + */ + protected Processor unwrap(Processor processor) { + while (true) { + if (processor instanceof DelegateAsyncProcessor) { + processor = ((DelegateAsyncProcessor)processor).getProcessor(); + } else if (processor instanceof DelegateProcessor) { + processor = ((DelegateProcessor)processor).getProcessor(); + } else { + return processor; + } + } + } + + /** + * Recursively delete a directory, useful to zapping test data + * + * @param file the directory to be deleted + */ + public static void deleteDirectory(String file) { + deleteDirectory(new File(file)); + } + + /** + * Recursively delete a directory, useful to zapping test data + * + * @param file the directory to be deleted + */ + public static void deleteDirectory(File file) { + if (file.isDirectory()) { + File[] files = file.listFiles(); + for (int i = 0; i < files.length; i++) { + deleteDirectory(files[i]); + } + } + file.delete(); + } + + /** + * create the directory + * + * @param file the directory to be created + */ + public static void createDirectory(String file) { + File dir = new File(file); + dir.mkdirs(); + } +} \ No newline at end of file Propchange: activemq/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/TestSupport.java ------------------------------------------------------------------------------ svn:eol-style = native Added: activemq/camel/trunk/components/camel-test/src/main/resources/META-INF/LICENSE.txt URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-test/src/main/resources/META-INF/LICENSE.txt?rev=722127&view=auto ============================================================================== --- activemq/camel/trunk/components/camel-test/src/main/resources/META-INF/LICENSE.txt (added) +++ activemq/camel/trunk/components/camel-test/src/main/resources/META-INF/LICENSE.txt Mon Dec 1 09:00:56 2008 @@ -0,0 +1,203 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed 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. + Propchange: activemq/camel/trunk/components/camel-test/src/main/resources/META-INF/LICENSE.txt ------------------------------------------------------------------------------ svn:executable = * Propchange: activemq/camel/trunk/components/camel-test/src/main/resources/META-INF/LICENSE.txt ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: activemq/camel/trunk/components/camel-test/src/main/resources/META-INF/NOTICE.txt URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-test/src/main/resources/META-INF/NOTICE.txt?rev=722127&view=auto ============================================================================== --- activemq/camel/trunk/components/camel-test/src/main/resources/META-INF/NOTICE.txt (added) +++ activemq/camel/trunk/components/camel-test/src/main/resources/META-INF/NOTICE.txt Mon Dec 1 09:00:56 2008 @@ -0,0 +1,11 @@ + ========================================================================= + == NOTICE file corresponding to the section 4 d of == + == the Apache License, Version 2.0, == + == in this case for the Apache Camel distribution. == + ========================================================================= + + This product includes software developed by + The Apache Software Foundation (http://www.apache.org/). + + Please read the different LICENSE files present in the licenses directory of + this distribution. Propchange: activemq/camel/trunk/components/camel-test/src/main/resources/META-INF/NOTICE.txt ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: activemq/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java?rev=722127&view=auto ============================================================================== --- activemq/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java (added) +++ activemq/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java Mon Dec 1 09:00:56 2008 @@ -0,0 +1,56 @@ +/** + * 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.test.patterns; + +import org.apache.camel.EndpointInject; +import org.apache.camel.test.CamelTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; + +/** + * @version $Revision: 630568 $ + */ +public class FilterTest extends CamelTestSupport { + + @EndpointInject(uri="mock:result") + protected MockEndpoint resultEndpoint; + + public void testSendMatchingMessage() throws Exception { + resultEndpoint.expectedMessageCount(1); + + template.sendBodyAndHeader("direct:start", "", "foo", "bar"); + + resultEndpoint.assertIsSatisfied(); + } + + public void testSendNotMatchingMessage() throws Exception { + resultEndpoint.expectedMessageCount(0); + + template.sendBodyAndHeader("direct:start", "", "foo", "notMatchedHeaderValue"); + + resultEndpoint.assertIsSatisfied(); + } + + + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result"); + } + }; + } +} \ No newline at end of file Propchange: activemq/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: activemq/camel/trunk/components/camel-test/src/test/resources/jndi.properties URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-test/src/test/resources/jndi.properties?rev=722127&view=auto ============================================================================== --- activemq/camel/trunk/components/camel-test/src/test/resources/jndi.properties (added) +++ activemq/camel/trunk/components/camel-test/src/test/resources/jndi.properties Mon Dec 1 09:00:56 2008 @@ -0,0 +1,22 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- + +# START SNIPPET: jndi + +java.naming.factory.initial = org.apache.camel.util.jndi.CamelInitialContextFactory + +# END SNIPPET: jndi Propchange: activemq/camel/trunk/components/camel-test/src/test/resources/jndi.properties ------------------------------------------------------------------------------ svn:eol-style = native Added: activemq/camel/trunk/components/camel-test/src/test/resources/log4j.properties URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-test/src/test/resources/log4j.properties?rev=722127&view=auto ============================================================================== --- activemq/camel/trunk/components/camel-test/src/test/resources/log4j.properties (added) +++ activemq/camel/trunk/components/camel-test/src/test/resources/log4j.properties Mon Dec 1 09:00:56 2008 @@ -0,0 +1,36 @@ +## ------------------------------------------------------------------------ +## 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. +## ------------------------------------------------------------------------ + +# +# The logging properties used for eclipse testing, We want to see debug output on the console. +# +log4j.rootLogger=INFO, file + +log4j.logger.org.springframework=WARN +#log4j.logger.org.apache.camel=DEBUG + +# CONSOLE appender not used by default +log4j.appender.out=org.apache.log4j.ConsoleAppender +log4j.appender.out.layout=org.apache.log4j.PatternLayout +log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n +#log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n + +# File appender +log4j.appender.file=org.apache.log4j.FileAppender +log4j.appender.file.layout=org.apache.log4j.PatternLayout +log4j.appender.file.layout.ConversionPattern=%d %-5p %c{1} - %m %n +log4j.appender.file.file=target/test.log Propchange: activemq/camel/trunk/components/camel-test/src/test/resources/log4j.properties ------------------------------------------------------------------------------ svn:eol-style = native