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