Skip to content

Commit 7cc5775

Browse files
author
Eugen Paraschiv
committed
testing work
1 parent 358f836 commit 7cc5775

3 files changed

Lines changed: 28 additions & 33 deletions

File tree

‎core-java-io/.gitignore‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
0.*
22

33
# Files generated by integration tests
4-
*.txt
5-
backup-pom.xml
6-
/bin/
7-
/temp
4+
# *.txt

‎core-java-io/src/test/java/com/baeldung/java/nio2/async/AsyncFileIntegrationTest.java‎

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,36 @@
1717
import static org.junit.Assert.assertEquals;
1818

1919
public class AsyncFileIntegrationTest {
20+
2021
@Test
2122
public void givenPath_whenReadsContentWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException {
22-
Path path = Paths.get(URI.create(this.getClass().getClassLoader().getResource("file.txt").toString()));
23-
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
23+
final Path path = Paths.get(URI.create(this.getClass().getClassLoader().getResource("file.txt").toString()));
24+
final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
2425

25-
ByteBuffer buffer = ByteBuffer.allocate(1024);
26+
final ByteBuffer buffer = ByteBuffer.allocate(1024);
2627

27-
Future<Integer> operation = fileChannel.read(buffer, 0);
28+
final Future<Integer> operation = fileChannel.read(buffer, 0);
2829

2930
operation.get();
3031

31-
String fileContent = new String(buffer.array()).trim();
32+
final String fileContent = new String(buffer.array()).trim();
3233
buffer.clear();
3334

3435
assertEquals(fileContent, "baeldung.com");
3536
}
3637

3738
@Test
3839
public void givenPath_whenReadsContentWithCompletionHandler_thenCorrect() throws IOException {
39-
Path path = Paths.get(URI.create(AsyncFileIntegrationTest.class.getResource("/file.txt").toString()));
40-
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
40+
final Path path = Paths.get(URI.create(this.getClass().getClassLoader().getResource("file.txt").toString()));
41+
final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
4142

42-
ByteBuffer buffer = ByteBuffer.allocate(1024);
43+
final ByteBuffer buffer = ByteBuffer.allocate(1024);
4344

4445
fileChannel.read(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
45-
4646
@Override
4747
public void completed(Integer result, ByteBuffer attachment) {
4848
// result is number of bytes read
4949
// attachment is the buffer
50-
5150
}
5251

5352
@Override
@@ -59,42 +58,40 @@ public void failed(Throwable exc, ByteBuffer attachment) {
5958

6059
@Test
6160
public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException {
62-
String fileName = "temp";
63-
Path path = Paths.get(fileName);
64-
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
61+
final String fileName = "temp";
62+
final Path path = Paths.get(fileName);
63+
final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
6564

66-
ByteBuffer buffer = ByteBuffer.allocate(1024);
67-
long position = 0;
65+
final ByteBuffer buffer = ByteBuffer.allocate(1024);
66+
final long position = 0;
6867

6968
buffer.put("hello world".getBytes());
7069
buffer.flip();
7170

72-
Future<Integer> operation = fileChannel.write(buffer, position);
71+
final Future<Integer> operation = fileChannel.write(buffer, position);
7372
buffer.clear();
7473

7574
operation.get();
7675

77-
String content = readContent(path);
76+
final String content = readContent(path);
7877
assertEquals("hello world", content);
7978
}
8079

8180
@Test
8281
public void givenPathAndContent_whenWritesToFileWithHandler_thenCorrect() throws IOException {
83-
String fileName = UUID.randomUUID().toString();
84-
Path path = Paths.get(fileName);
85-
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE);
82+
final String fileName = UUID.randomUUID().toString();
83+
final Path path = Paths.get(fileName);
84+
final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE);
8685

87-
ByteBuffer buffer = ByteBuffer.allocate(1024);
86+
final ByteBuffer buffer = ByteBuffer.allocate(1024);
8887
buffer.put("hello world".getBytes());
8988
buffer.flip();
9089

9190
fileChannel.write(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
92-
9391
@Override
9492
public void completed(Integer result, ByteBuffer attachment) {
9593
// result is number of bytes written
9694
// attachment is the buffer
97-
9895
}
9996

10097
@Override
@@ -104,23 +101,25 @@ public void failed(Throwable exc, ByteBuffer attachment) {
104101
});
105102
}
106103

107-
public static String readContent(Path file) throws ExecutionException, InterruptedException {
104+
//
105+
106+
private String readContent(Path file) throws ExecutionException, InterruptedException {
108107
AsynchronousFileChannel fileChannel = null;
109108
try {
110109
fileChannel = AsynchronousFileChannel.open(file, StandardOpenOption.READ);
111110
} catch (IOException e) {
112-
// TODO Auto-generated catch block
113111
e.printStackTrace();
114112
}
115113

116-
ByteBuffer buffer = ByteBuffer.allocate(1024);
114+
final ByteBuffer buffer = ByteBuffer.allocate(1024);
117115

118-
Future<Integer> operation = fileChannel.read(buffer, 0);
116+
final Future<Integer> operation = fileChannel.read(buffer, 0);
119117

120118
operation.get();
121119

122-
String fileContent = new String(buffer.array()).trim();
120+
final String fileContent = new String(buffer.array()).trim();
123121
buffer.clear();
124122
return fileContent;
125123
}
124+
126125
}

‎core-java/src/test/resources/file.txt‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)