Repository: camel
Updated Branches:
  refs/heads/master d2ea9b510 -> 14a7dd791


[CAMEL-8948] CamelBlueprintTestSupport detects if BP container will be reloaded 
and syncs with the reload


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

Branch: refs/heads/master
Commit: 9b6737bd904bc2ec893060d8fe50494f0713fa27
Parents: 692e479
Author: Grzegorz Grzybek <gr.grzy...@gmail.com>
Authored: Mon Jul 20 09:43:09 2015 +0200
Committer: Grzegorz Grzybek <gr.grzy...@gmail.com>
Committed: Mon Jul 20 14:51:37 2015 +0200

----------------------------------------------------------------------
 .../test/blueprint/CamelBlueprintHelper.java    | 27 ++++---
 .../blueprint/CamelBlueprintTestSupport.java    | 75 +++++++++++++++++---
 .../org/apache/camel/test/blueprint/Main.java   |  7 +-
 3 files changed, 87 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/9b6737bd/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintHelper.java
----------------------------------------------------------------------
diff --git 
a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintHelper.java
 
b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintHelper.java
index 4e84211..b517b29 100644
--- 
a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintHelper.java
+++ 
b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintHelper.java
@@ -179,7 +179,8 @@ public final class CamelBlueprintHelper {
     @SuppressWarnings({"unchecked", "rawtypes"})
     public static void setPersistentFileForConfigAdmin(BundleContext 
bundleContext, String pid,
                                                        String fileName, final 
Dictionary props,
-                                                       String symbolicName, 
Set<Long> bpEvents) throws IOException, InterruptedException {
+                                                       String symbolicName, 
Set<Long> bpEvents,
+                                                       boolean expectReload) 
throws IOException, InterruptedException {
         if (pid != null) {
             if (fileName == null) {
                 throw new IllegalArgumentException("The persistent file should 
not be null");
@@ -198,18 +199,22 @@ public final class CamelBlueprintHelper {
                     // we *have to* use "null" as 2nd arg to have correct 
bundle location for Configuration object
                     final Configuration config = 
configAdmin.getConfiguration(pid, null);
                     LOG.info("Updating ConfigAdmin {} by overriding properties 
{}", config, props);
-                    // we will have update and in consequence, BP container 
reload, let's wait for it to
+                    // we may have update and in consequence, BP container 
reload, let's wait for it to
                     // be CREATED again
-                    CamelBlueprintHelper.waitForBlueprintContainer(bpEvents, 
bundleContext, symbolicName, BlueprintEvent.CREATED, new Runnable() {
-                        @Override
-                        public void run() {
-                            try {
-                                config.update(props);
-                            } catch (IOException e) {
-                                throw new RuntimeException(e.getMessage(), e);
+                    if (expectReload) {
+                        
CamelBlueprintHelper.waitForBlueprintContainer(bpEvents, bundleContext, 
symbolicName, BlueprintEvent.CREATED, new Runnable() {
+                            @Override
+                            public void run() {
+                                try {
+                                    config.update(props);
+                                } catch (IOException e) {
+                                    throw new RuntimeException(e.getMessage(), 
e);
+                                }
                             }
-                        }
-                    });
+                        });
+                    } else {
+                        config.update(props);
+                    }
                 }
 
             }

http://git-wip-us.apache.org/repos/asf/camel/blob/9b6737bd/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 87f7256..88194ec 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
@@ -18,6 +18,8 @@ package org.apache.camel.test.blueprint;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Dictionary;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
@@ -28,6 +30,10 @@ import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.apache.aries.blueprint.compendium.cm.CmNamespaceHandler;
 import org.apache.camel.CamelContext;
 import org.apache.camel.component.properties.PropertiesComponent;
 import org.apache.camel.model.ModelCamelContext;
@@ -41,6 +47,10 @@ import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.blueprint.container.BlueprintEvent;
 import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
 /**
  * Base class for OSGi Blueprint unit tests with Camel.
@@ -71,6 +81,8 @@ public abstract class CamelBlueprintTestSupport extends 
CamelTestSupport {
         final BundleContext answer = 
CamelBlueprintHelper.createBundleContext(symbolicName, getBlueprintDescriptor(),
             includeTestBundle(), getBundleFilter(), getBundleVersion(), 
getBundleDirectives());
 
+        boolean expectReload = 
expectBlueprintContainerReloadOnConfigAdminUpdate();
+
         // must register override properties early in OSGi containers
         Properties extra = useOverridePropertiesWithPropertiesComponent();
         if (extra != null) {
@@ -129,7 +141,7 @@ public abstract class CamelBlueprintTestSupport extends 
CamelTestSupport {
             if (!new File(file[0]).exists()) {
                 throw new IllegalArgumentException("The provided file \"" + 
file[0] + "\" from loadConfigAdminConfigurationFile doesn't exist");
             }
-            CamelBlueprintHelper.setPersistentFileForConfigAdmin(answer, 
file[1], file[0], props, symbolicName, bpEvents);
+            CamelBlueprintHelper.setPersistentFileForConfigAdmin(answer, 
file[1], file[0], props, symbolicName, bpEvents, expectReload);
         }
 
         // allow end user to override properties
@@ -145,16 +157,20 @@ public abstract class CamelBlueprintTestSupport extends 
CamelTestSupport {
                 throw new IllegalArgumentException("Cannot find configuration 
with pid " + pid + " in OSGi ConfigurationAdmin service.");
             }
             log.info("Updating ConfigAdmin {} by overriding properties {}", 
config, props);
-            CamelBlueprintHelper.waitForBlueprintContainer(bpEvents, answer, 
symbolicName, BlueprintEvent.CREATED, new Runnable() {
-                @Override
-                public void run() {
-                    try {
-                        config.update(props);
-                    } catch (IOException e) {
-                        throw new RuntimeException(e.getMessage(), e);
+            if (expectReload) {
+                CamelBlueprintHelper.waitForBlueprintContainer(bpEvents, 
answer, symbolicName, BlueprintEvent.CREATED, new Runnable() {
+                    @Override
+                    public void run() {
+                        try {
+                            config.update(props);
+                        } catch (IOException e) {
+                            throw new RuntimeException(e.getMessage(), e);
+                        }
                     }
-                }
-            });
+                });
+            } else {
+                config.update(props);
+            }
         }
 
         return answer;
@@ -199,6 +215,45 @@ public abstract class CamelBlueprintTestSupport extends 
CamelTestSupport {
     }
 
     /**
+     * This method may be overriden to instruct BP test support that BP 
container will reloaded when
+     * Config Admin configuration is updated. By default, this is expected, 
when blueprint XML definition
+     * contains <code>&lt;cm:property-placeholder persistent-id="PID" 
update-strategy="reload"&gt;</code>
+     */
+    protected boolean expectBlueprintContainerReloadOnConfigAdminUpdate() {
+        boolean expectedReload = false;
+        String descriptor = getBlueprintDescriptor();
+        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+        dbf.setNamespaceAware(true);
+        try {
+            // cm-1.0 doesn't define update-strategy attribute
+            Set<String> cmNamesaces = new HashSet<>(Arrays.asList(
+                    CmNamespaceHandler.BLUEPRINT_CM_NAMESPACE_1_1,
+                    CmNamespaceHandler.BLUEPRINT_CM_NAMESPACE_1_2,
+                    CmNamespaceHandler.BLUEPRINT_CM_NAMESPACE_1_3
+            ));
+            DocumentBuilder db = dbf.newDocumentBuilder();
+            Document doc = 
db.parse(getClass().getClassLoader().getResourceAsStream(descriptor));
+            NodeList nl = doc.getDocumentElement().getChildNodes();
+            for (int i = 0; i < nl.getLength(); i++) {
+                Node node = nl.item(i);
+                if (node instanceof Element) {
+                    Element pp = (Element) node;
+                    if (cmNamesaces.contains(pp.getNamespaceURI())) {
+                        String us = pp.getAttribute("update-strategy");
+                        if (us != null && us.equals("reload")) {
+                            expectedReload = true;
+                            break;
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e.getMessage(), e);
+        }
+        return expectedReload;
+    }
+
+    /**
      * Override this method to add services to be registered on startup.
      * <p/>
      * You can use the builder methods {@link #asKeyValueService(String, 
Object, Dictionary)}

http://git-wip-us.apache.org/repos/asf/camel/blob/9b6737bd/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
----------------------------------------------------------------------
diff --git 
a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
 
b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
index 338ad7a..3092d2e 100644
--- 
a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
+++ 
b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
@@ -17,14 +17,17 @@
 package org.apache.camel.test.blueprint;
 
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.Map;
 import java.util.Properties;
+import java.util.Set;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.main.MainSupport;
 import org.osgi.framework.BundleContext;
+import org.osgi.service.blueprint.container.BlueprintEvent;
 
 /**
  * A command line tool for booting up a CamelContext using an OSGi Blueprint 
XML file
@@ -97,8 +100,10 @@ public class Main extends MainSupport {
             }
             LOG.debug("Starting Blueprint XML file: " + descriptors);
             bundleContext = createBundleContext(bundleName);
+            Set<Long> eventHistory = new HashSet<>();
+            CamelBlueprintHelper.waitForBlueprintContainer(eventHistory, 
bundleContext, bundleName, BlueprintEvent.CREATED, null);
             
CamelBlueprintHelper.setPersistentFileForConfigAdmin(bundleContext, 
configAdminPid, configAdminFileName, new Properties(),
-                                                                 bundleName, 
null);
+                                                                 bundleName, 
eventHistory, true);
             camelContext = CamelBlueprintHelper.getOsgiService(bundleContext, 
CamelContext.class);
             if (camelContext == null) {
                 throw new IllegalArgumentException("Cannot find CamelContext 
in blueprint XML file: " + descriptors);

Reply via email to