vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 28 Nov 2012 14:51:52 +0100
branchlazy
changeset 214 a0f4460130b9
parent 199 866e4f4820c7
child 226 907a52ed10e3
permissions -rw-r--r--
We can load in dependant classes
jaroslav@24
     1
/**
jaroslav@106
     2
 * Back 2 Browser Bytecode Translator
jaroslav@106
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@24
     4
 *
jaroslav@24
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@24
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@24
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@24
     8
 *
jaroslav@24
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@24
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@24
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@24
    12
 * GNU General Public License for more details.
jaroslav@24
    13
 *
jaroslav@24
    14
 * You should have received a copy of the GNU General Public License
jaroslav@24
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@24
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@8
    17
 */
jaroslav@22
    18
package org.apidesign.vm4brwsr;
jaroslav@8
    19
jaroslav@8
    20
import javax.script.Invocable;
jaroslav@8
    21
import javax.script.ScriptException;
jaroslav@8
    22
import org.testng.annotations.Test;
jaroslav@8
    23
import static org.testng.Assert.*;
jaroslav@103
    24
import org.testng.annotations.BeforeClass;
jaroslav@8
    25
jaroslav@8
    26
/**
jaroslav@8
    27
 *
jaroslav@8
    28
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@8
    29
 */
jaroslav@8
    30
public class InstanceTest {
jaroslav@10
    31
    @Test public void verifyDefaultDoubleValue() throws Exception {
jaroslav@10
    32
        assertExec(
jaroslav@10
    33
            "Will be zero",
jaroslav@203
    34
            Instance.class, "defaultDblValueD",
jaroslav@10
    35
            Double.valueOf(0)
jaroslav@10
    36
        );
jaroslav@10
    37
    }
jaroslav@102
    38
    @Test public void verifyStaticMethodCall() throws Exception {
jaroslav@102
    39
        assertExec(
jaroslav@102
    40
            "Will be zero",
jaroslav@203
    41
            InstanceSub.class, "recallDblD",
jaroslav@102
    42
            Double.valueOf(0)
jaroslav@102
    43
        );
jaroslav@102
    44
    }
jaroslav@10
    45
    @Test public void verifyAssignedByteValue() throws Exception {
jaroslav@10
    46
        assertExec(
jaroslav@10
    47
            "Will one thirty one",
jaroslav@203
    48
            Instance.class, "assignedByteValueB",
jaroslav@10
    49
            Double.valueOf(31)
jaroslav@10
    50
        );
jaroslav@10
    51
    }
jaroslav@8
    52
    @Test public void verifyMagicOne() throws Exception {
jaroslav@8
    53
        assertExec(
jaroslav@10
    54
            "Should be three and something",
jaroslav@203
    55
            Instance.class, "magicOneD",
jaroslav@8
    56
            Double.valueOf(3.3)
jaroslav@8
    57
        );
jaroslav@8
    58
    }
jaroslav@12
    59
    @Test public void verifyInstanceMethods() throws Exception {
jaroslav@12
    60
        assertExec(
jaroslav@14
    61
            "Should be eleven as we invoke overwritten method, plus 44",
jaroslav@203
    62
            Instance.class, "virtualBytesI",
jaroslav@14
    63
            Double.valueOf(55)
jaroslav@12
    64
        );
jaroslav@12
    65
    }
jaroslav@15
    66
    @Test public void verifyInterfaceMethods() throws Exception {
jaroslav@15
    67
        assertExec(
jaroslav@15
    68
            "Retruns default value",
jaroslav@203
    69
            Instance.class, "interfaceBytesF",
jaroslav@15
    70
            Double.valueOf(31)
jaroslav@15
    71
        );
jaroslav@15
    72
    }
jaroslav@16
    73
jaroslav@16
    74
    @Test public void isNull() throws Exception {
jaroslav@16
    75
        assertExec(
jaroslav@16
    76
            "Yes, we are instance",
jaroslav@203
    77
            Instance.class, "isNullZ",
jaroslav@16
    78
            Double.valueOf(0.0)
jaroslav@16
    79
        );
jaroslav@16
    80
    }
jaroslav@17
    81
jaroslav@16
    82
    @Test public void isInstanceOf() throws Exception {
jaroslav@16
    83
        assertExec(
jaroslav@16
    84
            "Yes, we are instance",
jaroslav@203
    85
            Instance.class, "instanceOfZZ",
jaroslav@16
    86
            Double.valueOf(1.0), true
jaroslav@16
    87
        );
jaroslav@16
    88
    }
jaroslav@16
    89
jaroslav@16
    90
    @Test public void notInstanceOf() throws Exception {
jaroslav@16
    91
        assertExec(
jaroslav@16
    92
            "No, we are not an instance",
jaroslav@203
    93
            Instance.class, "instanceOfZZ",
jaroslav@16
    94
            Double.valueOf(0.0), false
jaroslav@16
    95
        );
jaroslav@16
    96
    }
jaroslav@17
    97
    
jaroslav@30
    98
    @Test public void verifyCastToClass() throws Exception {
jaroslav@30
    99
        assertExec(
jaroslav@30
   100
            "Five signals all is good",
jaroslav@203
   101
            Instance.class, "castsWorkIZ",
jaroslav@30
   102
            Double.valueOf(5.0), false
jaroslav@30
   103
        );
jaroslav@30
   104
    }
jaroslav@30
   105
    @Test public void verifyCastToInterface() throws Exception {
jaroslav@30
   106
        assertExec(
jaroslav@30
   107
            "Five signals all is good",
jaroslav@203
   108
            Instance.class, "castsWorkIZ",
jaroslav@30
   109
            Double.valueOf(5.0), true
jaroslav@30
   110
        );
jaroslav@30
   111
    }
jaroslav@30
   112
    
jaroslav@98
   113
    protected String startCompilationWith() {
jaroslav@98
   114
        return "org/apidesign/vm4brwsr/Instance";
jaroslav@98
   115
    }
jaroslav@98
   116
    
jaroslav@103
   117
    private static CharSequence codeSeq;
jaroslav@103
   118
    private static Invocable code;
jaroslav@103
   119
    
jaroslav@113
   120
    @BeforeClass
jaroslav@103
   121
    public void compileTheCode() throws Exception {
jaroslav@103
   122
        if (codeSeq == null) {
jaroslav@103
   123
            StringBuilder sb = new StringBuilder();
jaroslav@103
   124
            code = StaticMethodTest.compileClass(sb, startCompilationWith());
jaroslav@103
   125
            codeSeq = sb;
jaroslav@103
   126
        }
jaroslav@103
   127
    }
jaroslav@103
   128
    
jaroslav@98
   129
    private void assertExec(
jaroslav@203
   130
        String msg, Class clazz, String method, Object expRes, Object... args
jaroslav@98
   131
    ) throws Exception {
jaroslav@203
   132
        StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
jaroslav@8
   133
    }
jaroslav@8
   134
    
jaroslav@8
   135
}