1
1
2
2
package org .scijava .io .handle ;
3
3
4
- import org .junit .Before ;
5
- import org .junit .Test ;
6
-
7
4
import static org .junit .Assert .assertEquals ;
8
5
import static org .mockito .AdditionalMatchers .aryEq ;
9
6
import static org .mockito .ArgumentMatchers .any ;
7
+ import static org .mockito .ArgumentMatchers .anyInt ;
10
8
import static org .mockito .ArgumentMatchers .anyLong ;
9
+ import static org .mockito .ArgumentMatchers .eq ;
10
+ import static org .mockito .Mockito .doAnswer ;
11
+ import static org .mockito .Mockito .mock ;
12
+ import static org .mockito .Mockito .times ;
13
+ import static org .mockito .Mockito .verify ;
14
+ import static org .mockito .Mockito .when ;
11
15
16
+ import java .io .IOException ;
17
+
18
+ import org .junit .Before ;
19
+ import org .junit .Test ;
12
20
import org .scijava .io .location .DummyLocation ;
13
21
import org .scijava .io .location .Location ;
14
22
15
- import static org .mockito .Mockito .*;
16
-
17
- import java .io .IOException ;
18
-
19
23
public class ReadBufferDataHandleMockTest {
20
24
21
25
private DataHandle <Location > mock ;
@@ -59,34 +63,40 @@ public void testBufferingSequence() throws IOException {
59
63
60
64
// set length of stubbed handle
61
65
when (mock .length ()).thenReturn (30l );
66
+ byte [] value = new byte [10 ];
67
+ when (mock .read (aryEq (value ), eq (0 ), eq (10 ))).thenReturn (10 );
68
+ when (mock .read (aryEq (value ), anyInt (), anyInt ())).thenReturn (10 );
62
69
63
70
// read the first byte
64
71
buf .read ();
65
72
verify (mock , times (0 )).seek (0 );
66
73
// buffer should read a whole page
67
- verify (mock ).read (aryEq (byteArrayLen10 ));
74
+ verify (mock ).read (aryEq (byteArrayLen10 ), eq ( 0 ), eq ( 10 ) );
68
75
69
76
buf .seek (0 );
70
77
// ensure seek was not called again
71
78
verify (mock , times (0 )).seek (0 );
72
79
80
+ when (mock .offset ()).thenReturn (10l );
81
+
73
82
// read over the edge of the current page
74
83
buf .read (new byte [12 ]);
75
84
verify (mock , times (0 )).seek (anyLong ());
76
- verify (mock , times (2 )).read (aryEq (byteArrayLen10 ));
85
+ verify (mock , times (2 )).read (aryEq (byteArrayLen10 ), eq ( 0 ), eq ( 10 ) );
77
86
78
87
assertEquals (12 , buf .offset ());
79
88
80
89
// read the last page
90
+ when (mock .offset ()).thenReturn (20l );
81
91
buf .read (new byte [12 ]);
82
92
verify (mock , times (0 )).seek (anyLong ());
83
- verify (mock , times (3 )).read (aryEq (byteArrayLen10 ));
93
+ verify (mock , times (3 )).read (aryEq (byteArrayLen10 ), eq ( 0 ), eq ( 10 ) );
84
94
85
95
// first page should no longer be buffered, must be reread in
86
96
buf .seek (0 );
87
97
buf .read ();
88
98
verify (mock ).seek (0 );
89
- verify (mock , times (4 )).read (aryEq (byteArrayLen10 ));
99
+ verify (mock , times (4 )).read (aryEq (byteArrayLen10 ), eq ( 0 ), eq ( 10 ) );
90
100
}
91
101
92
102
/**
@@ -99,19 +109,21 @@ public void testSkipForward() throws IOException {
99
109
100
110
// set length of stubbed handle
101
111
when (mock .length ()).thenReturn (40l );
112
+ when (mock .read (any (), anyInt (), anyInt ())).thenReturn (10 );
102
113
103
114
// read the first byte
104
115
buf .read ();
105
116
verify (mock , times (0 )).seek (anyLong ());
106
- verify (mock ) .read (aryEq (byteArrayLen10 ));
117
+ verify (mock , times ( 1 )) .read (aryEq (byteArrayLen10 ), eq ( 0 ), eq ( 10 ));
107
118
108
119
// skip the second page
109
120
buf .seek (30l );
110
121
buf .read ();
111
122
112
123
// read the third page
113
124
verify (mock ).seek (30l );
114
- verify (mock , times (2 )).read (aryEq (byteArrayLen10 ));
125
+ verify (mock , times (2 )).read (aryEq (byteArrayLen10 ), eq (0 ), eq (10 ));
126
+ when (mock .offset ()).thenReturn (40l );
115
127
116
128
// go back to already buffered page
117
129
buf .seek (0l );
@@ -123,6 +135,6 @@ public void testSkipForward() throws IOException {
123
135
buf .seek (35 );
124
136
buf .read ();
125
137
verify (mock , times (1 )).seek (anyLong ());
126
- verify (mock , times (2 )).read (any ());
138
+ verify (mock , times (2 )).read (any (), anyInt (), anyInt () );
127
139
}
128
140
}
0 commit comments