11package com .baeldung .java .nio2 .async ;
22
3- import static org .junit .Assert . assertEquals ;
3+ import org .junit .Test ;
44
55import java .io .IOException ;
66import java .net .URI ;
1111import java .nio .file .Paths ;
1212import java .nio .file .StandardOpenOption ;
1313import java .util .UUID ;
14+ import java .util .concurrent .ExecutionException ;
1415import java .util .concurrent .Future ;
1516
16- import org .junit .Test ;
17+ import static org .junit .Assert . assertEquals ;
1718
1819public class AsyncFileTest {
1920 @ Test
20- public void givenPath_whenReadsContentWithFuture_thenCorrect () throws IOException {
21- Path path = Paths .get (URI .create (new AsyncFileTest ().getClass ().getResource ("/ file.txt" ).toString ()));
21+ public void givenPath_whenReadsContentWithFuture_thenCorrect () throws IOException , ExecutionException , InterruptedException {
22+ Path path = Paths .get (URI .create (this . getClass ().getClassLoader ().getResource ("file.txt" ).toString ()));
2223 AsynchronousFileChannel fileChannel = AsynchronousFileChannel .open (path , StandardOpenOption .READ );
2324
2425 ByteBuffer buffer = ByteBuffer .allocate (1024 );
2526
2627 Future <Integer > operation = fileChannel .read (buffer , 0 );
2728
28- while (!operation .isDone ())
29- ;
29+ operation .get ();
3030
3131 String fileContent = new String (buffer .array ()).trim ();
3232 buffer .clear ();
@@ -36,7 +36,7 @@ public void givenPath_whenReadsContentWithFuture_thenCorrect() throws IOExceptio
3636
3737 @ Test
3838 public void givenPath_whenReadsContentWithCompletionHandler_thenCorrect () throws IOException {
39- Path path = Paths .get (URI .create (new AsyncFileTest (). getClass () .getResource ("/file.txt" ).toString ()));
39+ Path path = Paths .get (URI .create (AsyncFileTest . class .getResource ("/file.txt" ).toString ()));
4040 AsynchronousFileChannel fileChannel = AsynchronousFileChannel .open (path , StandardOpenOption .READ );
4141
4242 ByteBuffer buffer = ByteBuffer .allocate (1024 );
@@ -58,7 +58,7 @@ public void failed(Throwable exc, ByteBuffer attachment) {
5858 }
5959
6060 @ Test
61- public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect () throws IOException {
61+ public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect () throws IOException , ExecutionException , InterruptedException {
6262 String fileName = UUID .randomUUID ().toString ();
6363 Path path = Paths .get (fileName );
6464 AsynchronousFileChannel fileChannel = AsynchronousFileChannel .open (path , StandardOpenOption .WRITE , StandardOpenOption .CREATE ,StandardOpenOption .DELETE_ON_CLOSE );
@@ -72,9 +72,7 @@ public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect() throws
7272 Future <Integer > operation = fileChannel .write (buffer , position );
7373 buffer .clear ();
7474
75- while (!operation .isDone ()) {
76-
77- }
75+ operation .get ();
7876
7977 String content = readContent (path );
8078 assertEquals ("hello world" , content );
@@ -107,7 +105,7 @@ public void failed(Throwable exc, ByteBuffer attachment) {
107105 });
108106 }
109107
110- public static String readContent (Path file ) {
108+ public static String readContent (Path file ) throws ExecutionException , InterruptedException {
111109 AsynchronousFileChannel fileChannel = null ;
112110 try {
113111 fileChannel = AsynchronousFileChannel .open (file , StandardOpenOption .READ );
@@ -120,8 +118,7 @@ public static String readContent(Path file) {
120118
121119 Future <Integer > operation = fileChannel .read (buffer , 0 );
122120
123- while (!operation .isDone ())
124- ;
121+ operation .get ();
125122
126123 String fileContent = new String (buffer .array ()).trim ();
127124 buffer .clear ();
0 commit comments