Skip to content

Commit 40de4d6

Browse files
garydgregoryok2c
authored andcommitted
Refactor duplicate code in tests
- Add missing Javadoc - Do not allocate Message objects needlessly - Reduce String building - No need to call constructor with the default value, an AtomicBoolean is false by default. - Access ArgumentMatchers static methods directly - Use try-with-resources
1 parent 4016428 commit 40de4d6

File tree

53 files changed

+630
-488
lines changed

Some content is hidden

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

53 files changed

+630
-488
lines changed

httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientH2StreamHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ public void endStream() throws IOException {
110110
this.exchangeHandler = exchangeHandler;
111111
this.pushHandlerFactory = pushHandlerFactory;
112112
this.context = context;
113-
this.requestCommitted = new AtomicBoolean(false);
114-
this.failed = new AtomicBoolean(false);
115-
this.done = new AtomicBoolean(false);
113+
this.requestCommitted = new AtomicBoolean();
114+
this.failed = new AtomicBoolean();
115+
this.done = new AtomicBoolean();
116116
this.requestState = MessageState.HEADERS;
117117
this.responseState = MessageState.HEADERS;
118118
}

httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientPushH2StreamHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ class ClientPushH2StreamHandler implements H2StreamHandler {
7878
this.connMetrics = connMetrics;
7979
this.pushHandlerFactory = pushHandlerFactory;
8080
this.context = context;
81-
this.failed = new AtomicBoolean(false);
82-
this.done = new AtomicBoolean(false);
81+
this.failed = new AtomicBoolean();
82+
this.done = new AtomicBoolean();
8383
this.requestState = MessageState.HEADERS;
8484
this.responseState = MessageState.HEADERS;
8585
}

httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerH2StreamHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ public void pushPromise(
137137
this.connMetrics = connMetrics;
138138
this.exchangeHandlerFactory = exchangeHandlerFactory;
139139
this.context = context;
140-
this.responseCommitted = new AtomicBoolean(false);
141-
this.failed = new AtomicBoolean(false);
142-
this.done = new AtomicBoolean(false);
140+
this.responseCommitted = new AtomicBoolean();
141+
this.failed = new AtomicBoolean();
142+
this.done = new AtomicBoolean();
143143
this.requestState = MessageState.HEADERS;
144144
this.responseState = MessageState.IDLE;
145145
}

httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerPushH2StreamHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ public void endStream() throws IOException {
105105
this.connMetrics = connMetrics;
106106
this.pushProducer = pushProducer;
107107
this.context = context;
108-
this.responseCommitted = new AtomicBoolean(false);
109-
this.failed = new AtomicBoolean(false);
110-
this.done = new AtomicBoolean(false);
108+
this.responseCommitted = new AtomicBoolean();
109+
this.failed = new AtomicBoolean();
110+
this.done = new AtomicBoolean();
111111
this.requestState = MessageState.COMPLETE;
112112
this.responseState = MessageState.IDLE;
113113
}

httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/CancellableExecution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ final class CancellableExecution implements CancellableDependency {
3838
private final AtomicReference<Cancellable> dependencyRef;
3939

4040
CancellableExecution() {
41-
this.cancelled = new AtomicBoolean(false);
41+
this.cancelled = new AtomicBoolean();
4242
this.dependencyRef = new AtomicReference<>();
4343
}
4444

httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestAbstractH2StreamMultiplexer.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.junit.jupiter.api.Assertions;
5050
import org.junit.jupiter.api.BeforeEach;
5151
import org.junit.jupiter.api.Test;
52+
import org.mockito.ArgumentMatchers;
5253
import org.mockito.Mock;
5354
import org.mockito.Mockito;
5455
import org.mockito.MockitoAnnotations;
@@ -139,9 +140,9 @@ void testInputOneFrame() throws Exception {
139140
Assertions.assertThrows(H2ConnectionException.class, () ->
140141
streamMultiplexer.onInput(ByteBuffer.wrap(bytes)));
141142
Mockito.verify(h2StreamListener).onFrameInput(
142-
Mockito.same(streamMultiplexer),
143-
Mockito.eq(1),
144-
Mockito.any());
143+
ArgumentMatchers.same(streamMultiplexer),
144+
ArgumentMatchers.eq(1),
145+
ArgumentMatchers.any());
145146

146147
Assertions.assertThrows(H2ConnectionException.class, () -> {
147148
int pos = 0;
@@ -154,9 +155,9 @@ void testInputOneFrame() throws Exception {
154155
}
155156

156157
Mockito.verify(h2StreamListener).onFrameInput(
157-
Mockito.same(streamMultiplexer),
158-
Mockito.eq(1),
159-
Mockito.any());
158+
ArgumentMatchers.same(streamMultiplexer),
159+
ArgumentMatchers.eq(1),
160+
ArgumentMatchers.any());
160161
});
161162
}
162163

