rt/vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 28 Feb 2013 07:48:54 +0100
changeset 789 bb7506513353
parent 772 d382dacfd73f
child 857 43084920090b
permissions -rw-r--r--
releasing the compiled code as soon as it is no longer needed to prevent OutOfMemoryError when adding more and more tests
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.io.File;
    21 import java.io.FileWriter;
    22 import java.io.IOException;
    23 import static org.testng.Assert.*;
    24 import org.testng.annotations.AfterClass;
    25 import org.testng.annotations.BeforeClass;
    26 import org.testng.annotations.Test;
    27 
    28 /**
    29  *
    30  * @author Jaroslav Tulach <jtulach@netbeans.org>
    31  */
    32 public class VMinVMTest {
    33     private static TestVM code;
    34     
    35     @Test public void compareGeneratedCodeForArrayClass() throws Exception {
    36         compareCode("org/apidesign/vm4brwsr/Array.class");
    37     }
    38 
    39     @Test public void compareGeneratedCodeForClassesClass() throws Exception {
    40         compareCode("org/apidesign/vm4brwsr/Classes.class");
    41     }
    42 
    43     @BeforeClass
    44     public static void compileTheCode() throws Exception {
    45         code = TestVM.compileClass("org/apidesign/vm4brwsr/VMinVM");
    46     }
    47     @AfterClass
    48     public static void releaseTheCode() {
    49         code = null;
    50     }
    51     
    52     private void compareCode(final String nm) throws Exception, IOException {
    53         byte[] arr = BytesLoader.readClass(nm);
    54         String ret1 = VMinVM.toJavaScript(arr);
    55         
    56         Object ret;
    57         try {
    58             ret = code.invokeFunction("bck2brwsr");
    59             ret = code.invokeMethod(ret, "loadClass", VMinVM.class.getName());
    60             ret = code.invokeMethod(ret, "toJavaScript__Ljava_lang_String_2_3B", arr);
    61         } catch (Exception ex) {
    62             File f = File.createTempFile("execution", ".js");
    63             FileWriter w = new FileWriter(f);
    64             w.append("var byteCode = [\n  ");
    65             String sep = "";
    66             for (int i = 0; i < arr.length; i++) {
    67                 w.append(sep).append(Integer.toString((arr[i] + 256) % 256));
    68                 sep = ", ";
    69                 if (i % 20 == 0) {
    70                     w.append("\n  ");
    71                 }
    72             }
    73             w.append("\n];\n");
    74             w.append(code.toString());
    75             w.close();
    76             throw new Exception(ex.getMessage() + " file: " + f, ex);
    77         }
    78 
    79         
    80         assertTrue(ret instanceof String, "It is string: " + ret);
    81         
    82         if (!ret1.toString().equals(ret)) {
    83             StringBuilder msg = new StringBuilder("Difference found between ");
    84             msg.append(TestVM.dumpJS(ret1));
    85             msg.append(" ");
    86             msg.append(TestVM.dumpJS((CharSequence) ret));
    87             msg.append(" compiled by ");
    88             msg.append(code.toString());
    89             fail(msg.toString());
    90         }
    91     }
    92 }