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

File TimedInterruptGroovyCompilationCustomizerTest.java

 

Code metrics

0
14
2
1
73
38
3
0.21
7
2
1.5

Classes

Class Line # Actions
TimedInterruptGroovyCompilationCustomizerTest 42 14 0% 3 1
0.937593.8%
 

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    import java.util.Collections;
24   
25    import javax.script.ScriptEngine;
26    import javax.script.ScriptEngineFactory;
27    import javax.script.ScriptEngineManager;
28    import javax.script.ScriptException;
29   
30    import org.jmock.Expectations;
31    import org.junit.Assert;
32    import org.junit.Test;
33    import org.xwiki.configuration.ConfigurationSource;
34    import org.xwiki.test.jmock.AbstractComponentTestCase;
35   
36    /**
37    * Unit tests for {@link TimedInterruptGroovyCompilationCustomizer}.
38    *
39    * @version $Id: 72db30cbc03f2e66b09049ccb37032ff2816f228 $
40    * @since 4.1M1
41    */
 
42    public class TimedInterruptGroovyCompilationCustomizerTest extends AbstractComponentTestCase
43    {
44    // Ensure that the test will fail after 10 seconds
 
45  1 toggle @Test(timeout = 10000)
46    public void executeWithTimedInterruptCustomizer() throws Exception
47    {
48  1 final ConfigurationSource source = registerMockComponent(ConfigurationSource.class);
49   
50  1 getMockery().checking(new Expectations()
 
51  1 toggle {{
52  1 oneOf(source).getProperty("groovy.compilationCustomizers", Collections.emptyList());
53  1 will(returnValue(Arrays.asList("timedInterrupt")));
54  1 oneOf(source).getProperty("groovy.customizer.timedInterrupt.timeout", 60L);
55  1 will(returnValue(1L));
56    }});
57   
58  1 ScriptEngineManager manager = new ScriptEngineManager();
59  1 ScriptEngineFactory groovyScriptEngineFactory =
60    getComponentManager().getInstance(ScriptEngineFactory.class, "groovy");
61  1 manager.registerEngineName("groovy", groovyScriptEngineFactory);
62   
63  1 final ScriptEngine engine = manager.getEngineByName("groovy");
64   
65    // Simulate an infinite loop to verify that we timeout after 1 second
66  1 try {
67  1 engine.eval("while (true) {}");
68  0 Assert.fail("Should have thrown an exception here");
69    } catch (ScriptException e) {
70  1 Assert.assertTrue(e.getMessage().contains("Execution timed out after 1 units."));
71    }
72    }
73    }