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 2013/07/10 12:21:27 UTC

[4/4] git commit: CAME-6524: camel-test-blueprint - Using isMockEndpointsAndSkip doesnt work

CAME-6524: camel-test-blueprint - Using isMockEndpointsAndSkip doesnt work


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/82130fd4
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/82130fd4
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/82130fd4

Branch: refs/heads/camel-2.11.x
Commit: 82130fd49961d5ef84af55cdd1a025fe736ffc00
Parents: 81830bf
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Jul 10 07:59:04 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Jul 10 12:03:10 2013 +0200

----------------------------------------------------------------------
 .../camel/blueprint/BlueprintCamelContext.java  | 29 ++++++++++--
 .../core/osgi/OsgiCamelContextPublisher.java    | 45 +++++++++++++------
 .../blueprint/CamelBlueprintTestSupport.java    | 12 ++++-
 .../blueprint/MockEndpointsAndSkipTest.java     | 42 ++++++++++++++++++
 .../test/blueprint/MockEndpointsAndSkipTest.xml | 34 +++++++++++++++
 .../test/issues/MockEndpointsAndSkipTest.java   | 46 ++++++++++++++++++++
 .../test/issues/MockEndpointsAndSkipTest.xml    | 35 +++++++++++++++
 7 files changed, 226 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/82130fd4/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java
----------------------------------------------------------------------
diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java
index 33798ec..82f27d7 100644
--- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java
+++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java
@@ -18,11 +18,13 @@ package org.apache.camel.blueprint;
 
 import org.apache.camel.TypeConverter;
 import org.apache.camel.core.osgi.OsgiCamelContextHelper;
+import org.apache.camel.core.osgi.OsgiCamelContextPublisher;
 import org.apache.camel.core.osgi.OsgiFactoryFinderResolver;
 import org.apache.camel.core.osgi.OsgiTypeConverter;
 import org.apache.camel.core.osgi.utils.BundleContextUtils;
 import org.apache.camel.core.osgi.utils.BundleDelegatingClassLoader;
 import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.spi.EventNotifier;
 import org.apache.camel.spi.FactoryFinder;
 import org.apache.camel.spi.Registry;
 import org.apache.camel.util.ObjectHelper;
@@ -39,6 +41,9 @@ import org.osgi.service.cm.ConfigurationAdmin;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+/**
+ * OSGi Blueprint based {@link CamelContext}.
+ */
 public class BlueprintCamelContext extends DefaultCamelContext implements ServiceListener, BlueprintListener {
 
     private static final transient Logger LOG = LoggerFactory.getLogger(BlueprintCamelContext.class);
@@ -167,8 +172,28 @@ public class BlueprintCamelContext extends DefaultCamelContext implements Servic
 
     private void maybeStart() throws Exception {
         LOG.trace("maybeStart: {}", this);
+
+        // allow to regsiter the BluerintCamelContext eager in the OSGi Service Registry, which ex is needed
+        // for unit testing with camel-test-blueprint
+        boolean eager = "true".equalsIgnoreCase(System.getProperty("registerBlueprintCamelContextEager"));
+        if (eager) {
+            for (EventNotifier notifer : getManagementStrategy().getEventNotifiers()) {
+                if (notifer instanceof OsgiCamelContextPublisher) {
+                    OsgiCamelContextPublisher publisher = (OsgiCamelContextPublisher) notifer;
+                    publisher.registerCamelContext(this);
+                    break;
+                }
+            }
+        }
+
         // for example from unit testing we want to start Camel later and not
         // when blueprint loading the bundle
+        boolean skip = "true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"));
+        if (skip) {
+            LOG.trace("maybeStart: {} is skipping as System property skipStartingCamelContext is set", this);
+            return;
+        }
+
         if (!isStarted() && !isStarting()) {
             LOG.debug("Starting {}", this);
             start();
@@ -176,8 +201,6 @@ public class BlueprintCamelContext extends DefaultCamelContext implements Servic
             // ignore as Camel is already started
             LOG.trace("Ignoring maybeStart() as {} is already started", this);
         }
-
     }
-    
-    
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/82130fd4/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiCamelContextPublisher.java
----------------------------------------------------------------------
diff --git a/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiCamelContextPublisher.java b/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiCamelContextPublisher.java
index b542d19..7c9ed17 100644
--- a/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiCamelContextPublisher.java
+++ b/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiCamelContextPublisher.java
@@ -24,11 +24,14 @@ import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.management.event.CamelContextStartedEvent;
+import org.apache.camel.management.event.CamelContextStartingEvent;
 import org.apache.camel.management.event.CamelContextStoppingEvent;
 import org.apache.camel.support.EventNotifierSupport;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.framework.Version;
 
