camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1348394 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/component/properties/ camel-core/src/test/java/org/apache/camel/component/properties/ components/camel-blueprint/src/main/java/org/apache/camel/blueprint/ components/came...
Date Sat, 09 Jun 2012 12:57:10 GMT
Author: davsclaus
Date: Sat Jun  9 12:57:10 2012
New Revision: 1348394

URL: http://svn.apache.org/viewvc?rev=1348394&view=rev
Log:
CAMEL-5349: Made it easier from camel test kit to use override properties with Camel properties
component.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentOverridePropertiesTest.java
      - copied, changed from r1348324, camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentEndpointTest.java
    camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminOverridePropertiesTest.java
      - copied, changed from r1348324, camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
    camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesParser.java
    camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
    camel/trunk/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java
    camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java?rev=1348394&r1=1348393&r2=1348394&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
Sat Jun  9 12:57:10 2012
@@ -60,6 +60,12 @@ public class PropertiesComponent extends
      */
     public static final String SUFFIX_TOKEN = DEFAULT_SUFFIX_TOKEN;
 
+    /**
+     * Key for stores special override properties that containers such as OSGi can store
+     * in the OSGi service registry
+     */
+    public static final String OVERRIDE_PROPERTIES = PropertiesComponent.class.getName()
+ ".OverrideProperties";
+
     // must be non greedy patterns
     private static final Pattern ENV_PATTERN = Pattern.compile("\\$\\{env:(.*?)\\}", Pattern.DOTALL);
     private static final Pattern SYS_PATTERN = Pattern.compile("\\$\\{(.*?)\\}", Pattern.DOTALL);
@@ -76,6 +82,7 @@ public class PropertiesComponent extends
     private boolean fallbackToUnaugmentedProperty = true;
     private String prefixToken = DEFAULT_PREFIX_TOKEN;
     private String suffixToken = DEFAULT_SUFFIX_TOKEN;
+    private Properties overrideProperties;
     
     public PropertiesComponent() {
     }
@@ -130,6 +137,15 @@ public class PropertiesComponent extends
             }
         }
 
+        // use override properties
+        if (prop != null && overrideProperties != null) {
+            // make a copy to avoid affecting the original properties
+            Properties override = new Properties();
+            override.putAll(prop);
+            override.putAll(overrideProperties);
+            prop = override;
+        }
+
         // enclose tokens if missing
         if (!uri.contains(prefixToken) && !uri.startsWith(prefixToken)) {
             uri = prefixToken + uri;
@@ -248,6 +264,20 @@ public class PropertiesComponent extends
         }
     }
 
+    public Properties getOverrideProperties() {
+        return overrideProperties;
+    }
+
+    /**
+     * Sets a special list of override properties that take precedence
+     * and will use first, if a property exist.
+     *
+     * @param overrideProperties properties that is used first
+     */
+    public void setOverrideProperties(Properties overrideProperties) {
+        this.overrideProperties = overrideProperties;
+    }
+
     @Override
     protected void doStart() throws Exception {
         ServiceHelper.startService(cacheMap);

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentOverridePropertiesTest.java
(from r1348324, camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentEndpointTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentOverridePropertiesTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentOverridePropertiesTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentEndpointTest.java&r1=1348324&r2=1348394&rev=1348394&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentEndpointTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentOverridePropertiesTest.java
Sat Jun  9 12:57:10 2012
@@ -16,16 +16,16 @@
  */
 package org.apache.camel.component.properties;
 
+import java.util.Properties;
+
 import org.apache.camel.CamelContext;
 import org.apache.camel.ContextTestSupport;
-import org.apache.camel.FailedToCreateRouteException;
-import org.apache.camel.ResolveEndpointFailedException;
 import org.apache.camel.builder.RouteBuilder;
 
 /**
  * @version 
  */
-public class PropertiesComponentEndpointTest extends ContextTestSupport {
+public class PropertiesComponentOverridePropertiesTest extends ContextTestSupport {
 
     @Override
     public boolean isUseRouteBuilder() {
@@ -36,56 +36,22 @@ public class PropertiesComponentEndpoint
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").to("mock:{{cool.result}}");
-            }
-        });
-        context.start();
-
-        getMockEndpoint("mock:result").expectedMessageCount(1);
-
-        template.sendBody("direct:start", "Hello World");
-
-        assertMockEndpointsSatisfied();
-    }
-
-    public void testPropertiesComponentEndpoints() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("{{cool.start}}")
-                    .to("log:{{cool.start}}?showBodyType=false&showExchangeId={{cool.showid}}")
+                from("direct:start")
+                    .to("{{hey}}")
                     .to("mock:{{cool.result}}");
             }
         });
         context.start();
 
