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

File CacheMacroTest.java

 
executeWhenNoIdAndSameContent: Failed to lookup component [org.xwiki.test.rendering.velo...
executeWhenNoIdAndDifferentContent: Failed to lookup component [org.xwiki.test.rendering.velo...
executeWithIdGeneratedByVelocityMacro: Failed to lookup component [org.xwiki.test.rendering.velo...
executeWhenSameIdAndDifferentContent: Failed to lookup component [org.xwiki.test.rendering.velo...
 

Code metrics

0
71
8
1
226
155
8
0.11
8.88
8
1

Classes

Class Line # Actions
CacheMacroTest 47 71 0% 8 48
0.3924050639.2%
 

Contributing tests

This file is covered by 6 tests. .

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 org.xwiki.rendering.internal.macro.cache;
21   
22    import java.io.StringWriter;
23    import java.util.List;
24   
25    import org.junit.Test;
26    import org.xwiki.rendering.block.Block;
27    import org.xwiki.rendering.internal.transformation.macro.MacroTransformation;
28    import org.xwiki.rendering.macro.Macro;
29    import org.xwiki.rendering.macro.cache.CacheMacroParameters;
30    import org.xwiki.rendering.macro.script.ScriptMockSetup;
31    import org.xwiki.rendering.renderer.PrintRendererFactory;
32    import org.xwiki.rendering.syntax.Syntax;
33    import org.xwiki.rendering.transformation.MacroTransformationContext;
34    import org.xwiki.rendering.transformation.Transformation;
35    import org.xwiki.test.jmock.AbstractComponentTestCase;
36    import org.xwiki.velocity.VelocityManager;
37   
38    import static org.xwiki.rendering.test.BlockAssert.assertBlocks;
39    import static org.junit.Assert.*;
40   
41    /**
42    * Unit tests for {@link CacheMacro}.
43    *
44    * @version $Id: b4401376500bd568e0ba2ba3db9743bbbbca6fb9 $
45    * @since 3.0M1
46    */
 
47    public class CacheMacroTest extends AbstractComponentTestCase
48    {
49    private ScriptMockSetup mockSetup;
50   
51    private CacheMacro cacheMacro;
52   
53    private PrintRendererFactory rendererFactory;
54   
 
55  6 toggle @Override
56    protected void registerComponents() throws Exception
57    {
58  6 super.registerComponents();
59   
60  6 this.mockSetup = new ScriptMockSetup(getMockery(), getComponentManager());
61  6 this.cacheMacro = getComponentManager().getInstance(Macro.class, "cache");
62  6 this.rendererFactory = getComponentManager().getInstance(PrintRendererFactory.class, "event/1.0");
63    }
64   
 
65  0 toggle @Test
66    public void executeWhenNoIdAndSameContent() throws Exception
67    {
68  0 String expected = "beginDocument\n"
69    + "beginMacroMarkerStandalone [velocity] [] [$var]\n"
70    + "beginParagraph\n"
71    + "onWord [content]\n"
72    + "endParagraph\n"
73    + "endMacroMarkerStandalone [velocity] [] [$var]\n"
74    + "endDocument";
75   
76  0 CacheMacroParameters params = new CacheMacroParameters();
77  0 MacroTransformationContext context = createMacroTransformationContext();
78   
79  0 Test failure here VelocityManager velocityManager = getComponentManager().getInstance(VelocityManager.class);
80  0 StringWriter writer = new StringWriter();
81  0 velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template",
82    "#set ($var = 'content')");
83   
84  0 List<Block> result = this.cacheMacro.execute(params, "{{velocity}}$var{{/velocity}}", context);
85  0 assertBlocks(expected, result, this.rendererFactory);
86   
87    // Execute a second time with a different value for the velocity $var variable to ensure the returned result
88    // is the cached one.
89  0 velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template",
90    "#set ($var = 'newcontent')");
91  0 result = this.cacheMacro.execute(params, "{{velocity}}$var{{/velocity}}", context);
92  0 assertBlocks(expected, result, this.rendererFactory);
93    }
94   
 
95  0 toggle @Test
96    public void executeWhenNoIdAndDifferentContent() throws Exception
97    {
98  0 String expected1 = "beginDocument\n"
99    + "beginMacroMarkerStandalone [velocity] [] [$var]\n"
100    + "beginParagraph\n"
101    + "onWord [content]\n"
102    + "endParagraph\n"
103    + "endMacroMarkerStandalone [velocity] [] [$var]\n"
104    + "endDocument";
105   
106  0 String expected2 = "beginDocument\n"
107    + "beginMacroMarkerStandalone [velocity] [] [$var##]\n"
108    + "beginParagraph\n"
109    + "onWord [newcontent]\n"
110    + "endParagraph\n"
111    + "endMacroMarkerStandalone [velocity] [] [$var##]\n"
112    + "endDocument";
113   
114  0 CacheMacroParameters params = new CacheMacroParameters();
115  0 MacroTransformationContext context = createMacroTransformationContext();
116   
117  0 Test failure here VelocityManager velocityManager = getComponentManager().getInstance(VelocityManager.class);
118  0 StringWriter writer = new StringWriter();
119  0 velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template",
120    "#set ($var = 'content')");
121   
122  0 List<Block> result = this.cacheMacro.execute(params, "{{velocity}}$var{{/velocity}}", context);
123  0 assertBlocks(expected1, result, this.rendererFactory);
124   
125    // Execute a second time with a different cache macro content to ensure it's not cached
126  0 velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template",
127    "#set ($var = 'newcontent')");
128  0 result = this.cacheMacro.execute(params, "{{velocity}}$var##{{/velocity}}", context);
129  0 assertBlocks(expected2, result, this.rendererFactory);
130    }
131   
 
132  0 toggle @Test
133    public void executeWhenSameIdAndDifferentContent() throws Exception
134    {
135  0 String expected = "beginDocument\n"
136    + "beginMacroMarkerStandalone [velocity] [] [$var]\n"
137    + "beginParagraph\n"
138    + "onWord [content]\n"
139    + "endParagraph\n"
140    + "endMacroMarkerStandalone [velocity] [] [$var]\n"
141    + "endDocument";
142   
143  0 CacheMacroParameters params = new CacheMacroParameters();
144  0 params.setId("uniqueid");
145  0 MacroTransformationContext context = createMacroTransformationContext();
146   
147  0 Test failure here VelocityManager velocityManager = getComponentManager().getInstance(VelocityManager.class);
148  0 StringWriter writer = new StringWriter();
149  0 velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template",
150    "#set ($var = 'content')");
151   
152  0 List<Block> result = this.cacheMacro.execute(params, "{{velocity}}$var{{/velocity}}", context);
153  0 assertBlocks(expected, result, this.rendererFactory);
154   
155    // Execute a second time with a different content but with the same id, to ensure the returned result
156    // is the cached one.
157  0 result = this.cacheMacro.execute(params, "whatever here...", context);
158  0 assertBlocks(expected, result, this.rendererFactory);
159    }
160   
 
