diff --git a/core-java-io/.gitignore b/core-java-io/.gitignore index 520f176acc..13d4d1f833 100644 --- a/core-java-io/.gitignore +++ b/core-java-io/.gitignore @@ -1,7 +1,4 @@ 0.* # Files generated by integration tests -*.txt -backup-pom.xml -/bin/ -/temp +# *.txt diff --git a/core-java-io/src/test/java/com/baeldung/java/nio2/async/AsyncFileIntegrationTest.java b/core-java-io/src/test/java/com/baeldung/java/nio2/async/AsyncFileIntegrationTest.java index e2f7a0303a..cf37b92565 100644 --- a/core-java-io/src/test/java/com/baeldung/java/nio2/async/AsyncFileIntegrationTest.java +++ b/core-java-io/src/test/java/com/baeldung/java/nio2/async/AsyncFileIntegrationTest.java @@ -17,18 +17,19 @@ import java.util.concurrent.Future; import static org.junit.Assert.assertEquals; public class AsyncFileIntegrationTest { + @Test 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); + final Path path = Paths.get(URI.create(this.getClass().getClassLoader().getResource("file.txt").toString())); + final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); - ByteBuffer buffer = ByteBuffer.allocate(1024); + final ByteBuffer buffer = ByteBuffer.allocate(1024); - Future operation = fileChannel.read(buffer, 0); + final Future operation = fileChannel.read(buffer, 0); operation.get(); - String fileContent = new String(buffer.array()).trim(); + final String fileContent = new String(buffer.array()).trim(); buffer.clear(); assertEquals(fileContent, "baeldung.com"); @@ -36,18 +37,16 @@ public class AsyncFileIntegrationTest { @Test public void givenPath_whenReadsContentWithCompletionHandler_thenCorrect() throws IOException { - Path path = Paths.get(URI.create(AsyncFileIntegrationTest.class.getResource("/file.txt").toString())); - AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); + final Path path = Paths.get(URI.create(this.getClass().getClassLoader().getResource("file.txt").toString())); + final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); - ByteBuffer buffer = ByteBuffer.allocate(1024); + final ByteBuffer buffer = ByteBuffer.allocate(1024); fileChannel.read(buffer, 0, buffer, new CompletionHandler() { - @Override public void completed(Integer result, ByteBuffer attachment) { // result is number of bytes read // attachment is the buffer - } @Override @@ -59,42 +58,40 @@ public class AsyncFileIntegrationTest { @Test public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException { - String fileName = "temp"; - Path path = Paths.get(fileName); - AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE); + final String fileName = "temp"; + final Path path = Paths.get(fileName); + final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE); - ByteBuffer buffer = ByteBuffer.allocate(1024); - long position = 0; + final ByteBuffer buffer = ByteBuffer.allocate(1024); + final long position = 0; buffer.put("hello world".getBytes()); buffer.flip(); - Future operation = fileChannel.write(buffer, position); + final Future operation = fileChannel.write(buffer, position); buffer.clear(); operation.get(); - String content = readContent(path); + final String content = readContent(path); assertEquals("hello world", content); } @Test public void givenPathAndContent_whenWritesToFileWithHandler_thenCorrect() throws IOException { - String fileName = UUID.randomUUID().toString(); - Path path = Paths.get(fileName); - AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE); + final String fileName = UUID.randomUUID().toString(); + final Path path = Paths.get(fileName); + final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE); - ByteBuffer buffer = ByteBuffer.allocate(1024); + final ByteBuffer buffer = ByteBuffer.allocate(1024); buffer.put("hello world".getBytes()); buffer.flip(); fileChannel.write(buffer, 0, buffer, new CompletionHandler() { - @Override public void completed(Integer result, ByteBuffer attachment) { // result is number of bytes written // attachment is the buffer - } @Override @@ -104,23 +101,25 @@ public class AsyncFileIntegrationTest { }); } - public static String readContent(Path file) throws ExecutionException, InterruptedException { + // + + private String readContent(Path file) throws ExecutionException, InterruptedException { AsynchronousFileChannel fileChannel = null; try { fileChannel = AsynchronousFileChannel.open(file, StandardOpenOption.READ); } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } - ByteBuffer buffer = ByteBuffer.allocate(1024); + final ByteBuffer buffer = ByteBuffer.allocate(1024); - Future operation = fileChannel.read(buffer, 0); + final Future operation = fileChannel.read(buffer, 0); operation.get(); - String fileContent = new String(buffer.array()).trim(); + final String fileContent = new String(buffer.array()).trim(); buffer.clear(); return fileContent; } + } \ No newline at end of file diff --git a/core-java/src/test/resources/file.txt b/core-java/src/test/resources/file.txt deleted file mode 100644 index 558d8bbf35..0000000000 --- a/core-java/src/test/resources/file.txt +++ /dev/null @@ -1 +0,0 @@ -baeldung.com \ No newline at end of file