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@34
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@34
|
19 |
|
jaroslav@34
|
20 |
import javax.script.Invocable;
|
jaroslav@34
|
21 |
import javax.script.ScriptException;
|
jaroslav@34
|
22 |
import org.testng.annotations.Test;
|
jaroslav@34
|
23 |
import static org.testng.Assert.*;
|
jaroslav@103
|
24 |
import org.testng.annotations.BeforeClass;
|
jaroslav@34
|
25 |
|
jaroslav@34
|
26 |
/**
|
jaroslav@34
|
27 |
*
|
jaroslav@34
|
28 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@34
|
29 |
*/
|
jaroslav@34
|
30 |
public class StringTest {
|
jaroslav@34
|
31 |
@Test public void firstChar() throws Exception {
|
jaroslav@34
|
32 |
assertExec(
|
jaroslav@34
|
33 |
"First char in Hello is H",
|
jaroslav@34
|
34 |
"org_apidesign_vm4brwsr_StringSample_sayHelloCI",
|
jaroslav@35
|
35 |
"H", 0
|
jaroslav@34
|
36 |
);
|
jaroslav@34
|
37 |
}
|
jaroslav@36
|
38 |
|
jaroslav@45
|
39 |
@Test public void fromChars() throws Exception {
|
jaroslav@45
|
40 |
assertExec(
|
jaroslav@45
|
41 |
"First char in Hello is ABC",
|
jaroslav@45
|
42 |
"org_apidesign_vm4brwsr_StringSample_fromCharsLjava_lang_StringCCC",
|
jaroslav@45
|
43 |
"ABC", 'A', 'B', 'C'
|
jaroslav@45
|
44 |
);
|
jaroslav@45
|
45 |
}
|
jaroslav@45
|
46 |
|
jaroslav@93
|
47 |
@Test(timeOut=10000) public void toStringConcatenation() throws Exception {
|
jaroslav@36
|
48 |
assertExec(
|
jaroslav@36
|
49 |
"Five executions should generate 5Hello World!",
|
jaroslav@36
|
50 |
"org_apidesign_vm4brwsr_StringSample_toStringTestLjava_lang_StringI",
|
jaroslav@94
|
51 |
"Hello World!5", 5
|
jaroslav@36
|
52 |
);
|
jaroslav@36
|
53 |
}
|
jaroslav@94
|
54 |
@Test public void toStringConcatenationJava() throws Exception {
|
jaroslav@94
|
55 |
assertEquals("Hello World!5", StringSample.toStringTest(5));
|
jaroslav@94
|
56 |
}
|
jaroslav@104
|
57 |
|
jaroslav@104
|
58 |
@Test(timeOut=10000) public void stringStringConcat() throws Exception {
|
jaroslav@104
|
59 |
assertExec(
|
jaroslav@104
|
60 |
"Composes strings OK",
|
jaroslav@104
|
61 |
"org_apidesign_vm4brwsr_StringSample_concatStringsLjava_lang_String",
|
jaroslav@104
|
62 |
"Hello World!1Ahoj"
|
jaroslav@104
|
63 |
);
|
jaroslav@104
|
64 |
}
|
jaroslav@104
|
65 |
|
jaroslav@103
|
66 |
private static CharSequence codeSeq;
|
jaroslav@103
|
67 |
private static Invocable code;
|
jaroslav@34
|
68 |
|
jaroslav@103
|
69 |
@BeforeClass
|
jaroslav@103
|
70 |
public void compileTheCode() throws Exception {
|
jaroslav@34
|
71 |
StringBuilder sb = new StringBuilder();
|
jaroslav@103
|
72 |
code = StaticMethodTest.compileClass(sb,
|
jaroslav@34
|
73 |
"org/apidesign/vm4brwsr/StringSample",
|
jaroslav@34
|
74 |
"java/lang/String"
|
jaroslav@34
|
75 |
);
|
jaroslav@103
|
76 |
codeSeq = sb;
|
jaroslav@103
|
77 |
}
|
jaroslav@103
|
78 |
|
jaroslav@103
|
79 |
private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
|
jaroslav@34
|
80 |
Object ret = null;
|
jaroslav@34
|
81 |
try {
|
jaroslav@103
|
82 |
ret = code.invokeFunction(methodName, args);
|
jaroslav@34
|
83 |
} catch (ScriptException ex) {
|
jaroslav@103
|
84 |
fail("Execution failed in\n" + codeSeq, ex);
|
jaroslav@34
|
85 |
} catch (NoSuchMethodException ex) {
|
jaroslav@103
|
86 |
fail("Cannot find method in\n" + codeSeq, ex);
|
jaroslav@34
|
87 |
}
|
jaroslav@34
|
88 |
if (ret == null && expRes == null) {
|
jaroslav@34
|
89 |
return;
|
jaroslav@34
|
90 |
}
|
jaroslav@34
|
91 |
if (expRes.equals(ret)) {
|
jaroslav@34
|
92 |
return;
|
jaroslav@34
|
93 |
}
|
jaroslav@103
|
94 |
assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
|
jaroslav@34
|
95 |
|
jaroslav@34
|
96 |
}
|
jaroslav@34
|
97 |
|
jaroslav@34
|
98 |
}
|