| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
@version |
| 48 |
|
@since |
| 49 |
|
|
| 50 |
|
@MockingRequirement(GroovyScriptEngineFactory.class) |
| |
|
| 100% |
Uncovered Elements: 0 (21) |
Complexity: 4 |
Complexity Density: 0.22 |
|
| 51 |
|
public class GroovyExecutionTest extends AbstractMockingComponentTestCase<ScriptEngineFactory> |
| 52 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 53 |
1 |
@Before... |
| 54 |
|
public void configure() throws Exception |
| 55 |
|
{ |
| 56 |
1 |
getMockery().setImposteriser(ClassImposteriser.INSTANCE); |
| 57 |
|
} |
| 58 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
1PASS
|
|
| 59 |
1 |
@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() |
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
| 66 |
1 |
{{... |
| 67 |
1 |
oneOf(configuration).getCompilationCustomizers(); |
| 68 |
1 |
will(returnValue(Arrays.asList(customizer))); |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 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 |
|
} |