1   package delight.nashornsandbox.tests;
2   
3   import delight.nashornsandbox.NashornSandbox;
4   import delight.nashornsandbox.NashornSandboxes;
5   import delight.nashornsandbox.exceptions.ScriptCPUAbuseException;
6   import java.util.concurrent.ExecutorService;
7   import java.util.concurrent.Executors;
8   import org.eclipse.xtend2.lib.StringConcatenation;
9   import org.junit.Test;
10  
11  @SuppressWarnings("all")
12  public class TestScriptInterruptionAndCatch {
13    @Test(expected = ScriptCPUAbuseException.class)
14    public void test_catch() {
15      final NashornSandbox sandbox = NashornSandboxes.create();
16      try {
17        sandbox.setMaxCPUTime(50);
18        ExecutorService _newSingleThreadExecutor = Executors.newSingleThreadExecutor();
19        sandbox.setExecutor(_newSingleThreadExecutor);
20        StringConcatenation _builder = new StringConcatenation();
21        _builder.append("try {");
22        _builder.newLine();
23        _builder.append("\t");
24        _builder.append("var x = 1;");
25        _builder.newLine();
26        _builder.append("\t");
27        _builder.append("while (true) {");
28        _builder.newLine();
29        _builder.append("\t\t");
30        _builder.append("x=x+1;");
31        _builder.newLine();
32        _builder.append("\t");
33        _builder.append("}");
34        _builder.newLine();
35        _builder.append("} catch (e) {");
36        _builder.newLine();
37        _builder.append("\t");
38        _builder.append("// if this is called, test does not succeed.");
39        _builder.newLine();
40        _builder.append("}");
41        _builder.newLine();
42        sandbox.eval(_builder.toString());
43      } finally {
44        ExecutorService _executor = sandbox.getExecutor();
45        _executor.shutdown();
46      }
47    }
48  }