Repository: camel
Updated Branches:
  refs/heads/camel-2.19.x f96c58232 -> 6e7b70933
  refs/heads/master a724619a0 -> 46f429b3b


CAMEL-11305: camel-test - Using dump route coverage with custom processor may 
cause NPE


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

Branch: refs/heads/master
Commit: 46f429b3b11916eb6b4cb1898beb33174dbb76c6
Parents: a724619
Author: Claus Ibsen <davscl...@apache.org>
Authored: Sun May 21 18:56:48 2017 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Sun May 21 18:56:48 2017 +0200

----------------------------------------------------------------------
 .../apache/camel/impl/DefaultCamelContext.java  |  3 +-
 .../management/ManagedInlinedProcessorTest.java | 71 ++++++++++++++++++++
 .../camel/test/junit4/CamelTestSupport.java     | 42 ++++++------
 .../RouteProcessorDumpRouteCoverageTest.java    | 56 +++++++++++++++
 4 files changed, 151 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/46f429b3/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java 
b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
index 65c181d..13cf55b 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
@@ -878,7 +878,8 @@ public class DefaultCamelContext extends ServiceSupport 
implements ModelCamelCon
         Processor processor = getProcessor(id);
         ProcessorDefinition def = getProcessorDefinition(id);
 
-        if (processor != null && def != null) {
+        // processor may be null if its anonymous inner class or as lambda
+        if (def != null) {
             try {
                 ObjectName on = 
getManagementStrategy().getManagementNamingStrategy().getObjectNameForProcessor(this,
 processor, def);
                 return 
getManagementStrategy().getManagementAgent().newProxyClient(on, type);

http://git-wip-us.apache.org/repos/asf/camel/blob/46f429b3/camel-core/src/test/java/org/apache/camel/management/ManagedInlinedProcessorTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/management/ManagedInlinedProcessorTest.java
 
b/camel-core/src/test/java/org/apache/camel/management/ManagedInlinedProcessorTest.java
new file mode 100644
index 0000000..9604fe5
--- /dev/null
+++ 
b/camel-core/src/test/java/org/apache/camel/management/ManagedInlinedProcessorTest.java
@@ -0,0 +1,71 @@
+/**
+ * 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.management;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.api.management.mbean.ManagedProcessorMBean;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * @version 
+ */
+public class ManagedInlinedProcessorTest extends ManagementTestSupport {
+
+    public void testManageInlinedProcessor() throws Exception {
+        // JMX tests dont work well on AIX CI servers (hangs them)
+        if (isPlatform("aix")) {
+            return;
+        }
+
+        MBeanServer mbeanServer = getMBeanServer();
+        ObjectName on = 
ObjectName.getInstance("org.apache.camel:context=camel-1,type=processors,name=\"custom\"");
+
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+        template.sendBody("direct:start", "Hello World");
+        assertMockEndpointsSatisfied();
+
+        Long counter = (Long) mbeanServer.getAttribute(on, 
"ExchangesCompleted");
+        assertEquals(1L, counter.longValue());
+
+        ManagedProcessorMBean mb = context.getManagedProcessor("custom", 
ManagedProcessorMBean.class);
+        assertNotNull(mb);
+        assertEquals(1L, mb.getExchangesCompleted());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").routeId("foo")
+                    .process(new Processor() {
+                        @Override
+                        public void process(Exchange exchange) throws 
Exception {
+                            exchange.getOut().setBody("Bye World");
+                        }
+                    }).id("custom")
+                    .to("mock:result");
+            }
+        };
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/46f429b3/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
----------------------------------------------------------------------
diff --git 
a/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
 
b/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
index ed39ecb..632b8b0 100644
--- 
a/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
+++ 
b/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
@@ -23,6 +23,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.List;
@@ -460,11 +461,14 @@ public abstract class CamelTestSupport extends 
TestSupport {
             routesSummary.append("\t\tRoute ").append(route.getId()).append(" 
total: ").append(managedRoute.getExchangesTotal()).append(" 
(").append(routeCoveragePercentage).append("%)\n");
 
             if (server != null) {
-                for (ManagedProcessorMBean managedProcessor : 
processorsForRoute.get(route.getId())) {
-                    String processorId = managedProcessor.getProcessorId();
-                    long processorExchangesTotal = 
managedProcessor.getExchangesTotal();
-                    long processorCoveragePercentage = Math.round((double) 
processorExchangesTotal / contextExchangesTotal * 100);
-                    routesSummary.append("\t\t\tProcessor 
").append(processorId).append(" total: 
").append(processorExchangesTotal).append(" 
(").append(processorCoveragePercentage).append("%)\n");
+                List<ManagedProcessorMBean> processors = 
processorsForRoute.get(route.getId());
+                if (processors != null) {
+                    for (ManagedProcessorMBean managedProcessor : processors) {
+                        String processorId = managedProcessor.getProcessorId();
+                        long processorExchangesTotal = 
managedProcessor.getExchangesTotal();
+                        long processorCoveragePercentage = Math.round((double) 
processorExchangesTotal / contextExchangesTotal * 100);
+                        routesSummary.append("\t\t\tProcessor 
").append(processorId).append(" total: 
").append(processorExchangesTotal).append(" 
(").append(processorCoveragePercentage).append("%)\n");
+                    }
                 }
             }
         }
@@ -496,30 +500,28 @@ public abstract class CamelTestSupport extends 
TestSupport {
         ObjectName processorsObjectName = new ObjectName(domain + ":context=" 
+ context.getManagementName() + ",type=processors,name=*");
         Set<ObjectName> objectNames = server.queryNames(processorsObjectName, 
null);
 
-        if (server != null) {
-            for (ObjectName objectName : objectNames) {
-                String routeId = server.getAttribute(objectName, 
"RouteId").toString();
-                String name = objectName.getKeyProperty("name");
-                name = ObjectName.unquote(name);
+        for (ObjectName objectName : objectNames) {
+            String routeId = server.getAttribute(objectName, 
"RouteId").toString();
+            String name = objectName.getKeyProperty("name");
+            name = ObjectName.unquote(name);
 
-                ManagedProcessorMBean managedProcessor = 
context.getManagedProcessor(name, ManagedProcessorMBean.class);
+            ManagedProcessorMBean managedProcessor = 
context.getManagedProcessor(name, ManagedProcessorMBean.class);
 
-                if (managedProcessor != null) {
-                    if (processorsForRoute.get(routeId) == null) {
-                        List<ManagedProcessorMBean> processorsList = new 
ArrayList<>();
-                        processorsList.add(managedProcessor);
+            if (managedProcessor != null) {
+                if (processorsForRoute.get(routeId) == null) {
+                    List<ManagedProcessorMBean> processorsList = new 
ArrayList<>();
+                    processorsList.add(managedProcessor);
 
-                        processorsForRoute.put(routeId, processorsList);
-                    } else {
-                        processorsForRoute.get(routeId).add(managedProcessor);
-                    }
+                    processorsForRoute.put(routeId, processorsList);
+                } else {
+                    processorsForRoute.get(routeId).add(managedProcessor);
                 }
             }
         }
 
         // sort processors by position in route definition
         for (Map.Entry<String, List<ManagedProcessorMBean>> entry : 
processorsForRoute.entrySet()) {
-            Collections.sort(entry.getValue(), (o1, o2) -> 
o1.getIndex().compareTo(o2.getIndex()));
+            Collections.sort(entry.getValue(), 
Comparator.comparing(ManagedProcessorMBean::getIndex));
         }
 
         return processorsForRoute;

http://git-wip-us.apache.org/repos/asf/camel/blob/46f429b3/components/camel-test/src/test/java/org/apache/camel/test/patterns/RouteProcessorDumpRouteCoverageTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-test/src/test/java/org/apache/camel/test/patterns/RouteProcessorDumpRouteCoverageTest.java
 
b/components/camel-test/src/test/java/org/apache/camel/test/patterns/RouteProcessorDumpRouteCoverageTest.java
new file mode 100644
index 0000000..9ad0b84
--- /dev/null
+++ 
b/components/camel-test/src/test/java/org/apache/camel/test/patterns/RouteProcessorDumpRouteCoverageTest.java
@@ -0,0 +1,56 @@
+/**
+ * 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.patterns;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class RouteProcessorDumpRouteCoverageTest extends CamelTestSupport {
+
+    @Override
+    public boolean isDumpRouteCoverage() {
+        return true;
+    }
+
+    @Test
+    public void testProcessor() throws Exception {
+        String out = template.requestBody("direct:start", "Hello World", 
String.class);
+        assertEquals("Bye World", out);
+    }
+
+    @Override
+    public void tearDown() throws Exception {
+        super.tearDown();
+
+        // should create that file when test is done
+        
assertFileExists("target/camel-route-coverage/RouteProcessorDumpRouteCoverageTest-testProcessor.xml");
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .process(exchange -> exchange.getOut().setBody("Bye 
World"));
+            }
+        };
+    }
+
+}

Reply via email to