[camel-test-blueprint] add setConfigAdminInitialConfiguration() and clarify 
related javadocs


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

Branch: refs/heads/master
Commit: 88fede5e38ce9fc84f99d07f3c2dec0a0f551940
Parents: e15603a
Author: Grzegorz Grzybek <gr.grzy...@gmail.com>
Authored: Wed Feb 24 16:23:16 2016 +0100
Committer: Grzegorz Grzybek <gr.grzy...@gmail.com>
Committed: Wed Feb 24 16:23:16 2016 +0100

----------------------------------------------------------------------
 .../blueprint/CamelBlueprintTestSupport.java    | 49 +++++++++++++++++---
 ...InitialConfigurationUsingPropertiesTest.java | 47 +++++++++++++++++++
 2 files changed, 90 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/88fede5e/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 1340218..82a98f4 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
@@ -17,6 +17,7 @@
 package org.apache.camel.test.blueprint;
 
 import java.io.File;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
@@ -104,6 +105,12 @@ public abstract class CamelBlueprintTestSupport extends 
CamelTestSupport {
                 throw new IllegalArgumentException("The provided file \"" + 
file[0] + "\" from loadConfigAdminConfigurationFile doesn't exist");
             }
         }
+        // fetch initial configadmin configuration if provided programmatically
+        Properties initialConfiguration = new Properties();
+        String pid = setConfigAdminInitialConfiguration(initialConfiguration);
+        if (pid != null) {
+            file = new String[] 
{prepareInitialConfigFile(initialConfiguration), pid};
+        }
 
         final String symbolicName = getClass().getSimpleName();
         final BundleContext answer = 
CamelBlueprintHelper.createBundleContext(symbolicName, getBlueprintDescriptor(),
@@ -160,7 +167,7 @@ public abstract class CamelBlueprintTestSupport extends 
CamelTestSupport {
         final Dictionary props = new Properties();
 
         // allow end user to override properties
-        String pid = useOverridePropertiesWithConfigAdmin(props);
+        pid = useOverridePropertiesWithConfigAdmin(props);
         if (pid != null) {
             // we will update the configuration again
             ConfigurationAdmin configAdmin = 
CamelBlueprintHelper.getOsgiService(answer, ConfigurationAdmin.class);
@@ -322,14 +329,16 @@ public abstract class CamelBlueprintTestSupport extends 
CamelTestSupport {
     }
 
     /**
-     * Override this method to override config admin properties. Overriden 
properties will be passed to
+     * <p>Override this method to override config admin properties. Overriden 
properties will be passed to
      * {@link Configuration#update(Dictionary)} and may or may not lead to 
reload of Blueprint container - this
-     * depends on <code>update-strategy="reload|none"</code> in 
<code>&lt;cm:property-placeholder&gt;</code>
+     * depends on <code>update-strategy="reload|none"</code> in 
<code>&lt;cm:property-placeholder&gt;</code></p>
+     * <p>This method should be used to simulate configuration update 
<strong>after</strong> Blueprint container
+     * is already initialized and started. Don't use this method to 
initialized ConfigAdmin configuration.</p>
      *
      * @param props properties where you add the properties to override
      * @return the PID of the OSGi {@link ConfigurationAdmin} which are 
defined in the Blueprint XML file.
      */
-    protected String useOverridePropertiesWithConfigAdmin(Dictionary props) 
throws Exception {
+    protected String useOverridePropertiesWithConfigAdmin(Dictionary<String, 
String> props) throws Exception {
         return null;
     }
 
@@ -344,6 +353,18 @@ public abstract class CamelBlueprintTestSupport extends 
CamelTestSupport {
         return null;
     }
 
+    /**
+     * Override this method as an alternative to {@link 
#loadConfigAdminConfigurationFile()} if there's a need
+     * to set initial ConfigAdmin configuration without using files.
+     *
+     * @param props always non-null. Tests may initialize ConfigAdmin 
configuration by returning PID.
+     * @return persistence-id of the property placeholder. If non-null, 
<code>props</code> will be used as
+     * initial ConfigAdmin configuration
+     */
+    protected String setConfigAdminInitialConfiguration(Properties props) {
+        return null;
+    }
+
     @After
     @Override
     public void tearDown() throws Exception {
@@ -482,6 +503,22 @@ public abstract class CamelBlueprintTestSupport extends 
CamelTestSupport {
         return CamelBlueprintHelper.getOsgiService(bundleContext, type, 
filter, timeout);
     }
 
-}
-
+    /**
+     * Create a temporary File with persisted configuration for ConfigAdmin
+     * @param initialConfiguration
+     * @return
+     */
+    private String prepareInitialConfigFile(Properties initialConfiguration) 
throws IOException {
+        File dir = new File("target/etc");
+        dir.mkdirs();
+        File cfg = File.createTempFile("properties-", ".cfg", dir);
+        FileWriter writer = new FileWriter(cfg);
+        try {
+            initialConfiguration.store(writer, null);
+        } finally {
+            writer.close();
+        }
+        return cfg.getAbsolutePath();
+    }
 
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/88fede5e/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminInitialConfigurationUsingPropertiesTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminInitialConfigurationUsingPropertiesTest.java
 
b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminInitialConfigurationUsingPropertiesTest.java
new file mode 100644
index 0000000..27f4293
--- /dev/null
+++ 
b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminInitialConfigurationUsingPropertiesTest.java
@@ -0,0 +1,47 @@
+/**
+ * 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 java.util.Properties;
+
+import org.junit.Test;
+
+/**
+ * A test showing that if Blueprint XML contains property placeholders, some 
property source has to be defined.
+ */
+public class ConfigAdminInitialConfigurationUsingPropertiesTest extends 
CamelBlueprintTestSupport {
+
+    @Override
+    protected String getBlueprintDescriptor() {
+        return 
"org/apache/camel/test/blueprint/configadmin-endpoint-no-defaults.xml";
+    }
+
+    @Override
+    protected String setConfigAdminInitialConfiguration(Properties props) {
+        props.put("greeting", "Bye");
+        props.put("destination", "mock:result");
+        return "my-placeholders";
+    }
+
+    @Test
+    public void testConfigAdmin() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
+        template.sendBody("direct:start", "World");
+        assertMockEndpointsSatisfied();
+    }
+
+}

Reply via email to