/* --------------------------------------------------------- * * __________ D E L T A S C R I P T * * (_________() * * / === / - A fast, dynamic scripting language * * | == | - Version 4.13.11.0 * * / === / - Developed by Adam R. Nelson * * | = = | - 2011-2013 * * / === / - Distributed under GNU LGPL v3 * * (________() - http://github.com/ar-nelson/deltascript * * * * --------------------------------------------------------- */ package com.sector91.delta.script.test; import org.junit.Test; import org.junit.Before; import com.sector91.delta.script.*; import com.sector91.delta.script.objects.DS_Blank; import com.sector91.delta.script.objects.DS_Boolean; import com.sector91.delta.script.objects.DS_Integer; import com.sector91.delta.script.objects.DS_Scope; import com.sector91.delta.script.objects.DS_String; import com.sector91.delta.script.objects.DS_Tag; import com.sector91.delta.script.objects.VectorFactory; import com.sector91.delta.script.objects.reflect.DS_JavaObject; import com.sector91.delta.script.test.util.DeltaScriptTest; public class JavaObjectsTest extends DeltaScriptTest { private DS_Scope scope; private TestObject jObj; @Before public void setupTest() throws Exception { scope = context.createScope(); jObj = new TestObject("qwertyuiop"); scope.setJavaObject(DS_Tag.tag("jObj"), jObj); } @Test public void testAddJavaObject() { assertEval("jObj", new DS_JavaObject<Object>(jObj), scope); } @Test public void testReadFieldsFromJavaObject() { assertEval("jObj.name", new DS_String("qwertyuiop"), scope); assertEval("jObj.testField", integer(91), scope); } @Test public void testModifyJavaObjectFields() { assertEval("jObj.testField", integer(91), scope); eval("jObj.testField = 8281", scope); assertEval("jObj.testField", integer(8281), scope); } @Test public void testCallMethodWithVoidReturnValue() { eval("jObj.testVoid()", scope); } @Test public void testCallMethodWithDScriptReturnValue() { assertEval("jObj.testDSInt()", integer(91), scope); } @Test public void testCallMethodWithJavaReturnValue() { assertEval("jObj.testInt()", integer(91), scope); } @Test public void testBoxingVariousJavaObjects() { assertEval("jObj.testBoolean()", DS_Boolean.TRUE, scope); assertEval("jObj.testInt()", integer(91), scope); assertEval("jObj.testLong()", integer(91), scope); assertEval("jObj.testFloat()", decimal(91), scope); assertEval("jObj.testDouble()", decimal(91), scope); assertEval("jObj.testString()", new DS_String("test"), scope); assertEval("jObj.testIntArray()[0]", integer(1), scope); assertEval("jObj.testFloatArray()", VectorFactory.fromArray(new float[] {1f, 2f, 3f, 4f}, NumberTypes.SHORT_FLOAT), scope); assertEval("jObj.testStringArray()[0]", new DS_String("one"), scope); assertEval("jObj.testNull()", DS_Blank.BLANK, scope); } public final class TestObject { public String name; public int testField = 91; public TestObject(String name) {this.name = name;} public void testVoid() {} public DS_Integer testDSInt() {return (DS_Integer)integer(91);} public Object testNull() {return null;} public boolean testBoolean() {return true;} public int testInt() {return 91;} public long testLong() {return 91L;} public float testFloat() {return 91.0f;} public double testDouble() {return 91.0;} public String testString() {return "test";} public int[] testIntArray() {return new int[] {1, 2, 3, 4};} public float[] testFloatArray() {return new float[] {1.0f, 2.0f, 3.0f, 4.0f};} public boolean[] testBooleanArray() {return new boolean[] {true, false};} public String[] testStringArray() {return new String[] {"one", "two", "three", "four"};} } }