rt/vm/src/test/java/org/apidesign/vm4brwsr/ResourcesWithExtensionsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 09 Nov 2014 10:36:08 +0100
changeset 1722 fd3a354d6e8f
parent 1709 ce898bccdbc8
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Don't initialize @JavaScriptBody resources sooner than their methods are called.
jaroslav@106
     1
/**
jaroslav@106
     2
 * Back 2 Browser Bytecode Translator
jaroslav@106
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@106
     4
 *
jaroslav@106
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@106
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@106
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@106
     8
 *
jaroslav@106
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@106
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@106
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@106
    12
 * GNU General Public License for more details.
jaroslav@106
    13
 *
jaroslav@106
    14
 * You should have received a copy of the GNU General Public License
jaroslav@106
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@106
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@106
    17
 */
jaroslav@22
    18
package org.apidesign.vm4brwsr;
jaroslav@0
    19
jaroslav@1495
    20
import java.io.UnsupportedEncodingException;
jaroslav@1497
    21
import javax.script.ScriptEngine;
jaroslav@789
    22
import org.testng.annotations.AfterClass;
jaroslav@103
    23
import org.testng.annotations.BeforeClass;
jaroslav@0
    24
import org.testng.annotations.Test;
jaroslav@0
    25
jaroslav@1493
    26
/** Tests related to loading resources from the VM.
jaroslav@0
    27
 *
jaroslav@0
    28
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@0
    29
 */
jaroslav@1497
    30
public class ResourcesWithExtensionsTest {
jaroslav@1497
    31
    @Test public void checkHello() throws Exception {
jaroslav@1497
    32
        String exp = "Hello ";
jaroslav@1493
    33
        
jaroslav@1493
    34
        assertExec("Loading a precompiled resource:",
jaroslav@1497
    35
            Resources.class, "loadJustHello__Ljava_lang_String_2", 
jaroslav@1493
    36
            exp
jaroslav@291
    37
        );
jaroslav@291
    38
    }
jaroslav@1519
    39
jaroslav@1497
    40
    @Test public void checkHelloWorld() throws Exception {
jaroslav@1497
    41
        String exp = "Hello World!";
jaroslav@1709
    42
        exp = exp + exp.hashCode();
jaroslav@1496
    43
        
jaroslav@1497
    44
        assertExec("Loading precompiled resources:",
jaroslav@1497
    45
            Resources.class, "loadHello__Ljava_lang_String_2", 
jaroslav@1496
    46
            exp
jaroslav@1496
    47
        );
jaroslav@1496
    48
    }
jaroslav@1587
    49
    
jaroslav@1587
    50
    @Test public void objJSResourceIsNotFound() throws Exception {
jaroslav@1587
    51
        assertExec("Objects from @JavaScriptResource resources are available",
jaroslav@1587
    52
            Resources.class, "isObj__Z", 1.0
jaroslav@1587
    53
        );
jaroslav@1587
    54
    }
jaroslav@1587
    55
    @Test public void objJSIsFound() throws Exception {
jaroslav@1587
    56
        assertExec("The resources used as @JavaScriptResource aren't available",
jaroslav@1722
    57
            Resources.class, "isResource__ZLjava_lang_String_2", 0.0, "obj"
jaroslav@1722
    58
        );
jaroslav@1722
    59
    }
jaroslav@1722
    60
jaroslav@1722
    61
    @Test public void thisObjJSIsFound() throws Exception {
jaroslav@1722
    62
        assertExec("The resources used as @JavaScriptResource aren't available",
jaroslav@1722
    63
            Resources.class, "isResource__ZLjava_lang_String_2", 0.0, "thisObj"
jaroslav@1587
    64
        );
jaroslav@1587
    65
    }
jaroslav@789
    66
jaroslav@708
    67
    private static TestVM code;
jaroslav@103
    68
    
jaroslav@103
    69
    @BeforeClass 
jaroslav@789
    70
    public static void compileTheCode() throws Exception {
jaroslav@103
    71
        StringBuilder sb = new StringBuilder();
jaroslav@1497
    72
        ScriptEngine[] eng = { null };
jaroslav@1497
    73
        code = TestVM.compileClassAsExtension(sb, eng, 
jaroslav@1496
    74
            "org/apidesign/vm4brwsr/Resources", 
jaroslav@1497
    75
            "META-INF/ahoj", "Hello "
jaroslav@1497
    76
        );
jaroslav@1497
    77
        code = TestVM.compileClassAsExtension(sb, eng, 
jaroslav@1497
    78
            "org/apidesign/vm4brwsr/Resources", 
jaroslav@1497
    79
            "META-INF/ahoj", "World!"
jaroslav@1496
    80
        );
jaroslav@2
    81
    }
jaroslav@789
    82
    @AfterClass
jaroslav@789
    83
    public static void releaseTheCode() {
jaroslav@789
    84
        code = null;
jaroslav@789
    85
    }
jaroslav@2
    86
jaroslav@708
    87
    private void assertExec(
jaroslav@708
    88
        String msg, Class<?> clazz, String method, 
jaroslav@708
    89
        Object ret, Object... args
jaroslav@708
    90
    ) throws Exception {
jaroslav@708
    91
        code.assertExec(msg, clazz, method, ret, args);
jaroslav@298
    92
    }
jaroslav@1495
    93
    
jaroslav@1495
    94
    public static String parseBase64Binary(String s) throws UnsupportedEncodingException {
jaroslav@1496
    95
        final byte[] arr = javax.xml.bind.DatatypeConverter.parseBase64Binary(s);
jaroslav@1496
    96
        StringBuilder sb = new StringBuilder();
jaroslav@1496
    97
        for (int i = 0; i < arr.length; i++) {
jaroslav@1496
    98
            int ch = arr[i];
jaroslav@1496
    99
            sb.append((char)ch);
jaroslav@1496
   100
        }
jaroslav@1496
   101
        return sb.toString();
jaroslav@1495
   102
    }
jaroslav@0
   103
}