diff --git a/core-java/.gitignore b/core-java/.gitignore index 6ecc6405c2..bb70a5d3eb 100644 --- a/core-java/.gitignore +++ b/core-java/.gitignore @@ -13,4 +13,3 @@ *.ear # Files generated by integration tests -*.txt \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncFileTest.java b/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncFileTest.java index db30d32210..948c93ff0b 100644 --- a/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncFileTest.java +++ b/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncFileTest.java @@ -1,6 +1,6 @@ package com.baeldung.java.nio2.async; -import static org.junit.Assert.assertEquals; +import org.junit.Test; import java.io.IOException; import java.net.URI; @@ -11,22 +11,22 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.UUID; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -import org.junit.Test; +import static org.junit.Assert.assertEquals; public class AsyncFileTest { @Test - public void givenPath_whenReadsContentWithFuture_thenCorrect() throws IOException { - Path path = Paths.get(URI.create(new AsyncFileTest().getClass().getResource("/file.txt").toString())); + public void givenPath_whenReadsContentWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException { + Path path = Paths.get(URI.create(this.getClass().getClassLoader().getResource("file.txt").toString())); AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); ByteBuffer buffer = ByteBuffer.allocate(1024); Future operation = fileChannel.read(buffer, 0); - while (!operation.isDone()) - ; + operation.get(); String fileContent = new String(buffer.array()).trim(); buffer.clear(); @@ -36,7 +36,7 @@ public class AsyncFileTest { @Test 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); ByteBuffer buffer = ByteBuffer.allocate(1024); @@ -58,7 +58,7 @@ public class AsyncFileTest { } @Test - public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect() throws IOException { + public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException { String fileName = UUID.randomUUID().toString(); Path path = Paths.get(fileName); AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE,StandardOpenOption.DELETE_ON_CLOSE); @@ -72,9 +72,7 @@ public class AsyncFileTest { Future operation = fileChannel.write(buffer, position); buffer.clear(); - while (!operation.isDone()) { - - } + operation.get(); String content = readContent(path); 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; try { fileChannel = AsynchronousFileChannel.open(file, StandardOpenOption.READ); @@ -120,8 +118,7 @@ public class AsyncFileTest { Future operation = fileChannel.read(buffer, 0); - while (!operation.isDone()) - ; + operation.get(); String fileContent = new String(buffer.array()).trim(); buffer.clear(); diff --git a/core-java/src/test/resources/file.txt b/core-java/src/test/resources/file.txt new file mode 100644 index 0000000000..558d8bbf35 --- /dev/null +++ b/core-java/src/test/resources/file.txt @@ -0,0 +1 @@ +baeldung.com \ No newline at end of file