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.filter; |
35 |
|
|
36 |
|
import static org.easymock.EasyMock.*; |
37 |
|
import static org.junit.Assert.*; |
38 |
|
import static org.mockito.Mockito.mock; |
39 |
|
|
40 |
|
import info.magnolia.cms.beans.config.ServerConfiguration; |
41 |
|
import info.magnolia.cms.core.SystemProperty; |
42 |
|
import info.magnolia.cms.security.User; |
43 |
|
import info.magnolia.cms.security.UserManager; |
44 |
|
import info.magnolia.context.MgnlContext; |
45 |
|
import info.magnolia.jcr.node2bean.Node2BeanProcessor; |
46 |
|
import info.magnolia.jcr.util.NodeTypes; |
47 |
|
import info.magnolia.jcr.util.NodeUtil; |
48 |
|
import info.magnolia.module.ModuleManagementException; |
49 |
|
import info.magnolia.module.cache.CacheModule; |
50 |
|
import info.magnolia.module.cache.CachePolicyExecutor; |
51 |
|
import info.magnolia.module.cache.CachePolicyResult; |
52 |
|
import info.magnolia.module.cache.ContentCachingConfiguration; |
53 |
|
import info.magnolia.module.cache.cachepolicy.Default; |
54 |
|
import info.magnolia.module.cache.executor.CompositeExecutor; |
55 |
|
import info.magnolia.module.model.ModuleDefinition; |
56 |
|
import info.magnolia.module.model.reader.BetwixtModuleDefinitionReader; |
57 |
|
import info.magnolia.objectfactory.Components; |
58 |
|
import info.magnolia.repository.RepositoryConstants; |
59 |
|
import info.magnolia.test.ComponentsTestUtil; |
60 |
|
import info.magnolia.test.RepositoryTestCase; |
61 |
|
import info.magnolia.test.mock.MockUtil; |
62 |
|
import info.magnolia.test.mock.MockWebContext; |
63 |
|
import info.magnolia.voting.voters.VoterSet; |
64 |
|
|
65 |
|
import java.util.Arrays; |
66 |
|
import java.util.Collections; |
67 |
|
import java.util.HashMap; |
68 |
|
import java.util.List; |
69 |
|
import java.util.Map; |
70 |
|
|
71 |
|
import javax.inject.Provider; |
72 |
|
import javax.jcr.Node; |
73 |
|
import javax.servlet.http.HttpServletRequest; |
74 |
|
|
75 |
|
import org.junit.Before; |
76 |
|
import org.junit.Test; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (61) |
Complexity: 7 |
Complexity Density: 0.13 |
|
81 |
|
public class CacheConfigurationSetupTest extends RepositoryTestCase { |
82 |
|
|
83 |
|
private ContentCachingConfiguration cacheConf; |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
85 |
2 |
@Override... |
86 |
|
protected List<ModuleDefinition> getModuleDefinitionsForTests() throws ModuleManagementException { |
87 |
2 |
return Arrays.asList( |
88 |
|
new BetwixtModuleDefinitionReader().readFromResource("/META-INF/magnolia/core.xml"), |
89 |
|
new BetwixtModuleDefinitionReader().readFromResource("/META-INF/magnolia/cache.xml"), |
90 |
|
new BetwixtModuleDefinitionReader().readFromResource("/META-INF/magnolia/activation.xml") |
91 |
|
); |
92 |
|
} |
93 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
94 |
2 |
@Override... |
95 |
|
@Before |
96 |
|
public void setUp() throws Exception { |
97 |
2 |
super.setUp(); |
98 |
2 |
bootstrapSingleResource("/mgnl-bootstrap/cache/config.modules.cache.config.contentCaching.defaultPageCache.xml"); |
99 |
2 |
Node content = NodeUtil.createPath(MgnlContext.getJCRSession(RepositoryConstants.CONFIG).getRootNode(), "modules/cache/config/contentCaching/defaultPageCache", NodeTypes.Content.NAME); |
100 |
|
|
101 |
2 |
User anonymous = createMock(User.class); |
102 |
2 |
expect(anonymous.getName()).andStubReturn(UserManager.ANONYMOUS_USER); |
103 |
2 |
replay(anonymous); |
104 |
|
|
105 |
2 |
MgnlContext.login(MockUtil.createSubject(anonymous)); |
106 |
|
|
107 |
2 |
Provider<CacheModule> cacheModuleProvider = mock(Provider.class); |
108 |
2 |
ComponentsTestUtil.setInstance(Provider.class, cacheModuleProvider); |
109 |
2 |
Node2BeanProcessor n2b = Components.getComponent(Node2BeanProcessor.class); |
110 |
2 |
cacheConf = (ContentCachingConfiguration) n2b.toBean(content, ContentCachingConfiguration.class); |
111 |
|
} |
112 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
113 |
1 |
@Test... |
114 |
|
public void testVoting() { |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
1 |
assertTrue("normal page should pass", vote("/somepage.html", false, false) > 0); |
119 |
1 |
assertFalse("normal page with parameters should not pass", vote("/somepage.html", true, false) > 0); |
120 |
1 |
assertTrue(".resouces should pass", vote("/.resources/somthing.js", false, false) > 0); |
121 |
1 |
assertFalse(".resouces with parameters should not pass", vote("/.resources/somthing.js", true, false) > 0); |
122 |
1 |
assertFalse("adminCentral should not pass", vote("/.magnolia/pages/adminCentral.html", false, false) > 0); |
123 |
1 |
SystemProperty.setProperty("magnolia.develop", "true"); |
124 |
1 |
assertFalse("javascript.js should not pass if magnolia develop is true", vote("/.magnolia/pages/javascript.js", false, false) > 0); |
125 |
1 |
SystemProperty.setProperty("magnolia.develop", "false"); |
126 |
1 |
assertTrue("javascript.js should pass if magnolia develop is false", vote("/.magnolia/pages/javascript.js", false, false) > 0); |
127 |
|
|
128 |
|
|
129 |
1 |
assertFalse("normal page should not pass on author", vote("/somepage.html", false, true) > 0); |
130 |
1 |
assertTrue(".resources should pass", vote("/.resources/somthing.js", false, true) > 0); |
131 |
1 |
assertFalse(".resources with parameters should not pass", vote("/.resources/somthing.js", true, true) > 0); |
132 |
1 |
assertFalse("adminCentral should not pass", vote("/.magnolia/pages/adminCentral.html", false, true) > 0); |
133 |
1 |
SystemProperty.setProperty("magnolia.develop", "true"); |
134 |
1 |
assertFalse("javascript.js should not pass if magnolia develop is true", vote("/.magnolia/pages/javascript.js", false, true) > 0); |
135 |
1 |
SystemProperty.setProperty("magnolia.develop", "false"); |
136 |
1 |
assertTrue("javascript.js should pass if magnolia develop is false", vote("/.magnolia/pages/javascript.js", false, true) > 0); |
137 |
|
} |
138 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
139 |
1 |
@Test... |
140 |
|
public void testExecutorSetup() { |
141 |
|
|
142 |
1 |
CachePolicyExecutor executor = cacheConf.getExecutor(CachePolicyResult.bypass); |
143 |
1 |
assertTrue(executor instanceof CompositeExecutor); |
144 |
1 |
executor = cacheConf.getExecutor(CachePolicyResult.useCache); |
145 |
1 |
assertTrue(executor instanceof CompositeExecutor); |
146 |
1 |
assertEquals(((CompositeExecutor) executor).getExecutors().length, 2); |
147 |
|
} |
148 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
149 |
13 |
private int vote(String uri, boolean withParameters, boolean admin) {... |
150 |
13 |
setupRequest(uri, withParameters, admin); |
151 |
13 |
VoterSet voter = ((Default) cacheConf.getCachePolicy()).getShouldBypassVoters(); |
152 |
13 |
return voter.vote(uri); |
153 |
|
} |
154 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 2 |
Complexity Density: 0.12 |
|
155 |
13 |
private void setupRequest(String uri, boolean parameters, boolean admin) {... |
156 |
13 |
MockWebContext ctx = ((MockWebContext) MgnlContext.getInstance()); |
157 |
13 |
final ServerConfiguration serverConfiguration = new ServerConfiguration(); |
158 |
13 |
serverConfiguration.setAdmin(admin); |
159 |
13 |
ComponentsTestUtil.setInstance(ServerConfiguration.class, serverConfiguration); |
160 |
13 |
ctx.setCurrentURI(uri); |
161 |
13 |
HttpServletRequest request = createNiceMock(HttpServletRequest.class); |
162 |
13 |
if (!parameters) { |
163 |
10 |
expect(request.getMethod()).andReturn("GET"); |
164 |
10 |
expect(request.getParameterMap()).andReturn(Collections.EMPTY_MAP); |
165 |
|
} else { |
166 |
3 |
Map parameterMap = new HashMap(); |
167 |
3 |
parameterMap.put("kez", "value"); |
168 |
3 |
expect(request.getMethod()).andReturn("POST"); |
169 |
3 |
expect(request.getParameterMap()).andReturn(parameterMap); |
170 |
3 |
ctx.setParameters(parameterMap); |
171 |
|
} |
172 |
13 |
replay(request); |
173 |
13 |
ctx.setRequest(request); |
174 |
|
|
175 |
|
} |
176 |
|
|
177 |
|
} |