| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
import org.apache.bsf.BSFEngine; |
| 20 |
import org.apache.bsf.BSFException; |
| 21 |
import org.apache.bsf.BSFManager; |
| 22 |
import org.junit.Test; |
| 23 |
|
| 24 |
import java.util.List; |
| 25 |
import java.util.Vector; |
| 26 |
|
| 27 |
import static org.junit.Assert.assertEquals; |
| 28 |
|
| 29 |
public class BSFSpecTest { |
| 30 |
@Test |
| 31 |
public void testSimpleIntegration() throws BSFException { |
| 32 |
// tag::bsf_simple[] |
| 33 |
String myScript = "println('Hello World')\n return [1, 2, 3]"; |
| 34 |
BSFManager manager = new BSFManager(); |
| 35 |
List answer = (List) manager.eval("groovy", "myScript.groovy", 0, 0, myScript); |
| 36 |
assertEquals(3, answer.size()); |
| 37 |
// end::bsf_simple[] |
| 38 |
} |
| 39 |
|
| 40 |
@Test |
| 41 |
public void testVariablePassing() throws BSFException { |
| 42 |
// tag::bsf_variable_passing[] |
| 43 |
BSFManager manager = new BSFManager(); |
| 44 |
manager.declareBean("xyz", 4, Integer.class); |
| 45 |
Object answer = manager.eval("groovy", "test.groovy", 0, 0, "xyz + 1"); |
| 46 |
assertEquals(5, answer); |
| 47 |
// end::bsf_variable_passing[] |
| 48 |
} |
| 49 |
|
| 50 |
@Test |
| 51 |
public void testApply() throws BSFException { |
| 52 |
// tag::bsf_apply[] |
| 53 |
BSFManager manager = new BSFManager(); |
| 54 |
Vector<String> ignoreParamNames = null; |
| 55 |
Vector<Integer> args = new Vector<Integer>(); |
| 56 |
args.add(2); |
| 57 |
args.add(5); |
| 58 |
args.add(1); |
| 59 |
Integer actual = (Integer) manager.apply("groovy", "applyTest", 0, 0, |
| 60 |
"def summer = { a, b, c -> a * 100 + b * 10 + c }", ignoreParamNames, args); |
| 61 |
assertEquals(251, actual.intValue()); |
| 62 |
// end::bsf_apply[] |
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void testAccess() throws BSFException { |
| 67 |
// tag::bsf_access[] |
| 68 |
BSFManager manager = new BSFManager(); |
| 69 |
BSFEngine bsfEngine = manager.loadScriptingEngine("groovy"); |
| 70 |
manager.declareBean("myvar", "hello", String.class); |
| 71 |
Object myvar = manager.lookupBean("myvar"); |
| 72 |
String result = (String) bsfEngine.call(myvar, "reverse", new Object[0]); |
| 73 |
assertEquals("olleh", result); |
| 74 |
// end::bsf_access[] |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
} |