rt/vm/src/test/java/org/apidesign/vm4brwsr/ReloadingTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 14 Oct 2013 16:44:55 +0200
changeset 1373 c4e57ec5f0df
parent 1367 rt/vm/src/test/java/org/apidesign/vm4brwsr/DelayedLoadingTest.java@6193e735f4d1
child 1787 ea12a3bb4b33
permissions -rw-r--r--
VM.reload can now reload a class in existing virtual machine
     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 org.testng.annotations.BeforeClass;
    21 import org.testng.annotations.AfterClass;
    22 import org.testng.annotations.Test;
    23 
    24 /**
    25  *
    26  * @author Jaroslav Tulach <jtulach@netbeans.org>
    27  */
    28 public class ReloadingTest {
    29     private static TestVM code;
    30     
    31     @Test public void verifyUsageOf() throws Exception {
    32         code.execCode("First hello", 
    33             Hello.class, "hello__Ljava_lang_String_2",
    34             "Hello World!"
    35         );
    36 
    37         byte[] arr = BytesLoader.readClass("org/apidesign/vm4brwsr/Hello.class");
    38         for (int i = 0; i < arr.length; i++) {
    39             if (arr[i] == 'H' && arr[i + 1] == 'e' && arr[i + 2] == 'l') {
    40                 arr[i] = 'A';
    41                 arr[i + 1] = 'h';
    42                 arr[i + 2] = 'o';
    43                 arr[i + 3] = 'y';
    44                 arr[i + 4] = ' ';
    45                 break;
    46             }
    47         }
    48         
    49         code.execCode("Redefine class",
    50             Hello.class, "reloadYourSelf__Ljava_lang_Object_2_3B",
    51             null, arr
    52         );
    53 
    54         code.execCode("Second hello", 
    55             Hello.class, "hello__Ljava_lang_String_2",
    56             "Ahoy  World!"
    57         );
    58     }
    59     
    60     
    61     @BeforeClass 
    62     public static void compileTheCode() throws Exception {
    63         code = TestVM.compileClass(
    64             "org/apidesign/vm4brwsr/Hello");
    65     }
    66     @AfterClass
    67     public static void releaseTheCode() {
    68         code = null;
    69     }
    70     
    71 }
    72