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

File GroovyExecutionTest.java

 

Code metrics

0
18
3
1
92
54
4
0.22
6
3
1.33

Classes

Class Line # Actions
GroovyExecutionTest 51 18 0% 4 0
1.0100%
 

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 org.xwiki.groovy.internal;
21   
22    import java.util.Arrays;
23   
24    import javax.script.ScriptEngine;
25    import javax.script.ScriptEngineFactory;
26    import javax.script.ScriptEngineManager;
27    import javax.script.ScriptException;
28   
29    import org.codehaus.groovy.ast.ClassNode;
30    import org.codehaus.groovy.classgen.GeneratorContext;
31    import org.codehaus.groovy.control.CompilePhase;
32    import org.codehaus.groovy.control.SourceUnit;
33    import org.codehaus.groovy.control.customizers.CompilationCustomizer;
34    import org.jmock.Expectations;
35    import org.jmock.lib.legacy.ClassImposteriser;
36    import org.junit.Assert;
37    import org.junit.Before;
38    import org.junit.Test;
39    import org.xwiki.groovy.GroovyConfiguration;
40    import org.xwiki.test.jmock.AbstractMockingComponentTestCase;
41    import org.xwiki.test.jmock.annotation.MockingRequirement;
42   
43    /**
44    * Test Groovy Execution with some Compilation Customizers. This test also tests the {@link GroovyScriptEngineFactory}
45    * class.
46    *
47    * @version $Id: 53ccb3d52bd945942ceee16ac2e041ac9bb316ca $
48    * @since 4.1M1
49    */
50    @MockingRequirement(GroovyScriptEngineFactory.class)
 
51    public class GroovyExecutionTest extends AbstractMockingComponentTestCase<ScriptEngineFactory>
52    {
 
53  1 toggle @Before
54    public void configure() throws Exception
55    {
56  1 getMockery().setImposteriser(ClassImposteriser.INSTANCE);
57    }
58   
 
59  1 toggle @Test
60    public void execute() throws Exception
61    {
62  1 final GroovyConfiguration configuration = getComponentManager().getInstance(GroovyConfiguration.class);
63  1 final CompilationCustomizer customizer = getMockery().mock(CompilationCustomizer.class);
64   
65  1 getMockery().checking(new Expectations()
 
66  1 toggle {{
67  1 oneOf(configuration).getCompilationCustomizers();
68  1 will(returnValue(Arrays.asList(customizer)));
69   
70    // Simulate a Compilation Customizer that throws an error. This would happend for example with a Secure
71    // Customizer that would prevent executing some statements for example.
72  1 oneOf(customizer).getPhase();
73  1 will(returnValue(CompilePhase.CANONICALIZATION));
74  1 oneOf(customizer).needSortedInput();
75  1 will(returnValue(false));
76  1 oneOf(customizer).call(with(any(SourceUnit.class)), with(any(GeneratorContext.class)),
77    with(any(ClassNode.class)));
78  1 will(throwException(new SecurityException("test exception")));
79    }});
80   
81  1 ScriptEngineManager manager = new ScriptEngineManager();
82  1 manager.registerEngineName("groovy", getMockedComponent());
83   
84  1 ScriptEngine engine = manager.getEngineByName("groovy");
85   
86  1 try {
87  1 engine.eval("def dummy");
88    } catch (ScriptException expected) {
89  1 Assert.assertTrue(expected.getMessage().contains("test exception"));
90    }
91    }
92    }