You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2011/08/19 16:25:50 UTC

svn commit: r1159645 - in /camel/trunk/components: camel-test/src/main/java/org/apache/camel/test/ camel-test/src/main/java/org/apache/camel/test/junit4/ camel-test/src/test/java/org/apache/camel/test/ camel-testng/src/main/java/org/apache/camel/testng...

Author: davsclaus
Date: Fri Aug 19 14:25:49 2011
New Revision: 1159645

URL: http://svn.apache.org/viewvc?rev=1159645&view=rev
Log:
CAMEL-4343: Added isUseAdviceWith to test kit. This helps the test kit to not start Camel before the adviceWith. Meaning we can advice before Camel is started.

Added:
    camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/IsUseAdviceWithJUnit4Test.java
    camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/IsUseAdviceWithTest.java
    camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/IsUseAdviceWithTest.java
Modified:
    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/CamelTestSupport.java
    camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java

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=1159645&r1=1159644&r2=1159645&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 Fri Aug 19 14:25:49 2011
@@ -89,6 +89,21 @@ public abstract class CamelTestSupport e
         this.useRouteBuilder = useRouteBuilder;
     }
 
+    /**
+     * Override when using <a href="http://camel.apache.org/advicewith.html">advice with</a> and return <tt>true</tt>.
+     * This helps knowing advice with is to be used, and {@link CamelContext} will not be started before
+     * the advice with takes place. This helps by ensuring the advice with has been property setup before the
+     * {@link CamelContext} is started
+     * <p/>
+     * <b>Important:</b> Its important to start {@link CamelContext} manually from the unit test
+     * after you are done doing all the advice with.
+     *
+     * @return <tt>true</tt> if you use advice with in your unit tests.
+     */
+    public boolean isUseAdviceWith() {
+        return false;
+    }
+
     public Service getCamelContextService() {
         return camelContextService;
     }
@@ -145,10 +160,13 @@ public abstract class CamelTestSupport e
                 log.debug("Using created route builder: " + builder);
                 context.addRoutes(builder);
             }
-            if (!"true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"))) {
-                startCamelContext();
-            } else {
+            boolean skip = "true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"));
+            if (skip) {
                 log.info("Skipping starting CamelContext as system property skipStartingCamelContext is set to be true.");
+            } else if (isUseAdviceWith()) {
+                log.info("Skipping starting CamelContext as isUseAdviceWith is set to true.");
+            } else {
+                startCamelContext();
             }
         } else {
             log.debug("Using route builder from the created context: " + context);

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=1159645&r1=1159644&r2=1159645&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 Fri Aug 19 14:25:49 2011
@@ -21,7 +21,6 @@ import java.util.Hashtable;
 import java.util.Map;
 import java.util.Properties;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
 import javax.naming.Context;
 import javax.naming.InitialContext;
 
@@ -98,6 +97,21 @@ public abstract class CamelTestSupport e
     }
 
     /**
+     * Override when using <a href="http://camel.apache.org/advicewith.html">advice with</a> and return <tt>true</tt>.
+     * This helps knowing advice with is to be used, and {@link CamelContext} will not be started before
+     * the advice with takes place. This helps by ensuring the advice with has been property setup before the
+     * {@link CamelContext} is started
+     * <p/>
+     * <b>Important:</b> Its important to start {@link CamelContext} manually from the unit test
+     * after you are done doing all the advice with.
+     *
+     * @return <tt>true</tt> if you use advice with in your unit tests.
+     */
+    public boolean isUseAdviceWith() {
+        return false;
+    }
+
+    /**
      * Override to control whether {@link CamelContext} should be setup per test or per class.
      * <p/>
      * By default it will be setup/teardown per test (per test method). If you want to re-use
@@ -242,10 +256,13 @@ public abstract class CamelTestSupport e
                 log.debug("Using created route builder: " + builder);
                 context.addRoutes(builder);
             }
-            if (!"true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"))) {
-                startCamelContext();
-            } else {
+            boolean skip = "true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"));
+            if (skip) {
                 log.info("Skipping starting CamelContext as system property skipStartingCamelContext is set to be true.");
+            } else if (isUseAdviceWith()) {
+                log.info("Skipping starting CamelContext as isUseAdviceWith is set to true.");
+            } else {
+                startCamelContext();
             }
         } else {
             log.debug("Using route builder from the created context: " + context);

Added: camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/IsUseAdviceWithJUnit4Test.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/IsUseAdviceWithJUnit4Test.java?rev=1159645&view=auto
==============================================================================
--- camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/IsUseAdviceWithJUnit4Test.java (added)
+++ camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/IsUseAdviceWithJUnit4Test.java Fri Aug 19 14:25:49 2011
@@ -0,0 +1,70 @@
+/**
+ * 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.builder.AdviceWithRouteBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+/**
+ *
+ */
+// START SNIPPET: e1
+public class IsUseAdviceWithJUnit4Test extends org.apache.camel.test.junit4.CamelTestSupport {
+
+    @Test
+    public void testIsUseAdviceWith() throws Exception {
+        context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // replace the from with seda:foo
+                replaceFrom("seda:foo");
+            }
+        });
+        // we must manually start when we are done with all the advice with
+        context.start();
+
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        template.sendBody("seda:foo", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    public boolean isUseAdviceWith() {
+        // tell we are using advice with, which allows us to advice the route
+        // before Camel is being started, and thus can replace activemq with something else.
+        return true;
+    }
+
+    // This is the route we want to test
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // we do not have activemq on the classpath
+                // but the route has it included
+                from("activemq:queue:foo")
+                    .to("mock:result");
+            }
+        };
+    }
+
+}
+// END SNIPPET: e1

