Revision 47814
Added by Claudio Atzori almost 7 years ago
modules/cnr-resultset-service/tags/cnr-resultset-service-2.0.3/deploy.info | ||
---|---|---|
1 |
{"type_source": "SVN", "goal": "package -U -T 4C source:jar", "url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet45/modules/cnr-resultset-service/trunk/", "deploy_repository": "dnet45-snapshots", "version": "4", "mail": "sandro.labruzzo@isti.cnr.it,michele.artini@isti.cnr.it, claudio.atzori@isti.cnr.it, alessia.bardi@isti.cnr.it", "deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet45-snapshots", "name": "cnr-resultset-service"} |
modules/cnr-resultset-service/tags/cnr-resultset-service-2.0.3/src/test/java/eu/dnetlib/enabling/resultset/push/ResultSetDescriptorFactoryTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.resultset.push; |
|
2 |
|
|
3 |
|
|
4 |
import static org.junit.Assert.*; |
|
5 |
|
|
6 |
import javax.annotation.Resource; |
|
7 |
|
|
8 |
import org.junit.Before; |
|
9 |
import org.junit.Test; |
|
10 |
import org.junit.runner.RunWith; |
|
11 |
import org.springframework.test.context.ContextConfiguration; |
|
12 |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
|
13 |
|
|
14 |
import eu.dnetlib.miscutils.factory.Factory; |
|
15 |
|
|
16 |
@RunWith(SpringJUnit4ClassRunner.class) |
|
17 |
@ContextConfiguration |
|
18 |
public class ResultSetDescriptorFactoryTest { |
|
19 |
|
|
20 |
@Resource |
|
21 |
Factory<ResultSetDescriptor> factory; |
|
22 |
|
|
23 |
@Before |
|
24 |
public void setUp() throws Exception { |
|
25 |
} |
|
26 |
|
|
27 |
@Test |
|
28 |
public void testFactory() { |
|
29 |
ResultSetDescriptor desc1 = factory.newInstance(); |
|
30 |
ResultSetDescriptor desc2 = factory.newInstance(); |
|
31 |
|
|
32 |
assertTrue("check different instances", desc1 != desc2); |
|
33 |
assertEquals("check that it's a spring prototype", 200, desc1.getRangeLength()); |
|
34 |
assertEquals("check that it's a spring prototype", 200, desc2.getRangeLength()); |
|
35 |
} |
|
36 |
} |
modules/cnr-resultset-service/tags/cnr-resultset-service-2.0.3/src/test/java/eu/dnetlib/enabling/resultset/push/TransientPushResultSetImplTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.resultset.push; |
|
2 |
|
|
3 |
import static org.junit.Assert.*; // NOPMD |
|
4 |
import static org.mockito.Mockito.*; // NOPMD |
|
5 |
|
|
6 |
import java.util.ArrayList; |
|
7 |
import java.util.List; |
|
8 |
|
|
9 |
import org.junit.Before; |
|
10 |
import org.junit.Test; |
|
11 |
import org.junit.runner.RunWith; |
|
12 |
import org.mockito.Mock; |
|
13 |
import org.mockito.junit.MockitoJUnitRunner; |
|
14 |
|
|
15 |
import com.google.common.collect.Lists; |
|
16 |
|
|
17 |
import eu.dnetlib.enabling.resultset.ResultSetRegistry; |
|
18 |
|
|
19 |
/** |
|
20 |
* test the transient push resultset. |
|
21 |
* |
|
22 |
* @author marko |
|
23 |
* |
|
24 |
*/ |
|
25 |
@RunWith(MockitoJUnitRunner.class) |
|
26 |
public class TransientPushResultSetImplTest { |
|
27 |
|
|
28 |
/** |
|
29 |
* first test value. |
|
30 |
*/ |
|
31 |
private static final String ONE = "one"; |
|
32 |
|
|
33 |
/** |
|
34 |
* second test value. |
|
35 |
*/ |
|
36 |
private static final String TWO = "two"; |
|
37 |
|
|
38 |
/** |
|
39 |
* test rs id. |
|
40 |
*/ |
|
41 |
private static final String RS_ID = "123"; |
|
42 |
|
|
43 |
/** |
|
44 |
* instance to be tested. |
|
45 |
*/ |
|
46 |
private transient TransientPushResultSetImpl resultSet; |
|
47 |
|
|
48 |
/** |
|
49 |
* dao mock. |
|
50 |
*/ |
|
51 |
@Mock |
|
52 |
private transient TransientPushResultSetDao dao; |
|
53 |
|
|
54 |
/** |
|
55 |
* resultset registry mock. |
|
56 |
*/ |
|
57 |
@Mock |
|
58 |
private transient ResultSetRegistry registry; |
|
59 |
|
|
60 |
/** |
|
61 |
* setup class to be tested. |
|
62 |
* |
|
63 |
* @throws Exception |
|
64 |
*/ |
|
65 |
@Before |
|
66 |
public void setUp() { |
|
67 |
resultSet = new TransientPushResultSetImpl(dao); |
|
68 |
resultSet.setIdentifier(RS_ID); |
|
69 |
resultSet.getDao(); // getter code coverage hack |
|
70 |
} |
|
71 |
|
|
72 |
/** |
|
73 |
* test adding elements. |
|
74 |
*/ |
|
75 |
@Test |
|
76 |
public void testAddElements() { |
|
77 |
final List<String> list = new ArrayList<String>(); |
|
78 |
list.add(ONE); |
|
79 |
|
|
80 |
resultSet.addElements(list); |
|
81 |
|
|
82 |
verify(dao).addElements(RS_ID, list); |
|
83 |
assertNotNull("dummy", resultSet); |
|
84 |
} |
|
85 |
|
|
86 |
/** |
|
87 |
* test get number of results. |
|
88 |
*/ |
|
89 |
@Test |
|
90 |
public void testGetNumberOfResults() { |
|
91 |
when(dao.getSize(RS_ID)).thenReturn(1); |
|
92 |
|
|
93 |
assertEquals("check size", 1, resultSet.getNumberOfResults()); |
|
94 |
} |
|
95 |
|
|
96 |
/** |
|
97 |
* get result. |
|
98 |
*/ |
|
99 |
@Test |
|
100 |
public void testGetResults() { |
|
101 |
final List<String> list = new ArrayList<String>(); |
|
102 |
list.add(TWO); |
|
103 |
|
|
104 |
when(dao.getSize(RS_ID)).thenReturn(2); |
|
105 |
when(dao.getElements(RS_ID, 2, 2)).thenReturn(list); |
|
106 |
|
|
107 |
assertEquals("check list", TWO, resultSet.getResults(2, 2).get(0)); |
|
108 |
assertEquals("check size", 1, resultSet.getResults(2, 2).size()); |
|
109 |
} |
|
110 |
|
|
111 |
/** |
|
112 |
* test when indices are out of range, trailing stuff should be skipped. |
|
113 |
*/ |
|
114 |
@Test |
|
115 |
public void testGetResultOutOfRange() { |
|
116 |
final List<String> list = new ArrayList<String>(); |
|
117 |
list.add(TWO); |
|
118 |
|
|
119 |
when(dao.getSize(RS_ID)).thenReturn(2); |
|
120 |
when(dao.getElements(RS_ID, 2, 2)).thenReturn(list); |
|
121 |
|
|
122 |
assertEquals("check list", TWO, resultSet.getResults(2, 2 + 1).get(0)); |
|
123 |
assertEquals("check size", 1, resultSet.getResults(2, 2 + 1).size()); |
|
124 |
} |
|
125 |
|
|
126 |
/** |
|
127 |
* open. |
|
128 |
*/ |
|
129 |
@Test |
|
130 |
public void testIsOpen() { |
|
131 |
assertTrue("check is open by default", resultSet.isOpen()); |
|
132 |
} |
|
133 |
|
|
134 |
/** |
|
135 |
* test close. |
|
136 |
*/ |
|
137 |
@Test |
|
138 |
public void testClose() { |
|
139 |
resultSet.addObserver(registry); |
|
140 |
assertEquals("observer should be added", 1, resultSet.countObservers()); |
|
141 |
|
|
142 |
resultSet.destroy(); |
|
143 |
assertTrue("should be destroyed", resultSet.isDestroyed()); |
|
144 |
|
|
145 |
assertEquals("observers should be cleared", 0, resultSet.countObservers()); |
|
146 |
verify(registry, times(1)).update(resultSet, null); |
|
147 |
} |
|
148 |
|
|
149 |
@Test |
|
150 |
public void testFromAfterSize() { |
|
151 |
final List<String> list = new ArrayList<String>(); |
|
152 |
list.add(TWO); |
|
153 |
|
|
154 |
when(dao.getSize(RS_ID)).thenReturn(2); |
|
155 |
when(dao.getElements(RS_ID, 2, 2)).thenReturn(list); |
|
156 |
|
|
157 |
resultSet.getResults(3, 2); |
|
158 |
} |
|
159 |
|
|
160 |
@Test(expected = IllegalStateException.class) |
|
161 |
public void testWriteClosed() { |
|
162 |
resultSet.close(); |
|
163 |
resultSet.addElements(Lists.newArrayList("test")); |
|
164 |
} |
|
165 |
|
|
166 |
} |
modules/cnr-resultset-service/tags/cnr-resultset-service-2.0.3/src/test/java/eu/dnetlib/enabling/resultset/push/ResultSetDescriptorTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.resultset.push; |
|
2 |
|
|
3 |
import org.apache.commons.logging.Log; |
|
4 |
import org.apache.commons.logging.LogFactory; |
|
5 |
import org.junit.Before; |
|
6 |
import org.junit.Test; |
|
7 |
|
|
8 |
import eu.dnetlib.enabling.resultset.push.ResultSetDescriptor.Range; |
|
9 |
|
|
10 |
public class ResultSetDescriptorTest { |
|
11 |
|
|
12 |
private static final Log log = LogFactory.getLog(ResultSetDescriptorTest.class); // NOPMD by marko on 11/24/08 5:02 PM |
|
13 |
|
|
14 |
private transient ResultSetDescriptor descriptor; |
|
15 |
|
|
16 |
@Before |
|
17 |
public void setUp() throws Exception { |
|
18 |
descriptor = new ResultSetDescriptor(); |
|
19 |
} |
|
20 |
|
|
21 |
@Test |
|
22 |
public void testGetRangesContaining() { |
|
23 |
log.debug("--- 1, 1"); |
|
24 |
for(Range i : descriptor.getRangesContaining(1, 1)) |
|
25 |
log.debug("i " + i); |
|
26 |
|
|
27 |
log.debug("--- 2, 1000"); |
|
28 |
for(Range i : descriptor.getRangesContaining(2, 1000)) |
|
29 |
log.debug("i " + i); |
|
30 |
|
|
31 |
log.debug("--- 5, 2000"); |
|
32 |
for(Range i : descriptor.getRangesContaining(5, 2000)) |
|
33 |
log.debug("i " + i); |
|
34 |
|
|
35 |
log.debug("--- 5, 2001"); |
|
36 |
for(Range i : descriptor.getRangesContaining(5, 2001)) |
|
37 |
log.debug("i " + i); |
|
38 |
} |
|
39 |
|
|
40 |
@Test |
|
41 |
public void testNext() { |
|
42 |
} |
|
43 |
|
|
44 |
} |
modules/cnr-resultset-service/tags/cnr-resultset-service-2.0.3/src/test/java/eu/dnetlib/enabling/resultset/push/AbstractTransientPushResultSetDaoTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.resultset.push; |
|
2 |
|
|
3 |
import static org.junit.Assert.*; // NOPMD |
|
4 |
|
|
5 |
import java.util.ArrayList; |
|
6 |
import java.util.LinkedList; |
|
7 |
import java.util.List; |
|
8 |
|
|
9 |
import org.apache.commons.logging.Log; |
|
10 |
import org.apache.commons.logging.LogFactory; |
|
11 |
import org.junit.Before; |
|
12 |
import org.junit.Ignore; |
|
13 |
import org.junit.Test; |
|
14 |
|
|
15 |
import com.google.common.collect.Lists; |
|
16 |
|
|
17 |
import eu.dnetlib.miscutils.cache.Cache; |
|
18 |
|
|
19 |
/** |
|
20 |
* subclass this unit test class and provide a concrete transient push resultset dao implementation. |
|
21 |
* |
|
22 |
* @author marko |
|
23 |
* |
|
24 |
*/ |
|
25 |
@Ignore |
|
26 |
public abstract class AbstractTransientPushResultSetDaoTest { |
|
27 |
/** |
|
28 |
* logger. |
|
29 |
*/ |
|
30 |
private static final Log log = LogFactory.getLog(AbstractTransientPushResultSetDaoTest.class); // NOPMD by marko on 11/24/08 5:02 PM |
|
31 |
|
|
32 |
/** |
|
33 |
* test first element. |
|
34 |
*/ |
|
35 |
private static final String TWO = "two"; |
|
36 |
|
|
37 |
/** |
|
38 |
* test second element. |
|
39 |
*/ |
|
40 |
private static final String ONE = "one"; |
|
41 |
|
|
42 |
/** |
|
43 |
* some key. |
|
44 |
*/ |
|
45 |
private static final String RS_ID = "123"; |
|
46 |
|
|
47 |
/** |
|
48 |
* instance under test. |
|
49 |
*/ |
|
50 |
private TransientPushResultSetDao dao; |
|
51 |
|
|
52 |
/** |
|
53 |
* prepare the instance under test. |
|
54 |
* |
|
55 |
*/ |
|
56 |
@Before |
|
57 |
public void setUp() { |
|
58 |
setDao(newInstance()); |
|
59 |
} |
|
60 |
|
|
61 |
/** |
|
62 |
* specific unit tests will override this method and provide a concrete instance to test. |
|
63 |
* |
|
64 |
* @return new transient push resultset dao instance |
|
65 |
*/ |
|
66 |
protected abstract TransientPushResultSetDao newInstance(); |
|
67 |
|
|
68 |
/** |
|
69 |
* test add elements. |
|
70 |
*/ |
|
71 |
@Test |
|
72 |
public void testAddElements() { |
|
73 |
final List<String> freshGet = getDao().getElements(RS_ID, 1, 0); |
|
74 |
assertNotNull("fresh get", freshGet); |
|
75 |
assertEquals("fresh get", 0, freshGet.size()); |
|
76 |
|
|
77 |
final List<String> list = new ArrayList<String>(); |
|
78 |
list.add(ONE); |
|
79 |
|
|
80 |
getDao().addElements(RS_ID, list); |
|
81 |
|
|
82 |
final List<String> firstGet = getDao().getElements(RS_ID, 1, 1); |
|
83 |
assertEquals("first get", 1, firstGet.size()); |
|
84 |
assertEquals("first get", ONE, firstGet.get(0)); |
|
85 |
|
|
86 |
final List<String> listAppend = new ArrayList<String>(); |
|
87 |
listAppend.add(TWO); |
|
88 |
|
|
89 |
getDao().addElements(RS_ID, listAppend); |
|
90 |
|
|
91 |
final List<String> secondGet = getDao().getElements(RS_ID, 1, 2); |
|
92 |
assertEquals("second get", 2, secondGet.size()); |
|
93 |
assertEquals("second get", ONE, secondGet.get(0)); |
|
94 |
assertEquals("second get", TWO, secondGet.get(1)); |
|
95 |
|
|
96 |
final List<String> listBig = new ArrayList<String>(); |
|
97 |
for (int i = 0; i < 90; i++) |
|
98 |
listBig.add(Integer.toString(i)); |
|
99 |
|
|
100 |
getDao().addElements(RS_ID, listBig); |
|
101 |
|
|
102 |
final List<String> thirdGet = getDao().getElements(RS_ID, 3, 90); |
|
103 |
assertEquals("third get", 88, thirdGet.size()); |
|
104 |
assertEquals("third get", "0", thirdGet.get(0)); |
|
105 |
assertEquals("third get", "1", thirdGet.get(1)); |
|
106 |
} |
|
107 |
|
|
108 |
/** |
|
109 |
* check get elements. |
|
110 |
*/ |
|
111 |
@Test |
|
112 |
public void testGetElements() { |
|
113 |
final List<String> list = new ArrayList<String>(); |
|
114 |
list.add(ONE); |
|
115 |
|
|
116 |
getDao().addElements(RS_ID, list); |
|
117 |
|
|
118 |
final List<String> res = getDao().getElements(RS_ID, 1, 1); |
|
119 |
assertEquals("check size", 1, res.size()); |
|
120 |
assertEquals("check element", ONE, res.get(0)); |
|
121 |
} |
|
122 |
|
|
123 |
/** |
|
124 |
* introduced for reproducing of #904. Thanks to Jochen for this one. |
|
125 |
*/ |
|
126 |
@Test(expected = IllegalArgumentException.class) |
|
127 |
public void testAddElementsDifferentSize() { |
|
128 |
List<Integer> sizes = Lists.newArrayList(100, 200, 300, 99); |
|
129 |
int run = 0; |
|
130 |
int total = 0; |
|
131 |
|
|
132 |
List<String> records = new LinkedList<String>(); |
|
133 |
for (int size : sizes) { |
|
134 |
for (int i = 0; i < size; i++) |
|
135 |
records.add("bla-" + run + "-" + i); |
|
136 |
|
|
137 |
dao.addElements(RS_ID, records); |
|
138 |
records.clear(); |
|
139 |
|
|
140 |
total += size; |
|
141 |
run++; |
|
142 |
} |
|
143 |
|
|
144 |
assertEquals("check size", total, dao.getSize(RS_ID)); |
|
145 |
} |
|
146 |
|
|
147 |
/** |
|
148 |
* introduced for reproducing of #904. with correct size (limited by 100) |
|
149 |
*/ |
|
150 |
@Test |
|
151 |
public void testAddElementsDifferentSizeCorrectSize() { |
|
152 |
List<Integer> sizes = Lists.newArrayList(51, 51, 51, 51); |
|
153 |
|
|
154 |
int total = 0; |
|
155 |
int run = 0; |
|
156 |
String lastName = null; |
|
157 |
|
|
158 |
List<String> records = new LinkedList<String>(); |
|
159 |
for (int size : sizes) { |
|
160 |
for (int i = 0; i < size; i++) { |
|
161 |
lastName = "bla-" + run + "-" + i; |
|
162 |
records.add(lastName); |
|
163 |
} |
|
164 |
|
|
165 |
dao.addElements(RS_ID, records); |
|
166 |
records.clear(); |
|
167 |
|
|
168 |
total += size; |
|
169 |
run++; |
|
170 |
} |
|
171 |
|
|
172 |
inspect(); |
|
173 |
|
|
174 |
assertEquals("check size", total, dao.getSize(RS_ID)); |
|
175 |
assertEquals("check last", lastName, dao.getElements(RS_ID, total, total).get(0)); |
|
176 |
|
|
177 |
assertEquals("check first page", "bla-0-11", dao.getElements(RS_ID, 12, 12).get(0)); |
|
178 |
assertEquals("check last page", "bla-1-11", dao.getElements(RS_ID, 51 + 12, 51 + 12).get(0)); |
|
179 |
|
|
180 |
assertEquals("check range", 10, dao.getElements(RS_ID, 1, 10).size()); |
|
181 |
assertEquals("check range", 10, dao.getElements(RS_ID, 51 + 1, 51 + 10).size()); |
|
182 |
assertEquals("check range", 51, dao.getElements(RS_ID, 1, 51).size()); |
|
183 |
assertEquals("check range", 52, dao.getElements(RS_ID, 1, 52).size()); |
|
184 |
assertEquals("check range", 51, dao.getElements(RS_ID, 2, 52).size()); |
|
185 |
|
|
186 |
assertEquals("check range", 100, dao.getElements(RS_ID, 1, 100).size()); |
|
187 |
assertEquals("check range", 99, dao.getElements(RS_ID, 2, 100).size()); |
|
188 |
assertEquals("check range", 100, dao.getElements(RS_ID, 2, 101).size()); |
|
189 |
|
|
190 |
assertEquals("check range", 200, dao.getElements(RS_ID, 2, 201).size()); |
|
191 |
} |
|
192 |
|
|
193 |
private void inspect() { |
|
194 |
CacheTransientResultSetDaoImpl dao = (CacheTransientResultSetDaoImpl) this.dao; |
|
195 |
ResultSetDescriptor desc = dao.getResultSetCache().get(RS_ID); |
|
196 |
Cache<String, List<String>> cache = dao.getCache(); |
|
197 |
|
|
198 |
log.debug("----------------"); |
|
199 |
log.debug("ranges: " + desc.getRanges()); |
|
200 |
log.debug("last range index: " + desc.getLastRange()); |
|
201 |
for (int i = 0; i < desc.getRanges(); i++) { |
|
202 |
final List<String> range = cache.get(RS_ID + "-" + i); |
|
203 |
log.debug("range: " + i + " has " + range.size() + " elements"); |
|
204 |
log.debug(" " + range); |
|
205 |
} |
|
206 |
|
|
207 |
log.debug("----------------"); |
|
208 |
} |
|
209 |
|
|
210 |
@Test |
|
211 |
public void testSizeOfEmpty() { |
|
212 |
assertEquals("empty size", 0, dao.getSize(RS_ID)); |
|
213 |
} |
|
214 |
|
|
215 |
public TransientPushResultSetDao getDao() { |
|
216 |
return dao; |
|
217 |
} |
|
218 |
|
|
219 |
public void setDao(final TransientPushResultSetDao dao) { |
|
220 |
this.dao = dao; |
|
221 |
} |
|
222 |
|
|
223 |
} |
modules/cnr-resultset-service/tags/cnr-resultset-service-2.0.3/src/test/java/eu/dnetlib/enabling/resultset/push/CacheTransientPushResultSetDaoTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.resultset.push; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
import net.sf.ehcache.CacheManager; |
|
6 |
|
|
7 |
import org.junit.After; |
|
8 |
|
|
9 |
import eu.dnetlib.miscutils.cache.Cache; |
|
10 |
import eu.dnetlib.miscutils.cache.EhCache; |
|
11 |
import eu.dnetlib.miscutils.factory.Factory; |
|
12 |
|
|
13 |
/** |
|
14 |
* concrete push resultset dao test. |
|
15 |
* |
|
16 |
* @author marko |
|
17 |
* |
|
18 |
*/ |
|
19 |
public class CacheTransientPushResultSetDaoTest extends AbstractTransientPushResultSetDaoTest { // NOPMD |
|
20 |
|
|
21 |
/** |
|
22 |
* max in memory elements. |
|
23 |
*/ |
|
24 |
private static final int MAX_IN_MEM = 100; |
|
25 |
|
|
26 |
/** |
|
27 |
* default time. |
|
28 |
*/ |
|
29 |
private static final int DEFAULT_TTI = 1000; |
|
30 |
|
|
31 |
/** |
|
32 |
* default time to live. |
|
33 |
*/ |
|
34 |
private static final int DEFAULT_TTL = 1000; |
|
35 |
|
|
36 |
/** |
|
37 |
* cache manager. |
|
38 |
*/ |
|
39 |
private final transient CacheManager cacheManager = CacheManager.create(); |
|
40 |
|
|
41 |
/** |
|
42 |
* clean cache after test. |
|
43 |
*/ |
|
44 |
@After |
|
45 |
public void cleanCache() { |
|
46 |
cacheManager.removeAllCaches(); |
|
47 |
} |
|
48 |
|
|
49 |
/** |
|
50 |
* {@inheritDoc} |
|
51 |
* |
|
52 |
* @see eu.dnetlib.enabling.resultset.push.AbstractTransientPushResultSetDaoTest#newInstance() |
|
53 |
*/ |
|
54 |
@Override |
|
55 |
protected TransientPushResultSetDao newInstance() { |
|
56 |
final net.sf.ehcache.Cache ehcache = new net.sf.ehcache.Cache("testCache", MAX_IN_MEM, false, false, DEFAULT_TTL, DEFAULT_TTI); |
|
57 |
cacheManager.addCache(ehcache); |
|
58 |
|
|
59 |
final net.sf.ehcache.Cache rsehcache = new net.sf.ehcache.Cache("rsetCache", MAX_IN_MEM, false, false, DEFAULT_TTL, DEFAULT_TTI); |
|
60 |
cacheManager.addCache(rsehcache); |
|
61 |
|
|
62 |
final CacheTransientResultSetDaoImpl resultSet = new CacheTransientResultSetDaoImpl(); |
|
63 |
final Cache<String, List<String>> cache = new EhCache<String, List<String>>(ehcache); |
|
64 |
final Cache<String, ResultSetDescriptor> rscache = new EhCache<String, ResultSetDescriptor>(rsehcache); |
|
65 |
resultSet.setCache(cache); |
|
66 |
resultSet.setResultSetCache(rscache); |
|
67 |
|
|
68 |
resultSet.setResultSetDescriptorFactory(new Factory<ResultSetDescriptor>() { |
|
69 |
|
|
70 |
@Override |
|
71 |
public ResultSetDescriptor newInstance() { |
|
72 |
return new ResultSetDescriptor(); |
|
73 |
} |
|
74 |
}); |
|
75 |
return resultSet; |
|
76 |
} |
|
77 |
|
|
78 |
} |
modules/cnr-resultset-service/tags/cnr-resultset-service-2.0.3/src/test/java/eu/dnetlib/enabling/resultset/MappedResultSetTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.resultset; // NOPMD |
|
2 |
|
|
3 |
import static org.junit.Assert.assertFalse; |
|
4 |
import static org.junit.Assert.assertNotNull; |
|
5 |
import static org.junit.Assert.assertTrue; |
|
6 |
import static org.mockito.ArgumentMatchers.any; |
|
7 |
import static org.mockito.Matchers.anyInt; |
|
8 |
import static org.mockito.Matchers.anyObject; |
|
9 |
import static org.mockito.Matchers.anyString; |
|
10 |
import static org.mockito.Matchers.eq; |
|
11 |
import static org.mockito.Mockito.when; |
|
12 |
|
|
13 |
import java.util.List; |
|
14 |
|
|
15 |
import javax.xml.ws.wsaddressing.W3CEndpointReference; |
|
16 |
|
|
17 |
import org.junit.Before; |
|
18 |
import org.junit.Ignore; |
|
19 |
import org.junit.Test; |
|
20 |
import org.junit.runner.RunWith; |
|
21 |
import org.mockito.Mock; |
|
22 |
import org.mockito.invocation.InvocationOnMock; |
|
23 |
import org.mockito.junit.MockitoJUnitRunner; |
|
24 |
import org.mockito.stubbing.Answer; |
|
25 |
|
|
26 |
import com.google.common.collect.Lists; |
|
27 |
|
|
28 |
import eu.dnetlib.enabling.resultset.rmi.ResultSetException; |
|
29 |
import eu.dnetlib.enabling.resultset.rmi.ResultSetService; |
|
30 |
import eu.dnetlib.enabling.tools.ServiceResolver; |
|
31 |
import eu.dnetlib.miscutils.factory.Factory; |
|
32 |
import eu.dnetlib.miscutils.functional.ThreadSafeUnaryFunction; |
|
33 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
34 |
|
|
35 |
/** |
|
36 |
* test xslt resultset. |
|
37 |
* |
|
38 |
* @author marko |
|
39 |
* |
|
40 |
*/ |
|
41 |
@RunWith(MockitoJUnitRunner.class) |
|
42 |
public class MappedResultSetTest { |
|
43 |
|
|
44 |
/** |
|
45 |
* instance under test. |
|
46 |
*/ |
|
47 |
private transient MappedResultSet mappedResultSet; |
|
48 |
|
|
49 |
/** |
|
50 |
* service resolver. |
|
51 |
*/ |
|
52 |
@Mock |
|
53 |
private transient ServiceResolver serviceResolver; |
|
54 |
|
|
55 |
/** |
|
56 |
* resultset service mock. |
|
57 |
*/ |
|
58 |
@Mock |
|
59 |
private transient ResultSetService resultSetService; |
|
60 |
|
|
61 |
/** |
|
62 |
* resultset mock. |
|
63 |
*/ |
|
64 |
@Mock |
|
65 |
private transient ResultSet resultSet; |
|
66 |
|
|
67 |
private Answer<Object> resultSetAnswer = new Answer<Object>() { |
|
68 |
|
|
69 |
private List<String> list = Lists.newArrayList("1","2","3","4","5","6","7","8","9","10"); |
|
70 |
|
|
71 |
@Override |
|
72 |
public Object answer(InvocationOnMock invocation) throws Throwable { |
|
73 |
String method = invocation.getMethod().getName(); |
|
74 |
if (method.equals("getResult") || method.equals("getResults")) { |
|
75 |
int from = (Integer) invocation.getArguments()[1]; |
|
76 |
int to = (Integer) invocation.getArguments()[2]; |
|
77 |
|
|
78 |
return list.subList(from-1, to); |
|
79 |
} |
|
80 |
if (method.equals("getNumberOfElements") || method.equals("getNumberOfResults")) { |
|
81 |
return list.size(); |
|
82 |
} |
|
83 |
if (method.equals("getRSStatus")) { |
|
84 |
return "closed"; |
|
85 |
} |
|
86 |
if (method.equals("isOpen")) { |
|
87 |
return false; |
|
88 |
} |
|
89 |
System.out.println(invocation.toString()); |
|
90 |
return null; |
|
91 |
} |
|
92 |
}; |
|
93 |
|
|
94 |
/** |
|
95 |
* Common setup. |
|
96 |
* @throws ResultSetException |
|
97 |
* |
|
98 |
*/ |
|
99 |
@Before |
|
100 |
public void setUp() throws ResultSetException { |
|
101 |
|
|
102 |
when(serviceResolver.getService(eq(ResultSetService.class), any(W3CEndpointReference.class))).thenReturn(resultSetService); |
|
103 |
when(serviceResolver.getResourceIdentifier(any(W3CEndpointReference.class))).thenReturn("123"); |
|
104 |
|
|
105 |
when(resultSetService.getNumberOfElements(anyString())).thenAnswer(resultSetAnswer); |
|
106 |
when(resultSetService.getResult(anyString(), anyInt(), anyInt(), anyString())).thenAnswer(resultSetAnswer ); |
|
107 |
when(resultSetService.getRSStatus(anyString())).thenAnswer(resultSetAnswer); |
|
108 |
|
|
109 |
when(resultSet.getNumberOfResults()).thenAnswer(resultSetAnswer); |
|
110 |
when(resultSet.getResults(anyInt(), anyInt())).thenAnswer(resultSetAnswer ); |
|
111 |
when(resultSet.isOpen()).thenAnswer(resultSetAnswer); |
|
112 |
} |
|
113 |
|
|
114 |
@Test |
|
115 |
@Ignore |
|
116 |
public void testThreadSafeMappedResultSet_1() { |
|
117 |
|
|
118 |
final Factory<UnaryFunction<String, String>> functionFactory = new Factory<UnaryFunction<String,String>>() { |
|
119 |
@Override |
|
120 |
public UnaryFunction<String, String> newInstance() { |
|
121 |
return new UnaryFunction<String, String>() { |
|
122 |
@Override |
|
123 |
public String evaluate(String arg) { |
|
124 |
return "mapped-" + arg; |
|
125 |
} |
|
126 |
}; |
|
127 |
} |
|
128 |
}; |
|
129 |
mappedResultSet = new MappedResultSet(null, new ThreadSafeUnaryFunction<String, String>(functionFactory), serviceResolver); |
|
130 |
mappedResultSet.setResultSet(resultSet); |
|
131 |
|
|
132 |
for (String s : mappedResultSet.getResult(1, 10)) { |
|
133 |
assertNotNull("null result", s); |
|
134 |
assertFalse("empty result", s.isEmpty()); |
|
135 |
assertNotNull("null element", s); |
|
136 |
assertTrue("transformed correctly", s.startsWith("mapped-")); |
|
137 |
} |
|
138 |
} |
|
139 |
} |
|
140 |
|
|
0 | 141 |
modules/cnr-resultset-service/tags/cnr-resultset-service-2.0.3/src/test/java/eu/dnetlib/enabling/resultset/IterableResultSetTest.java | ||
---|---|---|
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 |
} |
modules/cnr-resultset-service/tags/cnr-resultset-service-2.0.3/src/test/java/eu/dnetlib/enabling/resultset/XSLTMappedResultSetFactoryTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.resultset; // NOPMD |
|
2 |
|
|
3 |
import static org.junit.Assert.assertFalse; |
|
4 |
import static org.junit.Assert.assertTrue; |
|
5 |
import static org.junit.Assert.assertNotNull; |
|
6 |
import static org.mockito.Matchers.anyObject; |
|
7 |
import static org.mockito.Matchers.anyString; |
|
8 |
import static org.mockito.Matchers.argThat; |
|
9 |
import static org.mockito.Matchers.eq; |
|
10 |
import static org.mockito.Mockito.verify; |
|
11 |
import static org.mockito.Mockito.times; |
|
12 |
import static org.mockito.Mockito.when; |
|
13 |
|
|
14 |
import java.io.IOException; |
|
15 |
import java.io.StringWriter; |
|
16 |
import java.util.List; |
|
17 |
|
|
18 |
import javax.xml.transform.TransformerConfigurationException; |
|
19 |
import javax.xml.ws.wsaddressing.W3CEndpointReference; |
|
20 |
|
|
21 |
import org.apache.commons.io.IOUtils; |
|
22 |
import org.junit.Before; |
|
23 |
import org.junit.Test; |
|
24 |
import org.junit.runner.RunWith; |
|
25 |
import org.mockito.ArgumentMatcher; |
|
26 |
import org.mockito.Mock; |
|
27 |
import org.mockito.junit.MockitoJUnitRunner; |
|
28 |
|
|
29 |
import com.google.common.collect.Lists; |
|
30 |
|
|
31 |
import eu.dnetlib.enabling.resultset.rmi.ResultSetException; |
|
32 |
import eu.dnetlib.enabling.resultset.rmi.ResultSetService; |
|
33 |
import eu.dnetlib.enabling.tools.ServiceResolver; |
|
34 |
|
|
35 |
/** |
|
36 |
* test xslt resultset. |
|
37 |
* |
|
38 |
* @author marko |
|
39 |
* |
|
40 |
*/ |
|
41 |
@RunWith(MockitoJUnitRunner.class) |
|
42 |
public class XSLTMappedResultSetFactoryTest { |
|
43 |
|
|
44 |
/** |
|
45 |
* instance under test. |
|
46 |
*/ |
|
47 |
private transient XSLTMappedResultSetFactory factory; |
|
48 |
|
|
49 |
/** |
|
50 |
* resultset factory mock. |
|
51 |
*/ |
|
52 |
@Mock |
|
53 |
private transient ResultSetFactory resultSetFactory; |
|
54 |
|
|
55 |
/** |
|
56 |
* service resolver. |
|
57 |
*/ |
|
58 |
@Mock |
|
59 |
private transient ServiceResolver serviceResolver; |
|
60 |
|
|
61 |
/** |
|
62 |
* resultset service mock. |
|
63 |
*/ |
|
64 |
@Mock |
|
65 |
private transient ResultSetService resultSetService; |
|
66 |
|
|
67 |
/** |
|
68 |
* underlying resultSet mock |
|
69 |
*/ |
|
70 |
@Mock |
|
71 |
private transient ResultSet resultSet; |
|
72 |
|
|
73 |
/** |
|
74 |
* Common setup. |
|
75 |
* |
|
76 |
*/ |
|
77 |
@Before |
|
78 |
public void setUp() { |
|
79 |
factory = new XSLTMappedResultSetFactory(); |
|
80 |
factory.setResultSetFactory(resultSetFactory); |
|
81 |
factory.setServiceResolver(serviceResolver); |
|
82 |
} |
|
83 |
|
|
84 |
/** |
|
85 |
* test invalid xslt. |
|
86 |
* |
|
87 |
* @throws TransformerConfigurationException |
|
88 |
* could happen |
|
89 |
*/ |
|
90 |
@Test(expected = IllegalStateException.class) |
|
91 |
public void testInvalidXslt() throws TransformerConfigurationException { |
|
92 |
factory.createMappedResultSet(null, "<bla/>"); |
|
93 |
} |
|
94 |
|
|
95 |
/** |
|
96 |
* test xslt. |
|
97 |
* |
|
98 |
* @throws TransformerConfigurationException |
|
99 |
* could happen |
|
100 |
* @throws IOException |
|
101 |
* could happen |
|
102 |
* @throws ResultSetException mock |
|
103 |
*/ |
|
104 |
@Test |
|
105 |
public void testXslt() throws TransformerConfigurationException, IOException, ResultSetException { |
|
106 |
when(serviceResolver.getService(eq(ResultSetService.class), (W3CEndpointReference) anyObject())).thenReturn(resultSetService); |
|
107 |
when(serviceResolver.getResourceIdentifier((W3CEndpointReference) anyObject())).thenReturn("123"); |
|
108 |
|
|
109 |
//when(resultSetService.getNumberOfElements(anyString())).thenReturn(1); |
|
110 |
when(resultSetService.getResult("123", 1, 1, "waiting")).thenReturn(Lists.newArrayList("<first>something</first>")); |
|
111 |
when(resultSetService.getRSStatus("123")).thenReturn("closed"); |
|
112 |
|
|
113 |
when(resultSet.isOpen()).thenReturn(true); |
|
114 |
|
|
115 |
final StringWriter xsltSource = new StringWriter(); |
|
116 |
IOUtils.copy(getClass().getResourceAsStream("test.xsl"), xsltSource); |
|
117 |
factory.createMappedResultSet(null, xsltSource.toString()); |
|
118 |
|
|
119 |
verify(resultSetFactory, times(1)).createResultSet(argThat(new ArgumentMatcher<ResultSetListener>() { |
|
120 |
|
|
121 |
@Override |
|
122 |
public boolean matches(final ResultSetListener argument) { |
|
123 |
final MappedResultSet listener = (MappedResultSet) argument; |
|
124 |
listener.setResultSet(resultSet); |
|
125 |
final List<String> res = listener.getResult(1, 1); |
|
126 |
|
|
127 |
assertNotNull("null result", res); |
|
128 |
assertFalse("empty result", res.isEmpty()); |
|
129 |
|
|
130 |
assertNotNull("null element", res.get(0)); |
|
131 |
|
|
132 |
assertTrue("transformed correctly", res.get(0).contains("<second>something</second>")); |
|
133 |
|
|
134 |
return true; |
|
135 |
} |
|
136 |
})); |
|
137 |
} |
|
138 |
|
|
139 |
} |
modules/cnr-resultset-service/tags/cnr-resultset-service-2.0.3/src/test/java/eu/dnetlib/enabling/resultset/FetchListTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.resultset; |
|
2 |
|
|
3 |
import static org.junit.Assert.*; |
|
4 |
|
|
5 |
import java.util.ArrayList; |
|
6 |
import java.util.Iterator; |
|
7 |
import java.util.List; |
|
8 |
import org.junit.Before; |
|
9 |
import org.junit.Test; |
|
10 |
import org.junit.runner.RunWith; |
|
11 |
import org.mockito.junit.MockitoJUnitRunner; |
|
12 |
|
|
13 |
@RunWith(MockitoJUnitRunner.class) |
|
14 |
public class FetchListTest { |
|
15 |
|
|
16 |
/** |
|
17 |
* object to test |
|
18 |
*/ |
|
19 |
private FetchList<String> fetchList; |
|
20 |
|
|
21 |
private List<String> list; |
|
22 |
private Iterator<String> iter; |
|
23 |
|
|
24 |
private int FETCH_SIZE = 10; |
|
25 |
|
|
26 |
private int TOTAL_ELEMENTS = 100; |
|
27 |
|
|
28 |
@Before |
|
29 |
public void setUp() throws Exception { |
|
30 |
list = new ArrayList<String>(); |
|
31 |
for (int i = 0; i < TOTAL_ELEMENTS; i++) { |
|
32 |
list.add("XXX" + i); |
|
33 |
} |
|
34 |
this.iter = list.iterator(); |
|
35 |
fetchList = new FetchList<String>(iter, FETCH_SIZE); |
|
36 |
|
|
37 |
} |
|
38 |
|
|
39 |
@Test |
|
40 |
public void testFetchList() { |
|
41 |
assertNotNull(fetchList); |
|
42 |
} |
|
43 |
|
|
44 |
@Test |
|
45 |
public void testPoll_consumedElements() { |
|
46 |
assertEquals(0, fetchList.getConsumedElements()); |
|
47 |
fetchList.poll(); |
|
48 |
assertEquals(1, fetchList.getConsumedElements()); |
|
49 |
} |
|
50 |
|
|
51 |
@Test |
|
52 |
public void testFill_totalElements() { |
|
53 |
assertEquals(FETCH_SIZE, fetchList.getTotalElements()); |
|
54 |
} |
|
55 |
|
|
56 |
@Test |
|
57 |
public void testPoll() { |
|
58 |
for (int i = 0; i < TOTAL_ELEMENTS; i++) { |
|
59 |
assertEquals(i, fetchList.getConsumedElements()); |
|
60 |
assertEquals(list.get(i), fetchList.poll()); |
|
61 |
} |
|
62 |
assertEquals(TOTAL_ELEMENTS, fetchList.getConsumedElements()); |
|
63 |
assertEquals(TOTAL_ELEMENTS, fetchList.getTotalElements()); |
|
64 |
} |
|
65 |
|
|
66 |
@Test |
|
67 |
public void testPoll_null() { |
|
68 |
for (int i = 0; i < TOTAL_ELEMENTS; i++) { |
|
69 |
fetchList.poll(); |
|
70 |
} |
|
71 |
assertNull(fetchList.poll()); |
|
72 |
assertNull(fetchList.poll()); |
|
73 |
assertNull(fetchList.poll()); |
|
74 |
} |
|
75 |
} |
modules/cnr-resultset-service/tags/cnr-resultset-service-2.0.3/src/test/java/eu/dnetlib/enabling/resultset/ResultSetRegistryImplTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.resultset; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertEquals; |
|
4 |
import static org.junit.Assert.assertFalse; |
|
5 |
import static org.junit.Assert.assertNotNull; |
|
6 |
import static org.junit.Assert.assertNull; |
|
7 |
import static org.junit.Assert.assertTrue; |
|
8 |
import static org.mockito.Matchers.matches; |
|
9 |
import static org.mockito.Mockito.mock; |
|
10 |
import static org.mockito.Mockito.verify; |
|
11 |
import static org.mockito.Mockito.when; |
|
12 |
import net.sf.ehcache.Cache; |
|
13 |
import net.sf.ehcache.CacheManager; |
|
14 |
|
|
15 |
import org.junit.After; |
|
16 |
import org.junit.Before; |
|
17 |
import org.junit.Test; |
|
18 |
import org.junit.runner.RunWith; |
|
19 |
import org.mockito.Mock; |
|
20 |
import org.mockito.junit.MockitoJUnitRunner; |
|
21 |
|
|
22 |
import eu.dnetlib.miscutils.cache.EhCache; |
|
23 |
|
|
24 |
/** |
|
25 |
* Test ResultSetRegistryImpl. |
|
26 |
* |
|
27 |
* @author marko |
|
28 |
* |
|
29 |
*/ |
|
30 |
@RunWith(MockitoJUnitRunner.class) |
|
31 |
public class ResultSetRegistryImplTest { |
|
32 |
|
|
33 |
/** |
|
34 |
* max in memory elements. |
|
35 |
*/ |
|
36 |
private static final int MAX_IN_MEM = 100; |
|
37 |
|
|
38 |
/** |
|
39 |
* default time. |
|
40 |
*/ |
|
41 |
private static final int DEFAULT_TTI = 1000; |
|
42 |
|
|
43 |
/** |
|
44 |
* default time to live. |
|
45 |
*/ |
|
46 |
private static final int DEFAULT_TTL = 1000; |
|
47 |
|
|
48 |
/** |
|
49 |
* class under test. |
|
50 |
*/ |
|
51 |
private transient ResultSetRegistryImpl registry; |
|
52 |
|
|
53 |
/** |
|
54 |
* mock of a resultset object. |
|
55 |
*/ |
|
56 |
@Mock |
|
57 |
private transient ResultSet resultSet; |
|
58 |
|
|
59 |
/** |
|
60 |
* ehcache manager. |
|
61 |
*/ |
|
62 |
private final transient CacheManager cacheManager = CacheManager.create(); |
|
63 |
|
|
64 |
/** |
|
65 |
* Set up basic mocks. |
|
66 |
*/ |
|
67 |
@Before |
|
68 |
public void setUp() { |
|
69 |
final Cache ehCache = new net.sf.ehcache.Cache("testCache", MAX_IN_MEM, false, false, DEFAULT_TTL, DEFAULT_TTI); |
|
70 |
cacheManager.addCache(ehCache); |
|
71 |
|
|
72 |
final Cache mtEhCache = new net.sf.ehcache.Cache("testMTCache", MAX_IN_MEM, false, false, DEFAULT_TTL, DEFAULT_TTI); |
|
73 |
cacheManager.addCache(mtEhCache); |
|
74 |
|
|
75 |
final EhCache<String, ResultSet> cache = new EhCache<String, ResultSet>(ehCache); |
|
76 |
cache.setCache(ehCache); |
|
77 |
|
|
78 |
final EhCache<String, Integer> mtCache = new EhCache<String, Integer>(mtEhCache); |
|
79 |
mtCache.setCache(mtEhCache); |
|
80 |
|
|
81 |
registry = new ResultSetRegistryImpl(); |
|
82 |
registry.setCache(cache); |
|
83 |
registry.setMaxIdleTimeCache(mtCache); |
|
84 |
|
|
85 |
when(resultSet.getIdentifier()).thenReturn("123456"); |
|
86 |
when(resultSet.isOpen()).thenReturn(true); |
|
87 |
} |
|
88 |
|
|
89 |
/** |
|
90 |
* remove ehcache from ehcachemanager. |
|
91 |
*/ |
|
92 |
@After |
|
93 |
public void tearDown() { |
|
94 |
cacheManager.removeAllCaches(); |
|
95 |
} |
|
96 |
|
|
97 |
/** |
|
98 |
* Test that registered resultsets are really registered. |
|
99 |
*/ |
|
100 |
@Test |
|
101 |
public void testAddResultSet() { |
|
102 |
|
|
103 |
final String rsId = resultSet.getIdentifier(); |
|
104 |
assertNotNull("the resultset should not be null", rsId); |
|
105 |
|
|
106 |
registry.addResultSet(resultSet); |
|
107 |
verify(resultSet).setIdentifier(matches("rs-" + registry.getIdGenerator().getRegEx())); |
|
108 |
|
|
109 |
final ResultSet res = registry.getResultSetById(rsId); // NOPMD |
|
110 |
assertNotNull("the resultset is not found", res); |
|
111 |
assertEquals("the resultsets don't have the same id", rsId, res.getIdentifier()); |
|
112 |
assertEquals("the resultsets are not the same instance", resultSet, res); |
|
113 |
} |
|
114 |
|
|
115 |
/** |
|
116 |
* Test that registered resultsets are really registered. |
|
117 |
*/ |
|
118 |
@Test |
|
119 |
public void testAddResultSetWithId() { |
|
120 |
resultSet = mock(ResultSet.class); |
|
121 |
when(resultSet.getIdentifier()).thenReturn("123"); |
|
122 |
|
|
123 |
final String rsId = resultSet.getIdentifier(); |
|
124 |
|
|
125 |
registry.addResultSet(resultSet, rsId); |
|
126 |
verify(resultSet).setIdentifier(rsId); |
|
127 |
|
|
128 |
final ResultSet res = registry.getResultSetById(rsId); // NOPMD |
|
129 |
assertNotNull("the resultset is not found", res); |
|
130 |
assertEquals("the resultsets don't have the same id", rsId, res.getIdentifier()); |
|
131 |
assertEquals("the resultsets are not the same instance", resultSet, res); |
|
132 |
} |
|
133 |
|
|
134 |
/** |
|
135 |
* try to obtain a inexistent resultset. |
|
136 |
*/ |
|
137 |
@Test |
|
138 |
public void testInexistentResultSet() { |
|
139 |
final String rsId = resultSet.getIdentifier(); |
|
140 |
assertNull("inexisten resultset returns null", registry.getResultSetById(rsId)); |
|
141 |
} |
|
142 |
|
|
143 |
/** |
|
144 |
* test closing of resultset and its pruning from the registry. |
|
145 |
*/ |
|
146 |
@Test |
|
147 |
public void testResultSetClose() { |
|
148 |
final String rsId = resultSet.getIdentifier(); |
|
149 |
|
|
150 |
assertTrue("check if resultset is open", resultSet.isOpen()); |
|
151 |
|
|
152 |
registry.addResultSet(resultSet); |
|
153 |
verify(resultSet).addObserver(registry); |
|
154 |
|
|
155 |
// simulate a resultset close on the mock |
|
156 |
when(resultSet.isOpen()).thenReturn(false); |
|
157 |
registry.update(resultSet, null); |
|
158 |
|
|
159 |
assertFalse("check if resultset is closed", resultSet.isOpen()); |
|
160 |
assertNull("check if the object is pruned from the registry", registry.getResultSetById(rsId)); |
|
161 |
} |
|
162 |
|
|
163 |
/** |
|
164 |
* same as testResultSetClose() but with a real observer/observable mechanism, i.e. without resultset mock class. |
|
165 |
* |
|
166 |
*/ |
|
167 |
@Test |
|
168 |
public void testWithRealObservable() { |
|
169 |
final LocalResultSetImpl rset = new LocalResultSetImpl(null); |
|
170 |
rset.setIdentifier("123456"); |
|
171 |
resultSet = rset; |
|
172 |
|
|
173 |
final String rsId = resultSet.getIdentifier(); |
|
174 |
|
|
175 |
assertFalse("check if resultset is not destroyed", resultSet.isDestroyed()); |
|
176 |
|
|
177 |
registry.addResultSet(resultSet); |
|
178 |
resultSet.destroy(); |
|
179 |
|
|
180 |
assertTrue("check if resultset is destroyed", resultSet.isDestroyed()); |
|
181 |
assertNull("check if the object is pruned from the registry", registry.getResultSetById(rsId)); |
|
182 |
} |
|
183 |
|
|
184 |
} |
modules/cnr-resultset-service/tags/cnr-resultset-service-2.0.3/src/test/java/eu/dnetlib/enabling/resultset/LocalResultSetImplTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.resultset; |
|
2 |
|
|
3 |
import static org.junit.Assert.*; // NOPMD |
|
4 |
import static org.mockito.Mockito.*; // NOPMD by marko on 11/26/08 9:13 PM |
|
5 |
|
|
6 |
import java.util.ArrayList; |
|
7 |
import java.util.List; |
|
8 |
|
|
9 |
import org.junit.Before; |
|
10 |
import org.junit.Test; |
|
11 |
import org.junit.runner.RunWith; |
|
12 |
import org.mockito.Mock; |
|
13 |
import org.mockito.junit.MockitoJUnitRunner; |
|
14 |
|
|
15 |
/** |
|
16 |
* test the resultset concrete implementation. |
|
17 |
* |
|
18 |
* @author marko |
|
19 |
* |
|
20 |
*/ |
|
21 |
@RunWith(MockitoJUnitRunner.class) |
|
22 |
public class LocalResultSetImplTest { |
|
23 |
|
|
24 |
/** |
|
25 |
* This class could be an anonymous class, but is here to be shared among tests. |
|
26 |
* |
|
27 |
* @author marko |
|
28 |
* |
|
29 |
*/ |
|
30 |
private static final class TestResultSetListener implements ResultSetListener { |
|
31 |
/** |
|
32 |
* {@inheritDoc} |
|
33 |
* |
|
34 |
* @see eu.dnetlib.enabling.resultset.ResultSetListener#getResult(int, int) |
|
35 |
*/ |
|
36 |
@Override |
|
37 |
public List<String> getResult(final int fromPosition, final int toPosition) { |
|
38 |
final List<String> res = new ArrayList<String>(); |
|
39 |
for (int i = fromPosition; i <= toPosition; i++) |
|
40 |
res.add(((Integer) (i - 1)).toString()); |
|
41 |
return res; |
|
42 |
} |
|
43 |
|
|
44 |
@Override |
|
45 |
public int getSize() { |
|
46 |
return RESULT_SIZE; |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
50 |
/** |
|
51 |
* Mockito lacks of multiple interfaces mocking. |
|
52 |
* |
|
53 |
* @author marko |
|
54 |
* |
|
55 |
*/ |
|
56 |
private abstract static class AbstractResultSetAware implements ResultSetListener, ResultSetAware { |
|
57 |
} |
|
58 |
|
|
59 |
/** |
|
60 |
* resultset registry mock. |
|
61 |
*/ |
|
62 |
@Mock |
|
63 |
private transient ResultSetRegistry registry; |
|
64 |
|
|
65 |
/** |
|
66 |
* object under test. |
|
67 |
*/ |
|
68 |
private transient LocalResultSetImpl resultSet; |
|
69 |
|
|
70 |
/** |
|
71 |
* number of returned test results. |
|
72 |
*/ |
|
73 |
private static final int RESULT_SIZE = 3; |
|
74 |
|
|
75 |
/** |
|
76 |
* provides data to the resultset under test. |
|
77 |
*/ |
|
78 |
private transient TestResultSetListener resultSetListener; |
|
79 |
|
|
80 |
/** |
|
81 |
* prepare. |
|
82 |
* |
|
83 |
*/ |
|
84 |
@Before |
|
85 |
public void setUp() { |
|
86 |
resultSetListener = new TestResultSetListener(); |
|
87 |
resultSet = new LocalResultSetImpl(resultSetListener); |
|
88 |
} |
|
89 |
|
|
90 |
/** |
|
91 |
* test the get number of results method. |
|
92 |
*/ |
|
93 |
@Test |
|
94 |
public void testGetNumberOfResults() { |
|
95 |
assertEquals("check number of results", RESULT_SIZE, resultSet.getNumberOfResults()); |
|
96 |
} |
|
97 |
|
|
98 |
/** |
|
99 |
* test the get result method. |
|
100 |
*/ |
|
101 |
@Test |
|
102 |
public void testGetResults() { |
|
103 |
final List<String> res = resultSet.getResults(1, RESULT_SIZE); // NOPMD by marko on 11/27/08 3:00 AM |
|
104 |
assertEquals("whole length", RESULT_SIZE, res.size()); |
|
105 |
for (int i = 0; i < RESULT_SIZE; i++) |
|
106 |
assertEquals("check element", ((Integer) i).toString(), res.get(i)); |
|
107 |
|
|
108 |
final List<String> res2 = resultSet.getResults(1, RESULT_SIZE - 1); // NOPMD by marko on 11/27/08 3:00 AM |
|
109 |
assertEquals("shorter", RESULT_SIZE - 1, res2.size()); |
|
110 |
|
|
111 |
final List<String> res3 = resultSet.getResults(1, RESULT_SIZE + 1); // NOPMD by marko on 11/27/08 3:00 AM |
|
112 |
assertEquals("to out of bounds", RESULT_SIZE, res3.size()); |
|
113 |
|
|
114 |
final List<String> res4 = resultSet.getResults(RESULT_SIZE + 1, RESULT_SIZE); // NOPMD by marko on 11/27/08 3:00 AM |
|
115 |
assertEquals("from out of bounds", 0, res4.size()); |
|
116 |
|
|
117 |
final List<String> res5 = resultSet.getResults(0, RESULT_SIZE); // NOPMD by marko on 11/27/08 3:00 AM |
|
118 |
assertEquals("from lesser than 1", RESULT_SIZE, res5.size()); |
|
119 |
|
|
120 |
final List<String> res6 = resultSet.getResults(RESULT_SIZE, 1); // NOPMD by marko on 11/27/08 3:00 AM |
|
121 |
assertEquals("inverted from and to", 1, res6.size()); |
|
122 |
|
|
123 |
resultSet.setOpen(true); |
|
124 |
final List<String> res7 = resultSet.getResults(1, RESULT_SIZE + 1); // NOPMD by marko on 11/27/08 3:00 AM |
|
125 |
assertEquals("to out of bounds - pass through", RESULT_SIZE + 1, res7.size()); |
|
126 |
} |
|
127 |
|
|
128 |
/** |
|
129 |
* test observer/observable pattern. |
|
130 |
*/ |
|
131 |
@Test |
|
132 |
public void testDestroy() { |
|
133 |
resultSet.addObserver(registry); |
|
134 |
assertEquals("observer should be added", 1, resultSet.countObservers()); |
|
135 |
|
|
136 |
resultSet.destroy(); |
|
137 |
assertTrue("should be destroyed", resultSet.isDestroyed()); |
|
138 |
|
|
139 |
assertEquals("observers should be cleared", 0, resultSet.countObservers()); |
|
140 |
verify(registry, times(1)).update(resultSet, null); |
|
141 |
} |
|
142 |
|
|
143 |
/** |
|
144 |
* test getter ... let's make code coverage happy. |
|
145 |
*/ |
|
146 |
@Test |
|
147 |
public void testGetListener() { |
|
148 |
assertEquals("test getter?", resultSetListener, resultSet.getListener()); |
|
149 |
} |
|
150 |
|
|
151 |
/** |
|
152 |
* test setter ... let's make code coverage happy. |
|
153 |
*/ |
|
154 |
@Test |
|
155 |
public void testSetListener() { |
|
156 |
assertEquals("test getter?", resultSetListener, resultSet.getListener()); |
|
157 |
resultSet.setListener(null); |
|
158 |
assertNull("test setter?", resultSet.getListener()); |
|
159 |
} |
|
160 |
|
|
161 |
/** |
|
162 |
* test. |
|
163 |
*/ |
|
164 |
@Test |
|
165 |
public void testResultSetAware() { |
|
166 |
final AbstractResultSetAware abr = mock(AbstractResultSetAware.class); |
|
167 |
resultSet = new LocalResultSetImpl(abr); |
|
168 |
assertNotNull("dummy", resultSet); |
|
169 |
|
|
170 |
verify(abr).setResultSet((ResultSet) anyObject()); |
|
171 |
|
|
172 |
} |
|
173 |
|
|
174 |
} |
|
0 | 175 |
modules/cnr-resultset-service/tags/cnr-resultset-service-2.0.3/src/test/java/eu/dnetlib/enabling/resultset/StreamingResultSetListenerTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.resultset; |
|
2 |
|
|
3 |
import static org.junit.Assert.*; |
|
4 |
|
|
5 |
import java.util.List; |
|
6 |
|
|
7 |
import org.junit.Before; |
|
8 |
import org.junit.Test; |
|
9 |
|
|
10 |
import com.google.common.collect.Lists; |
|
11 |
|
|
12 |
public class StreamingResultSetListenerTest { |
|
13 |
|
Also available in: Unified diff
[maven-release-plugin] copy for tag cnr-resultset-service-2.0.3