1 16 package scriptella.driver.janino; 17 18 import org.codehaus.janino.CompileException; 19 import scriptella.AbstractTestCase; 20 import scriptella.configuration.MockConnectionEl; 21 import scriptella.configuration.StringResource; 22 import scriptella.spi.ConnectionParameters; 23 import scriptella.spi.MockDriverContext; 24 import scriptella.spi.MockParametersCallbacks; 25 import scriptella.spi.ParametersCallback; 26 import scriptella.spi.ProviderException; 27 import scriptella.spi.QueryCallback; 28 29 import java.util.ArrayList ; 30 import java.util.Arrays ; 31 import java.util.List ; 32 33 39 public class JaninoConnectionTest extends AbstractTestCase { 40 public static int field; 42 45 public void testScript() { 46 JaninoConnection c = new JaninoConnection(new ConnectionParameters(new MockConnectionEl(), MockDriverContext.INSTANCE)); 47 field = 0; 48 c.executeScript(new StringResource(JaninoConnectionTest.class.getName() + ".field=1;"), null); 49 try { 50 c.executeScript(new StringResource(JaninoConnectionTest.class.getName() + ".nosuchfield=1;"), null); 51 fail("This script should fail"); 52 } catch (ProviderException e) { 53 Throwable ne = e.getNativeException(); 54 assertTrue(ne instanceof CompileException); 55 } 57 c.close(); 58 assertEquals(1, field); 59 } 60 61 public void testQuery() { 62 JaninoConnection c = new JaninoConnection(new ConnectionParameters(new MockConnectionEl(), MockDriverContext.INSTANCE)); 63 field = 0; 64 final List <String > rows = new ArrayList <String >(); 65 66 c.executeQuery(new StringResource( 67 "set(\"p\", \"//\"+get(\"p\") );" + 68 "next();" + 69 "next(new String[] {\"p\"}, new Object[] {\"v2\"});"), 70 MockParametersCallbacks.SIMPLE, new QueryCallback() { 71 public void processRow(final ParametersCallback parameters) { 72 rows.add(parameters.getParameter("p").toString()); 73 } 74 }); 75 c.close(); 76 List <String > expected = Arrays.asList("//*p*", "v2"); 77 assertEquals(expected, rows); 78 } 79 80 public void testErrorSourceCode() { 81 JaninoConnection c = new JaninoConnection(new ConnectionParameters(new MockConnectionEl(), MockDriverContext.INSTANCE)); 82 try { 84 c.executeScript(new StringResource("int b=1;\na='"), MockParametersCallbacks.NULL); 85 fail("Error statements must be recognized"); 86 } catch (JaninoProviderException e) { 87 assertEquals("a='", e.getErrorStatement()); 88 } 89 try { 91 c.executeScript(new StringResource("int b=1;\nObject a=get(\"1\");"), MockParametersCallbacks.UNSUPPORTED); 92 fail("Error statements must be recognized"); 93 } catch (JaninoProviderException e) { 94 assertEquals("Object a=get(\"1\");", e.getErrorStatement()); 95 } 96 97 98 99 } 100 } | Popular Tags |