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.filter

File CacheConfigurationSetupTest.java

 

Code metrics

2
53
6
1
177
117
7
0.13
8.83
6
1.17

Classes

Class Line # Actions
CacheConfigurationSetupTest 81 53 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /**
2    * This file Copyright (c) 2008-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.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    * Test for common cache configuration setup.
80    */
 
81    public class CacheConfigurationSetupTest extends RepositoryTestCase {
82   
83    private ContentCachingConfiguration cacheConf;
84   
 
85  2 toggle @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   
 
94  2 toggle @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   
 
113  1 toggle @Test
114    public void testVoting() {
115    //Logger.getLogger("info.magnolia.voting").setLevel(Level.DEBUG);
116   
117    // test public
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    // test author
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   
 
139  1 toggle @Test
140    public void testExecutorSetup() {
141    // this test is mainly testing if the content2bean transformation does what we expect
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   
 
149  13 toggle 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   
 
155  13 toggle 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    }