@Test public void testVarAccess() { Configuration config = Configuration.instance(); try { config.load(); } catch (ConfigurationException e1) { e1.printStackTrace(); fail(); } BSFManager bsfManager = new BSFManager(); BSFEngine engine = null; try { engine = bsfManager.loadScriptingEngine("BeanShell"); } catch (BSFException e) { e.printStackTrace(); fail(e.getMessage()); } assertNotNull(engine); try { // ObjectRegistry registry = bsfManager.getObjectRegistry(); // registry.register("mmm", new Integer(3)); // engine.exec("source", 1, 1, "print( mmm );"); engine.exec("source", 1, 1, "mmm = 1000;"); engine.exec("source", 1, 1, "print( mmm );"); } catch (BSFException e) { e.printStackTrace(); fail(e.getMessage()); } engine.terminate(); }
@Test public void testEval() { Configuration config = Configuration.instance(); try { config.load(); } catch (ConfigurationException ex) { ex.printStackTrace(); fail(); } BSFManager bsfManager = new BSFManager(); Integer i = new Integer(5); try { bsfManager.declareBean("i", i, Integer.class); } catch (BSFException e) { e.printStackTrace(); fail(e.getMessage()); } BSFEngine engine = null; try { engine = bsfManager.loadScriptingEngine("BeanShell"); } catch (BSFException e) { e.printStackTrace(); fail(e.getMessage()); } assertNotNull(engine); Integer j = new Integer(7); try { bsfManager.declareBean("j", j, Integer.class); } catch (BSFException e) { e.printStackTrace(); fail(e.getMessage()); } try { engine.exec("source", 1, 1, "a=3;\nprint(a);"); engine.exec("source", 1, 1, "a=a+1;\nprint(a);"); } catch (BSFException e) { e.printStackTrace(); fail(e.getMessage()); } try { engine.exec("source", 1, 1, "print(\"i=\"+i);"); engine.exec("source", 1, 1, "j=j*10; print(\"j=\"+j);"); } catch (BSFException e) { e.printStackTrace(); fail(e.getMessage()); } // BSFDeclaredBean toto = new BSFDeclaredBean(); // toto.beannew BSFDeclaredBean("i", i, Integer.class); ObjectRegistry registry = bsfManager.getObjectRegistry(); Object jValue = registry.lookup("j"); if (jValue != null) { System.out.println("j-value = " + jValue.toString()); } registry.register("k", new Integer(1000)); Object kValue = registry.lookup("k"); assertNotNull(kValue); System.out.println("k=" + kValue.toString()); try { engine.exec("source", 1, 1, "print(\"k=\"+k);"); } catch (BSFException e) { e.printStackTrace(); fail(e.getMessage()); } try { bsfManager.declareBean("k", kValue, Integer.class); } catch (BSFException e) { e.printStackTrace(); fail(e.getMessage()); } try { engine.exec("source", 1, 1, "print(\"k=\"+k);"); } catch (BSFException e) { e.printStackTrace(); fail(e.getMessage()); } engine.terminate(); }