Added: camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/IsUseAdviceWithTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/IsUseAdviceWithTest.java?rev=1159645&view=auto
==============================================================================
--- camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/IsUseAdviceWithTest.java (added)
+++ camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/IsUseAdviceWithTest.java Fri Aug 19 14:25:49 2011
@@ -0,0 +1,66 @@
+/**
+ * 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.builder.AdviceWithRouteBuilder;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ *
+ */
+public class IsUseAdviceWithTest extends CamelTestSupport {
+
+    public void testIsUseAdviceWith() throws Exception {
+        context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // replace the from with seda:foo
+                replaceFrom("seda:foo");
+            }
+        });
+        // we must manually start when we are done with all the advice with
+        context.start();
+
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        template.sendBody("seda:foo", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    public boolean isUseAdviceWith() {
+        // tell we are using advice with, which allows us to advice the route
+        // before Camel is being started, and thus can replace activemq with something else.
+        return true;
+    }
+
+    // This is the route we want to test
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // we do not have activemq on the classpath
+                // but the route has it included
+                from("activemq:queue:foo")
+                    .to("mock:result");
+            }
+        };
+    }
+
+}

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=1159645&r1=1159644&r2=1159645&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 Fri Aug 19 14:25:49 2011
@@ -86,6 +86,21 @@ public abstract class CamelTestSupport e
     }
 
     /**
+     * Override when using <a href="http://camel.apache.org/advicewith.html">advice with</a> and return <tt>true</tt>.
+     * This helps knowing advice with is to be used, and {@link CamelContext} will not be started before
+     * the advice with takes place. This helps by ensuring the advice with has been property setup before the
+     * {@link CamelContext} is started
+     * <p/>
+     * <b>Important:</b> Its important to start {@link CamelContext} manually from the unit test
+     * after you are done doing all the advice with.
+     *
+     * @return <tt>true</tt> if you use advice with in your unit tests.
+     */
+    public boolean isUseAdviceWith() {
+        return false;
+    }
+
+    /**
      * Override to control whether {@link CamelContext} should be setup per test or per class.
      * <p/>
      * By default it will be setup/teardown per test (per test method). If you want to re-use
@@ -187,10 +202,13 @@ public abstract class CamelTestSupport e
                 log.debug("Using created route builder: " + builder);
                 context.addRoutes(builder);
             }
-            if (!"true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"))) {
-                startCamelContext();
-            } else {
+            boolean skip = "true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"));
+            if (skip) {
                 log.info("Skipping starting CamelContext as system property skipStartingCamelContext is set to be true.");
+            } else if (isUseAdviceWith()) {
+                log.info("Skipping starting CamelContext as isUseAdviceWith is set to true.");
+            } else {
+                startCamelContext();
             }
         } else {
             log.debug("Using route builder from the created context: " + context);

Added: camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/IsUseAdviceWithTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/IsUseAdviceWithTest.java?rev=1159645&view=auto
==============================================================================
--- camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/IsUseAdviceWithTest.java (added)
+++ camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/IsUseAdviceWithTest.java Fri Aug 19 14:25:49 2011
@@ -0,0 +1,69 @@
+/**
+ * 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.testng.patterns;
+
+import org.apache.camel.builder.AdviceWithRouteBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.testng.CamelTestSupport;
+import org.testng.annotations.Test;
+
+/**
+ *
+ */
+public class IsUseAdviceWithTest extends CamelTestSupport {
+
+    @Test
+    public void testIsUseAdviceWith() throws Exception {
+        context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // replace the from with seda:foo
+                replaceFrom("seda:foo");
+            }
+        });
+        // we must manually start when we are done with all the advice with
+        context.start();
+
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        template.sendBody("seda:foo", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    public boolean isUseAdviceWith() {
+        // tell we are using advice with, which allows us to advice the route
+        // before Camel is being started, and thus can replace activemq with something else.
+        return true;
+    }
+
+    // This is the route we want to test
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // we do not have activemq on the classpath
+                // but the route has it included
+                from("activemq:queue:foo")
+                    .to("mock:result");
+            }
+        };
+    }
+
+}