1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal.cache.rendering

File DefaultRenderingCacheTest.java

 

Code metrics

0
45
10
2
164
101
10
0.22
4.5
5
1

Classes

Class Line # Actions
DefaultRenderingCacheTest 51 35 0% 4 0
1.0100%
DefaultRenderingCacheTest.TestRenderingCacheAware 141 10 0% 6 8
0.550%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.internal.cache.rendering;
21   
22    import java.util.Collections;
23    import java.util.HashMap;
24    import java.util.HashSet;
25    import java.util.Map;
26    import java.util.Set;
27   
28    import org.jmock.Expectations;
29    import org.junit.Assert;
30    import org.junit.Test;
31    import org.xwiki.bridge.event.DocumentUpdatedEvent;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.observation.ObservationManager;
34    import org.xwiki.test.internal.MockConfigurationSource;
35   
36    import com.xpn.xwiki.XWiki;
37    import com.xpn.xwiki.XWikiContext;
38    import com.xpn.xwiki.doc.XWikiDocument;
39    import com.xpn.xwiki.internal.cache.rendering.CachedItem.UsedExtension;
40    import com.xpn.xwiki.plugin.XWikiDefaultPlugin;
41    import com.xpn.xwiki.plugin.XWikiPluginManager;
42    import com.xpn.xwiki.test.AbstractBridgedComponentTestCase;
43    import com.xpn.xwiki.web.XWikiServletRequestStub;
44   
45    /**
46    * Unit test for {@link DefaultRenderingCache}.
47    *
48    * @version $Id: db8b9f45e1b273537308a2a9d620d4eb28d9c13f $
49    * @since 2.4M1
50    */
 
51    public class DefaultRenderingCacheTest extends AbstractBridgedComponentTestCase
52    {
53    private XWiki mockXWiki;
54   
55    private XWikiDocument document;
56   
57    private RenderingCache renderingCache;
58   
59    private XWikiServletRequestStub mockRequest;
60   
61    private XWikiPluginManager mockPluginManager;
62   
63    private Map<String, String[]> parameters = new HashMap<String, String[]>();
64   
65    private String refresh;
66   
67    private TestRenderingCacheAware testRenderingCacheAware;
68   
 
69  1 toggle @Override
70    public void setUp() throws Exception
71    {
72  1 super.setUp();
73   
74  1 this.mockPluginManager = getMockery().mock(XWikiPluginManager.class);
75  1 this.testRenderingCacheAware = new TestRenderingCacheAware("Test","Test",getContext());
76   
77  1 this.document = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
78  1 this.document.setOriginalDocument(this.document.clone());
79   
80  1 this.mockXWiki = getMockery().mock(XWiki.class);
81  1 getContext().setWiki(this.mockXWiki);
82   
83  1 this.mockRequest = getMockery().mock(XWikiServletRequestStub.class);
84  1 getContext().setRequest(this.mockRequest);
85   
86  1 this.renderingCache = getComponentManager().getInstance(RenderingCache.class);
87   
88    // @formatter:off
 
89  1 toggle getMockery().checking(new Expectations() {{
90  1 allowing(mockXWiki).getPluginManager(); will(returnValue(mockPluginManager));
91  1 allowing(mockXWiki).getDocument(document.getDocumentReference(), getContext()); will(returnValue(document));
92  1 allowing(mockPluginManager).getPlugin("jsx"); will(returnValue(testRenderingCacheAware));
93  1 allowing(mockRequest).getParameterMap(); will(returnValue(parameters));
94  1 allowing(mockRequest).getParameter("refresh"); will(returnValue(refresh));
95  1 allowing(mockPluginManager).getPlugins();
96    }});
97    //@formatter:on
98    }
99   
 
100  1 toggle @Override
101    protected void registerComponents() throws Exception
102    {
103  1 super.registerComponents();
104   
105  1 getConfigurationSource().setProperty("core.renderingcache.enabled", true);
106    }
107   
 
108  1 toggle @Test
109    public void testGetSetRenderedContent() throws Exception
110    {
111  1 MockConfigurationSource source = getConfigurationSource();
112   
113  1 source.setProperty("core.renderingcache.documents",
114    Collections.singletonList(this.document.getPrefixedFullName()));
115   
116  1 this.renderingCache.setRenderedContent(this.document.getDocumentReference(), "source", "renderedContent",
117    getContext());
118   
119  1 Assert.assertEquals("renderedContent",
120    this.renderingCache.getRenderedContent(this.document.getDocumentReference(), "source", getContext()));
121   
122  1 this.parameters.put("param", new String[] {"value1", "value2"});
123   
124  1 Assert.assertNull(this.renderingCache.getRenderedContent(this.document.getDocumentReference(), "source",
125    getContext()));
126   
127  1 this.parameters.remove("param");
128   
129  1 Assert.assertEquals("renderedContent",
130    this.renderingCache.getRenderedContent(this.document.getDocumentReference(), "source", getContext()));
131   
132  1 ObservationManager observationManager = getComponentManager().getInstance(ObservationManager.class);
133  1 observationManager.notify(new DocumentUpdatedEvent(this.document.getDocumentReference()), this.document,
134    getContext());
135   
136  1 Assert.assertNull(this.renderingCache.getRenderedContent(this.document.getDocumentReference(), "source",
137    getContext()));
138    }
139   
140   
 
141    private static class TestRenderingCacheAware extends XWikiDefaultPlugin implements RenderingCacheAware {
 
142  1 toggle public TestRenderingCacheAware(String name, String className, XWikiContext context) {
143  1 super(name, className, context);
144    }
145   
 
146  1 toggle private static Map<String,Map<String,Object>> markerMap = new HashMap<String, Map<String,Object>>(){{put("Hello",new HashMap<String, Object>(){{put("a","A");}});}};
 
147  1 toggle private static Set<String> markerSet = new HashSet<String>(){{add("Hello");}};
148   
 
149  0 toggle @Override
150    public UsedExtension getCacheResources(XWikiContext context) {
151  0 Assert.assertNotNull(context);
152  0 return new UsedExtension(markerSet,markerMap);
153    }
154   
 
155  0 toggle @Override
156    public void restoreCacheResources(XWikiContext context, UsedExtension extension) {
157  0 Assert.assertNotNull(extension);
158  0 Assert.assertNotNull(context);
159  0 Assert.assertEquals(markerMap, extension.parameters);
160  0 Assert.assertEquals(markerSet, extension.resources);
161    }
162    }
163   
164    }