@@ -52,18 +55,7 @@ public class OsgiCamelContextPublisher extends EventNotifierSupport {
     public void notify(EventObject event) throws Exception {
         if (event instanceof CamelContextStartedEvent) {
             CamelContext context = ((CamelContextStartedEvent) event).getContext();
-
-            Dictionary<String, Object > props = new Hashtable<String, Object>();
-            props.put(CONTEXT_SYMBOLIC_NAME_PROPERTY, bundleContext.getBundle().getSymbolicName());
-            props.put(CONTEXT_VERSION_PROPERTY, getBundleVersion(bundleContext.getBundle()));
-            props.put(CONTEXT_NAME_PROPERTY, context.getName());
-
-            if (log.isDebugEnabled()) {
-                log.debug("Registering CamelContext [{}] of in OSGi registry", context.getName());
-            }
-
-            ServiceRegistration reg = bundleContext.registerService(CamelContext.class.getName(), context, props);
-            registrations.put(context, reg);
+            registerCamelContext(context);
         } else if (event instanceof CamelContextStoppingEvent) {
             CamelContext context = ((CamelContextStoppingEvent) event).getContext();
             ServiceRegistration reg = registrations.remove(context);
@@ -81,7 +73,10 @@ public class OsgiCamelContextPublisher extends EventNotifierSupport {
     }
 
     public boolean isEnabled(EventObject event) {
-        return true;
+        if (event instanceof CamelContextStartedEvent || event instanceof CamelContextStoppingEvent) {
+            return true;
+        }
+        return false;
     }
 
     @Override
@@ -97,9 +92,33 @@ public class OsgiCamelContextPublisher extends EventNotifierSupport {
         registrations.clear();
     }
 
+    public ServiceRegistration registerCamelContext(CamelContext camelContext) throws InvalidSyntaxException {
+        // avoid registering the same service again
+        ServiceReference[] refs = bundleContext.getServiceReferences(CamelContext.class.getName(), "(camel.context.symbolicname=" + bundleContext.getBundle().getSymbolicName() + ")");
+        if (refs == null) {
+            Dictionary<String, Object > props = new Hashtable<String, Object>();
+            props.put(CONTEXT_SYMBOLIC_NAME_PROPERTY, bundleContext.getBundle().getSymbolicName());
+            props.put(CONTEXT_VERSION_PROPERTY, getBundleVersion(bundleContext.getBundle()));
+            props.put(CONTEXT_NAME_PROPERTY, camelContext.getName());
+
+            if (log.isDebugEnabled()) {
+                log.debug("Registering CamelContext [{}] of in OSGi registry", camelContext.getName());
+            }
+
+            ServiceRegistration reg = bundleContext.registerService(CamelContext.class.getName(), camelContext, props);
+            if (reg != null) {
+                registrations.put(camelContext, reg);
+            }
+            return reg;
+        } else {
+            return null;
+        }
+    }
+
     public static Version getBundleVersion(Bundle bundle) {
         Dictionary<?, ?> headers = bundle.getHeaders();
         String version = (String) headers.get(Constants.BUNDLE_VERSION);
         return (version != null) ? Version.parseVersion(version) : Version.emptyVersion;
     }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/82130fd4/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java
index bdbd45d..03c0a07 100644
--- a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java
+++ b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java
@@ -101,6 +101,9 @@ public abstract class CamelBlueprintTestSupport extends CamelTestSupport {
     @Before
     @Override
     public void setUp() throws Exception {
+        System.setProperty("skipStartingCamelContext", "true");
+        System.setProperty("registerBlueprintCamelContextEager", "true");
+
         String symbolicName = getClass().getSimpleName();
         if (isCreateCamelContextPerClass()) {
             // test is per class, so only setup once (the first time)
@@ -114,6 +117,11 @@ public abstract class CamelBlueprintTestSupport extends CamelTestSupport {
         }
 
         super.setUp();
+
+        // start context when we are ready
+        log.debug("Staring CamelContext: {}", context.getName());
+        context.start();
+
         // must wait for blueprint container to be published then the namespace parser is complete and we are ready for testing
         log.debug("Waiting for BlueprintContainer to be published with symbolicName: {}", symbolicName);
         getOsgiService(BlueprintContainer.class, "(osgi.blueprint.container.symbolicname=" + symbolicName + ")");
@@ -142,6 +150,8 @@ public abstract class CamelBlueprintTestSupport extends CamelTestSupport {
     @After
     @Override
     public void tearDown() throws Exception {
+        System.clearProperty("skipStartingCamelContext");
+        System.clearProperty("registerBlueprintCamelContextEager");
         super.tearDown();
         if (isCreateCamelContextPerClass()) {
             // we tear down in after class
@@ -202,8 +212,8 @@ public abstract class CamelBlueprintTestSupport extends CamelTestSupport {
 
     /**
      * Gets the bundle directives.
+     * <p/>
      * Modify this method if you wish to add some directives.
-     * @return
      */
     protected String getBundleDirectives() {
         return null;

http://git-wip-us.apache.org/repos/asf/camel/blob/82130fd4/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MockEndpointsAndSkipTest.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MockEndpointsAndSkipTest.java b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MockEndpointsAndSkipTest.java
new file mode 100644
index 0000000..1b16711
--- /dev/null
+++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MockEndpointsAndSkipTest.java
@@ -0,0 +1,42 @@
+/**
+ * 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.blueprint;
+
+import org.junit.Test;
+
+public class MockEndpointsAndSkipTest extends CamelBlueprintTestSupport {
+
+    @Override
+    public String isMockEndpointsAndSkip() {
+        return "seda*";
+    }
+
+    @Override
+    protected String getBlueprintDescriptor() {
+        return "org/apache/camel/test/blueprint/MockEndpointsAndSkipTest.xml";
+    }
+
+    @Test
+    public void testHelloWorld() throws Exception {
+        getMockEndpoint("mock:seda:foo").expectedBodiesReceived("Hello World");
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/82130fd4/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/MockEndpointsAndSkipTest.xml
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/MockEndpointsAndSkipTest.xml b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/MockEndpointsAndSkipTest.xml
new file mode 100644
index 0000000..38be1b8
--- /dev/null
+++ b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/MockEndpointsAndSkipTest.xml
@@ -0,0 +1,34 @@
+<?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.
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+           xsi:schemaLocation="
+             http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
+
+  <camelContext xmlns="http://camel.apache.org/schema/blueprint">
+
+    <route>
+      <from uri="direct:start"/>
+      <to uri="log:foo"/>
+      <to uri="seda:foo"/>
+    </route>
+
+  </camelContext>
+
+</blueprint>
+

http://git-wip-us.apache.org/repos/asf/camel/blob/82130fd4/components/camel-test-spring/src/test/java/org/apache/camel/test/issues/MockEndpointsAndSkipTest.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/src/test/java/org/apache/camel/test/issues/MockEndpointsAndSkipTest.java b/components/camel-test-spring/src/test/java/org/apache/camel/test/issues/MockEndpointsAndSkipTest.java
new file mode 100644
index 0000000..d1bf95e
--- /dev/null
+++ b/components/camel-test-spring/src/test/java/org/apache/camel/test/issues/MockEndpointsAndSkipTest.java
@@ -0,0 +1,46 @@
+/**
+ * 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.issues;
+
+import org.apache.camel.test.spring.CamelSpringTestSupport;
+import org.junit.Test;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+public class MockEndpointsAndSkipTest extends CamelSpringTestSupport {
+
+    @Override
+    protected AbstractApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/test/issues/MockEndpointsAndSkipTest.xml");
+    }
+
+    @Override
+    public String isMockEndpointsAndSkip() {
+        return "seda*";
+    }
+
+    @Test
+    public void testHelloWorld() throws Exception {
+        getMockEndpoint("mock:seda:foo").expectedBodiesReceived("Hello World");
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/82130fd4/components/camel-test-spring/src/test/resources/org/apache/camel/test/issues/MockEndpointsAndSkipTest.xml
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/src/test/resources/org/apache/camel/test/issues/MockEndpointsAndSkipTest.xml b/components/camel-test-spring/src/test/resources/org/apache/camel/test/issues/MockEndpointsAndSkipTest.xml
new file mode 100644
index 0000000..81b9093
--- /dev/null
+++ b/components/camel-test-spring/src/test/resources/org/apache/camel/test/issues/MockEndpointsAndSkipTest.xml
@@ -0,0 +1,35 @@
+<?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"
+       xmlns:camel="http://camel.apache.org/schema/spring"
+       xsi:schemaLocation="
+		http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
+
+  <camelContext xmlns="http://camel.apache.org/schema/spring">
+
+    <route>
+      <from uri="direct:start"/>
+      <to uri="log:foo"/>
+      <to uri="seda:foo"/>
+    </route>
+
+  </camelContext>
+
+</beans>
\ No newline at end of file