-
Notifications
You must be signed in to change notification settings - Fork 213
/
InfinispanSourceTest.java
218 lines (194 loc) · 8.43 KB
/
InfinispanSourceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* ModeShape (http://www.modeshape.org)
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
* See the AUTHORS.txt file in the distribution for a full listing of
* individual contributors.
*
* ModeShape is free software. Unless otherwise indicated, all code in ModeShape
* is licensed to you under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* ModeShape is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.modeshape.connector.infinispan;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;
import org.infinispan.manager.CacheManager;
import org.modeshape.graph.cache.BasicCachePolicy;
import org.modeshape.graph.connector.RepositoryConnection;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
import org.mockito.Mock;
/**
*/
public class InfinispanSourceTest {
private InfinispanSource source;
private RepositoryConnection connection;
private String validName;
private String validCacheConfigurationName;
private String validCacheManagerJndiName;
private UUID validRootNodeUuid;
@Mock
private Context jndiContext;
@Mock
private CacheManager cacheManager;
@Before
public void beforeEach() throws Exception {
MockitoAnnotations.initMocks(this);
validName = "cache source";
validCacheConfigurationName = "cache config name";
validCacheManagerJndiName = "cache factory jndi name";
validRootNodeUuid = UUID.randomUUID();
source = new InfinispanSource();
// Set up the fake JNDI context ...
source.setContext(jndiContext);
when(jndiContext.lookup(validCacheManagerJndiName)).thenReturn(cacheManager);
}
@After
public void afterEach() throws Exception {
if (connection != null) {
connection.close();
}
}
@Test
public void shouldReturnNonNullCapabilities() {
assertThat(source.getCapabilities(), is(notNullValue()));
}
@Test
public void shouldSupportSameNameSiblings() {
assertThat(source.getCapabilities().supportsSameNameSiblings(), is(true));
}
@Test
public void shouldSupportUpdates() {
assertThat(source.getCapabilities().supportsUpdates(), is(true));
}
@Test
public void shouldHaveNullSourceNameUponConstruction() {
source = new InfinispanSource();
assertThat(source.getName(), is(nullValue()));
}
@Test
public void shouldAllowSettingName() {
source.setName("Something");
assertThat(source.getName(), is("Something"));
source.setName("another name");
assertThat(source.getName(), is("another name"));
}
@Test
public void shouldAllowSettingNameToNull() {
source.setName("some name");
source.setName(null);
assertThat(source.getName(), is(nullValue()));
}
@Test
public void shouldHaveDefaultRetryLimit() {
assertThat(source.getRetryLimit(), is(InfinispanSource.DEFAULT_RETRY_LIMIT));
}
@Test
public void shouldSetRetryLimitToZeroWhenSetWithNonPositiveValue() {
source.setRetryLimit(0);
assertThat(source.getRetryLimit(), is(0));
source.setRetryLimit(-1);
assertThat(source.getRetryLimit(), is(0));
source.setRetryLimit(-100);
assertThat(source.getRetryLimit(), is(0));
}
@Test
public void shouldAllowRetryLimitToBeSet() {
for (int i = 0; i != 100; ++i) {
source.setRetryLimit(i);
assertThat(source.getRetryLimit(), is(i));
}
}
@Test
public void shouldCreateJndiReferenceAndRecreatedObjectFromReference() throws Exception {
BasicCachePolicy cachePolicy = new BasicCachePolicy();
cachePolicy.setTimeToLive(1000L, TimeUnit.MILLISECONDS);
convertToAndFromJndiReference(validName,
validRootNodeUuid,
validCacheConfigurationName,
validCacheManagerJndiName,
cachePolicy,
100);
}
@Test
public void shouldCreateJndiReferenceAndRecreatedObjectFromReferenceWithNullProperties() throws Exception {
BasicCachePolicy cachePolicy = new BasicCachePolicy();
cachePolicy.setTimeToLive(1000L, TimeUnit.MILLISECONDS);
convertToAndFromJndiReference("some source", null, null, null, null, 100);
convertToAndFromJndiReference(null, null, null, null, null, 100);
}
private void convertToAndFromJndiReference( String sourceName,
UUID rootNodeUuid,
String cacheConfigName,
String cacheManagerJndiName,
BasicCachePolicy cachePolicy,
int retryLimit ) throws Exception {
source.setRetryLimit(retryLimit);
source.setName(sourceName);
source.setCacheConfigurationName(cacheConfigName);
source.setCacheManagerJndiName(cacheManagerJndiName);
source.setDefaultCachePolicy(cachePolicy);
source.setRootNodeUuid(rootNodeUuid != null ? rootNodeUuid.toString() : null);
Reference ref = source.getReference();
assertThat(ref.getClassName(), is(InfinispanSource.class.getName()));
assertThat(ref.getFactoryClassName(), is(InfinispanSource.class.getName()));
Map<String, Object> refAttributes = new HashMap<String, Object>();
Enumeration<RefAddr> enumeration = ref.getAll();
while (enumeration.hasMoreElements()) {
RefAddr addr = enumeration.nextElement();
refAttributes.put(addr.getType(), addr.getContent());
}
// Recreate the object, use a newly constructed source ...
ObjectFactory factory = new InfinispanSource();
Name name = mock(Name.class);
Context context = mock(Context.class);
Hashtable<?, ?> env = new Hashtable<Object, Object>();
InfinispanSource recoveredSource = (InfinispanSource)factory.getObjectInstance(ref, name, context, env);
assertThat(recoveredSource, is(notNullValue()));
assertThat(recoveredSource.getName(), is(source.getName()));
assertThat(recoveredSource.getRootNodeUuid(), is(source.getRootNodeUuid()));
assertThat(recoveredSource.getRetryLimit(), is(source.getRetryLimit()));
assertThat(recoveredSource.getCacheManagerJndiName(), is(source.getCacheManagerJndiName()));
assertThat(recoveredSource.getCacheConfigurationName(), is(source.getCacheConfigurationName()));
assertThat(recoveredSource.getDefaultCachePolicy(), is(source.getDefaultCachePolicy()));
assertThat(recoveredSource.equals(source), is(true));
assertThat(source.equals(recoveredSource), is(true));
}
@Test
public void shouldCreateCacheUsingDefaultCacheManagerWhenNoCacheOrCacheManagerOrCacheConfigurationNameIsFound()
throws Exception {
source.setName(validName);
connection = source.getConnection();
assertThat(connection, is(notNullValue()));
// assertThat(connection.getCache(), is(notNullValue()));
}
}