Fix tests
This commit is contained in:
parent
d07891bfc8
commit
13dbf6603b
|
@ -13,4 +13,3 @@
|
||||||
*.ear
|
*.ear
|
||||||
|
|
||||||
# Files generated by integration tests
|
# Files generated by integration tests
|
||||||
*.txt
|
|
|
@ -1,6 +1,6 @@
|
||||||
package com.baeldung.java.nio2.async;
|
package com.baeldung.java.nio2.async;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
@ -11,22 +11,22 @@ import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
import org.junit.Test;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class AsyncFileTest {
|
public class AsyncFileTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenPath_whenReadsContentWithFuture_thenCorrect() throws IOException {
|
public void givenPath_whenReadsContentWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException {
|
||||||
Path path = Paths.get(URI.create(new AsyncFileTest().getClass().getResource("/file.txt").toString()));
|
Path path = Paths.get(URI.create(this.getClass().getClassLoader().getResource("file.txt").toString()));
|
||||||
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
|
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
|
||||||
|
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||||
|
|
||||||
Future<Integer> operation = fileChannel.read(buffer, 0);
|
Future<Integer> operation = fileChannel.read(buffer, 0);
|
||||||
|
|
||||||
while (!operation.isDone())
|
operation.get();
|
||||||
;
|
|
||||||
|
|
||||||
String fileContent = new String(buffer.array()).trim();
|
String fileContent = new String(buffer.array()).trim();
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
|
@ -36,7 +36,7 @@ public class AsyncFileTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPath_whenReadsContentWithCompletionHandler_thenCorrect() throws IOException {
|
public void givenPath_whenReadsContentWithCompletionHandler_thenCorrect() throws IOException {
|
||||||
Path path = Paths.get(URI.create(new AsyncFileTest().getClass().getResource("/file.txt").toString()));
|
Path path = Paths.get(URI.create(AsyncFileTest.class.getResource("/file.txt").toString()));
|
||||||
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
|
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
|
||||||
|
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||||
|
@ -58,7 +58,7 @@ public class AsyncFileTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect() throws IOException {
|
public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException {
|
||||||
String fileName = UUID.randomUUID().toString();
|
String fileName = UUID.randomUUID().toString();
|
||||||
Path path = Paths.get(fileName);
|
Path path = Paths.get(fileName);
|
||||||
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE,StandardOpenOption.DELETE_ON_CLOSE);
|
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE,StandardOpenOption.DELETE_ON_CLOSE);
|
||||||
|
@ -72,9 +72,7 @@ public class AsyncFileTest {
|
||||||
Future<Integer> operation = fileChannel.write(buffer, position);
|
Future<Integer> operation = fileChannel.write(buffer, position);
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
|
|
||||||
while (!operation.isDone()) {
|
operation.get();
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
String content = readContent(path);
|
String content = readContent(path);
|
||||||
assertEquals("hello world", content);
|
assertEquals("hello world", content);
|
||||||
|
@ -107,7 +105,7 @@ public class AsyncFileTest {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String readContent(Path file) {
|
public static String readContent(Path file) throws ExecutionException, InterruptedException {
|
||||||
AsynchronousFileChannel fileChannel = null;
|
AsynchronousFileChannel fileChannel = null;
|
||||||
try {
|
try {
|
||||||
fileChannel = AsynchronousFileChannel.open(file, StandardOpenOption.READ);
|
fileChannel = AsynchronousFileChannel.open(file, StandardOpenOption.READ);
|
||||||
|
@ -120,8 +118,7 @@ public class AsyncFileTest {
|
||||||
|
|
||||||
Future<Integer> operation = fileChannel.read(buffer, 0);
|
Future<Integer> operation = fileChannel.read(buffer, 0);
|
||||||
|
|
||||||
while (!operation.isDone())
|
operation.get();
|
||||||
;
|
|
||||||
|
|
||||||
String fileContent = new String(buffer.array()).trim();
|
String fileContent = new String(buffer.array()).trim();
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
baeldung.com
|
Loading…
Reference in New Issue