161  0 toggle @Test
162    public void executeWithIdGeneratedByVelocityMacro() throws Exception
163    {
164  0 Test failure here VelocityManager velocityManager = getComponentManager().getInstance(VelocityManager.class);
165  0 StringWriter writer = new StringWriter();
166  0 velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template",
167    "#set ($var = 'generatedid')");
168   
169  0 CacheMacroParameters params = new CacheMacroParameters();
170  0 params.setId("{{velocity}}$var{{/velocity}}");
171  0 MacroTransformationContext context = createMacroTransformationContext();
172   
173  0 List<Block> result1 = this.cacheMacro.execute(params, "whatever", context);
174   
175    // Execute a second time with the same id (specified explicitly this time) and ensures that the returned result
176    // is the same even the cache macro content is different.
177  0 params.setId("generatedid");
178  0 List<Block> result2 = this.cacheMacro.execute(params, "something else", context);
179  0 assertEquals(result1, result2);
180    }
181   
 
182  1 toggle @Test
183    public void executeWithDifferentTimeToLive() throws Exception
184    {
185  1 CacheMacroParameters params = new CacheMacroParameters();
186  1 MacroTransformationContext context = createMacroTransformationContext();
187   
188  1 params.setId("id");
189  1 params.setMaxEntries(10);
190  1 params.setTimeToLive(100);
191  1 List<Block> result1 = this.cacheMacro.execute(params, "content1", context);
192   
193    // Execute a second time with different content but with different time to live param. This means another
194    // cache will be used and thus the first cached content won't be returned.
195  1 params.setTimeToLive(200);
196  1 List<Block> result2 = this.cacheMacro.execute(params, "content2", context);
197  1 assertFalse(result2.equals(result1));
198    }
199   
 
200  1 toggle @Test
201    public void executeWithDifferentMaxEntries() throws Exception
202    {
203  1 CacheMacroParameters params = new CacheMacroParameters();
204  1 MacroTransformationContext context = createMacroTransformationContext();
205   
206  1 params.setId("id");
207  1 params.setMaxEntries(10);
208  1 params.setTimeToLive(100);
209  1 List<Block> result1 = this.cacheMacro.execute(params, "content1", context);
210   
211    // Execute a second time with different content but with different time to live param. This means another
212    // cache will be used and thus the first cached content won't be returned.
213  1 params.setMaxEntries(11);
214  1 List<Block> result2 = this.cacheMacro.execute(params, "content2", context);
215  1 assertFalse(result2.equals(result1));
216    }
217   
 
218  5 toggle private MacroTransformationContext createMacroTransformationContext() throws Exception
219    {
220  5 MacroTransformation macroTransformation = getComponentManager().getInstance(Transformation.class, "macro");
221  5 MacroTransformationContext context = new MacroTransformationContext();
222  5 context.setTransformation(macroTransformation);
223  5 context.setSyntax(Syntax.XWIKI_2_0);
224  5 return context;
225    }
226    }