Skip to content

Commit b3dcad8

Browse files
Add unit test to prevent axiom loading with enableJSONOnly=true
1 parent 0c31026 commit b3dcad8

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed

modules/jaxws-integration/src/test/java/org/apache/axis2/jaxws/sample/asyncdoclit/client/AsyncClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
public class AsyncClient {
2525

26-
private static final int max_isasleep_check = 30;
26+
private static final int max_isasleep_check = 60;
2727

2828
/**
2929
* Auxiliary method used for doiing isAsleep checks. Will perform isAsleep

modules/jaxws-integration/test-resources/axis2_addressing.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173
<!-- ================================================= -->
174174
<transportReceiver name="http"
175175
class="org.apache.axis2.transport.http.SimpleHTTPServer">
176-
<parameter name="port">9090</parameter>
176+
<!-- <parameter name="port">9090</parameter> Port dynamically allocated by test framework -->
177177
<!-- Here is the complete list of supported parameters (see example settings further below):
178178
port: the port to listen on (default 6060)
179179
hostname: if non-null, url prefix used in reply-to endpoint references (default null)

modules/testutils/src/main/java/org/apache/axis2/testutils/Axis2Server.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,17 @@ protected void startServer(ConfigurationContext configurationContext) throws Thr
7373
@Override
7474
protected void stopServer() {
7575
System.out.println("[Axis2Server] stopServer() invoked, setting port to -1");
76+
if (server != null) {
77+
server.stop();
78+
server = null;
79+
80+
// Add small delay to ensure port is fully released before next test
81+
try {
82+
Thread.sleep(100);
83+
} catch (InterruptedException e) {
84+
Thread.currentThread().interrupt();
85+
}
86+
}
7687
port = -1;
77-
server.stop();
78-
server = null;
7988
}
8089
}

modules/testutils/src/main/java/org/apache/axis2/testutils/PortAllocator.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,47 @@
2020

2121
import java.io.IOException;
2222
import java.net.ServerSocket;
23+
import java.util.concurrent.ThreadLocalRandom;
2324

2425
public final class PortAllocator {
2526
private PortAllocator() {}
2627

2728
/**
2829
* Allocate a TCP port.
29-
*
30+
*
3031
* @return the allocated port
3132
*/
3233
public static int allocatePort() {
3334
try {
3435
ServerSocket ss = new ServerSocket(0);
3536
int port = ss.getLocalPort();
3637
ss.close();
38+
39+
// Add retry mechanism to reduce race condition where another process
40+
// grabs the port between close() and actual bind
41+
for (int retry = 0; retry < 5; retry++) {
42+
try {
43+
// Test if the port is still available by trying to bind again
44+
ServerSocket testSocket = new ServerSocket(port);
45+
testSocket.close();
46+
47+
// Add small random delay to reduce parallel test conflicts
48+
if (retry > 0) {
49+
Thread.sleep(ThreadLocalRandom.current().nextInt(10, 50));
50+
}
51+
52+
return port;
53+
} catch (IOException bindEx) {
54+
// Port already taken, try allocating a new one
55+
ss = new ServerSocket(0);
56+
port = ss.getLocalPort();
57+
ss.close();
58+
} catch (InterruptedException ie) {
59+
Thread.currentThread().interrupt();
60+
break;
61+
}
62+
}
63+
3764
return port;
3865
} catch (IOException ex) {
3966
throw new Error("Unable to allocate TCP port", ex);

modules/transport/http/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
<groupId>jakarta.servlet</groupId>
8383
<artifactId>jakarta.servlet-api</artifactId>
8484
</dependency>
85+
<dependency>
86+
<groupId>org.mockito</groupId>
87+
<artifactId>mockito-core</artifactId>
88+
<scope>test</scope>
89+
</dependency>
8590
</dependencies>
8691

8792
<build>

modules/transport/http/src/main/java/org/apache/axis2/transport/http/AxisServlet.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,10 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
247247
throw new ServletException(e2);
248248
}
249249
} finally {
250-
closeStaxBuilder(msgContext);
251-
TransportUtils.deleteAttachments(msgContext);
250+
if (!enableJSONOnly) {
251+
closeStaxBuilder(msgContext);
252+
TransportUtils.deleteAttachments(msgContext);
253+
}
252254
}
253255
} else {
254256
if (!disableREST) {

0 commit comments

Comments
 (0)