@@ -190,9 +191,9 @@ void testInputMultipleFrames() throws Exception {
190191
Assertions.assertThrows(H2ConnectionException.class, () ->
191192
streamMultiplexer.onInput(ByteBuffer.wrap(bytes)));
192193
Mockito.verify(h2StreamListener).onFrameInput(
193-
Mockito.same(streamMultiplexer),
194-
Mockito.eq(1),
195-
Mockito.any());
194+
ArgumentMatchers.same(streamMultiplexer),
195+
ArgumentMatchers.eq(1),
196+
ArgumentMatchers.any());
196197

197198
Assertions.assertThrows(H2ConnectionException.class, () -> {
198199
int pos = 0;
@@ -205,9 +206,9 @@ void testInputMultipleFrames() throws Exception {
205206
}
206207

207208
Mockito.verify(h2StreamListener).onFrameInput(
208-
Mockito.same(streamMultiplexer),
209-
Mockito.eq(1),
210-
Mockito.any());
209+
ArgumentMatchers.same(streamMultiplexer),
210+
ArgumentMatchers.eq(1),
211+
ArgumentMatchers.any());
211212
});
212213
}
213214

httpcore5-reactive/src/main/java/org/apache/hc/core5/reactive/ReactiveDataConsumer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ final class ReactiveDataConsumer implements AsyncDataConsumer, Publisher<ByteBuf
5858
private final AtomicLong requests = new AtomicLong(0);
5959

6060
private final BlockingQueue<ByteBuffer> buffers = new LinkedBlockingQueue<>();
61-
private final AtomicBoolean flushInProgress = new AtomicBoolean(false);
61+
private final AtomicBoolean flushInProgress = new AtomicBoolean();
6262
private final AtomicInteger windowScalingIncrement = new AtomicInteger(0);
6363
private volatile boolean cancelled;
6464
private volatile boolean completed;

httpcore5-reactive/src/main/java/org/apache/hc/core5/reactive/ReactiveDataProducer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ final class ReactiveDataProducer implements AsyncDataProducer, Subscriber<ByteBu
5555

5656
private final AtomicReference<DataStreamChannel> requestChannel = new AtomicReference<>();
5757
private final AtomicReference<Throwable> exception = new AtomicReference<>();
58-
private final AtomicBoolean complete = new AtomicBoolean(false);
58+
private final AtomicBoolean complete = new AtomicBoolean();
5959
private final Publisher<ByteBuffer> publisher;
6060
private final AtomicReference<Subscription> subscription = new AtomicReference<>();
6161
private final ArrayDeque<ByteBuffer> buffers = new ArrayDeque<>(); // This field requires synchronization

httpcore5-testing/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
<artifactId>rxjava</artifactId>
7171
<version>${rxjava3.version}</version>
7272
</dependency>
73+
<dependency>
74+
<groupId>org.apache.httpcomponents.core5</groupId>
75+
<artifactId>httpcore5</artifactId>
76+
<classifier>tests</classifier>
77+
</dependency>
7378
<dependency>
7479
<groupId>org.apache.logging.log4j</groupId>
7580
<artifactId>log4j-slf4j-impl</artifactId>

httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/ClientSessionEndpoint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public final class ClientSessionEndpoint implements ModalCloseable {
6666
public ClientSessionEndpoint(final IOSession ioSession) {
6767
super();
6868
this.ioSession = ioSession;
69-
this.closed = new AtomicBoolean(false);
69+
this.closed = new AtomicBoolean();
7070
}
7171

7272
public void execute(final Command command, final Command.Priority priority) {

0 commit comments

Comments
 (0)