Skip to content

Commit d9540ef

Browse files
committed
changes for review comments
1 parent fe53a23 commit d9540ef

File tree

1 file changed

+51
-80
lines changed

1 file changed

+51
-80
lines changed

core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java

Lines changed: 51 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ public class FileChannelUnitTest {
2121

2222
@Test
2323
public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
24-
String expected_value = "Hello world";
25-
String file = "src/test/resources/test_read.in";
26-
27-
try (RandomAccessFile reader = new RandomAccessFile(file, "r");
24+
25+
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
2826
FileChannel channel = reader.getChannel();
2927
ByteArrayOutputStream out = new ByteArrayOutputStream();) {
3028

@@ -35,25 +33,20 @@ public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect()
3533
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
3634

3735
while (channel.read(buff) > 0) {
38-
if (buff.hasArray()) {
39-
out.write(buff.array(), 0, buff.position());
40-
buff.clear();
41-
}
36+
out.write(buff.array(), 0, buff.position());
37+
buff.clear();
4238
}
4339

44-
assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8));
40+
assertEquals("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8));
4541
}
4642
}
4743

48-
4944
@Test
5045
public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException {
51-
String expected_value = "Hello world";
52-
String file = "src/test/resources/test_read.in";
5346

5447
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
55-
FileInputStream fin = new FileInputStream(file);
56-
FileChannel channel = fin.getChannel();) {
48+
FileInputStream fin = new FileInputStream("src/test/resources/test_read.in");
49+
FileChannel channel = fin.getChannel();) {
5750

5851
int bufferSize = 1024;
5952
if (bufferSize > channel.size()) {
@@ -62,89 +55,72 @@ public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect()
6255
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
6356

6457
while (channel.read(buff) > 0) {
65-
if (buff.hasArray()) {
66-
out.write(buff.array(), 0, buff.position());
67-
buff.clear();
68-
}
58+
out.write(buff.array(), 0, buff.position());
59+
buff.clear();
6960
}
7061

71-
assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8));
62+
assertEquals("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8));
7263

7364
}
7465
}
75-
76-
66+
7767
@Test
7868
public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException {
79-
String expected_value = "world";
80-
String file = "src/test/resources/test_read.in";
8169

82-
try (RandomAccessFile reader = new RandomAccessFile(file, "r");
70+
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
8371
FileChannel channel = reader.getChannel();
8472
ByteArrayOutputStream out = new ByteArrayOutputStream();) {
8573

8674
MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5);
8775

88-
if(buff.hasRemaining()) {
76+
if (buff.hasRemaining()) {
8977
byte[] data = new byte[buff.remaining()];
9078
buff.get(data);
91-
assertEquals(expected_value, new String(data, StandardCharsets.UTF_8));
92-
79+
assertEquals("world", new String(data, StandardCharsets.UTF_8));
80+
9381
}
94-
82+
9583
}
9684
}
97-
98-
85+
9986
@Test
100-
public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect()
101-
throws IOException {
102-
String expected = "Hello world";
103-
String file = "src/test/resources/test_write_using_filechannel.txt";
104-
105-
try (RandomAccessFile writer = new RandomAccessFile(file, "rw");
106-
FileChannel channel = writer.getChannel();){
107-
ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8));
108-
109-
if (buff.hasRemaining()) {
110-
channel.write(buff);
111-
}
112-
113-
// verify
114-
RandomAccessFile reader = new RandomAccessFile(file, "r");
115-
assertEquals(expected, reader.readLine());
116-
reader.close();
117-
}
87+
public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
88+
String file = "src/test/resources/test_write_using_filechannel.txt";
89+
try (RandomAccessFile writer = new RandomAccessFile(file, "rw");
90+
FileChannel channel = writer.getChannel();) {
91+
ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8));
92+
93+
if (buff.hasRemaining()) {
94+
channel.write(buff);
95+
}
96+
97+
// verify
98+
RandomAccessFile reader = new RandomAccessFile(file, "r");
99+
assertEquals("Hello world", reader.readLine());
100+
reader.close();
101+
}
118102
}
119-
120-
103+
121104
@Test
122105
public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException {
123-
String file = "src/test/resources/test_read.in";
124-
125-
try (RandomAccessFile reader = new RandomAccessFile(file, "rw");
126-
FileChannel channel = reader.getChannel();
127-
FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE );){
128-
129-
130-
assertNotNull(fileLock);
131-
132-
}catch(OverlappingFileLockException | IOException ex) {
133-
134-
106+
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "rw");
107+
FileChannel channel = reader.getChannel();
108+
FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE);) {
109+
110+
assertNotNull(fileLock);
111+
112+
} catch (OverlappingFileLockException | IOException ex) {
113+
135114
}
136-
115+
137116
}
138-
139117

140118
@Test
141119
public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException {
142-
long expected_value = 11;
143-
String file = "src/test/resources/test_read.in";
144120

145121
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
146-
RandomAccessFile reader = new RandomAccessFile(file, "r");
147-
FileChannel channel = reader.getChannel();) {
122+
RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
123+
FileChannel channel = reader.getChannel();) {
148124

149125
int bufferSize = 1024;
150126
if (bufferSize > channel.size()) {
@@ -157,44 +133,39 @@ public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IO
157133
buff.clear();
158134
}
159135

160-
assertEquals(expected_value, channel.position());
136+
assertEquals(11, channel.position());
161137

162138
channel.position(4);
163139
assertEquals(4, channel.position());
164140

165141
}
166142
}
167143

168-
169144
@Test
170145
public void whenGetFileSize_thenCorrect() throws IOException {
171-
long expectedSize = 11;
172-
String file = "src/test/resources/test_read.in";
173-
174-
try (RandomAccessFile reader = new RandomAccessFile(file, "r");
175-
FileChannel channel = reader.getChannel();) {
146+
147+
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
148+
FileChannel channel = reader.getChannel();) {
176149

177150
long imageFileSize = channel.size();
178-
assertEquals(expectedSize, imageFileSize);
151+
assertEquals(11, imageFileSize);
179152
}
180153

181154
}
182155

183156
@Test
184157
public void whenTruncateFile_thenCorrect() throws IOException {
185-
long expectedSize = 5;
186158
String input = "this is a test input";
187-
String file = "src/test/resources/test_truncate.txt";
188159

189-
FileOutputStream fout = new FileOutputStream(file);
160+
FileOutputStream fout = new FileOutputStream("src/test/resources/test_truncate.txt");
190161
FileChannel channel = fout.getChannel();
191162

192163
ByteBuffer buff = ByteBuffer.wrap(input.getBytes());
193164
channel.write(buff);
194165
buff.flip();
195166

196167
channel = channel.truncate(5);
197-
assertEquals(expectedSize, channel.size());
168+
assertEquals(5, channel.size());
198169

199170
fout.close();
200171
channel.close();

0 commit comments

Comments
 (0)