Merge pull request #8843 from rojyates/BAEL-3603-Java-IO-versus-NIO

BAEL-3603 Add test examples for Java IO versus NIO article
This commit is contained in:
Greg 2020-03-18 15:50:22 -04:00 committed by GitHub
commit 1528acf120
3 changed files with 154 additions and 0 deletions

View File

@ -47,6 +47,14 @@
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.tomakehurst/wiremock -->
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.26.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,52 @@
package com.baeldung.blockingnonblocking;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.io.*;
import java.net.Socket;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.Assert.assertTrue;
public class BlockingClientUnitTest {
private static final String REQUESTED_RESOURCE = "/test.json";
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
@Before
public void setup() {
stubFor(get(urlEqualTo(REQUESTED_RESOURCE)).willReturn(aResponse()
.withStatus(200)
.withBody("{ \"response\" : \"It worked!\" }\r\n\r\n")));
}
@Test
public void givenJavaIOSocket_whenReadingAndWritingWithStreams_thenSuccess() throws IOException {
// given an IO socket and somewhere to store our result
Socket socket = new Socket("localhost", wireMockRule.port());
StringBuilder ourStore = new StringBuilder();
// when we write and read (using try-with-resources so our resources are auto-closed)
try (InputStream serverInput = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(serverInput));
OutputStream clientOutput = socket.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(clientOutput))) {
writer.print("GET " + REQUESTED_RESOURCE + " HTTP/1.0\r\n\r\n");
writer.flush(); // important - without this the request is never sent, and the test will hang on readLine()
for (String line; (line = reader.readLine()) != null; ) {
ourStore.append(line);
ourStore.append(System.lineSeparator());
}
}
// then we read and saved our data
assertTrue(ourStore
.toString()
.contains("It worked!"));
}
}

View File

@ -0,0 +1,94 @@
package com.baeldung.blockingnonblocking;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.Assert.assertTrue;
public class NonBlockingClientUnitTest {
private String REQUESTED_RESOURCE = "/test.json";
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
@Before
public void setup() {
stubFor(get(urlEqualTo(REQUESTED_RESOURCE)).willReturn(aResponse()
.withStatus(200)
.withBody("{ \"response\" : \"It worked!\" }")));
}
@Test
public void givenJavaNIOSocketChannel_whenReadingAndWritingWithBuffers_thenSuccess() throws IOException {
// given a NIO SocketChannel and a charset
InetSocketAddress address = new InetSocketAddress("localhost", wireMockRule.port());
SocketChannel socketChannel = SocketChannel.open(address);
Charset charset = StandardCharsets.UTF_8;
// when we write and read using buffers
socketChannel.write(charset.encode(CharBuffer.wrap("GET " + REQUESTED_RESOURCE + " HTTP/1.0\r\n\r\n")));
ByteBuffer byteBuffer = ByteBuffer.allocate(8192); // or allocateDirect if we need direct memory access
CharBuffer charBuffer = CharBuffer.allocate(8192);
CharsetDecoder charsetDecoder = charset.newDecoder();
StringBuilder ourStore = new StringBuilder();
while (socketChannel.read(byteBuffer) != -1 || byteBuffer.position() > 0) {
byteBuffer.flip();
storeBufferContents(byteBuffer, charBuffer, charsetDecoder, ourStore);
byteBuffer.compact();
}
socketChannel.close();
// then we read and saved our data
assertTrue(ourStore
.toString()
.contains("It worked!"));
}
@Test
public void givenJavaNIOSocketChannel_whenReadingAndWritingWithSmallBuffers_thenSuccess() throws IOException {
// given a NIO SocketChannel and a charset
InetSocketAddress address = new InetSocketAddress("localhost", wireMockRule.port());
SocketChannel socketChannel = SocketChannel.open(address);
Charset charset = StandardCharsets.UTF_8;
// when we write and read using buffers that are too small for our message
socketChannel.write(charset.encode(CharBuffer.wrap("GET " + REQUESTED_RESOURCE + " HTTP/1.0\r\n\r\n")));
ByteBuffer byteBuffer = ByteBuffer.allocate(8); // or allocateDirect if we need direct memory access
CharBuffer charBuffer = CharBuffer.allocate(8);
CharsetDecoder charsetDecoder = charset.newDecoder();
StringBuilder ourStore = new StringBuilder();
while (socketChannel.read(byteBuffer) != -1 || byteBuffer.position() > 0) {
byteBuffer.flip();
storeBufferContents(byteBuffer, charBuffer, charsetDecoder, ourStore);
byteBuffer.compact();
}
socketChannel.close();
// then we read and saved our data
assertTrue(ourStore
.toString()
.contains("It worked!"));
}
void storeBufferContents(ByteBuffer byteBuffer, CharBuffer charBuffer, CharsetDecoder charsetDecoder, StringBuilder ourStore) {
charsetDecoder.decode(byteBuffer, charBuffer, true);
charBuffer.flip();
ourStore.append(charBuffer);
charBuffer.clear();
}
}