/clustering/infinispan/extension/src/test/java/org/jboss/as/clustering/infinispan/DefaultCacheContainerTestCase.java

https://github.com/fharms/wildfly · Java · 344 lines · 226 code · 93 blank · 25 comment · 0 complexity · 10231f3c428f53363b2679c97114c2c4 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.clustering.infinispan;
  23. import static org.junit.Assert.*;
  24. import static org.mockito.Mockito.*;
  25. import java.util.Collections;
  26. import java.util.List;
  27. import java.util.Set;
  28. import org.infinispan.AdvancedCache;
  29. import org.infinispan.Cache;
  30. import org.infinispan.configuration.cache.Configuration;
  31. import org.infinispan.configuration.cache.ConfigurationBuilder;
  32. import org.infinispan.configuration.global.GlobalConfiguration;
  33. import org.infinispan.configuration.global.GlobalConfigurationBuilder;
  34. import org.infinispan.lifecycle.ComponentStatus;
  35. import org.infinispan.manager.EmbeddedCacheManager;
  36. import org.infinispan.remoting.transport.Address;
  37. import org.junit.After;
  38. import org.junit.Test;
  39. import org.wildfly.clustering.infinispan.spi.CacheContainer;
  40. import org.wildfly.clustering.service.SubGroupServiceNameFactory;
  41. /**
  42. * Unit test for {@link DefaultCacheContainer}.
  43. * @author Paul Ferraro
  44. */
  45. public class DefaultCacheContainerTestCase {
  46. private static final String NAME = "name";
  47. private static final String DEFAULT_CACHE = "default-cache";
  48. private final BatcherFactory batcherFactory = mock(BatcherFactory.class);
  49. private final EmbeddedCacheManager manager = mock(EmbeddedCacheManager.class);
  50. private final CacheContainer subject = new DefaultCacheContainer("name", this.manager, DEFAULT_CACHE, this.batcherFactory);
  51. @After
  52. public void cleanup() {
  53. reset(this.manager);
  54. }
  55. @Test
  56. public void getName() {
  57. assertSame(NAME, this.subject.getName());
  58. }
  59. @Test
  60. public void getDefaultCacheName() {
  61. assertSame(DEFAULT_CACHE, this.subject.getDefaultCacheName());
  62. }
  63. @Test
  64. public void getDefaultCache() {
  65. AdvancedCache<Object, Object> cache = mock(AdvancedCache.class);
  66. when(this.manager.<Object, Object>getCache(DEFAULT_CACHE, DEFAULT_CACHE)).thenReturn(cache);
  67. when(cache.getAdvancedCache()).thenReturn(cache);
  68. Cache<Object, Object> result = this.subject.getCache();
  69. assertNotSame(cache, result);
  70. assertEquals(result, cache);
  71. assertSame(this.subject, result.getCacheManager());
  72. }
  73. @Test
  74. public void getCache() {
  75. AdvancedCache<Object, Object> defaultCache = mock(AdvancedCache.class);
  76. AdvancedCache<Object, Object> otherCache = mock(AdvancedCache.class);
  77. when(this.manager.<Object, Object>getCache(DEFAULT_CACHE, DEFAULT_CACHE)).thenReturn(defaultCache);
  78. when(this.manager.<Object, Object>getCache("other", "other")).thenReturn(otherCache);
  79. when(defaultCache.getAdvancedCache()).thenReturn(defaultCache);
  80. when(otherCache.getAdvancedCache()).thenReturn(otherCache);
  81. Cache<Object, Object> result = this.subject.getCache("other");
  82. assertNotSame(otherCache, result);
  83. assertEquals(result, otherCache);
  84. assertSame(this.subject, result.getCacheManager());
  85. result = this.subject.getCache(SubGroupServiceNameFactory.DEFAULT_SUB_GROUP);
  86. assertNotSame(defaultCache, result);
  87. assertEquals(result, defaultCache);
  88. assertSame(this.subject, result.getCacheManager());
  89. result = this.subject.getCache(null);
  90. assertNotSame(defaultCache, result);
  91. assertEquals(result, defaultCache);
  92. assertSame(this.subject, result.getCacheManager());
  93. }
  94. @Test
  95. public void getCacheFromTemplate() {
  96. AdvancedCache<Object, Object> defaultCache = mock(AdvancedCache.class);
  97. AdvancedCache<Object, Object> otherCache = mock(AdvancedCache.class);
  98. String templateName = "template";
  99. when(this.manager.<Object, Object>getCache(DEFAULT_CACHE, templateName)).thenReturn(defaultCache);
  100. when(this.manager.<Object, Object>getCache("other", templateName)).thenReturn(otherCache);
  101. when(defaultCache.getAdvancedCache()).thenReturn(defaultCache);
  102. when(otherCache.getAdvancedCache()).thenReturn(otherCache);
  103. Cache<Object, Object> result = this.subject.getCache("other", templateName);
  104. assertNotSame(otherCache, result);
  105. assertEquals(result, otherCache);
  106. assertSame(this.subject, result.getCacheManager());
  107. result = this.subject.getCache(SubGroupServiceNameFactory.DEFAULT_SUB_GROUP, templateName);
  108. assertNotSame(defaultCache, result);
  109. assertEquals(result, defaultCache);
  110. assertSame(this.subject, result.getCacheManager());
  111. result = this.subject.getCache(null, templateName);
  112. assertNotSame(defaultCache, result);
  113. assertEquals(result, defaultCache);
  114. assertSame(this.subject, result.getCacheManager());
  115. }
  116. @Test
  117. public void start() {
  118. this.subject.start();
  119. verify(this.manager, never()).start();
  120. }
  121. @Test
  122. public void stop() {
  123. this.subject.stop();
  124. verify(this.manager, never()).stop();
  125. }
  126. @Test
  127. public void addListener() {
  128. Object listener = new Object();
  129. this.subject.addListener(listener);
  130. verify(this.manager).addListener(listener);
  131. }
  132. @Test
  133. public void removeListener() {
  134. Object listener = new Object();
  135. this.subject.removeListener(listener);
  136. verify(this.manager).removeListener(listener);
  137. }
  138. @Test
  139. public void getListeners() {
  140. Set<Object> expected = Collections.singleton(new Object());
  141. when(this.manager.getListeners()).thenReturn(expected);
  142. Set<Object> result = this.subject.getListeners();
  143. assertSame(expected, result);
  144. }
  145. @Test
  146. public void defineConfiguration() {
  147. ConfigurationBuilder builder = new ConfigurationBuilder();
  148. Configuration defaultConfig = builder.build();
  149. Configuration otherConfig = builder.build();
  150. when(this.manager.defineConfiguration(DEFAULT_CACHE, defaultConfig)).thenReturn(defaultConfig);
  151. when(this.manager.defineConfiguration("other", otherConfig)).thenReturn(otherConfig);
  152. Configuration result = this.subject.defineConfiguration(SubGroupServiceNameFactory.DEFAULT_SUB_GROUP, defaultConfig);
  153. assertSame(defaultConfig, result);
  154. result = this.subject.defineConfiguration(null, otherConfig);
  155. assertSame(defaultConfig, result);
  156. result = this.subject.defineConfiguration("other", otherConfig);
  157. assertSame(otherConfig, result);
  158. }
  159. @Test
  160. public void undefineConfiguration() {
  161. this.subject.undefineConfiguration("test");
  162. verify(this.manager).undefineConfiguration("test");
  163. }
  164. @Test
  165. public void getClusterName() {
  166. String expected = "cluster";
  167. when(this.manager.getClusterName()).thenReturn(expected);
  168. String result = this.subject.getClusterName();
  169. assertSame(expected, result);
  170. }
  171. @Test
  172. public void getMembers() {
  173. List<Address> expected = Collections.singletonList(mock(Address.class));
  174. when(this.manager.getMembers()).thenReturn(expected);
  175. List<Address> result = this.subject.getMembers();
  176. assertSame(expected, result);
  177. }
  178. @Test
  179. public void getAddress() {
  180. Address expected = mock(Address.class);
  181. when(this.manager.getAddress()).thenReturn(expected);
  182. Address result = this.subject.getAddress();
  183. assertSame(expected, result);
  184. }
  185. @Test
  186. public void getCoordinator() {
  187. Address expected = mock(Address.class);
  188. when(this.manager.getCoordinator()).thenReturn(expected);
  189. Address result = this.subject.getCoordinator();
  190. assertSame(expected, result);
  191. }
  192. @Test
  193. public void getStatus() {
  194. ComponentStatus expected = ComponentStatus.INITIALIZING;
  195. when(this.manager.getStatus()).thenReturn(expected);
  196. ComponentStatus result = this.subject.getStatus();
  197. assertSame(expected, result);
  198. }
  199. @Test
  200. public void getCacheManagerConfiguration() {
  201. GlobalConfiguration global = new GlobalConfigurationBuilder().build();
  202. when(this.manager.getCacheManagerConfiguration()).thenReturn(global);
  203. GlobalConfiguration result = this.subject.getCacheManagerConfiguration();
  204. assertSame(global, result);
  205. }
  206. @Test
  207. public void getDefaultCacheConfiguration() {
  208. Configuration config = new ConfigurationBuilder().build();
  209. when(this.manager.getCacheConfiguration(DEFAULT_CACHE)).thenReturn(config);
  210. Configuration result = this.subject.getDefaultCacheConfiguration();
  211. assertSame(config, result);
  212. }
  213. @Test
  214. public void getCacheConfiguration() {
  215. Configuration config = new ConfigurationBuilder().build();
  216. when(this.manager.getCacheConfiguration("cache")).thenReturn(config);
  217. Configuration result = this.subject.getCacheConfiguration("cache");
  218. assertSame(config, result);
  219. }
  220. @Test
  221. public void getCacheNames() {
  222. Set<String> caches = Collections.singleton("other");
  223. when(this.manager.getCacheNames()).thenReturn(caches);
  224. Set<String> result = this.subject.getCacheNames();
  225. assertEquals(1, result.size());
  226. assertTrue(result.contains("other"));
  227. }
  228. @Test
  229. public void isRunning() {
  230. when(this.manager.isRunning("other")).thenReturn(false);
  231. when(this.manager.isRunning(DEFAULT_CACHE)).thenReturn(true);
  232. boolean result = this.subject.isRunning("other");
  233. assertFalse(result);
  234. result = this.subject.isRunning(SubGroupServiceNameFactory.DEFAULT_SUB_GROUP);
  235. assertTrue(result);
  236. result = this.subject.isRunning(null);
  237. assertTrue(result);
  238. }
  239. @Test
  240. public void isDefaultRunning() {
  241. when(this.manager.isRunning(DEFAULT_CACHE)).thenReturn(true);
  242. boolean result = this.subject.isDefaultRunning();
  243. assertTrue(result);
  244. }
  245. @Test
  246. public void startCaches() {
  247. when(this.manager.startCaches("other", DEFAULT_CACHE)).thenReturn(this.manager);
  248. EmbeddedCacheManager result = this.subject.startCaches("other", SubGroupServiceNameFactory.DEFAULT_SUB_GROUP);
  249. assertSame(this.subject, result);
  250. }
  251. }