-        getMockEndpoint("mock:{{cool.result}}").expectedMessageCount(1);
+        getMockEndpoint("mock:extra").expectedMessageCount(1);
+        getMockEndpoint("mock:hey").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedMessageCount(0);
 
-        template.sendBody("{{cool.start}}", "Hello World");
+        template.sendBody("direct:start", "Hello World");
 
         assertMockEndpointsSatisfied();
     }
 
-    public void testPropertiesComponentMandatory() throws Exception {
-        context.removeComponent("properties");
-        try {
-            context.addRoutes(new RouteBuilder() {
-                @Override
-                public void configure() throws Exception {
-                    from("{{cool.start}}")
-                        .to("log:{{cool.start}}?showBodyType=false&showExchangeId={{cool.showid}}")
-                        .to("mock:{{cool.result}}");
-                }
-            });
-            context.start();
-            fail("Should throw exception");
-        } catch (FailedToCreateRouteException e) {
-            ResolveEndpointFailedException cause = assertIsInstanceOf(ResolveEndpointFailedException.class,
e.getCause());
-            IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class,
cause.getCause());
-            assertEquals("PropertiesComponent with name properties must be defined in CamelContext
to support property placeholders.", iae.getMessage());
-        }
-    }
-
     @Override
     protected CamelContext createCamelContext() throws Exception {
         CamelContext context = super.createCamelContext();
@@ -94,6 +60,11 @@ public class PropertiesComponentEndpoint
         pc.setLocation("classpath:org/apache/camel/component/properties/myproperties.properties");
         context.addComponent("properties", pc);
 
+        Properties extra = new Properties();
+        extra.put("cool.result", "extra");
+        extra.put("hey", "mock:hey");
+        pc.setOverrideProperties(extra);
+
         return context;
     }
 

Modified: camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesParser.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesParser.java?rev=1348394&r1=1348393&r2=1348394&view=diff
==============================================================================
--- camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesParser.java
(original)
+++ camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesParser.java
Sat Jun  9 12:57:10 2012
@@ -26,6 +26,7 @@ import java.util.Set;
 import org.apache.aries.blueprint.ExtendedBeanMetadata;
 import org.apache.aries.blueprint.ext.AbstractPropertyPlaceholder;
 import org.apache.camel.component.properties.DefaultPropertiesParser;
+import org.apache.camel.component.properties.PropertiesComponent;
 import org.apache.camel.component.properties.PropertiesParser;
 import org.apache.camel.util.ObjectHelper;
 import org.osgi.service.blueprint.container.BlueprintContainer;
