testing work

This commit is contained in:
Eugen Paraschiv 2018-03-04 17:28:06 +02:00
parent 358f836ecf
commit 7cc57756e1
3 changed files with 28 additions and 33 deletions

View File

@ -1,7 +1,4 @@
0.* 0.*
# Files generated by integration tests # Files generated by integration tests
*.txt # *.txt
backup-pom.xml
/bin/
/temp

View File

@ -17,18 +17,19 @@ import java.util.concurrent.Future;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class AsyncFileIntegrationTest { public class AsyncFileIntegrationTest {
@Test @Test
public void givenPath_whenReadsContentWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException { public void givenPath_whenReadsContentWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException {
Path path = Paths.get(URI.create(this.getClass().getClassLoader().getResource("file.txt").toString())); final Path path = Paths.get(URI.create(this.getClass().getClassLoader().getResource("file.txt").toString()));
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
ByteBuffer buffer = ByteBuffer.allocate(1024); final ByteBuffer buffer = ByteBuffer.allocate(1024);
Future<Integer> operation = fileChannel.read(buffer, 0); final Future<Integer> operation = fileChannel.read(buffer, 0);
operation.get(); operation.get();
String fileContent = new String(buffer.array()).trim(); final String fileContent = new String(buffer.array()).trim();
buffer.clear(); buffer.clear();
assertEquals(fileContent, "baeldung.com"); assertEquals(fileContent, "baeldung.com");
@ -36,18 +37,16 @@ public class AsyncFileIntegrationTest {
@Test @Test
public void givenPath_whenReadsContentWithCompletionHandler_thenCorrect() throws IOException { public void givenPath_whenReadsContentWithCompletionHandler_thenCorrect() throws IOException {
Path path = Paths.get(URI.create(AsyncFileIntegrationTest.class.getResource("/file.txt").toString())); final Path path = Paths.get(URI.create(this.getClass().getClassLoader().getResource("file.txt").toString()));
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); 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<Integer, ByteBuffer>() { fileChannel.read(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
@Override @Override
public void completed(Integer result, ByteBuffer attachment) { public void completed(Integer result, ByteBuffer attachment) {
// result is number of bytes read // result is number of bytes read
// attachment is the buffer // attachment is the buffer
} }
@Override @Override
@ -59,42 +58,40 @@ public class AsyncFileIntegrationTest {
@Test @Test
public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException { public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException {
String fileName = "temp"; final String fileName = "temp";
Path path = Paths.get(fileName); final Path path = Paths.get(fileName);
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE); final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
ByteBuffer buffer = ByteBuffer.allocate(1024); final ByteBuffer buffer = ByteBuffer.allocate(1024);
long position = 0; final long position = 0;
buffer.put("hello world".getBytes()); buffer.put("hello world".getBytes());
buffer.flip(); buffer.flip();
Future<Integer> operation = fileChannel.write(buffer, position); final Future<Integer> operation = fileChannel.write(buffer, position);
buffer.clear(); buffer.clear();
operation.get(); operation.get();
String content = readContent(path); final String content = readContent(path);
assertEquals("hello world", content); assertEquals("hello world", content);
} }
@Test @Test
public void givenPathAndContent_whenWritesToFileWithHandler_thenCorrect() throws IOException { public void givenPathAndContent_whenWritesToFileWithHandler_thenCorrect() throws IOException {
String fileName = UUID.randomUUID().toString(); final String fileName = UUID.randomUUID().toString();
Path path = Paths.get(fileName); final Path path = Paths.get(fileName);
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE); 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.put("hello world".getBytes());
buffer.flip(); buffer.flip();
fileChannel.write(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() { fileChannel.write(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
@Override @Override
public void completed(Integer result, ByteBuffer attachment) { public void completed(Integer result, ByteBuffer attachment) {
// result is number of bytes written // result is number of bytes written
// attachment is the buffer // attachment is the buffer
} }
@Override @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; AsynchronousFileChannel fileChannel = null;
try { try {
fileChannel = AsynchronousFileChannel.open(file, StandardOpenOption.READ); fileChannel = AsynchronousFileChannel.open(file, StandardOpenOption.READ);
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
ByteBuffer buffer = ByteBuffer.allocate(1024); final ByteBuffer buffer = ByteBuffer.allocate(1024);
Future<Integer> operation = fileChannel.read(buffer, 0); final Future<Integer> operation = fileChannel.read(buffer, 0);
operation.get(); operation.get();
String fileContent = new String(buffer.array()).trim(); final String fileContent = new String(buffer.array()).trim();
buffer.clear(); buffer.clear();
return fileContent; return fileContent;
} }
} }

View File

@ -1 +0,0 @@
baeldung.com