You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by gg...@apache.org on 2016/02/24 18:20:33 UTC

[1/2] camel git commit: [camel-test-blueprint] add setConfigAdminInitialConfiguration() and clarify related javadocs

Repository: camel
Updated Branches:
  refs/heads/camel-2.16.x ed5790bd9 -> c65de1026


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

(cherry picked from commit 88fede5e38ce9fc84f99d07f3c2dec0a0f551940)


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

Branch: refs/heads/camel-2.16.x
Commit: c65de10261b742f61281e6bdfa0a616b1180930d
Parents: a1acec6
Author: Grzegorz Grzybek <gr...@gmail.com>
Authored: Wed Feb 24 16:23:16 2016 +0100
Committer: Grzegorz Grzybek <gr...@gmail.com>
Committed: Wed Feb 24 18:20:03 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/c65de102/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/c65de102/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();
+    }
+
+}


[2/2] camel git commit: [CAMEL-9636] Fail fast in case of empty cm:property-placeholder + test

Posted by gg...@apache.org.
[CAMEL-9636] Fail fast in case of empty cm:property-placeholder + test

(cherry picked from commit e15603a977b825a89fcad20b8da0a8ba59d982d3)


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

Branch: refs/heads/camel-2.16.x
Commit: a1acec615e400a008d6d083d6a84ad68ebb7ab43
Parents: ed5790b
Author: Grzegorz Grzybek <gr...@gmail.com>
Authored: Wed Feb 24 14:27:25 2016 +0100
Committer: Grzegorz Grzybek <gr...@gmail.com>
Committed: Wed Feb 24 18:20:03 2016 +0100

----------------------------------------------------------------------
 .../test/blueprint/CamelBlueprintHelper.java    | 19 ++++++++++---
 .../blueprint/CamelBlueprintTestSupport.java    |  6 ++--
 ...minNoDefaultValuesBlueprintCreationTest.java | 29 ++++++++------------
 3 files changed, 28 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a1acec61/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 b99814c..9f811a2 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
@@ -314,13 +314,20 @@ public final class CamelBlueprintHelper {
                                                  final String symbolicName, final int bpEvent, final Runnable runAndWait)
         throws InterruptedException {
         final CountDownLatch latch = new CountDownLatch(1);
+        final Throwable[] pThrowable = new Throwable[] {null};
         ServiceRegistration<BlueprintListener> registration = context.registerService(BlueprintListener.class, new BlueprintListener() {
             @Override
             public void blueprintEvent(BlueprintEvent event) {
-                if (event.getType() == bpEvent && event.getBundle().getSymbolicName().equals(symbolicName)) {
-                    // we skip events that we've already seen
-                    // it works with BP container reloads if next CREATE state is at least 1ms after previous one
-                    if (eventHistory == null || eventHistory.add(event.getTimestamp())) {
+                if (event.getBundle().getSymbolicName().equals(symbolicName)) {
+                    if (event.getType() == bpEvent) {
+                        // we skip events that we've already seen
+                        // it works with BP container reloads if next CREATE state is at least 1ms after previous one
+                        if (eventHistory == null || eventHistory.add(event.getTimestamp())) {
+                            latch.countDown();
+                        }
+                    } else if (event.getType() == BlueprintEvent.FAILURE) {
+                        // we didn't wait for FAILURE, but we got it - fail fast then
+                        pThrowable[0] = event.getCause();
                         latch.countDown();
                     }
                 }
@@ -331,6 +338,10 @@ public final class CamelBlueprintHelper {
         }
         latch.await(CamelBlueprintHelper.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
         registration.unregister();
+
+        if (pThrowable[0] != null) {
+            throw new RuntimeException(pThrowable[0].getMessage(), pThrowable[0]);
+        }
     }
 
     protected static TinyBundle createTestBundle(String name, String version, String descriptors, String[] ... configAdminPidFiles) throws IOException {

http://git-wip-us.apache.org/repos/asf/camel/blob/a1acec61/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 928d257..1340218 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
@@ -154,10 +154,8 @@ public abstract class CamelBlueprintTestSupport extends CamelTestSupport {
         // to the same state of BP container
         Set<Long> bpEvents = new HashSet<>();
 
-        if (!expectReload) {
-            CamelBlueprintHelper.waitForBlueprintContainer(bpEvents, answer, symbolicName, BlueprintEvent.CREATED, null);
-        }
-        
+        CamelBlueprintHelper.waitForBlueprintContainer(bpEvents, answer, symbolicName, BlueprintEvent.CREATED, null);
+
         // must reuse props as we can do both load from .cfg file and override afterwards
         final Dictionary props = new Properties();
 

http://git-wip-us.apache.org/repos/asf/camel/blob/a1acec61/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminNoDefaultValuesBlueprintCreationTest.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminNoDefaultValuesBlueprintCreationTest.java b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminNoDefaultValuesBlueprintCreationTest.java
index 8ace8c9..260d2e1 100644
--- a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminNoDefaultValuesBlueprintCreationTest.java
+++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminNoDefaultValuesBlueprintCreationTest.java
@@ -16,18 +16,23 @@
  */
 package org.apache.camel.test.blueprint;
 
-import java.util.Dictionary;
-
 import org.junit.Test;
 
+import static org.hamcrest.CoreMatchers.equalTo;
+
 /**
- *
+ * A test showing that if Blueprint XML contains property placeholders, some property source has to be defined.
  */
 public class ConfigAdminNoDefaultValuesBlueprintCreationTest extends CamelBlueprintTestSupport {
 
     @Override
-    protected boolean expectBlueprintContainerReloadOnConfigAdminUpdate() {
-        return true;
+    public void setUp() throws Exception {
+        try {
+            super.setUp();
+            fail("Should fail, because Blueprint XML uses property placeholders, but we didn't define any property sources");
+        } catch (Exception e) {
+            assertThat(e.getCause().getCause().getMessage(), equalTo("Property placeholder key: destination not found"));
+        }
     }
 
     @Override
@@ -35,20 +40,8 @@ public class ConfigAdminNoDefaultValuesBlueprintCreationTest extends CamelBluepr
         return "org/apache/camel/test/blueprint/configadmin-endpoint-no-defaults.xml";
     }
 
-    @Override
-    protected String useOverridePropertiesWithConfigAdmin(Dictionary props) throws Exception {
-        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();
+    public void test() throws Exception {
     }
 
 }