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 |
|
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 |
|
@link |
73 |
|
|
74 |
|
@RunWith(PowerMockRunner.class) |
75 |
|
@PrepareForTest(ObservationUtil.class) |
|
|
| 100% |
Uncovered Elements: 0 (53) |
Complexity: 7 |
Complexity Density: 0.15 |
|
76 |
|
public class FlushAllListeningPolicyTest { |
77 |
|
|
78 |
|
private FlushAllListeningPolicy policy; |
79 |
|
private final Cache cache1 = mock(Cache.class); |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 1 |
Complexity Density: 0.05 |
|
81 |
3 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
107 |
3 |
@After... |
108 |
|
public void tearDown() throws Exception { |
109 |
3 |
MgnlContext.setInstance(null); |
110 |
3 |
ComponentsTestUtil.clear(); |
111 |
|
} |
112 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
113 |
1 |
@Test... |
114 |
|
public void start() throws Exception { |
115 |
|
|
116 |
1 |
PowerMockito.mockStatic(ObservationUtil.class); |
117 |
1 |
policy.setExcludedWorkspaces(Arrays.asList(RepositoryConstants.USERS)); |
118 |
|
|
119 |
|
|
120 |
1 |
policy.start(cache1); |
121 |
|
|
122 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
129 |
1 |
@Test... |
130 |
|
public void stop() throws Exception { |
131 |
|
|
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 |
|
|
138 |
1 |
policy.stop(cache1); |
139 |
|
|
140 |
|
|
141 |
1 |
PowerMockito.verifyStatic(times(1)); |
142 |
1 |
ObservationUtil.unregisterChangeListener(eq(RepositoryConstants.WEBSITE), any(EventListener.class)); |
143 |
|
} |
144 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
145 |
1 |
@Test... |
146 |
|
public void concurrency() throws Exception { |
147 |
|
|
148 |
1 |
ExecutorService executor = Executors.newFixedThreadPool(4); |
149 |
|
|
150 |
1 |
Callable startCache = new Callable<Object>() { |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
151 |
4 |
@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>() { |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
158 |
4 |
@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 |
|
|
167 |
1 |
List<Future<Object>> results = executor.invokeAll(tasks); |
168 |
|
|
169 |
|
|
170 |
1 |
for (Future future : results) { |
171 |
8 |
future.get(); |
172 |
|
} |
173 |
|
} |
174 |
|
|
175 |
|
} |