@@ -41,12 +42,14 @@ import org.osgi.service.blueprint.reflec
  */
 public class BlueprintPropertiesParser extends DefaultPropertiesParser {
 
+    private final PropertiesComponent propertiesComponent;
     private final BlueprintContainer container;
-    private PropertiesParser delegate;
+    private final PropertiesParser delegate;
     private final Set<AbstractPropertyPlaceholder> placeholders = new LinkedHashSet<AbstractPropertyPlaceholder>();
     private Method method;
 
-    public BlueprintPropertiesParser(BlueprintContainer container, PropertiesParser delegate)
{
+    public BlueprintPropertiesParser(PropertiesComponent propertiesComponent, BlueprintContainer
container, PropertiesParser delegate) {
+        this.propertiesComponent = propertiesComponent;
         this.container = container;
         this.delegate = delegate;
     }
@@ -104,24 +107,34 @@ public class BlueprintPropertiesParser e
     public String parseProperty(String key, String value, Properties properties) {
         log.trace("Parsing property key: {} with value: {}", key, value);
 
+        String answer = null;
+
+        // prefer any override properties
+        // this logic is special for BlueprintPropertiesParser as we otherwise prefer
+        // to use the AbstractPropertyPlaceholder from OSGi blueprint config admins
+        // service to lookup otherwise
+        if (key != null && propertiesComponent.getOverrideProperties() != null) {
+            answer = (String) propertiesComponent.getOverrideProperties().get(key);
+        }
+
         // lookup key in blueprint and return its value
-        if (key != null) {
+        if (answer == null && key != null) {
             for (AbstractPropertyPlaceholder placeholder : placeholders) {
-                value = (String) ObjectHelper.invokeMethod(method, placeholder, key);
-                if (value != null) {
-                    log.debug("Blueprint parsed property key: {} as value: {}", key, value);
+                answer = (String) ObjectHelper.invokeMethod(method, placeholder, key);
+                if (answer != null) {
+                    log.debug("Blueprint parsed property key: {} as value: {}", key, answer);
                     break;
                 }
             }
         }
 
-        if (value == null && delegate != null) {
+        if (answer == null && delegate != null) {
             // let delegate have a try since blueprint didn't resolve it
-            value = delegate.parseProperty(key, value, properties);
+            answer = delegate.parseProperty(key, value, properties);
         }
 
-        log.trace("Returning parsed property key: {} as value: {}", key, value);
-        return value;
+        log.trace("Returning parsed property key: {} as value: {}", key, answer);
+        return answer;
     }
 
 }

Modified: camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java?rev=1348394&r1=1348393&r2=1348394&view=diff
==============================================================================
--- camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
(original)
+++ camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
Sat Jun  9 12:57:10 2012
@@ -19,6 +19,7 @@ package org.apache.camel.blueprint;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.Properties;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAttribute;
@@ -56,6 +57,7 @@ import org.apache.camel.model.dataformat
 import org.apache.camel.spi.PackageScanFilter;
 import org.apache.camel.spi.Registry;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
 import org.osgi.service.blueprint.container.BlueprintContainer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -212,9 +214,18 @@ public class CamelContextFactoryBean ext
             // lookup existing configured properties component
             PropertiesComponent pc = getContext().getComponent("properties", PropertiesComponent.class);
 
-            BlueprintPropertiesParser parser = new BlueprintPropertiesParser(blueprintContainer,
pc.getPropertiesParser());
+            BlueprintPropertiesParser parser = new BlueprintPropertiesParser(pc, blueprintContainer,
pc.getPropertiesParser());
             BlueprintPropertiesResolver resolver = new BlueprintPropertiesResolver(pc.getPropertiesResolver(),
parser);
 
+            // any extra properties
+            ServiceReference ref = bundleContext.getServiceReference(PropertiesComponent.OVERRIDE_PROPERTIES);
+            if (ref != null) {
+                Properties extra = (Properties) bundleContext.getService(ref);
+                if (extra != null) {
+                    pc.setOverrideProperties(extra);
+                }
+            }
+
             // no locations has been set, so its a default component
             if (pc.getLocations() == null) {
                 StringBuilder sb = new StringBuilder();

Modified: camel/trunk/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java?rev=1348394&r1=1348393&r2=1348394&view=diff
==============================================================================
--- camel/trunk/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java
(original)
+++ camel/trunk/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java
Sat Jun  9 12:57:10 2012
@@ -16,7 +16,10 @@
  */
 package org.apache.camel.test.blueprint;
 
+import java.util.Properties;
+
 import org.apache.camel.CamelContext;
+import org.apache.camel.component.properties.PropertiesComponent;
 import org.apache.camel.model.ModelCamelContext;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.After;
@@ -37,6 +40,13 @@ public abstract class CamelBlueprintTest
         String symbolicName = getClass().getSimpleName();
         this.bundleContext = CamelBlueprintHelper.createBundleContext(symbolicName, getBlueprintDescriptor(),
                 getBundleFilter(), getBundleVersion(), true);
+
+        // must register override properties early in OSGi containers
+        Properties extra = useOverridePropertiesWithPropertiesComponent();
+        if (extra != null) {
+            bundleContext.registerService(PropertiesComponent.OVERRIDE_PROPERTIES, extra,
null);
+        }
+
         super.setUp();
 
         // must wait for blueprint container to be published then the namespace parser is
complete and we are ready for testing

Copied: camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminOverridePropertiesTest.java
(from r1348324, camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminOverridePropertiesTest.java?p2=camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminOverridePropertiesTest.java&p1=camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminTest.java&r1=1348324&r2=1348394&rev=1348394&view=diff
==============================================================================
--- camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminTest.java
(original)
+++ camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminOverridePropertiesTest.java
Sat Jun  9 12:57:10 2012
@@ -16,21 +16,35 @@
  */
 package org.apache.camel.test.blueprint;
 
+import java.util.Properties;
+
 import org.junit.Test;
 
 /**
  *
  */
-public class ConfigAdminTest extends CamelBlueprintTestSupport {
+public class ConfigAdminOverridePropertiesTest extends CamelBlueprintTestSupport {
 
     @Override
     protected String getBlueprintDescriptor() {
         return "org/apache/camel/test/blueprint/configadmin.xml";
     }
 
+    // START SNIPPET: e1
+    // override this method to provide our custom properties we use in this unit test
+    @Override
+    protected Properties useOverridePropertiesWithPropertiesComponent() {
+        Properties extra = new Properties();
+        extra.put("destination", "mock:extra");
+        extra.put("greeting", "Bye");
+        return extra;
+    }
+    // END SNIPPET: e1
+
     @Test
     public void testConfigAdmin() throws Exception {
-        getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
+        getMockEndpoint("mock:extra").expectedBodiesReceived("Bye World");
+        getMockEndpoint("mock:result").expectedMessageCount(0);
 
         template.sendBody("direct:start", "World");
 

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=1348394&r1=1348393&r2=1348394&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
Sat Jun  9 12:57:10 2012
@@ -36,6 +36,7 @@ import org.apache.camel.ProducerTemplate
 import org.apache.camel.Service;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.properties.PropertiesComponent;
 import org.apache.camel.impl.BreakpointSupport;
 import org.apache.camel.impl.DefaultCamelBeanPostProcessor;
 import org.apache.camel.impl.DefaultCamelContext;
@@ -276,6 +277,18 @@ public abstract class CamelTestSupport e
             context.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(pattern,
true));
         }
 
+        // configure properties component
+        Properties extra = useOverridePropertiesWithPropertiesComponent();
+        if (extra != null && !extra.isEmpty()) {
+            PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+            pc.setOverrideProperties(extra);
+        }
+        Boolean ignore = ignoreMissingLocationWithPropertiesComponent();
+        if (ignore != null) {
+            PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+            pc.setIgnoreMissingLocation(ignore);
+        }
+
         postProcessTest();
         
         if (isUseRouteBuilder()) {
@@ -359,6 +372,28 @@ public abstract class CamelTestSupport e
         return false;
     }
 
+    /**
+     * Override this method to include and override properties
+     * with the Camel {@link PropertiesComponent}.
+     *
+     * @return additional properties to add/override.
+     */
+    protected Properties useOverridePropertiesWithPropertiesComponent() {
+        return null;
+    }
+
+    /**
+     * Whether to ignore missing locations with the {@link PropertiesComponent}.
+     * For example when unit testing you may want to ignore locations that are
+     * not available in the environment you use for testing.
+     *
+     * @return <tt>true</tt> to ignore, <tt>false</tt> to not ignore,
and <tt>null</tt> to leave as configured
+     * on the {@link PropertiesComponent}
+     */
+    protected Boolean ignoreMissingLocationWithPropertiesComponent() {
+        return null;
+    }
+
     protected void postProcessTest() throws Exception {
         context = threadCamelContext.get();
         template = threadTemplate.get();
@@ -374,40 +409,34 @@ public abstract class CamelTestSupport e
         doStopCamelContext(context, camelContextService);
     }
 
-    private static void doStopCamelContext(CamelContext context,
-                                           Service camelContextService) throws Exception
{
+    private static void doStopCamelContext(CamelContext context, Service camelContextService)
throws Exception {
         if (camelContextService != null) {
             if (camelContextService == threadService.get()) {
                 threadService.remove();
             }
             camelContextService.stop();
-            camelContextService = null;
         } else {
             if (context != null) {
                 if (context == threadCamelContext.get()) {
                     threadCamelContext.remove();
                 }
                 context.stop();
-                context = null;
             }
         }
     }
 
-    private static void doStopTemplates(ConsumerTemplate consumer,
-                                        ProducerTemplate template) throws Exception {
+    private static void doStopTemplates(ConsumerTemplate consumer, ProducerTemplate template)
throws Exception {
         if (consumer != null) {
             if (consumer == threadConsumer.get()) {
                 threadConsumer.remove();
             }
             consumer.stop();
-            consumer = null;
         }
         if (template != null) {
             if (template == threadTemplate.get()) {
                 threadTemplate.remove();
             }
             template.stop();
-            template = null;
         }
     }
 



Mime
View raw message