1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.ehcache.terracotta;
18
19 import static org.mockito.Matchers.any;
20 import static org.mockito.Mockito.when;
21
22 import java.lang.reflect.Method;
23 import java.util.Map;
24
25 import net.sf.ehcache.Ehcache;
26 import net.sf.ehcache.cluster.CacheCluster;
27 import net.sf.ehcache.concurrent.CacheLockProvider;
28 import net.sf.ehcache.config.CacheConfiguration;
29 import net.sf.ehcache.config.TerracottaClientConfiguration;
30 import net.sf.ehcache.config.TerracottaConfiguration.StorageStrategy;
31 import net.sf.ehcache.store.TerracottaStore;
32 import net.sf.ehcache.terracotta.TerracottaClusteredInstanceHelper.TerracottaRuntimeType;
33
34 import org.mockito.Mockito;
35 import org.mockito.invocation.InvocationOnMock;
36 import org.mockito.stubbing.Answer;
37
38 /***
39 * @author Abhishek Sanoujam
40 */
41 public class TerracottaUnitTesting {
42
43 public static void setupTerracottaTesting(ClusteredInstanceFactory mockFactory) throws Exception {
44 setupTerracottaTesting(mockFactory, null, TerracottaRuntimeType.EnterpriseExpress, StorageStrategy.DCV2);
45 }
46
47 public static void setupTerracottaTesting(ClusteredInstanceFactory mockFactory, Runnable onNewClusteredInstanceFactory)
48 throws Exception {
49 setupTerracottaTesting(mockFactory, onNewClusteredInstanceFactory, TerracottaRuntimeType.EnterpriseExpress, StorageStrategy.DCV2);
50 }
51
52 public static void setupTerracottaTesting(ClusteredInstanceFactory mockFactory, TerracottaRuntimeType terracottaRuntimeType,
53 StorageStrategy defaultStorageStrategyForCurrentRuntime) throws Exception {
54 setupTerracottaTesting(mockFactory, null, terracottaRuntimeType, defaultStorageStrategyForCurrentRuntime);
55 }
56
57 public static void setupTerracottaTesting(final ClusteredInstanceFactory mockFactory, final Runnable onNewClusteredInstanceFactory,
58 TerracottaRuntimeType terracottaRuntimeType, StorageStrategy defaultStorageStrategyForCurrentRuntime)
59 throws Exception {
60 TerracottaStore terracottaStore = Mockito.mock(TerracottaStore.class);
61 CacheCluster mockCacheCluster = Mockito.mock(CacheCluster.class);
62 when(mockFactory.createStore((Ehcache) any())).thenReturn(terracottaStore);
63 when(mockFactory.getTopology()).thenReturn(mockCacheCluster);
64 CacheLockProvider mockCacheLockProvider = Mockito.mock(CacheLockProvider.class);
65 when(terracottaStore.getInternalContext()).thenReturn(mockCacheLockProvider);
66
67 TerracottaClusteredInstanceHelper mockHelper = Mockito.mock(TerracottaClusteredInstanceHelper.class);
68 when(mockHelper.newClusteredInstanceFactory((Map<String, CacheConfiguration>) any(), (TerracottaClientConfiguration) any()))
69 .thenAnswer(new Answer<ClusteredInstanceFactory>() {
70 public ClusteredInstanceFactory answer(InvocationOnMock invocation) throws Throwable {
71 if (onNewClusteredInstanceFactory != null) {
72 onNewClusteredInstanceFactory.run();
73 }
74 return mockFactory;
75 }
76 });
77 when(mockHelper.getTerracottaRuntimeTypeOrNull()).thenReturn(terracottaRuntimeType);
78 when(mockHelper.getDefaultStorageStrategyForCurrentRuntime((CacheConfiguration) any())).thenReturn(defaultStorageStrategyForCurrentRuntime);
79
80 Method method = TerracottaClient.class.getDeclaredMethod("setTestMode", TerracottaClusteredInstanceHelper.class);
81 method.setAccessible(true);
82 method.invoke(null, mockHelper);
83 }
84 }