Skip to content

Commit aed48e7

Browse files
authored
CAMEL-23214: Migrate AvailablePortFinder in miscellaneous component tests
Migrate from deprecated getNextAvailable() to find() across 69 test files to eliminate TOCTOU port races.
1 parent 32acb7d commit aed48e7

72 files changed

Lines changed: 429 additions & 310 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketCamelRouterTestSupport.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
import org.apache.camel.test.junit6.CamelTestSupport;
2424
import org.junit.jupiter.api.AfterEach;
2525
import org.junit.jupiter.api.BeforeEach;
26+
import org.junit.jupiter.api.extension.RegisterExtension;
2627

2728
public abstract class WebsocketCamelRouterTestSupport extends CamelTestSupport {
28-
protected static final int PORT = AvailablePortFinder.getNextAvailable();
29+
@RegisterExtension
30+
protected static AvailablePortFinder.Port PORT = AvailablePortFinder.find();
2931

3032
// This test needs to run with its own lifecycle management, so we cannot use extensions
3133
private JettyEmbeddedService service;
@@ -34,7 +36,7 @@ public abstract class WebsocketCamelRouterTestSupport extends CamelTestSupport {
3436
void setupJetty() {
3537
final JettyConfiguration jettyConfiguration = JettyConfigurationBuilder
3638
.emptyTemplate()
37-
.withPort(PORT)
39+
.withPort(PORT.getPort())
3840
.withContextPath(JettyConfiguration.ROOT_CONTEXT_PATH)
3941
.withWebSocketConfiguration()
4042
.addServletConfiguration(new JettyConfiguration.WebSocketContextHandlerConfiguration.ServletConfiguration<>(

components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketCamelRouterWithInitParamTestSupport.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
import org.apache.camel.test.junit6.CamelTestSupport;
2424
import org.junit.jupiter.api.AfterEach;
2525
import org.junit.jupiter.api.BeforeEach;
26+
import org.junit.jupiter.api.extension.RegisterExtension;
2627

2728
public abstract class WebsocketCamelRouterWithInitParamTestSupport extends CamelTestSupport {
28-
protected static final int PORT = AvailablePortFinder.getNextAvailable();
29+
@RegisterExtension
30+
protected static AvailablePortFinder.Port PORT = AvailablePortFinder.find();
2931

3032
// This test needs to run with its own lifecycle management, so we cannot use extensions
3133
protected JettyEmbeddedService service;
@@ -42,7 +44,7 @@ void setupJetty() {
4244

4345
final JettyConfiguration jettyConfiguration = JettyConfigurationBuilder
4446
.emptyTemplate()
45-
.withPort(PORT)
47+
.withPort(PORT.getPort())
4648
.withContextPath(JettyConfiguration.ROOT_CONTEXT_PATH)
4749
.withWebSocketConfiguration().addServletConfiguration(servletConfiguration).build()
4850
.build();

components/camel-avro-rpc/camel-avro-rpc-component/src/test/java/org/apache/camel/component/avro/AvroConsumerTestSupport.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.camel.avro.test.TestPojo;
2828
import org.apache.camel.avro.test.TestReflection;
2929
import org.apache.camel.avro.test.TestReflectionImpl;
30+
import org.apache.camel.test.AvailablePortFinder;
3031
import org.apache.camel.test.junit6.TestNameExtension;
3132
import org.junit.jupiter.api.Order;
3233
import org.junit.jupiter.api.Test;
@@ -40,8 +41,13 @@ public abstract class AvroConsumerTestSupport extends AvroTestSupport {
4041
public static final String REFLECTION_TEST_NAME = "Chucky";
4142
public static final int REFLECTION_TEST_AGE = 100;
4243

43-
protected int avroPortMessageInRoute = setupFreePort("avroPortMessageInRoute");
44-
protected int avroPortForWrongMessages = setupFreePort("avroPortForWrongMessages");
44+
@RegisterExtension
45+
AvailablePortFinder.Port avroPortMessageInRouteHolder = AvailablePortFinder.find();
46+
@RegisterExtension
47+
AvailablePortFinder.Port avroPortForWrongMessagesHolder = AvailablePortFinder.find();
48+
49+
protected int avroPortMessageInRoute;
50+
protected int avroPortForWrongMessages;
4551

4652
Transceiver transceiver;
4753
Requestor requestor;
@@ -62,6 +68,15 @@ public abstract class AvroConsumerTestSupport extends AvroTestSupport {
6268
@Order(10)
6369
TestNameExtension testNameExtension = new TestNameExtension();
6470

71+
@Override
72+
protected void doPreSetup() throws Exception {
73+
super.doPreSetup();
74+
avroPortMessageInRoute = avroPortMessageInRouteHolder.getPort();
75+
avroPortForWrongMessages = avroPortForWrongMessagesHolder.getPort();
76+
System.setProperty("avroPortMessageInRoute", String.valueOf(avroPortMessageInRoute));
77+
System.setProperty("avroPortForWrongMessages", String.valueOf(avroPortForWrongMessages));
78+
}
79+
6580
protected abstract void initializeTranceiver() throws IOException;
6681

6782
@Override

components/camel-avro-rpc/camel-avro-rpc-component/src/test/java/org/apache/camel/component/avro/AvroTestSupport.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,23 @@
1919
import org.apache.camel.CamelContext;
2020
import org.apache.camel.test.AvailablePortFinder;
2121
import org.apache.camel.test.junit6.CamelTestSupport;
22+
import org.junit.jupiter.api.extension.RegisterExtension;
2223

2324
public class AvroTestSupport extends CamelTestSupport {
24-
protected int port = 9100;
25-
protected int avroPort = setupFreePort("avroport");
26-
protected int avroPortReflection = setupFreePort("avroPortReflection");
25+
@RegisterExtension
26+
AvailablePortFinder.Port avroPortHolder = AvailablePortFinder.find();
27+
@RegisterExtension
28+
AvailablePortFinder.Port avroPortReflectionHolder = AvailablePortFinder.find();
2729

28-
public int setupFreePort(String name) {
29-
port = AvailablePortFinder.getNextAvailable();
30-
System.setProperty(name, String.valueOf(port));
31-
return port;
30+
protected int avroPort;
31+
protected int avroPortReflection;
32+
33+
@Override
34+
protected void doPreSetup() throws Exception {
35+
avroPort = avroPortHolder.getPort();
36+
avroPortReflection = avroPortReflectionHolder.getPort();
37+
System.setProperty("avroport", String.valueOf(avroPort));
38+
System.setProperty("avroPortReflection", String.valueOf(avroPortReflection));
3239
}
3340

3441
@Override

components/camel-clickup/src/test/java/org/apache/camel/component/clickup/ClickUpWebhookCallTest.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import org.apache.camel.component.mock.MockEndpoint;
2828
import org.apache.camel.component.webhook.WebhookConfiguration;
2929
import org.apache.camel.component.webhook.WebhookEndpoint;
30-
import org.apache.camel.test.AvailablePortFinder;
31-
import org.junit.jupiter.api.BeforeAll;
3230
import org.junit.jupiter.api.Test;
3331
import org.slf4j.Logger;
3432
import org.slf4j.LoggerFactory;
@@ -46,13 +44,6 @@ public class ClickUpWebhookCallTest extends ClickUpTestSupport {
4644
public static final String MESSAGES_EVENTS_TIME_TRACKING_CREATED_SIGNATURE
4745
= "ac99f10017e28db6839941c184964890ec3262b1d6b1756d33ff53d972d5a361";
4846

49-
private static int port;
50-
51-
@BeforeAll
52-
public static void initPort() {
53-
port = AvailablePortFinder.getNextAvailable();
54-
}
55-
5647
@Test
5748
public void testWebhookCall() throws Exception {
5849
WebhookConfiguration config
@@ -85,7 +76,7 @@ protected RoutesBuilder createRouteBuilder() {
8576
public void configure() {
8677
restConfiguration()
8778
.host("localhost")
88-
.port(port);
79+
.port(port.getPort());
8980

9081
from("webhook:clickup:" + WORKSPACE_ID + "?authorizationToken=" + AUTHORIZATION_TOKEN + "&webhookSecret="
9182
+ WEBHOOK_SECRET + "&events=" + String.join(",", EVENTS) + "&webhookAutoRegister=false")

components/camel-clickup/src/test/java/org/apache/camel/component/clickup/ClickUpWebhookRegistrationAlreadyExistsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void configure() {
138138

139139
@Override
140140
protected ClickUpMockRoutes createMockRoutes() {
141-
ClickUpMockRoutes clickUpMockRoutes = new ClickUpMockRoutes(port);
141+
ClickUpMockRoutes clickUpMockRoutes = new ClickUpMockRoutes(port.getPort());
142142

143143
clickUpMockRoutes.addEndpoint(
144144
"health",

components/camel-clickup/src/test/java/org/apache/camel/component/clickup/ClickUpWebhookRegistrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public void configure() {
150150

151151
@Override
152152
protected ClickUpMockRoutes createMockRoutes() {
153-
ClickUpMockRoutes clickUpMockRoutes = new ClickUpMockRoutes(port);
153+
ClickUpMockRoutes clickUpMockRoutes = new ClickUpMockRoutes(port.getPort());
154154

155155
clickUpMockRoutes.addEndpoint(
156156
"health",

components/camel-clickup/src/test/java/org/apache/camel/component/clickup/util/ClickUpTestSupport.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,18 @@
2424
import org.apache.camel.component.clickup.ClickUpComponent;
2525
import org.apache.camel.test.AvailablePortFinder;
2626
import org.apache.camel.test.junit6.CamelTestSupport;
27-
import org.junit.jupiter.api.BeforeAll;
27+
import org.junit.jupiter.api.extension.RegisterExtension;
2828

2929
/**
3030
* A support test class for ClickUp tests.
3131
*/
3232
public class ClickUpTestSupport extends CamelTestSupport {
3333

34-
protected static volatile int port;
34+
@RegisterExtension
35+
protected static AvailablePortFinder.Port port = AvailablePortFinder.find();
3536

3637
private ClickUpMockRoutes mockRoutes;
3738

38-
@BeforeAll
39-
public static void initPort() {
40-
port = AvailablePortFinder.getNextAvailable();
41-
}
42-
4339
/**
4440
* Retrieves a response from a JSON file on classpath.
4541
*

components/camel-elytron/src/test/java/org/apache/camel/component/elytron/BaseElytronTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.camel.test.AvailablePortFinder;
3434
import org.apache.camel.test.junit6.CamelTestSupport;
3535
import org.junit.jupiter.api.BeforeAll;
36+
import org.junit.jupiter.api.extension.RegisterExtension;
3637
import org.wildfly.security.WildFlyElytronBaseProvider;
3738
import org.wildfly.security.auth.permission.LoginPermission;
3839
import org.wildfly.security.auth.realm.token.TokenSecurityRealm;
@@ -46,7 +47,8 @@
4647
*/
4748
public abstract class BaseElytronTest extends CamelTestSupport {
4849

49-
private static volatile int port;
50+
@RegisterExtension
51+
static AvailablePortFinder.Port port = AvailablePortFinder.find();
5052
private static KeyPair keyPair;
5153

5254
private final AtomicInteger counter = new AtomicInteger(1);
@@ -59,7 +61,6 @@ public abstract class BaseElytronTest extends CamelTestSupport {
5961

6062
@BeforeAll
6163
public static void initPort() throws Exception {
62-
port = AvailablePortFinder.getNextAvailable();
6364
keyPair = null;
6465

6566
URL location = ElytronSecurityProvider.class.getProtectionDomain().getCodeSource().getLocation();
@@ -74,7 +75,7 @@ public static void initPort() throws Exception {
7475
}
7576

7677
protected static int getPort() {
77-
return port;
78+
return port.getPort();
7879
}
7980

8081
@BindToRegistry("prop")

components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpClientSoTimeoutTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.camel.test.junit6.CamelTestSupport;
2525
import org.apache.commons.net.ftp.FTPFile;
2626
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.api.extension.RegisterExtension;
2728

2829
import static org.junit.jupiter.api.Assertions.assertEquals;
2930

@@ -34,10 +35,11 @@
3435
*/
3536
public class FromFtpClientSoTimeoutTest extends CamelTestSupport {
3637

37-
int port = AvailablePortFinder.getNextAvailable();
38+
@RegisterExtension
39+
AvailablePortFinder.Port port = AvailablePortFinder.find();
3840

3941
private String getFtpUrl() {
40-
return "ftp://admin@localhost:" + port + "/timeout/?soTimeout=5000";
42+
return "ftp://admin@localhost:" + port.getPort() + "/timeout/?soTimeout=5000";
4143
}
4244

4345
@Test

0 commit comments

Comments
 (0)