vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 04 Dec 2012 09:16:53 +0100
changeset 248 0bfcb6585290
parent 239 8ceee38f5840
child 442 b107ed66f2e7
permissions -rw-r--r--
Using the same mangling scheme as JNI
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 org.testng.annotations.Test;
jaroslav@103
    22
import org.testng.annotations.BeforeClass;
jaroslav@8
    23
jaroslav@8
    24
/**
jaroslav@8
    25
 *
jaroslav@8
    26
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@8
    27
 */
jaroslav@8
    28
public class InstanceTest {
jaroslav@10
    29
    @Test public void verifyDefaultDoubleValue() throws Exception {
jaroslav@10
    30
        assertExec(
jaroslav@10
    31
            "Will be zero",
jaroslav@248
    32
            Instance.class, "defaultDblValue__D",
jaroslav@10
    33
            Double.valueOf(0)
jaroslav@10
    34
        );
jaroslav@10
    35
    }
jaroslav@102
    36
    @Test public void verifyStaticMethodCall() throws Exception {
jaroslav@102
    37
        assertExec(
jaroslav@102
    38
            "Will be zero",
jaroslav@248
    39
            InstanceSub.class, "recallDbl__D",
jaroslav@102
    40
            Double.valueOf(0)
jaroslav@102
    41
        );
jaroslav@102
    42
    }
jaroslav@10
    43
    @Test public void verifyAssignedByteValue() throws Exception {
jaroslav@10
    44
        assertExec(
jaroslav@10
    45
            "Will one thirty one",
jaroslav@248
    46
            Instance.class, "assignedByteValue__B",
jaroslav@10
    47
            Double.valueOf(31)
jaroslav@10
    48
        );
jaroslav@10
    49
    }
jaroslav@8
    50
    @Test public void verifyMagicOne() throws Exception {
jaroslav@8
    51
        assertExec(
jaroslav@10
    52
            "Should be three and something",
jaroslav@248
    53
            Instance.class, "magicOne__D",
jaroslav@8
    54
            Double.valueOf(3.3)
jaroslav@8
    55
        );
jaroslav@8
    56
    }
jaroslav@12
    57
    @Test public void verifyInstanceMethods() throws Exception {
jaroslav@12
    58
        assertExec(
jaroslav@14
    59
            "Should be eleven as we invoke overwritten method, plus 44",
jaroslav@248
    60
            Instance.class, "virtualBytes__I",
jaroslav@14
    61
            Double.valueOf(55)
jaroslav@12
    62
        );
jaroslav@12
    63
    }
jaroslav@15
    64
    @Test public void verifyInterfaceMethods() throws Exception {
jaroslav@15
    65
        assertExec(
jaroslav@15
    66
            "Retruns default value",
jaroslav@248
    67
            Instance.class, "interfaceBytes__F",
jaroslav@15
    68
            Double.valueOf(31)
jaroslav@15
    69
        );
jaroslav@15
    70
    }
jaroslav@16
    71
jaroslav@16
    72
    @Test public void isNull() throws Exception {
jaroslav@16
    73
        assertExec(
jaroslav@16
    74
            "Yes, we are instance",
jaroslav@248
    75
            Instance.class, "isNull__Z",
jaroslav@16
    76
            Double.valueOf(0.0)
jaroslav@16
    77
        );
jaroslav@16
    78
    }
jaroslav@17
    79
jaroslav@16
    80
    @Test public void isInstanceOf() throws Exception {
jaroslav@16
    81
        assertExec(
jaroslav@16
    82
            "Yes, we are instance",
jaroslav@248
    83
            Instance.class, "instanceOf__ZZ",
jaroslav@16
    84
            Double.valueOf(1.0), true
jaroslav@16
    85
        );
jaroslav@16
    86
    }
jaroslav@16
    87
jaroslav@16
    88
    @Test public void notInstanceOf() throws Exception {
jaroslav@16
    89
        assertExec(
jaroslav@16
    90
            "No, we are not an instance",
jaroslav@248
    91
            Instance.class, "instanceOf__ZZ",
jaroslav@16
    92
            Double.valueOf(0.0), false
jaroslav@16
    93
        );
jaroslav@16
    94
    }
jaroslav@17
    95
    
jaroslav@30
    96
    @Test public void verifyCastToClass() throws Exception {
jaroslav@30
    97
        assertExec(
jaroslav@30
    98
            "Five signals all is good",
jaroslav@248
    99
            Instance.class, "castsWork__IZ",
jaroslav@30
   100
            Double.valueOf(5.0), false
jaroslav@30
   101
        );
jaroslav@30
   102
    }
jaroslav@30
   103
    @Test public void verifyCastToInterface() throws Exception {
jaroslav@30
   104
        assertExec(
jaroslav@30
   105
            "Five signals all is good",
jaroslav@248
   106
            Instance.class, "castsWork__IZ",
jaroslav@30
   107
            Double.valueOf(5.0), true
jaroslav@30
   108
        );
jaroslav@30
   109
    }
jaroslav@30
   110
    
jaroslav@226
   111
    @Test public void sharedConstructor() throws Exception {
jaroslav@226
   112
        assertExec(
jaroslav@226
   113
            "Constructor of first and 2nd instance should be the same",
jaroslav@248
   114
            Instance.class, "sharedConstructor__Z",
jaroslav@226
   115
            Double.valueOf(1.0)
jaroslav@226
   116
        );
jaroslav@226
   117
    }
jaroslav@226
   118
jaroslav@226
   119
    @Test public void differentConstructor() throws Exception {
jaroslav@226
   120
        assertExec(
jaroslav@226
   121
            "Constructor of X and Y should be the different",
jaroslav@248
   122
            Instance.class, "differentConstructor__Z",
jaroslav@226
   123
            Double.valueOf(0)
jaroslav@226
   124
        );
jaroslav@226
   125
    }
jaroslav@239
   126
jaroslav@239
   127
    @Test public void jsObjectIsLikeJavaObject() throws Exception {
jaroslav@239
   128
        assertExec(
jaroslav@239
   129
            "JavaScript object is instance of Java Object",
jaroslav@248
   130
            Instance.class, "iofObject__Z",
jaroslav@239
   131
            Double.valueOf(1)
jaroslav@239
   132
        );
jaroslav@239
   133
    }
jaroslav@226
   134
    
jaroslav@98
   135
    protected String startCompilationWith() {
jaroslav@98
   136
        return "org/apidesign/vm4brwsr/Instance";
jaroslav@98
   137
    }
jaroslav@98
   138
    
jaroslav@103
   139
    private static CharSequence codeSeq;
jaroslav@103
   140
    private static Invocable code;
jaroslav@103
   141
    
jaroslav@113
   142
    @BeforeClass
jaroslav@103
   143
    public void compileTheCode() throws Exception {
jaroslav@103
   144
        if (codeSeq == null) {
jaroslav@103
   145
            StringBuilder sb = new StringBuilder();
jaroslav@103
   146
            code = StaticMethodTest.compileClass(sb, startCompilationWith());
jaroslav@103
   147
            codeSeq = sb;
jaroslav@103
   148
        }
jaroslav@103
   149
    }
jaroslav@103
   150
    
jaroslav@98
   151
    private void assertExec(
jaroslav@203
   152
        String msg, Class clazz, String method, Object expRes, Object... args
jaroslav@98
   153
    ) throws Exception {
jaroslav@203
   154
        StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
jaroslav@8
   155
    }
jaroslav@8
   156
    
jaroslav@8
   157
}