jaroslav@24: /** jaroslav@106: * Back 2 Browser Bytecode Translator jaroslav@106: * Copyright (C) 2012 Jaroslav Tulach jaroslav@24: * jaroslav@24: * This program is free software: you can redistribute it and/or modify jaroslav@24: * it under the terms of the GNU General Public License as published by jaroslav@24: * the Free Software Foundation, version 2 of the License. jaroslav@24: * jaroslav@24: * This program is distributed in the hope that it will be useful, jaroslav@24: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@24: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@24: * GNU General Public License for more details. jaroslav@24: * jaroslav@24: * You should have received a copy of the GNU General Public License jaroslav@24: * along with this program. Look for COPYING file in the top folder. jaroslav@24: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@8: */ jaroslav@22: package org.apidesign.vm4brwsr; jaroslav@8: jaroslav@8: import javax.script.Invocable; jaroslav@8: import javax.script.ScriptException; jaroslav@8: import org.testng.annotations.Test; jaroslav@8: import static org.testng.Assert.*; jaroslav@103: import org.testng.annotations.BeforeClass; jaroslav@8: jaroslav@8: /** jaroslav@8: * jaroslav@8: * @author Jaroslav Tulach jaroslav@8: */ jaroslav@8: public class InstanceTest { jaroslav@10: @Test public void verifyDefaultDoubleValue() throws Exception { jaroslav@10: assertExec( jaroslav@10: "Will be zero", jaroslav@203: Instance.class, "defaultDblValueD", jaroslav@10: Double.valueOf(0) jaroslav@10: ); jaroslav@10: } jaroslav@102: @Test public void verifyStaticMethodCall() throws Exception { jaroslav@102: assertExec( jaroslav@102: "Will be zero", jaroslav@203: InstanceSub.class, "recallDblD", jaroslav@102: Double.valueOf(0) jaroslav@102: ); jaroslav@102: } jaroslav@10: @Test public void verifyAssignedByteValue() throws Exception { jaroslav@10: assertExec( jaroslav@10: "Will one thirty one", jaroslav@203: Instance.class, "assignedByteValueB", jaroslav@10: Double.valueOf(31) jaroslav@10: ); jaroslav@10: } jaroslav@8: @Test public void verifyMagicOne() throws Exception { jaroslav@8: assertExec( jaroslav@10: "Should be three and something", jaroslav@203: Instance.class, "magicOneD", jaroslav@8: Double.valueOf(3.3) jaroslav@8: ); jaroslav@8: } jaroslav@12: @Test public void verifyInstanceMethods() throws Exception { jaroslav@12: assertExec( jaroslav@14: "Should be eleven as we invoke overwritten method, plus 44", jaroslav@203: Instance.class, "virtualBytesI", jaroslav@14: Double.valueOf(55) jaroslav@12: ); jaroslav@12: } jaroslav@15: @Test public void verifyInterfaceMethods() throws Exception { jaroslav@15: assertExec( jaroslav@15: "Retruns default value", jaroslav@203: Instance.class, "interfaceBytesF", jaroslav@15: Double.valueOf(31) jaroslav@15: ); jaroslav@15: } jaroslav@16: jaroslav@16: @Test public void isNull() throws Exception { jaroslav@16: assertExec( jaroslav@16: "Yes, we are instance", jaroslav@203: Instance.class, "isNullZ", jaroslav@16: Double.valueOf(0.0) jaroslav@16: ); jaroslav@16: } jaroslav@17: jaroslav@16: @Test public void isInstanceOf() throws Exception { jaroslav@16: assertExec( jaroslav@16: "Yes, we are instance", jaroslav@203: Instance.class, "instanceOfZZ", jaroslav@16: Double.valueOf(1.0), true jaroslav@16: ); jaroslav@16: } jaroslav@16: jaroslav@16: @Test public void notInstanceOf() throws Exception { jaroslav@16: assertExec( jaroslav@16: "No, we are not an instance", jaroslav@203: Instance.class, "instanceOfZZ", jaroslav@16: Double.valueOf(0.0), false jaroslav@16: ); jaroslav@16: } jaroslav@17: jaroslav@30: @Test public void verifyCastToClass() throws Exception { jaroslav@30: assertExec( jaroslav@30: "Five signals all is good", jaroslav@203: Instance.class, "castsWorkIZ", jaroslav@30: Double.valueOf(5.0), false jaroslav@30: ); jaroslav@30: } jaroslav@30: @Test public void verifyCastToInterface() throws Exception { jaroslav@30: assertExec( jaroslav@30: "Five signals all is good", jaroslav@203: Instance.class, "castsWorkIZ", jaroslav@30: Double.valueOf(5.0), true jaroslav@30: ); jaroslav@30: } jaroslav@30: jaroslav@98: protected String startCompilationWith() { jaroslav@98: return "org/apidesign/vm4brwsr/Instance"; jaroslav@98: } jaroslav@98: jaroslav@103: private static CharSequence codeSeq; jaroslav@103: private static Invocable code; jaroslav@103: jaroslav@113: @BeforeClass jaroslav@103: public void compileTheCode() throws Exception { jaroslav@103: if (codeSeq == null) { jaroslav@103: StringBuilder sb = new StringBuilder(); jaroslav@103: code = StaticMethodTest.compileClass(sb, startCompilationWith()); jaroslav@103: codeSeq = sb; jaroslav@103: } jaroslav@103: } jaroslav@103: jaroslav@98: private void assertExec( jaroslav@203: String msg, Class clazz, String method, Object expRes, Object... args jaroslav@98: ) throws Exception { jaroslav@203: StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args); jaroslav@8: } jaroslav@8: jaroslav@8: }