You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/11/14 07:01:20 UTC

(camel) 01/02: CAMEL-20109: Remove endpoint did not work if URI has # sign. Thanks to Peter Nowak for unit test.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch remove-endpoint
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 64c82aac7b65143bcdee785dcee16f78609006a4
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Nov 14 07:28:23 2023 +0100

    CAMEL-20109: Remove endpoint did not work if URI has # sign. Thanks to Peter Nowak for unit test.
---
 .../component/redis/RedisRemoveEndpointTest.java   | 50 ++++++++++++++++
 .../component/redis/RedisRemoveRouteTest.java      | 67 ++++++++++++++++++++++
 .../camel/impl/engine/AbstractCamelContext.java    | 14 +++--
 3 files changed, 127 insertions(+), 4 deletions(-)

diff --git a/components/camel-spring-redis/src/test/java/org/apache/camel/component/redis/RedisRemoveEndpointTest.java b/components/camel-spring-redis/src/test/java/org/apache/camel/component/redis/RedisRemoveEndpointTest.java
new file mode 100644
index 00000000000..79b0414c987
--- /dev/null
+++ b/components/camel-spring-redis/src/test/java/org/apache/camel/component/redis/RedisRemoveEndpointTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.component.redis;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.spi.Registry;
+import org.apache.camel.support.SimpleRegistry;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.springframework.data.redis.core.RedisTemplate;
+
+public class RedisRemoveEndpointTest extends CamelTestSupport {
+
+    @Override
+    protected Registry createCamelRegistry() throws Exception {
+        Registry registry = new SimpleRegistry();
+        redisTemplate = new RedisTemplate<>();
+        registry.bind("redisTemplate", redisTemplate);
+        return registry;
+    }
+
+    @Mock
+    protected RedisTemplate<String, String> redisTemplate;
+
+    @Test
+    public void testRemoveEndpointWithHashtag() throws Exception {
+        Endpoint e = context.getEndpoint("spring-redis://localhost:6379?redisTemplate=#redisTemplate");
+        context.getEndpoint("direct:someotherendpoint");
+
+        Assertions.assertEquals(2, context.getEndpoints().size());
+        context.removeEndpoint(e);
+        Assertions.assertEquals(1, context.getEndpoints().size());
+    }
+}
diff --git a/components/camel-spring-redis/src/test/java/org/apache/camel/component/redis/RedisRemoveRouteTest.java b/components/camel-spring-redis/src/test/java/org/apache/camel/component/redis/RedisRemoveRouteTest.java
new file mode 100644
index 00000000000..ccc4d417aec
--- /dev/null
+++ b/components/camel-spring-redis/src/test/java/org/apache/camel/component/redis/RedisRemoveRouteTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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.component.redis;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.Registry;
+import org.apache.camel.support.SimpleRegistry;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.springframework.data.redis.core.RedisTemplate;
+
+public class RedisRemoveRouteTest extends CamelTestSupport {
+
+    @Override
+    protected Registry createCamelRegistry() throws Exception {
+        Registry registry = new SimpleRegistry();
+        redisTemplate = new RedisTemplate<>();
+
+        registry.bind("redisTemplate", redisTemplate);
+        return registry;
+    }
+
+    @Mock
+    protected RedisTemplate<String, String> redisTemplate;
+
+    private static final String ROUTE_ID = "spring-redis-test-route";
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:start")
+                        .routeId(ROUTE_ID)
+                        .to("spring-redis://localhost:6379?redisTemplate=#redisTemplate");
+            }
+        };
+    }
+
+    @Test
+    public void testRemoveRouteShouldRemoveAllEndpoints() throws Exception {
+        // two endpoints available after starting the route
+        Assertions.assertEquals(2, context.getEndpoints().size());
+
+        // stop it before trying to remove it, otherwise it won't get removed
+        context.getRouteController().stopRoute(ROUTE_ID);
+        context.removeRoute(ROUTE_ID);
+
+        // after the route ahs been removed all endpoints should be removed
+        Assertions.assertEquals(0, context.getEndpoints().size());
+    }
+}
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 2388a943465..2d2726ea1d2 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -665,11 +665,17 @@ public abstract class AbstractCamelContext extends BaseService
 
     @Override
     public void removeEndpoint(Endpoint endpoint) throws Exception {
-        // optimize as uri on endpoint is already normalized
-        String uri = endpoint.getEndpointUri();
-        NormalizedUri key = NormalizedUri.newNormalizedUri(uri, true);
-        Endpoint oldEndpoint = endpoints.remove(key);
+        Endpoint oldEndpoint = null;
+        NormalizedUri oldKey = null;
+        for (Map.Entry<NormalizedUri, Endpoint> entry : endpoints.entrySet()) {
+            if (endpoint == entry.getValue()) {
+                oldKey = entry.getKey();
+                oldEndpoint = endpoint;
+                break;
+            }
+        }
         if (oldEndpoint != null) {
+            endpoints.remove(oldKey);
             try {
                 stopServices(oldEndpoint);
             } catch (Exception e) {