Clover icon

Magnolia Module Cache 5.5.9

  1. Project Clover database Mon Nov 25 2019 16:46:50 CET
  2. Package info.magnolia.module.cache

File FlushAllListeningPolicyTest.java

 

Code metrics

0
46
7
1
175
107
7
0.15
6.57
7
1

Classes

Class Line # Actions
FlushAllListeningPolicyTest 76 46 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /**
2    * This file Copyright (c) 2014-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.module.cache;
35   
36    import static org.mockito.Matchers.any;
37    import static org.mockito.Matchers.anyInt;
38    import static org.mockito.Matchers.eq;
39    import static org.mockito.Mockito.*;
40   
41    import info.magnolia.cms.util.ObservationUtil;
42    import info.magnolia.context.MgnlContext;
43    import info.magnolia.event.EventBus;
44    import info.magnolia.jcr.node2bean.TypeMapping;
45    import info.magnolia.jcr.node2bean.impl.TypeMappingImpl;
46    import info.magnolia.repository.RepositoryConstants;
47    import info.magnolia.repository.RepositoryManager;
48    import info.magnolia.repository.definition.RepositoryDefinition;
49    import info.magnolia.repository.definition.WorkspaceMappingDefinition;
50    import info.magnolia.test.ComponentsTestUtil;
51    import info.magnolia.test.mock.MockContext;
52    import info.magnolia.test.mock.jcr.MockSession;
53   
54    import java.util.Arrays;
55    import java.util.List;
56    import java.util.concurrent.Callable;
57    import java.util.concurrent.ExecutorService;
58    import java.util.concurrent.Executors;
59    import java.util.concurrent.Future;
60   
61    import javax.jcr.observation.EventListener;
62   
63    import org.junit.After;
64    import org.junit.Before;
65    import org.junit.Test;
66    import org.junit.runner.RunWith;
67    import org.powermock.api.mockito.PowerMockito;
68    import org.powermock.core.classloader.annotations.PrepareForTest;
69    import org.powermock.modules.junit4.PowerMockRunner;
70   
71    /**
72    * Test class for {@link FlushAllListeningPolicy}.
73    */
74    @RunWith(PowerMockRunner.class)
75    @PrepareForTest(ObservationUtil.class)
 
76    public class FlushAllListeningPolicyTest {
77   
78    private FlushAllListeningPolicy policy;
79    private final Cache cache1 = mock(Cache.class);
80   
 
81  3 toggle @Before
82    public void setUp() throws Exception {
83   
84  3 MockContext ctx = new MockContext();
85  3 MgnlContext.setInstance(ctx);
86  3 MockSession session = new MockSession(RepositoryConstants.WEBSITE);
87  3 ctx.addSession(RepositoryConstants.WEBSITE, session);
88  3 RepositoryManager repositoryManager = mock(RepositoryManager.class);
89  3 RepositoryDefinition repositoryDefinition = mock(RepositoryDefinition.class);
90  3 WorkspaceMappingDefinition workspaceMappingDefinition = mock(WorkspaceMappingDefinition.class);
91  3 EventBus eventBus = mock(EventBus.class);
92  3 when(repositoryManager.getRepositoryDefinition(RepositoryConstants.WEBSITE)).thenReturn(repositoryDefinition);
93  3 when(repositoryManager.getWorkspaceMapping(RepositoryConstants.WEBSITE)).thenReturn(workspaceMappingDefinition);
94  3 when(repositoryManager.getWorkspaceMapping(RepositoryConstants.USERS)).thenReturn(workspaceMappingDefinition);
95  3 when(repositoryManager.getWorkspaceNames()).thenReturn(Arrays.asList(RepositoryConstants.WEBSITE, RepositoryConstants.USERS));
96   
97  3 PowerMockito.mockStatic(ObservationUtil.class);
98  3 EventListener eventListener = mock(EventListener.class);
99  3 when(ObservationUtil.instanciateDeferredEventListener(any(EventListener.class), anyInt(), anyInt())).thenReturn(eventListener);
100   
101  3 ComponentsTestUtil.setInstance(RepositoryManager.class, repositoryManager);
102  3 ComponentsTestUtil.setImplementation(TypeMapping.class, TypeMappingImpl.class);
103  3 policy = new FlushAllListeningPolicy(new CacheModule(null, eventBus), repositoryManager);
104  3 when(cache1.getName()).thenReturn("cache1");
105    }
106   
 
107  3 toggle @After
108    public void tearDown() throws Exception {
109  3 MgnlContext.setInstance(null);
110  3 ComponentsTestUtil.clear();
111    }
112   
 
113  1 toggle @Test
114    public void start() throws Exception {
115    // GIVEN
116  1 PowerMockito.mockStatic(ObservationUtil.class);
117  1 policy.setExcludedWorkspaces(Arrays.asList(RepositoryConstants.USERS));
118   
119    // WHEN
120  1 policy.start(cache1);
121   
122    // THEN
123  1 PowerMockito.verifyStatic();
124  1 ObservationUtil.registerChangeListener(eq(RepositoryConstants.WEBSITE), eq("/"), any(EventListener.class));
125  1 PowerMockito.verifyStatic(never());
126  1 ObservationUtil.registerChangeListener(eq(RepositoryConstants.USERS), eq("/"), any(EventListener.class));
127    }
128   
 
129  1 toggle @Test
130    public void stop() throws Exception {
131    // GIVEN
132  1 Cache cache2 = mock(Cache.class);
133  1 when(cache2.getName()).thenReturn("cache2");
134  1 policy.start(cache1);
135  1 policy.start(cache2);
136   
137    // WHEN
138  1 policy.stop(cache1);
139   
140    // THEN
141  1 PowerMockito.verifyStatic(times(1));
142  1 ObservationUtil.unregisterChangeListener(eq(RepositoryConstants.WEBSITE), any(EventListener.class));
143    }
144   
 
145  1 toggle @Test
146    public void concurrency() throws Exception {
147    // GIVEN
148  1 ExecutorService executor = Executors.newFixedThreadPool(4);
149   
150  1 Callable startCache = new Callable<Object>() {
 
151  4 toggle @Override
152    public Object call() throws Exception {
153  4 policy.start(cache1);
154  4 return null;
155    }
156    };
157  1 Callable stopCache = new Callable<Object>() {
 
158  4 toggle @Override
159    public Object call() throws Exception {
160  4 policy.stop(cache1);
161  4 return null;
162    }
163    };
164  1 List<? extends Callable<Object>> tasks = Arrays.<Callable<Object>>asList(startCache, stopCache, startCache, stopCache, startCache, stopCache, startCache, stopCache);
165   
166    // WHEN
167  1 List<Future<Object>> results = executor.invokeAll(tasks);
168   
169    // THEN ConcurrentModificationException should not occur
170  1 for (Future future : results) {
171  8 future.get();
172    }
173    }
174   
175    }