1
|
package eu.dnetlib.enabling.resultset;
|
2
|
|
3
|
import static org.junit.Assert.assertEquals;
|
4
|
import static org.junit.Assert.assertNotNull;
|
5
|
import static org.junit.Assert.assertNotSame;
|
6
|
import static org.junit.Assert.assertTrue;
|
7
|
import static org.mockito.Mockito.times;
|
8
|
import static org.mockito.Mockito.verify;
|
9
|
import static org.mockito.Mockito.when;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Iterator;
|
13
|
import java.util.List;
|
14
|
|
15
|
import org.junit.Before;
|
16
|
import org.junit.Test;
|
17
|
import org.junit.runner.RunWith;
|
18
|
import org.mockito.Mock;
|
19
|
import org.mockito.junit.MockitoJUnitRunner;
|
20
|
|
21
|
/**
|
22
|
* tests the IterableResult class
|
23
|
*
|
24
|
* @author claudio
|
25
|
*
|
26
|
*/
|
27
|
@RunWith(MockitoJUnitRunner.class)
|
28
|
public class IterableResultSetTest {
|
29
|
|
30
|
/**
|
31
|
* number of total elements
|
32
|
*/
|
33
|
private static int NUM_ELEM = 100;
|
34
|
|
35
|
/**
|
36
|
* number of elements available to fetch
|
37
|
*/
|
38
|
private static int FETCH_SIZE = 10;
|
39
|
|
40
|
@Mock
|
41
|
private ResultSet mockResultSet;
|
42
|
|
43
|
/**
|
44
|
* a list used to compare elements.
|
45
|
*/
|
46
|
private List<String> list;
|
47
|
|
48
|
/**
|
49
|
* Iterator associated with the test list.
|
50
|
*/
|
51
|
private Iterator<String> iter;
|
52
|
|
53
|
/**
|
54
|
* Object to test.
|
55
|
*/
|
56
|
private IterableResultSet iterResultSet;
|
57
|
|
58
|
@Before
|
59
|
public void setUp() {
|
60
|
list = new ArrayList<String>();
|
61
|
for (int i=0; i< NUM_ELEM; i++)
|
62
|
list.add("XXXXX " + i);
|
63
|
|
64
|
iter = list.iterator();
|
65
|
when(mockResultSet.isOpen()).thenReturn(iter.hasNext());
|
66
|
|
67
|
iterResultSet = new IterableResultSet(list, FETCH_SIZE);
|
68
|
iterResultSet.setResultSet(mockResultSet);
|
69
|
}
|
70
|
|
71
|
@Test
|
72
|
public void testIterableResultSet() {
|
73
|
assertNotNull(iterResultSet);
|
74
|
assertTrue(iterResultSet.getSize() > 0);
|
75
|
}
|
76
|
|
77
|
@Test(expected=RuntimeException.class)
|
78
|
public void testGetResult_fail_1() {
|
79
|
iterResultSet.getResult(0, 5);
|
80
|
}
|
81
|
|
82
|
@Test(expected=RuntimeException.class)
|
83
|
public void testGetResult_fail_2() {
|
84
|
iterResultSet.getResult(1, 5);
|
85
|
iterResultSet.getResult(1, 5);
|
86
|
}
|
87
|
|
88
|
@Test(expected=RuntimeException.class)
|
89
|
public void testGetResult_fail_3() {
|
90
|
iterResultSet.getResult(1, 5);
|
91
|
iterResultSet.getResult(6, 5);
|
92
|
}
|
93
|
|
94
|
@Test(expected=RuntimeException.class)
|
95
|
public void testGetResult_fail_4() {
|
96
|
iterResultSet.getResult(1, 5);
|
97
|
iterResultSet.getResult(7, 10);
|
98
|
}
|
99
|
|
100
|
@Test
|
101
|
public void testGetResult_success_1() {
|
102
|
List<String> result = iterResultSet.getResult(1, NUM_ELEM);
|
103
|
for (int i=0; i<NUM_ELEM; i++) {
|
104
|
assertEquals(list.get(i), result.get(i));
|
105
|
}
|
106
|
}
|
107
|
|
108
|
@Test
|
109
|
public void testGetResult_success_2() {
|
110
|
List<String> result = iterResultSet.getResult(1, NUM_ELEM);
|
111
|
|
112
|
for (int i=0; i<NUM_ELEM; i++) {
|
113
|
assertEquals(list.get(i), result.get(i));
|
114
|
iter.next();
|
115
|
}
|
116
|
verify(mockResultSet).close();
|
117
|
}
|
118
|
|
119
|
@Test
|
120
|
public void testGetResult_success_3() {
|
121
|
for (int i = 1; i < NUM_ELEM; i+= FETCH_SIZE)
|
122
|
iterResultSet.getResult(i, i + FETCH_SIZE-1);
|
123
|
verify(mockResultSet, times(1)).close();
|
124
|
}
|
125
|
|
126
|
@Test
|
127
|
public void testGetResult_success_4() {
|
128
|
iterResultSet.getResult(1, NUM_ELEM + 1);
|
129
|
verify(mockResultSet, times(1)).close();
|
130
|
}
|
131
|
|
132
|
@Test
|
133
|
public void testGetResultSet_success_1() {
|
134
|
assertNotNull(iterResultSet.getResultSet());
|
135
|
}
|
136
|
|
137
|
@Test
|
138
|
public void testGetSize_fail_1() {
|
139
|
iterResultSet.getResult(1, 8);
|
140
|
assertNotSame(7, iterResultSet.getSize());
|
141
|
assertNotSame(9, iterResultSet.getSize());
|
142
|
}
|
143
|
|
144
|
@Test
|
145
|
public void testGetSize_success_1() {
|
146
|
iterResultSet.getResult(1, 8);
|
147
|
assertEquals(FETCH_SIZE, iterResultSet.getSize());
|
148
|
}
|
149
|
|
150
|
}
|