Skip to content

Commit 13dbf66

Browse files
committed
Fix tests
1 parent d07891b commit 13dbf66

3 files changed

Lines changed: 12 additions & 15 deletions

File tree

core-java/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@
1313
*.ear
1414

1515
# Files generated by integration tests
16-
*.txt

core-java/src/test/java/com/baeldung/java/nio2/async/AsyncFileTest.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.baeldung.java.nio2.async;
22

3-
import static org.junit.Assert.assertEquals;
3+
import org.junit.Test;
44

55
import java.io.IOException;
66
import java.net.URI;
@@ -11,22 +11,22 @@
1111
import java.nio.file.Paths;
1212
import java.nio.file.StandardOpenOption;
1313
import java.util.UUID;
14+
import java.util.concurrent.ExecutionException;
1415
import java.util.concurrent.Future;
1516

16-
import org.junit.Test;
17+
import static org.junit.Assert.assertEquals;
1718

1819
public 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();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
baeldung.com

0 commit comments

Comments
 (0)