changes as per review comments

This commit is contained in:
Anurag Goyal 2019-04-11 19:06:41 +05:30
parent d9540eff75
commit fece0dc4b8
1 changed files with 111 additions and 119 deletions

View File

@ -12,7 +12,6 @@ import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer; import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.channels.FileLock; import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import org.junit.Test; import org.junit.Test;
@ -37,7 +36,9 @@ public class FileChannelUnitTest {
buff.clear(); buff.clear();
} }
assertEquals("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8)); String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8);
assertEquals("Hello world", fileContent);
} }
} }
@ -58,9 +59,9 @@ public class FileChannelUnitTest {
out.write(buff.array(), 0, buff.position()); out.write(buff.array(), 0, buff.position());
buff.clear(); buff.clear();
} }
String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8);
assertEquals("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8)); assertEquals("Hello world", fileContent);
} }
} }
@ -77,9 +78,7 @@ public class FileChannelUnitTest {
byte[] data = new byte[buff.remaining()]; byte[] data = new byte[buff.remaining()];
buff.get(data); buff.get(data);
assertEquals("world", new String(data, StandardCharsets.UTF_8)); assertEquals("world", new String(data, StandardCharsets.UTF_8));
} }
} }
} }
@ -90,11 +89,9 @@ public class FileChannelUnitTest {
FileChannel channel = writer.getChannel();) { FileChannel channel = writer.getChannel();) {
ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8)); ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8));
if (buff.hasRemaining()) {
channel.write(buff); channel.write(buff);
}
// verify // now we verify whether the file was written correctly
RandomAccessFile reader = new RandomAccessFile(file, "r"); RandomAccessFile reader = new RandomAccessFile(file, "r");
assertEquals("Hello world", reader.readLine()); assertEquals("Hello world", reader.readLine());
reader.close(); reader.close();
@ -108,11 +105,7 @@ public class FileChannelUnitTest {
FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE);) { FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE);) {
assertNotNull(fileLock); assertNotNull(fileLock);
} catch (OverlappingFileLockException | IOException ex) {
} }
} }
@Test @Test
@ -133,11 +126,11 @@ public class FileChannelUnitTest {
buff.clear(); buff.clear();
} }
// the original file is 11 bytes long, so that's where the position pointer should be
assertEquals(11, channel.position()); assertEquals(11, channel.position());
channel.position(4); channel.position(4);
assertEquals(4, channel.position()); assertEquals(4, channel.position());
} }
} }
@ -147,10 +140,9 @@ public class FileChannelUnitTest {
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
FileChannel channel = reader.getChannel();) { FileChannel channel = reader.getChannel();) {
long imageFileSize = channel.size(); // the original file is 11 bytes long, so that's where the position pointer should be
assertEquals(11, imageFileSize); assertEquals(11, channel.size());
} }
} }
@Test @Test