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

File ClassLoadingTest.java

 

Code metrics

0
21
5
1
122
57
5
0.24
4.2
5
1

Classes

Class Line # Actions
ClassLoadingTest 39 21 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 4 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.macro.groovy;
21   
22    import java.util.Collections;
23   
24    import org.junit.Test;
25    import org.xwiki.rendering.block.MacroBlock;
26    import org.xwiki.rendering.macro.Macro;
27    import org.xwiki.rendering.macro.script.JSR223ScriptMacroParameters;
28    import org.xwiki.rendering.macro.script.ScriptMockSetup;
29    import org.xwiki.rendering.syntax.Syntax;
30    import org.xwiki.rendering.transformation.MacroTransformationContext;
31    import org.xwiki.test.jmock.AbstractComponentTestCase;
32   
33    /**
34    * Integration test to verify that we can pass extra JARs to the Groovy engine when executing a script.
35    *
36    * @version $Id: fb765083a466acdce4425fb01c6b0db7e23b40e5 $
37    * @since 2.0RC1
38    */
 
39    public class ClassLoadingTest extends AbstractComponentTestCase
40    {
41    private ScriptMockSetup mockSetup;
42   
43    private Macro<JSR223ScriptMacroParameters> macro;
44   
45    private MacroTransformationContext context;
46   
 
47  4 toggle @Override
48    protected void registerComponents() throws Exception
49    {
50  4 super.registerComponents();
51  4 this.mockSetup = new ScriptMockSetup(getComponentManager());
52   
53  4 this.macro = getComponentManager().getInstance(Macro.class, "groovy");
54   
55  4 this.context = new MacroTransformationContext();
56    // The script macro checks the current block (which is a macro block) to see what engine to use
57  4 this.context.setCurrentMacroBlock(new MacroBlock("groovy", Collections.<String, String>emptyMap(), false));
58    // Set the syntax since the script macro needs it to parse the script result using that syntax
59  4 this.context.setSyntax(Syntax.XWIKI_2_0);
60    }
61   
 
62  1 toggle @Test
63    public void testDefineClassInOneExecutionAndUseInAnother() throws Exception
64    {
65    // First execution: define a class
66  1 JSR223ScriptMacroParameters params = new JSR223ScriptMacroParameters();
67  1 this.macro.execute(params, "class MyClass {}", this.context);
68   
69    // Second execution: use the defined class
70  1 this.macro.execute(params, "def var = new MyClass()", this.context);
71    }
72   
 
73  1 toggle @Test
74    public void testExtraJarLocatedAtURL() throws Exception
75    {
76    // Use a dummy JAR to verify that some passed URL is indeed added to the CL. That JAR only contains
77    // an empty Dummy class.
78  1 JSR223ScriptMacroParameters params = new JSR223ScriptMacroParameters();
79  1 params.setJars(getClass().getClassLoader().getResource("dummy.jar").toString());
80   
81    // The test would fail after the next line if the Dummy class wasn't present in the classloader used to
82    // execute the script.
83  1 this.macro.execute(params, "def var = new Dummy()", this.context);
84    }
85   
86    /**
87    * Verify that it's possible to add jars to the class loader used by the Groovy engine
88    * across multiple invocations of the Groovy macro.
89    */
 
90  1 toggle @Test
91    public void testJarParamsInSecondMacro() throws Exception
92    {
93  1 JSR223ScriptMacroParameters params = new JSR223ScriptMacroParameters();
94   
95    // Execute a first macro without any jars param passed
96  1 this.macro.execute(params, "def var", this.context);
97   
98    // Execute a second macro this time with jars param and verify it works
99  1 params.setJars(getClass().getClassLoader().getResource("dummy.jar").toString());
100  1 this.macro.execute(params, "def var = new Dummy()", this.context);
101    }
102   
103    /**
104    * Verify that it works if we define an interface in a first macro execution and then define a second
105    * macro execution which uses the defined interface and also has jar params different from the first
106    * macro execution. Also test if a third execution with no params but using a previously defined
107    * interface also works.
108    */
 
109  1 toggle @Test
110    public void testDefineClassInFirstExecutionAndJarParamsInAnother() throws Exception
111    {
112  1 this.macro.execute(new JSR223ScriptMacroParameters(), "class MyClass {}", this.context);
113   
114    // Second execution: use the defined class but with different jar params
115  1 JSR223ScriptMacroParameters params = new JSR223ScriptMacroParameters();
116  1 params.setJars(getClass().getClassLoader().getResource("dummy.jar").toString());
117  1 this.macro.execute(params, "def var = new MyClass()", this.context);
118   
119    // Third execution without
120  1 this.macro.execute(new JSR223ScriptMacroParameters(), "def var = new MyClass()", this.context);
121    }
122    }