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 2025/01/29 16:00:48 UTC

(camel) branch main updated: CAMEL-21661: Added unit test

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 27c87ae2fdc CAMEL-21661: Added unit test
27c87ae2fdc is described below

commit 27c87ae2fdc10b85c54ea7c81e9477faf2639492
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Jan 29 17:00:32 2025 +0100

    CAMEL-21661: Added unit test
---
 .../processor/CustomRoutePolicyDirectTest.java     | 105 +++++++++++++++++++++
 1 file changed, 105 insertions(+)

diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/CustomRoutePolicyDirectTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/CustomRoutePolicyDirectTest.java
new file mode 100644
index 00000000000..e03f39c7f13
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/CustomRoutePolicyDirectTest.java
@@ -0,0 +1,105 @@
+/*
+ * 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.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Route;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.support.RoutePolicySupport;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class CustomRoutePolicyDirectTest extends ContextTestSupport {
+
+    private final MyCustomRoutePolicy policy = new MyCustomRoutePolicy();
+
+    private static class MyCustomRoutePolicy extends RoutePolicySupport {
+
+        private volatile int inflight1;
+        private volatile int inflight2;
+
+        @Override
+        public void onExchangeBegin(Route route, Exchange exchange) {
+            if ("foo".equals(route.getId())) {
+                inflight1++;
+            } else {
+                inflight2++;
+            }
+        }
+
+        @Override
+        public void onExchangeDone(Route route, Exchange exchange) {
+            if ("foo".equals(route.getId())) {
+                inflight1--;
+            } else {
+                inflight2--;
+            }
+        }
+
+        public int getInflight1() {
+            return inflight1;
+        }
+
+        public int getInflight2() {
+            return inflight2;
+        }
+    }
+
+    @Test
+    public void testCustomPolicy() throws Exception {
+        assertEquals(0, policy.getInflight1());
+        assertEquals(0, policy.getInflight2());
+
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+        template.sendBody("direct:foo", "Hello World");
+        assertMockEndpointsSatisfied();
+
+        assertEquals(0, policy.getInflight1());
+        assertEquals(0, policy.getInflight2());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:foo").routeId("foo").routePolicy(policy)
+                        .process(e -> {
+                            Assertions.assertEquals(1, policy.getInflight1());
+                            Assertions.assertEquals(0, policy.getInflight2());
+                        })
+                        .to("direct:bar")
+                        .process(e -> {
+                            Assertions.assertEquals(1, policy.getInflight1());
+                            Assertions.assertEquals(0, policy.getInflight2());
+                        })
+                        .to("mock:result");
+
+                from("direct:bar").routeId("bar").routePolicy(policy)
+                        .process(e -> {
+                            Assertions.assertEquals(1, policy.getInflight1());
+                            Assertions.assertEquals(1, policy.getInflight2());
+                        })
+                        .to("mock:b");
+
+            }
+        };
+    }
+}