1 package com.atlassian.plugin.webresource;
2
3 import java.io.ByteArrayOutputStream;
4
5 import javax.script.ScriptEngine;
6 import javax.script.ScriptEngineManager;
7 import javax.script.ScriptException;
8
9 import junit.framework.TestCase;
10
11 public class TestTryCatchJsResourceContentAnnotator extends TestCase
12 {
13 public void testGoodSyntax() throws Exception
14 {
15 ResourceContentAnnotator annotator = new TryCatchJsResourceContentAnnotator();
16 ByteArrayOutputStream stream = new ByteArrayOutputStream();
17
18 annotator.before(stream);
19 stream.write("6 * 2;\n".getBytes());
20 annotator.after(stream);
21
22 String tryCatchBlock = new String(stream.toByteArray());
23 assertTrue(tryCatchBlock.startsWith("try"));
24
25 ScriptEngineManager mgr = new ScriptEngineManager();
26 ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
27 try
28 {
29 Object eval = jsEngine.eval(tryCatchBlock);
30 assertEquals(12.0, ((Number) eval).doubleValue());
31 }
32 catch (ScriptException ex) {
33 fail("Exception running the created javascript: " + ex);
34 }
35 }
36 }