Clean up warnings and polish tests

This commit also modifies ResourceWebHandlerTests.getResourceFromFileSystem()
so that it passes in the IDE.
This commit is contained in:
Sam Brannen
2022-08-26 15:20:16 +02:00
parent e53c7ae6f5
commit b50415062b
20 changed files with 206 additions and 214 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -47,12 +47,11 @@ import static org.springframework.util.MimeTypeUtils.TEXT_HTML;
import static org.springframework.util.MimeTypeUtils.TEXT_PLAIN;
import static org.springframework.util.MimeTypeUtils.TEXT_XML;
/**
* Unit tests for {@link DefaultMetadataExtractor}.
* @author Rossen Stoyanchev
*/
public class DefaultMetadataExtractorTests {
class DefaultMetadataExtractorTests {
private static MimeType COMPOSITE_METADATA =
MimeTypeUtils.parseMimeType(WellKnownMimeType.MESSAGE_RSOCKET_COMPOSITE_METADATA.getString());
@@ -64,21 +63,21 @@ public class DefaultMetadataExtractorTests {
@BeforeEach
public void setUp() {
void setUp() {
DataBufferFactory bufferFactory = new LeakAwareNettyDataBufferFactory(PooledByteBufAllocator.DEFAULT);
this.strategies = RSocketStrategies.builder().dataBufferFactory(bufferFactory).build();
this.extractor = new DefaultMetadataExtractor(StringDecoder.allMimeTypes());
}
@AfterEach
public void tearDown() throws InterruptedException {
void tearDown() throws InterruptedException {
DataBufferFactory bufferFactory = this.strategies.dataBufferFactory();
((LeakAwareNettyDataBufferFactory) bufferFactory).checkForLeaks(Duration.ofSeconds(5));
}
@Test
public void compositeMetadataWithDefaultSettings() {
void compositeMetadataWithDefaultSettings() {
MetadataEncoder metadataEncoder = new MetadataEncoder(COMPOSITE_METADATA, this.strategies)
.route("toA")
.metadata("text data", TEXT_PLAIN)
@@ -94,7 +93,7 @@ public class DefaultMetadataExtractorTests {
}
@Test
public void compositeMetadataWithMimeTypeRegistrations() {
void compositeMetadataWithMimeTypeRegistrations() {
this.extractor.metadataToExtract(TEXT_PLAIN, String.class, "text-entry");
this.extractor.metadataToExtract(TEXT_HTML, String.class, "html-entry");
this.extractor.metadataToExtract(TEXT_XML, String.class, "xml-entry");
@@ -118,7 +117,7 @@ public class DefaultMetadataExtractorTests {
}
@Test
public void route() {
void route() {
MimeType metaMimeType = MimeTypeUtils.parseMimeType(WellKnownMimeType.MESSAGE_RSOCKET_ROUTING.getString());
MetadataEncoder metadataEncoder = new MetadataEncoder(metaMimeType, this.strategies).route("toA");
DataBuffer metadata = metadataEncoder.encode().block();
@@ -130,7 +129,7 @@ public class DefaultMetadataExtractorTests {
}
@Test
public void routeAsText() {
void routeAsText() {
this.extractor.metadataToExtract(TEXT_PLAIN, String.class, ROUTE_KEY);
MetadataEncoder metadataEncoder = new MetadataEncoder(TEXT_PLAIN, this.strategies).route("toA");
@@ -143,7 +142,7 @@ public class DefaultMetadataExtractorTests {
}
@Test
public void routeWithCustomFormatting() {
void routeWithCustomFormatting() {
this.extractor.metadataToExtract(TEXT_PLAIN, String.class, (text, result) -> {
String[] items = text.split(":");
Assert.isTrue(items.length == 2, "Expected two items");
@@ -163,7 +162,7 @@ public class DefaultMetadataExtractorTests {
}
@Test
public void nonCompositeMetadataCanBeReadTwice() {
void nonCompositeMetadataCanBeReadTwice() {
DefaultMetadataExtractor extractor = new DefaultMetadataExtractor(new TestDecoder());
extractor.metadataToExtract(TEXT_PLAIN, String.class, "name");
@@ -181,7 +180,7 @@ public class DefaultMetadataExtractorTests {
}
@Test
public void noDecoder() {
void noDecoder() {
DefaultMetadataExtractor extractor =
new DefaultMetadataExtractor(Collections.singletonList(new ByteArrayDecoder())
);
@@ -193,7 +192,7 @@ public class DefaultMetadataExtractorTests {
private Payload createPayload(DataBuffer metadata) {
return PayloadUtils.createPayload(this.strategies.dataBufferFactory().allocateBuffer(), metadata);
return PayloadUtils.createPayload(this.strategies.dataBufferFactory().allocateBuffer(256), metadata);
}
@@ -203,7 +202,7 @@ public class DefaultMetadataExtractorTests {
*/
private static class TestDecoder extends AbstractDataBufferDecoder<String> {
public TestDecoder() {
TestDecoder() {
super(TEXT_PLAIN);
}
@@ -217,4 +216,5 @@ public class DefaultMetadataExtractorTests {
return new String(bytes, StandardCharsets.UTF_8);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -52,8 +52,8 @@ public class LeakAwareNettyDataBufferFactory extends NettyDataBufferFactory {
while (true) {
try {
this.created.forEach(info -> {
if (((PooledDataBuffer) info.getDataBuffer()).isAllocated()) {
throw info.getError();
if (((PooledDataBuffer) info.dataBuffer()).isAllocated()) {
throw info.error();
}
});
break;
@@ -73,6 +73,7 @@ public class LeakAwareNettyDataBufferFactory extends NettyDataBufferFactory {
@Override
@SuppressWarnings("deprecation")
public NettyDataBuffer allocateBuffer() {
return (NettyDataBuffer) recordHint(super.allocateBuffer());
}
@@ -105,23 +106,7 @@ public class LeakAwareNettyDataBufferFactory extends NettyDataBufferFactory {
}
private static class DataBufferLeakInfo {
private final DataBuffer dataBuffer;
private final AssertionError error;
DataBufferLeakInfo(DataBuffer dataBuffer, AssertionError error) {
this.dataBuffer = dataBuffer;
this.error = error;
}
DataBuffer getDataBuffer() {
return this.dataBuffer;
}
AssertionError getError() {
return this.error;
}
private static record DataBufferLeakInfo(DataBuffer dataBuffer, AssertionError error) {
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -42,11 +42,10 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
*
* @author Rossen Stoyanchev
* @since 5.2
*/
public class MetadataEncoderTests {
class MetadataEncoderTests {
private static MimeType COMPOSITE_METADATA =
MimeTypeUtils.parseMimeType(WellKnownMimeType.MESSAGE_RSOCKET_COMPOSITE_METADATA.getString());
@@ -56,7 +55,7 @@ public class MetadataEncoderTests {
@Test
public void compositeMetadata() {
void compositeMetadata() {
Mono<String> asyncMeta1 = Mono.delay(Duration.ofMillis(1)).map(aLong -> "Async Metadata 1");
Mono<String> asyncMeta2 = Mono.delay(Duration.ofMillis(1)).map(aLong -> "Async Metadata 2");
@@ -102,7 +101,7 @@ public class MetadataEncoderTests {
}
@Test
public void routeWithRoutingMimeType() {
void routeWithRoutingMimeType() {
MimeType mimeType = MimeTypeUtils.parseMimeType(
WellKnownMimeType.MESSAGE_RSOCKET_ROUTING.getString());
@@ -117,7 +116,7 @@ public class MetadataEncoderTests {
}
@Test
public void routeWithTextPlainMimeType() {
void routeWithTextPlainMimeType() {
DataBuffer buffer =
new MetadataEncoder(MimeTypeUtils.TEXT_PLAIN, this.strategies)
.route("toA")
@@ -128,7 +127,7 @@ public class MetadataEncoderTests {
}
@Test
public void routeWithVars() {
void routeWithVars() {
DataBuffer buffer =
new MetadataEncoder(MimeTypeUtils.TEXT_PLAIN, this.strategies)
.route("a.{b}.{c}.d", "BBB", "C.C.C")
@@ -139,7 +138,7 @@ public class MetadataEncoderTests {
}
@Test
public void metadataWithTextPlainMimeType() {
void metadataWithTextPlainMimeType() {
DataBuffer buffer =
new MetadataEncoder(MimeTypeUtils.TEXT_PLAIN, this.strategies)
.metadata(Unpooled.wrappedBuffer("Raw data".getBytes(UTF_8)), null)
@@ -150,7 +149,7 @@ public class MetadataEncoderTests {
}
@Test
public void metadataWithByteBuf() {
void metadataWithByteBuf() {
DataBuffer buffer =
new MetadataEncoder(MimeTypeUtils.TEXT_PLAIN, this.strategies)
.metadata("toA", null)
@@ -161,7 +160,7 @@ public class MetadataEncoderTests {
}
@Test
public void compositeRequiredForMultipleEntries() {
void compositeRequiredForMultipleEntries() {
// Route, metadata
MetadataEncoder encoder1 = new MetadataEncoder(MimeTypeUtils.TEXT_PLAIN, this.strategies);
@@ -186,7 +185,7 @@ public class MetadataEncoderTests {
}
@Test
public void mimeTypeRequiredForCompositeEntries() {
void mimeTypeRequiredForCompositeEntries() {
MetadataEncoder encoder = new MetadataEncoder(COMPOSITE_METADATA, this.strategies);
assertThatThrownBy(() -> encoder.metadata("toA", null))
@@ -194,7 +193,7 @@ public class MetadataEncoderTests {
}
@Test
public void mimeTypeDoesNotMatchConnectionMetadataMimeType() {
void mimeTypeDoesNotMatchConnectionMetadataMimeType() {
MetadataEncoder encoder = new MetadataEncoder(MimeTypeUtils.TEXT_PLAIN, this.strategies);
assertThatThrownBy(() -> encoder.metadata("toA", MimeTypeUtils.APPLICATION_JSON))
@@ -203,7 +202,7 @@ public class MetadataEncoderTests {
}
@Test
public void defaultDataBufferFactory() {
void defaultDataBufferFactory() {
DefaultDataBufferFactory bufferFactory = DefaultDataBufferFactory.sharedInstance;
RSocketStrategies strategies = RSocketStrategies.builder().dataBufferFactory(bufferFactory).build();
@@ -213,7 +212,7 @@ public class MetadataEncoderTests {
.block();
ByteBuf byteBuf = new NettyDataBufferFactory(ByteBufAllocator.DEFAULT)
.wrap(buffer.asByteBuffer())
.wrap(buffer.toByteBuffer())
.getNativeBuffer();
CompositeMetadata entries = new CompositeMetadata(byteBuf, false);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,23 +37,24 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* Unit tests for {@link PayloadUtils}.
*
* @author Rossen Stoyanchev
* @since 5.2
*/
public class PayloadUtilsTests {
class PayloadUtilsTests {
private LeakAwareNettyDataBufferFactory nettyBufferFactory =
new LeakAwareNettyDataBufferFactory(PooledByteBufAllocator.DEFAULT);
@AfterEach
public void tearDown() throws Exception {
void tearDown() throws Exception {
this.nettyBufferFactory.checkForLeaks(Duration.ofSeconds(5));
}
@Test
public void retainAndReleaseWithNettyFactory() {
void retainAndReleaseWithNettyFactory() {
Payload payload = ByteBufPayload.create("sample data");
DataBuffer buffer = PayloadUtils.retainDataAndReleasePayload(payload, this.nettyBufferFactory);
try {
@@ -67,7 +68,7 @@ public class PayloadUtilsTests {
}
@Test
public void retainAndReleaseWithDefaultFactory() {
void retainAndReleaseWithDefaultFactory() {
Payload payload = ByteBufPayload.create("sample data");
DataBuffer buffer = PayloadUtils.retainDataAndReleasePayload(payload, DefaultDataBufferFactory.sharedInstance);
@@ -76,7 +77,7 @@ public class PayloadUtilsTests {
}
@Test
public void createWithNettyBuffers() {
void createWithNettyBuffers() {
NettyDataBuffer data = createNettyDataBuffer("sample data");
NettyDataBuffer metadata = createNettyDataBuffer("sample metadata");
@@ -92,7 +93,7 @@ public class PayloadUtilsTests {
}
@Test
public void createWithDefaultBuffers() {
void createWithDefaultBuffers() {
DataBuffer data = createDefaultDataBuffer("sample data");
DataBuffer metadata = createDefaultDataBuffer("sample metadata");
Payload payload = PayloadUtils.createPayload(data, metadata);
@@ -103,7 +104,7 @@ public class PayloadUtilsTests {
}
@Test
public void createWithNettyAndDefaultBuffers() {
void createWithNettyAndDefaultBuffers() {
NettyDataBuffer data = createNettyDataBuffer("sample data");
DefaultDataBuffer metadata = createDefaultDataBuffer("sample metadata");
Payload payload = PayloadUtils.createPayload(data, metadata);
@@ -118,7 +119,7 @@ public class PayloadUtilsTests {
}
@Test
public void createWithDefaultAndNettyBuffers() {
void createWithDefaultAndNettyBuffers() {
DefaultDataBuffer data = createDefaultDataBuffer("sample data");
NettyDataBuffer metadata = createNettyDataBuffer("sample metadata");
Payload payload = PayloadUtils.createPayload(data, metadata);
@@ -133,7 +134,7 @@ public class PayloadUtilsTests {
}
@Test
public void createWithNettyBuffer() {
void createWithNettyBuffer() {
NettyDataBuffer data = createNettyDataBuffer("sample data");
Payload payload = PayloadUtils.createPayload(data);
try {
@@ -146,7 +147,7 @@ public class PayloadUtilsTests {
}
@Test
public void createWithDefaultBuffer() {
void createWithDefaultBuffer() {
DataBuffer data = createDefaultDataBuffer("sample data");
Payload payload = PayloadUtils.createPayload(data);
@@ -162,7 +163,7 @@ public class PayloadUtilsTests {
}
private DefaultDataBuffer createDefaultDataBuffer(String content) {
DefaultDataBuffer buffer = DefaultDataBufferFactory.sharedInstance.allocateBuffer();
DefaultDataBuffer buffer = DefaultDataBufferFactory.sharedInstance.allocateBuffer(256);
buffer.write(content, StandardCharsets.UTF_8);
return buffer;
}