Author: davsclaus Date: Mon May 9 18:45:20 2011 New Revision: 1101146 URL: http://svn.apache.org/viewvc?rev=1101146&view=rev Log: CAMEL-3959: Fixed debugBefore not working with CamelSpringTestSupport.
Added: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/test/ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/test/SpringDebugBeforeTest.java camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/test/ camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/test/SpringDebugBeforeTest.xml Modified: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelSpringTestSupport.java camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelTestSupport.java camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelSpringTestSupport.java camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelSpringTestSupport.java camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java Modified: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java?rev=1101146&r1=1101145&r2=1101146&view=diff ============================================================================== --- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java (original) +++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java Mon May 9 18:45:20 2011 @@ -199,11 +199,19 @@ public class SpringCamelContext extends } private void maybeStart() throws Exception { - if (!isStarted() && !isStarting()) { - start(); + // for example from unit testing we want to start Camel later and not when Spring framework + // publish a ContextRefreshedEvent + String maybeStart = System.getProperty("maybeStartCamelContext", "true"); + + if ("true".equals(maybeStart)) { + if (!isStarted() && !isStarting()) { + start(); + } else { + // ignore as Camel is already started + LOG.trace("Ignoring maybeStart() as Apache Camel is already started"); + } } else { - // ignore as Camel is already started - LOG.trace("Ignoring maybeStart() as Apache Camel is already started"); + LOG.trace("Ignoring maybeStart() as System property maybeStartCamelContext is false"); } } Modified: camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelSpringTestSupport.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelSpringTestSupport.java?rev=1101146&r1=1101145&r2=1101146&view=diff ============================================================================== --- camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelSpringTestSupport.java (original) +++ camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelSpringTestSupport.java Mon May 9 18:45:20 2011 @@ -44,9 +44,17 @@ public abstract class CamelSpringTestSup @Override protected void setUp() throws Exception { - applicationContext = createApplicationContext(); - assertNotNull("Should have created a valid spring context", applicationContext); - super.setUp(); + if (!"true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"))) { + // tell camel-spring it should not trigger starting CamelContext, since we do that later + // after we are finished setting up the unit test + System.setProperty("maybeStartCamelContext", "false"); + applicationContext = createApplicationContext(); + assertNotNull("Should have created a valid spring context", applicationContext); + super.setUp(); + System.clearProperty("maybeStartCamelContext"); + } else { + log.info("Skipping starting CamelContext as system property skipStartingCamelContext is set to be true."); + } } @Override Modified: camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelTestSupport.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelTestSupport.java?rev=1101146&r1=1101145&r2=1101146&view=diff ============================================================================== --- camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelTestSupport.java (original) +++ camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/CamelTestSupport.java Mon May 9 18:45:20 2011 @@ -113,7 +113,7 @@ public abstract class CamelTestSupport e } context = createCamelContext(); - assertValidContext(context); + assertNotNull("No context found!", context); // reduce default shutdown timeout to avoid waiting for 300 seconds context.getShutdownStrategy().setTimeout(getShutdownTimeout()); @@ -143,9 +143,13 @@ public abstract class CamelTestSupport e context.addRoutes(builder); } startCamelContext(); + log.debug("Routing Rules are: " + context.getRoutes()); } else { log.debug("Using route builder from the created context: " + context); } + log.debug("Routing Rules are: " + context.getRoutes()); + + assertValidContext(context); } @Override Modified: camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelSpringTestSupport.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelSpringTestSupport.java?rev=1101146&r1=1101145&r2=1101146&view=diff ============================================================================== --- camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelSpringTestSupport.java (original) +++ camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelSpringTestSupport.java Mon May 9 18:45:20 2011 @@ -48,9 +48,13 @@ public abstract class CamelSpringTestSup @Before public void setUp() throws Exception { if (!"true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"))) { + // tell camel-spring it should not trigger starting CamelContext, since we do that later + // after we are finished setting up the unit test + System.setProperty("maybeStartCamelContext", "false"); applicationContext = createApplicationContext(); assertNotNull("Should have created a valid spring context", applicationContext); super.setUp(); + System.clearProperty("maybeStartCamelContext"); } else { log.info("Skipping starting CamelContext as system property skipStartingCamelContext is set to be true."); } Modified: camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java?rev=1101146&r1=1101145&r2=1101146&view=diff ============================================================================== --- camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java (original) +++ camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java Mon May 9 18:45:20 2011 @@ -115,7 +115,7 @@ public abstract class CamelTestSupport e } context = createCamelContext(); - assertValidContext(context); + assertNotNull("No context found!", context); // reduce default shutdown timeout to avoid waiting for 300 seconds context.getShutdownStrategy().setTimeout(getShutdownTimeout()); @@ -150,6 +150,8 @@ public abstract class CamelTestSupport e log.debug("Using route builder from the created context: " + context); } log.debug("Routing Rules are: " + context.getRoutes()); + + assertValidContext(context); } @After Modified: camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelSpringTestSupport.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelSpringTestSupport.java?rev=1101146&r1=1101145&r2=1101146&view=diff ============================================================================== --- camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelSpringTestSupport.java (original) +++ camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelSpringTestSupport.java Mon May 9 18:45:20 2011 @@ -47,9 +47,17 @@ public abstract class CamelSpringTestSup @Override @BeforeTest public void setUp() throws Exception { - applicationContext = createApplicationContext(); - assertNotNull(applicationContext, "Should have created a valid spring context"); - super.setUp(); + if (!"true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"))) { + // tell camel-spring it should not trigger starting CamelContext, since we do that later + // after we are finished setting up the unit test + System.setProperty("maybeStartCamelContext", "false"); + applicationContext = createApplicationContext(); + assertNotNull(applicationContext, "Should have created a valid spring context"); + super.setUp(); + System.clearProperty("maybeStartCamelContext"); + } else { + log.info("Skipping starting CamelContext as system property skipStartingCamelContext is set to be true."); + } } @Override Modified: camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java?rev=1101146&r1=1101145&r2=1101146&view=diff ============================================================================== --- camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java (original) +++ camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java Mon May 9 18:45:20 2011 @@ -94,7 +94,7 @@ public abstract class CamelTestSupport e } context = createCamelContext(); - assertValidContext(context); + assertNotNull(context, "No context found!"); // reduce default shutdown timeout to avoid waiting for 300 seconds context.getShutdownStrategy().setTimeout(getShutdownTimeout()); @@ -123,6 +123,8 @@ public abstract class CamelTestSupport e log.debug("Using route builder from the created context: " + context); } log.debug("Routing Rules are: " + context.getRoutes()); + + assertValidContext(context); } @AfterMethod Added: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/test/SpringDebugBeforeTest.java URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/test/SpringDebugBeforeTest.java?rev=1101146&view=auto ============================================================================== --- camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/test/SpringDebugBeforeTest.java (added) +++ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/test/SpringDebugBeforeTest.java Mon May 9 18:45:20 2011 @@ -0,0 +1,59 @@ +/** + * 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.itest.test; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.model.ProcessorDefinition; +import org.apache.camel.test.junit4.CamelSpringTestSupport; +import org.junit.Test; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * + */ +public class SpringDebugBeforeTest extends CamelSpringTestSupport { + + private final List<String> before = new ArrayList<String>(); + + @Override + protected AbstractApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/itest/test/SpringDebugBeforeTest.xml"); + } + + @Override + protected void debugBefore(Exchange exchange, Processor processor, ProcessorDefinition definition, String id, String label) { + before.add(id); + } + + @Test + public void testDebugBefore() throws Exception { + getMockEndpoint("mock:result").expectedMessageCount(1); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + + assertEquals(2, before.size()); + assertEquals("log1", before.get(0)); + assertEquals("to1", before.get(1)); + } +} Added: camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/test/SpringDebugBeforeTest.xml URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/test/SpringDebugBeforeTest.xml?rev=1101146&view=auto ============================================================================== --- camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/test/SpringDebugBeforeTest.xml (added) +++ camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/test/SpringDebugBeforeTest.xml Mon May 9 18:45:20 2011 @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:start"/> + <log message="Got ${body}"/> + <to uri="mock:result"/> + </route> + </camelContext> + +</beans>