Skip to content

Commit b878119

Browse files
committed
Add mockito based mock testing for ReadBufferDataHandle
1 parent ff8deab commit b878119

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ Konstanz, and KNIME GmbH.</license.copyrightOwners>
184184
<artifactId>junit</artifactId>
185185
<scope>test</scope>
186186
</dependency>
187+
<dependency>
188+
<groupId>org.mockito</groupId>
189+
<artifactId>mockito-core</artifactId>
190+
<scope>test</scope>
191+
</dependency>
187192
</dependencies>
188193

189194
<build>
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
2+
package org.scijava.io.handle;
3+
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.mockito.AdditionalMatchers.aryEq;
9+
import static org.mockito.ArgumentMatchers.any;
10+
import static org.mockito.ArgumentMatchers.anyLong;
11+
12+
import org.scijava.io.location.DummyLocation;
13+
import org.scijava.io.location.Location;
14+
15+
import static org.mockito.Mockito.*;
16+
17+
import java.io.IOException;
18+
19+
public class ReadBufferDataHandleMockTest {
20+
21+
private DataHandle<Location> mock;
22+
private AbstractDataHandle<Location> buf;
23+
private byte[] byteArrayLen10;
24+
private long innerOffset;
25+
26+
@SuppressWarnings("unchecked")
27+
@Before
28+
public void setup() throws IOException {
29+
innerOffset = 0l;
30+
mock = mock(DataHandle.class);
31+
32+
// needed to get around type checking in AbstractWrapperPlugin
33+
when(mock.get()).thenReturn(new DummyLocation());
34+
when(mock.getType()).thenReturn(Location.class);
35+
36+
buf = new ReadBufferDataHandle(mock, 10, 2);
37+
byteArrayLen10 = new byte[10];
38+
39+
// update offset on mock read
40+
when(mock.read(any(byte[].class))).thenAnswer(inv -> {
41+
innerOffset += inv.<byte[]> getArgument(0).length;
42+
return null;
43+
});
44+
45+
// update offset on mock seek
46+
doAnswer(inv -> {
47+
innerOffset = inv.getArgument(0);
48+
return null;
49+
}).when(mock).seek(anyLong());
50+
51+
// mock offset
52+
when(mock.offset()).then(inv -> {
53+
return innerOffset;
54+
});
55+
}
56+
57+
@Test
58+
public void testBufferingSequence() throws IOException {
59+
60+
// set length of stubbed handle
61+
when(mock.length()).thenReturn(30l);
62+
63+
// read the first byte
64+
buf.read();
65+
verify(mock, times(0)).seek(0);
66+
// buffer should read a whole page
67+
verify(mock).read(aryEq(byteArrayLen10));
68+
69+
buf.seek(0);
70+
// ensure seek was not called again
71+
verify(mock, times(0)).seek(0);
72+
73+
// read over the edge of the current page
74+
buf.read(new byte[12]);
75+
verify(mock, times(0)).seek(anyLong());
76+
verify(mock, times(2)).read(aryEq(byteArrayLen10));
77+
78+
assertEquals(12, buf.offset());
79+
80+
// read the last page
81+
buf.read(new byte[12]);
82+
verify(mock, times(0)).seek(anyLong());
83+
verify(mock, times(3)).read(aryEq(byteArrayLen10));
84+
85+
// first page should no longer be buffered, must be reread in
86+
buf.seek(0);
87+
buf.read();
88+
verify(mock).seek(0);
89+
verify(mock, times(4)).read(aryEq(byteArrayLen10));
90+
}
91+
92+
/**
93+
* Tests that we do not buffer pages that are not needed and
94+
*
95+
* @throws IOException
96+
*/
97+
@Test
98+
public void testSkipForward() throws IOException {
99+
100+
// set length of stubbed handle
101+
when(mock.length()).thenReturn(40l);
102+
103+
// read the first byte
104+
buf.read();
105+
verify(mock, times(0)).seek(anyLong());
106+
verify(mock).read(aryEq(byteArrayLen10));
107+
108+
// skip the second page
109+
buf.seek(30l);
110+
buf.read();
111+
112+
// read the third page
113+
verify(mock).seek(30l);
114+
verify(mock, times(2)).read(aryEq(byteArrayLen10));
115+
116+
// go back to already buffered page
117+
buf.seek(0l);
118+
buf.read();
119+
120+
verify(mock, times(1)).seek(anyLong());
121+
122+
// go back to third page
123+
buf.seek(35);
124+
buf.read();
125+
verify(mock, times(1)).seek(anyLong());
126+
verify(mock, times(2)).read(any());
127+
}
128+
}

0 commit comments

Comments
 (0)