-
Notifications
You must be signed in to change notification settings - Fork 5.1k
CAMEL-24301: Fix RouteService NPE when startup-failure exception carries no message #25205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mayurbm
wants to merge
4
commits into
apache:main
Choose a base branch
from
mayurbm:fix/route-service-null-message-on-startup-failure
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+287
−2
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d489daf
Fix RouteService to avoid NullPointerException when startup failure h…
mayurmohan eb06378
Apply formatting and code-style fixes to RouteService fix
mayurmohan 3b0d444
Fix test to exercise RouteService.setUp() path, not consumer start
mayurmohan 16a1e50
Fix cause-chain test to use NPE with initCause instead of RuntimeExce…
mayurmohan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
264 changes: 264 additions & 0 deletions
264
...el-core/src/test/java/org/apache/camel/impl/engine/RouteServiceWarmUpNullMessageTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,264 @@ | ||
| /* | ||
| * 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.impl.engine; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.apache.camel.CamelContext; | ||
| import org.apache.camel.Consumer; | ||
| import org.apache.camel.Endpoint; | ||
| import org.apache.camel.Exchange; | ||
| import org.apache.camel.FailedToStartRouteException; | ||
| import org.apache.camel.Processor; | ||
| import org.apache.camel.Producer; | ||
| import org.apache.camel.builder.RouteBuilder; | ||
| import org.apache.camel.impl.DefaultCamelContext; | ||
| import org.apache.camel.support.DefaultComponent; | ||
| import org.apache.camel.support.DefaultConsumer; | ||
| import org.apache.camel.support.DefaultEndpoint; | ||
| import org.apache.camel.support.DefaultProducer; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| /** | ||
| * Verifies that {@link RouteService#warmUp()} and {@link RouteService#setUp()} wrap startup failures in a | ||
| * {@link FailedToStartRouteException} whose message is always meaningful — even when the root cause exception carries a | ||
| * {@code null} message (e.g. a bare {@link NullPointerException}). | ||
| * | ||
| * <p> | ||
| * Before the fix, {@code RouteService} passed {@code e.getLocalizedMessage()} directly to the | ||
| * {@link FailedToStartRouteException} constructor, which calls {@code Objects.requireNonNull} on that argument. A | ||
| * message-less exception therefore caused a secondary {@link NullPointerException} to be thrown from inside the | ||
| * exception constructor rather than a proper {@link FailedToStartRouteException}. | ||
| * | ||
| * <p> | ||
| * The tests trigger the failure during endpoint initialisation (inside {@code doSetup()}), which is the code path | ||
| * covered by the {@code RouteService} fix. | ||
| */ | ||
| class RouteServiceWarmUpNullMessageTest { | ||
|
|
||
| /** | ||
| * When the endpoint throws a message-less {@link NullPointerException} during route setup, the result must be a | ||
| * {@link FailedToStartRouteException}, not a raw NPE. | ||
| */ | ||
| @Test | ||
| void testSetUpNullMessageExceptionProducesFailedToStartRouteException() { | ||
| CamelContext context = new DefaultCamelContext(); | ||
| context.addComponent("fail", new NullMessageFailComponent()); | ||
|
|
||
| assertThatThrownBy(() -> { | ||
| context.addRoutes(new RouteBuilder() { | ||
| @Override | ||
| public void configure() { | ||
| from("fail:trigger").routeId("test-route").to("direct:out"); | ||
| } | ||
| }); | ||
| context.start(); | ||
| }).isInstanceOf(FailedToStartRouteException.class); | ||
| } | ||
|
|
||
| /** | ||
| * The {@link FailedToStartRouteException} message must contain the route id and must not use the literal string | ||
| * "null" as the failure description. | ||
| */ | ||
| @Test | ||
| void testFailedToStartMessageIsNonNullAndMeaningful() { | ||
| CamelContext context = new DefaultCamelContext(); | ||
| context.addComponent("fail", new NullMessageFailComponent()); | ||
|
|
||
| FailedToStartRouteException caught = null; | ||
| try { | ||
| context.addRoutes(new RouteBuilder() { | ||
| @Override | ||
| public void configure() { | ||
| from("fail:trigger").routeId("meaningful-route").to("direct:out"); | ||
| } | ||
| }); | ||
| context.start(); | ||
| } catch (FailedToStartRouteException e) { | ||
| caught = e; | ||
| } catch (Exception e) { | ||
| Throwable t = e; | ||
| while (t != null) { | ||
| if (t instanceof FailedToStartRouteException ftsre) { | ||
| caught = ftsre; | ||
| break; | ||
| } | ||
| t = t.getCause(); | ||
| } | ||
| } finally { | ||
| try { | ||
| context.stop(); | ||
| } catch (Exception ignored) { | ||
| } | ||
| } | ||
|
|
||
| assertThat(caught).as("Expected FailedToStartRouteException").isNotNull(); | ||
| assertThat(caught.getMessage()) | ||
| .as("FailedToStartRouteException message must not be null") | ||
| .isNotNull() | ||
| .as("Message must contain the route id") | ||
| .contains("meaningful-route") | ||
| .as("Message must not contain 'because: null'") | ||
| .doesNotContain("because: null"); | ||
| } | ||
|
|
||
| /** | ||
| * When the endpoint throws a message-less outer exception wrapping an inner exception that has a message, the inner | ||
| * message must be surfaced in the {@link FailedToStartRouteException}. | ||
| */ | ||
| @Test | ||
| void testSetUpWalksCauseChainForMessage() { | ||
| CamelContext context = new DefaultCamelContext(); | ||
| String expectedFragment = "real cause message from chain"; | ||
| context.addComponent("fail", new ChainedNullMessageFailComponent(expectedFragment)); | ||
|
|
||
| FailedToStartRouteException caught = null; | ||
| try { | ||
| context.addRoutes(new RouteBuilder() { | ||
| @Override | ||
| public void configure() { | ||
| from("fail:trigger").routeId("chain-route").to("direct:out"); | ||
| } | ||
| }); | ||
| context.start(); | ||
| } catch (FailedToStartRouteException e) { | ||
| caught = e; | ||
| } catch (Exception e) { | ||
| Throwable t = e; | ||
| while (t != null) { | ||
| if (t instanceof FailedToStartRouteException ftsre) { | ||
| caught = ftsre; | ||
| break; | ||
| } | ||
| t = t.getCause(); | ||
| } | ||
| } finally { | ||
| try { | ||
| context.stop(); | ||
| } catch (Exception ignored) { | ||
| } | ||
| } | ||
|
|
||
| assertThat(caught).as("Expected FailedToStartRouteException").isNotNull(); | ||
| assertThat(caught.getMessage()) | ||
| .as("Message should surface cause chain message") | ||
| .contains(expectedFragment); | ||
| } | ||
|
|
||
| // ---- helpers ---- | ||
|
|
||
| /** | ||
| * A component whose endpoint throws a message-less {@link NullPointerException} during its own {@code doStart()} — | ||
| * which is invoked by {@code RouteService.doSetup()} via {@code ServiceHelper.initService(endpoint)}, exercising | ||
| * the {@code setUp()} fix. | ||
| */ | ||
| private static class NullMessageFailComponent extends DefaultComponent { | ||
| @Override | ||
| protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) { | ||
| return new NullMessageFailEndpoint(uri, this); | ||
| } | ||
| } | ||
|
|
||
| private static class NullMessageFailEndpoint extends DefaultEndpoint { | ||
| NullMessageFailEndpoint(String uri, NullMessageFailComponent component) { | ||
| super(uri, component); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doStart() { | ||
| throw new NullPointerException(); | ||
| } | ||
|
|
||
| @Override | ||
| public Consumer createConsumer(Processor processor) { | ||
| return new DefaultConsumer(this, processor) { | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public Producer createProducer() { | ||
| return new DefaultProducer(this) { | ||
| @Override | ||
| public void process(Exchange exchange) { | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isSingleton() { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A component whose endpoint throws a message-less outer exception wrapping an inner exception that does have a | ||
| * message — used to test cause-chain walking in {@code extractUsefulMessage}. | ||
| */ | ||
| private static class ChainedNullMessageFailComponent extends DefaultComponent { | ||
| private final String causeMessage; | ||
|
|
||
| ChainedNullMessageFailComponent(String causeMessage) { | ||
| this.causeMessage = causeMessage; | ||
| } | ||
|
|
||
| @Override | ||
| protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) { | ||
| return new ChainedNullMessageFailEndpoint(uri, this, causeMessage); | ||
| } | ||
| } | ||
|
|
||
| private static class ChainedNullMessageFailEndpoint extends DefaultEndpoint { | ||
| private final String causeMessage; | ||
|
|
||
| ChainedNullMessageFailEndpoint(String uri, ChainedNullMessageFailComponent component, String causeMessage) { | ||
| super(uri, component); | ||
| this.causeMessage = causeMessage; | ||
| } | ||
|
|
||
| @Override | ||
| protected void doStart() { | ||
| // Outer NPE has no message; initCause sets the cause without supplying a message to | ||
| // the outer exception — forces extractUsefulMessage to walk the chain to find causeMessage. | ||
| NullPointerException outer = new NullPointerException(); | ||
| outer.initCause(new IllegalStateException(causeMessage)); | ||
| throw outer; | ||
| } | ||
|
|
||
| @Override | ||
| public Consumer createConsumer(Processor processor) { | ||
| return new DefaultConsumer(this, processor) { | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public Producer createProducer() { | ||
| return new DefaultProducer(this) { | ||
| @Override | ||
| public void process(Exchange exchange) { | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isSingleton() { | ||
| return true; | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.