You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2026/07/23 10:59:44 UTC

(httpcomponents-core) branch master updated: H2 stream handler test improvements

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

ok2c pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 59e384884 H2 stream handler test improvements
59e384884 is described below

commit 59e3848848e2c8d882d76309eb035af20b32c580
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Thu Jul 23 12:55:45 2026 +0200

    H2 stream handler test improvements
---
 .../http2/impl/nio/TestClientH2StreamHandler.java  | 58 +++++++++-------------
 .../impl/nio/TestClientPushH2StreamHandler.java    | 35 +++++++------
 .../http2/impl/nio/TestServerH2StreamHandler.java  | 58 +++++++++-------------
 3 files changed, 66 insertions(+), 85 deletions(-)

diff --git a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestClientH2StreamHandler.java b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestClientH2StreamHandler.java
index a85186ff9..ff3f9ea74 100644
--- a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestClientH2StreamHandler.java
+++ b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestClientH2StreamHandler.java
@@ -41,57 +41,56 @@
 import org.apache.hc.core5.http.protocol.HttpCoreContext;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
 import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
 import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
 
 class TestClientH2StreamHandler {
 
-    private ClientH2StreamHandler newHandler() {
-        final H2StreamChannel channel = Mockito.mock(H2StreamChannel.class);
-        final HttpProcessor httpProcessor = Mockito.mock(HttpProcessor.class);
-        final BasicHttpConnectionMetrics metrics = new BasicHttpConnectionMetrics(
-                new BasicHttpTransportMetrics(), new BasicHttpTransportMetrics());
-        final AsyncClientExchangeHandler exchangeHandler = Mockito.mock(AsyncClientExchangeHandler.class);
-        @SuppressWarnings("unchecked")
-        final HandlerFactory<AsyncPushConsumer> pushHandlerFactory =
-                (HandlerFactory<AsyncPushConsumer>) Mockito.mock(HandlerFactory.class);
-        return new ClientH2StreamHandler(channel, httpProcessor, metrics, exchangeHandler, pushHandlerFactory,
+    @Mock
+    H2StreamChannel channel;
+    @Mock
+    HttpProcessor httpProcessor;
+    @Mock
+    AsyncClientExchangeHandler exchangeHandler;
+    @Mock
+    HandlerFactory<AsyncPushConsumer> pushHandlerFactory;
+
+    ClientH2StreamHandler handler;
+
+    @BeforeEach
+    void prepareMocks() {
+        MockitoAnnotations.openMocks(this);
+        handler = new ClientH2StreamHandler(
+                channel, httpProcessor,
+                new BasicHttpConnectionMetrics(
+                        new BasicHttpTransportMetrics(), new BasicHttpTransportMetrics()),
+                exchangeHandler,
+                pushHandlerFactory,
                 HttpCoreContext.create());
     }
 
     @Test
     void defaults() {
-        final ClientH2StreamHandler handler = newHandler();
         Assertions.assertNotNull(handler.getPushHandlerFactory());
         Assertions.assertTrue(handler.isOutputReady());
     }
 
     @Test
     void consumePromiseRejected() {
-        final ClientH2StreamHandler handler = newHandler();
         Assertions.assertThrows(ProtocolException.class, () -> handler.consumePromise(null));
     }
 
     @Test
     void consumeDataRejectedBeforeHeaders() {
-        final ClientH2StreamHandler handler = newHandler();
         Assertions.assertThrows(ProtocolException.class, () ->
                 handler.consumeData(ByteBuffer.allocate(0), true));
     }
 
     @Test
     void updateCapacityDelegates() throws Exception {
-        final H2StreamChannel channel = Mockito.mock(H2StreamChannel.class);
-        final HttpProcessor httpProcessor = Mockito.mock(HttpProcessor.class);
-        final BasicHttpConnectionMetrics metrics = new BasicHttpConnectionMetrics(
-                new BasicHttpTransportMetrics(), new BasicHttpTransportMetrics());
-        final AsyncClientExchangeHandler exchangeHandler = Mockito.mock(AsyncClientExchangeHandler.class);
-        @SuppressWarnings("unchecked")
-        final HandlerFactory<AsyncPushConsumer> pushHandlerFactory =
-                (HandlerFactory<AsyncPushConsumer>) Mockito.mock(HandlerFactory.class);
-        final ClientH2StreamHandler handler = new ClientH2StreamHandler(
-                channel, httpProcessor, metrics, exchangeHandler, pushHandlerFactory, HttpCoreContext.create());
-
         handler.updateInputCapacity();
 
         Mockito.verify(exchangeHandler).updateCapacity(channel);
@@ -99,7 +98,6 @@ void updateCapacityDelegates() throws Exception {
 
     @Test
     void toStringIncludesStates() {
-        final ClientH2StreamHandler handler = newHandler();
         final String text = handler.toString();
         Assertions.assertTrue(text.contains("requestState"));
         Assertions.assertTrue(text.contains("responseState"));
@@ -107,16 +105,6 @@ void toStringIncludesStates() {
 
     @Test
     void consumeTrailersWithPseudoHeaderRejected() throws Exception {
-        final H2StreamChannel channel = Mockito.mock(H2StreamChannel.class);
-        final HttpProcessor httpProcessor = Mockito.mock(HttpProcessor.class);
-        final BasicHttpConnectionMetrics metrics = new BasicHttpConnectionMetrics(
-                new BasicHttpTransportMetrics(), new BasicHttpTransportMetrics());
-        final AsyncClientExchangeHandler exchangeHandler = Mockito.mock(AsyncClientExchangeHandler.class);
-        @SuppressWarnings("unchecked") final HandlerFactory<AsyncPushConsumer> pushHandlerFactory =
-                (HandlerFactory<AsyncPushConsumer>) Mockito.mock(HandlerFactory.class);
-        final ClientH2StreamHandler handler = new ClientH2StreamHandler(
-                channel, httpProcessor, metrics, exchangeHandler, pushHandlerFactory, HttpCoreContext.create());
-
         final List<Header> responseHeaders = Collections.singletonList(
                 new BasicHeader(":status", "200"));
         handler.consumeHeader(responseHeaders, false);
diff --git a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestClientPushH2StreamHandler.java b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestClientPushH2StreamHandler.java
index 2b6c42974..a7264c0be 100644
--- a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestClientPushH2StreamHandler.java
+++ b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestClientPushH2StreamHandler.java
@@ -39,28 +39,35 @@
 import org.apache.hc.core5.http2.H2Error;
 import org.apache.hc.core5.http2.H2StreamResetException;
 import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
 
 class TestClientPushH2StreamHandler {
 
-    private ClientPushH2StreamHandler newHandler(final HandlerFactory<AsyncPushConsumer> pushHandlerFactory) {
-        final H2StreamChannel channel = Mockito.mock(H2StreamChannel.class);
-        final HttpProcessor httpProcessor = Mockito.mock(HttpProcessor.class);
-        final BasicHttpConnectionMetrics metrics = new BasicHttpConnectionMetrics(
-                new BasicHttpTransportMetrics(), new BasicHttpTransportMetrics());
-        return new ClientPushH2StreamHandler(channel, httpProcessor, metrics, pushHandlerFactory,
+    @Mock
+    H2StreamChannel channel;
+    @Mock
+    HttpProcessor httpProcessor;
+    @Mock
+    HandlerFactory<AsyncPushConsumer> pushHandlerFactory;
+
+    ClientPushH2StreamHandler handler;
+
+    @BeforeEach
+    void prepareMocks() {
+        MockitoAnnotations.openMocks(this);
+        handler = new ClientPushH2StreamHandler(
+                channel, httpProcessor,
+                new BasicHttpConnectionMetrics(
+                        new BasicHttpTransportMetrics(), new BasicHttpTransportMetrics()),
+                pushHandlerFactory,
                 HttpCoreContext.create());
     }
 
     @Test
     void consumePromiseRefusedWhenFactoryReturnsNull() throws Exception {
-        @SuppressWarnings("unchecked")
-        final HandlerFactory<AsyncPushConsumer> pushHandlerFactory =
-                (HandlerFactory<AsyncPushConsumer>) Mockito.mock(HandlerFactory.class);
-        Mockito.when(pushHandlerFactory.create(Mockito.any(), Mockito.any())).thenReturn(null);
-        final ClientPushH2StreamHandler handler = newHandler(pushHandlerFactory);
-
         final H2StreamResetException ex = Assertions.assertThrows(H2StreamResetException.class, () ->
                 handler.consumePromise(java.util.Arrays.asList(
                         new BasicHeader(":method", "GET"),
@@ -72,14 +79,12 @@ void consumePromiseRefusedWhenFactoryReturnsNull() throws Exception {
 
     @Test
     void consumeDataRejectedBeforeBody() {
-        final ClientPushH2StreamHandler handler = newHandler(null);
         Assertions.assertThrows(ProtocolException.class, () ->
                 handler.consumeData(ByteBuffer.allocate(0), true));
     }
 
     @Test
     void updateCapacityFailsWithoutHandler() {
-        final ClientPushH2StreamHandler handler = newHandler(null);
         Assertions.assertThrows(IllegalStateException.class, handler::updateInputCapacity);
     }
 
diff --git a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestServerH2StreamHandler.java b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestServerH2StreamHandler.java
index 6d4e71f96..28c1d539b 100644
--- a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestServerH2StreamHandler.java
+++ b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestServerH2StreamHandler.java
@@ -43,47 +43,57 @@
 import org.apache.hc.core5.http2.H2Error;
 import org.apache.hc.core5.http2.H2StreamResetException;
 import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.mockito.ArgumentMatchers;
+import org.mockito.Mock;
 import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
 
 class TestServerH2StreamHandler {
 
-    private ServerH2StreamHandler newHandler() {
-        final H2StreamChannel channel = Mockito.mock(H2StreamChannel.class);
-        final HttpProcessor httpProcessor = Mockito.mock(HttpProcessor.class);
-        final BasicHttpConnectionMetrics metrics = new BasicHttpConnectionMetrics(
-                new BasicHttpTransportMetrics(), new BasicHttpTransportMetrics());
-        @SuppressWarnings("unchecked")
-        final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory =
-                (HandlerFactory<AsyncServerExchangeHandler>) Mockito.mock(HandlerFactory.class);
-        return new ServerH2StreamHandler(channel, httpProcessor, metrics, exchangeHandlerFactory,
+    @Mock
+    H2StreamChannel channel;
+    @Mock
+    HttpProcessor httpProcessor;
+    @Mock
+    AsyncServerExchangeHandler exchangeHandler;
+    @Mock
+    HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory;
+
+    ServerH2StreamHandler handler;
+
+    @BeforeEach
+    void prepareMocks() {
+        MockitoAnnotations.openMocks(this);
+        handler = new ServerH2StreamHandler(
+                channel,
+                httpProcessor,
+                new BasicHttpConnectionMetrics(
+                        new BasicHttpTransportMetrics(), new BasicHttpTransportMetrics()),
+                exchangeHandlerFactory,
                 HttpCoreContext.create());
     }
 
     @Test
     void defaults() {
-        final ServerH2StreamHandler handler = newHandler();
         Assertions.assertNull(handler.getPushHandlerFactory());
         Assertions.assertFalse(handler.isOutputReady());
     }
 
     @Test
     void consumeDataBeforeHeadersRejected() {
-        final ServerH2StreamHandler handler = newHandler();
         Assertions.assertThrows(ProtocolException.class, () ->
                 handler.consumeData(ByteBuffer.allocate(0), true));
     }
 
     @Test
     void updateCapacityWithoutHandlerFails() {
-        final ServerH2StreamHandler handler = newHandler();
         Assertions.assertThrows(IllegalStateException.class, () -> handler.updateInputCapacity());
     }
 
     @Test
     void toStringIncludesStates() {
-        final ServerH2StreamHandler handler = newHandler();
         final String text = handler.toString();
         Assertions.assertTrue(text.contains("requestState"));
         Assertions.assertTrue(text.contains("responseState"));
@@ -91,20 +101,9 @@ void toStringIncludesStates() {
 
     @Test
     void consumeTrailersWithPseudoHeaderRejected() throws Exception {
-        final H2StreamChannel channel = Mockito.mock(H2StreamChannel.class);
-        final HttpProcessor httpProcessor = Mockito.mock(HttpProcessor.class);
-        final BasicHttpConnectionMetrics metrics = new BasicHttpConnectionMetrics(
-                new BasicHttpTransportMetrics(), new BasicHttpTransportMetrics());
-        final AsyncServerExchangeHandler exchangeHandler = Mockito.mock(AsyncServerExchangeHandler.class);
-        @SuppressWarnings("unchecked")
-        final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory =
-                (HandlerFactory<AsyncServerExchangeHandler>) Mockito.mock(HandlerFactory.class);
         Mockito.when(exchangeHandlerFactory.create(ArgumentMatchers.any(), ArgumentMatchers.any()))
                 .thenReturn(exchangeHandler);
 
-        final ServerH2StreamHandler handler = new ServerH2StreamHandler(
-                channel, httpProcessor, metrics, exchangeHandlerFactory, HttpCoreContext.create());
-
         final List<Header> requestHeaders = Arrays.asList(
                 new BasicHeader(":method", "POST"),
                 new BasicHeader(":scheme", "https"),
@@ -119,20 +118,9 @@ void consumeTrailersWithPseudoHeaderRejected() throws Exception {
 
     @Test
     void consumeTrailersWithoutEndStreamRejected() throws Exception {
-        final H2StreamChannel channel = Mockito.mock(H2StreamChannel.class);
-        final HttpProcessor httpProcessor = Mockito.mock(HttpProcessor.class);
-        final BasicHttpConnectionMetrics metrics = new BasicHttpConnectionMetrics(
-                new BasicHttpTransportMetrics(), new BasicHttpTransportMetrics());
-        final AsyncServerExchangeHandler exchangeHandler = Mockito.mock(AsyncServerExchangeHandler.class);
-        @SuppressWarnings("unchecked")
-        final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory =
-                (HandlerFactory<AsyncServerExchangeHandler>) Mockito.mock(HandlerFactory.class);
         Mockito.when(exchangeHandlerFactory.create(ArgumentMatchers.any(), ArgumentMatchers.any()))
                 .thenReturn(exchangeHandler);
 
-        final ServerH2StreamHandler handler = new ServerH2StreamHandler(
-                channel, httpProcessor, metrics, exchangeHandlerFactory, HttpCoreContext.create());
-
         final List<Header> requestHeaders = Arrays.asList(
                 new BasicHeader(":method", "POST"),
                 new BasicHeader(":scheme", "https"),