Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
StubVelocityManager | 49 | 20 | 0% | 9 | 2 | 0.935483993.5% |
Class StubVelocityManager |
Line # 49 |
20 |
0% |
9 |
2 |
0.935483993.5% |
---|---|---|---|---|---|---|
initialize() : void initialize() : void | 6565 | 5.05 | 0.00% | 2.02 | 1.01 | 0.8 0.880% |
getVelocityContext() : VelocityContext getVelocityContext() : VelocityContext | 8080 | 4.04 | 0.00% | 2.02 | 0.00 | 1.0 1.0100% |
getCurrentVelocityContext() : VelocityContext getCurrentVelocityContext() : VelocityContext | 9696 | 1.01 | 0.00% | 1.01 | 0.00 | 1.0 1.0100% |
getVelocityEngine() : VelocityEngine getVelocityEngine() : VelocityEngine | 102102 | 1.01 | 0.00% | 1.01 | 0.00 | 1.0 1.0100% |
evaluate(Writer,String,Reader) : boolean evaluate(Writer,String,Reader) : boolean | 108108 | 9.09 | 0.00% | 3.03 | 1.01 | 0.9230769 0.923076992.3% |
0.9032258
|
org.xwiki.rendering.test.integration.RenderingTest.executeorg.xwiki.rendering.test.integration.RenderingTest.execute | 1PASS | ||
0.9032258
|
org.xwiki.rendering.test.integration.RenderingTest.executeorg.xwiki.rendering.test.integration.RenderingTest.execute | 1PASS | ||
0.9032258
|
org.xwiki.rendering.test.integration.RenderingTest.executeorg.xwiki.rendering.test.integration.RenderingTest.execute | 1PASS | ||
0.9032258
|
org.xwiki.rendering.test.integration.RenderingTest.executeorg.xwiki.rendering.test.integration.RenderingTest.execute | 1PASS | ||
0.9032258
|
org.xwiki.rendering.test.integration.RenderingTest.executeorg.xwiki.rendering.test.integration.RenderingTest.execute | 1PASS | ||
0.9032258
|
org.xwiki.rendering.test.integration.RenderingTest.executeorg.xwiki.rendering.test.integration.RenderingTest.execute | 1PASS | ||
0.9032258
|
org.xwiki.rendering.test.integration.RenderingTest.executeorg.xwiki.rendering.test.integration.RenderingTest.execute | 1PASS | ||
0.87096775
|
org.xwiki.rendering.test.integration.RenderingTest.executeorg.xwiki.rendering.test.integration.RenderingTest.execute | 1PASS | ||
0.87096775
|
org.xwiki.rendering.test.integration.RenderingTest.executeorg.xwiki.rendering.test.integration.RenderingTest.execute | 1PASS | ||
0.87096775
|
org.xwiki.rendering.test.integration.RenderingTest.executeorg.xwiki.rendering.test.integration.RenderingTest.execute | 1PASS | ||
0.87096775
|
org.xwiki.rendering.test.integration.RenderingTest.executeorg.xwiki.rendering.test.integration.RenderingTest.execute | 1PASS | ||
0.87096775
|
org.xwiki.rendering.test.integration.RenderingTest.executeorg.xwiki.rendering.test.integration.RenderingTest.execute | 1PASS | ||
0.87096775
|
org.xwiki.rendering.test.integration.RenderingTest.executeorg.xwiki.rendering.test.integration.RenderingTest.execute | 1PASS | ||
0.87096775
|
org.xwiki.rendering.test.integration.RenderingTest.executeorg.xwiki.rendering.test.integration.RenderingTest.execute | 1PASS | ||
0.7419355
|
org.xwiki.rendering.macro.velocity.VelocityMacroIsolationTest.testVelocityMacroIsolationorg.xwiki.rendering.macro.velocity.VelocityMacroIsolationTest.testVelocityMacroIsolation | 1PASS |
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.test.rendering.velocity; | ||||||
21 | |||||||
22 | import java.io.Reader; | ||||||
23 | import java.io.Writer; | ||||||
24 | import java.util.Map; | ||||||
25 | import java.util.Properties; | ||||||
26 | |||||||
27 | import javax.inject.Inject; | ||||||
28 | import javax.inject.Singleton; | ||||||
29 | import javax.script.ScriptContext; | ||||||
30 | |||||||
31 | import org.apache.velocity.VelocityContext; | ||||||
32 | import org.xwiki.component.annotation.Component; | ||||||
33 | import org.xwiki.component.phase.Initializable; | ||||||
34 | import org.xwiki.component.phase.InitializationException; | ||||||
35 | import org.xwiki.script.ScriptContextManager; | ||||||
36 | import org.xwiki.velocity.VelocityEngine; | ||||||
37 | import org.xwiki.velocity.VelocityManager; | ||||||
38 | import org.xwiki.velocity.XWikiVelocityException; | ||||||
39 | |||||||
40 | /** | ||||||
41 | * Mock VelocityManager implementation used for testing, since we don't want to pull any dependency on the | ||||||
42 | * Model/Skin/etc for the Rendering module's unit tests. | ||||||
43 | * | ||||||
44 | * @version $Id: b452be253d28b702c54d004678a1ef0406fe2e33 $ | ||||||
45 | * @since 1.5M2 | ||||||
46 | */ | ||||||
47 | @Component | ||||||
48 | @Singleton | ||||||
|
|||||||
49 | public class StubVelocityManager implements VelocityManager, Initializable | ||||||
50 | { | ||||||
51 | /** | ||||||
52 | * Note that we use a single Velocity Engine instance in this Mock. | ||||||
53 | */ | ||||||
54 | @Inject | ||||||
55 | private VelocityEngine velocityEngine; | ||||||
56 | |||||||
57 | /** | ||||||
58 | * Used to get the current script context. | ||||||
59 | */ | ||||||
60 | @Inject | ||||||
61 | private ScriptContextManager scriptContextManager; | ||||||
62 | |||||||
63 | private VelocityContext velocityContext = new VelocityContext(); | ||||||
64 | |||||||
|
|||||||
65 | 15 | @Override... | |||||
66 | public void initialize() throws InitializationException | ||||||
67 | { | ||||||
68 | 15 | try { | |||||
69 | // Configure the Velocity Engine not to use the Resource Webapp Loader since we don't | ||||||
70 | // need it and we would need to setup the Container component's ApplicationContext | ||||||
71 | // otherwise. | ||||||
72 | 15 | Properties properties = new Properties(); | |||||
73 | 15 | properties.setProperty("resource.loader", "file"); | |||||
74 | 15 | this.velocityEngine.initialize(properties); | |||||
75 | } catch (XWikiVelocityException e) { | ||||||
76 | 0 | throw new InitializationException("Failed to initialize Velocity Engine", e); | |||||
77 | } | ||||||
78 | } | ||||||
79 | |||||||
|
|||||||
80 | 22 | @Override... | |||||
81 | public VelocityContext getVelocityContext() | ||||||
82 | { | ||||||
83 | // Copy current JSR223 ScriptContext binding | ||||||
84 | 22 | for (Map.Entry<String, Object> entry : this.scriptContextManager.getScriptContext() | |||||
85 | .getBindings(ScriptContext.ENGINE_SCOPE).entrySet()) { | ||||||
86 | // Not ideal since it does not allow to modify a binding but it's too dangerous for existing velocity script | ||||||
87 | // otherwise | ||||||
88 | 28 | if (!this.velocityContext.containsKey(entry.getKey())) { | |||||
89 | 19 | this.velocityContext.put(entry.getKey(), entry.getValue()); | |||||
90 | } | ||||||
91 | } | ||||||
92 | |||||||
93 | 22 | return this.velocityContext; | |||||
94 | } | ||||||
95 | |||||||
|
|||||||
96 | 22 | @Override... | |||||
97 | public VelocityContext getCurrentVelocityContext() | ||||||
98 | { | ||||||
99 | 22 | return this.velocityContext; | |||||
100 | } | ||||||
101 | |||||||
|
|||||||
102 | 50 | @Override... | |||||
103 | public VelocityEngine getVelocityEngine() throws XWikiVelocityException | ||||||
104 | { | ||||||
105 | 50 | return this.velocityEngine; | |||||
106 | } | ||||||
107 | |||||||
|
|||||||
108 | 22 | @Override... | |||||
109 | public boolean evaluate(Writer out, String templateName, Reader source) throws XWikiVelocityException | ||||||
110 | { | ||||||
111 | // Get up to date Velocity context | ||||||
112 | 22 | VelocityContext velocityContext = getVelocityContext(); | |||||
113 | |||||||
114 | // Execute Velocity context | ||||||
115 | 22 | boolean result = getVelocityEngine().evaluate(velocityContext, out, templateName, source); | |||||
116 | |||||||
117 | // Update current script context with potentially modified Velocity context | ||||||
118 | 22 | ScriptContext scontext = this.scriptContextManager.getCurrentScriptContext(); | |||||
119 | 22 | for (Object vkey : velocityContext.getKeys()) { | |||||
120 | 32 | if (vkey instanceof String) { | |||||
121 | 32 | String svkey = (String) vkey; | |||||
122 | // context is a reserved binding in JSR223 specification | ||||||
123 | 32 | if (!"context".equals(svkey)) { | |||||
124 | 31 | scontext.setAttribute(svkey, velocityContext.get(svkey), ScriptContext.ENGINE_SCOPE); | |||||
125 | } | ||||||
126 | } | ||||||
127 | } | ||||||
128 | |||||||
129 | 22 | return result; | |||||
130 | } | ||||||
131 | } |