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
|
}
|