1.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/CompareVMs.java Thu Dec 06 22:02:10 2012 +0100
1.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/CompareVMs.java Fri Dec 07 14:10:37 2012 +0100
1.3 @@ -36,61 +36,14 @@
1.4 * @author Jaroslav Tulach <jtulach@netbeans.org>
1.5 */
1.6 public final class CompareVMs implements ITest {
1.7 + private final Run first, second;
1.8 private final Method m;
1.9 - private final boolean js;
1.10 - private final CompareVMs first, second;
1.11 - private Object value;
1.12 - private static Invocable code;
1.13 - private static CharSequence codeSeq;
1.14
1.15 - private CompareVMs(Method m, boolean js) {
1.16 - this.m = m;
1.17 - this.js = js;
1.18 - this.first = null;
1.19 - this.second = null;
1.20 - }
1.21 -
1.22 - private CompareVMs(Method m, CompareVMs first, CompareVMs second) {
1.23 + private CompareVMs(Method m, Run first, Run second) {
1.24 this.first = first;
1.25 this.second = second;
1.26 this.m = m;
1.27 - this.js = false;
1.28 }
1.29 -
1.30 - private static void compileTheCode(Class<?> clazz) throws Exception {
1.31 - if (code != null) {
1.32 - return;
1.33 - }
1.34 - StringBuilder sb = new StringBuilder();
1.35 - class SkipMe extends GenJS {
1.36 - public SkipMe(Appendable out) {
1.37 - super(out);
1.38 - }
1.39 -
1.40 - @Override
1.41 - protected boolean requireReference(String cn) {
1.42 - if (cn.contains("CompareVMs")) {
1.43 - return true;
1.44 - }
1.45 - return super.requireReference(cn);
1.46 - }
1.47 -
1.48 -
1.49 - }
1.50 - SkipMe sm = new SkipMe(sb);
1.51 - sm.doCompile(CompareVMs.class.getClassLoader(), StringArray.asList(
1.52 - clazz.getName().replace('.', '/')
1.53 - ));
1.54 -
1.55 - ScriptEngineManager sem = new ScriptEngineManager();
1.56 - ScriptEngine js = sem.getEngineByExtension("js");
1.57 -
1.58 - Object res = js.eval(sb.toString());
1.59 - Assert.assertTrue(js instanceof Invocable, "It is invocable object: " + res);
1.60 - code = (Invocable)js;
1.61 - codeSeq = sb;
1.62 - }
1.63 -
1.64
1.65 public static Object[] create(Class<?> clazz) {
1.66 Method[] arr = clazz.getMethods();
1.67 @@ -101,8 +54,8 @@
1.68 if (c == null) {
1.69 continue;
1.70 }
1.71 - final CompareVMs real = new CompareVMs(m, false);
1.72 - final CompareVMs js = new CompareVMs(m, true);
1.73 + final Run real = new Run(m, false);
1.74 + final Run js = new Run(m, true);
1.75 ret[cnt++] = real;
1.76 ret[cnt++] = js;
1.77 ret[cnt++] = new CompareVMs(m, real, js);
1.78 @@ -114,15 +67,65 @@
1.79 return r;
1.80 }
1.81
1.82 - @Test public void runTest() throws Throwable {
1.83 - if (first != null) {
1.84 - Object v1 = first.value;
1.85 - Object v2 = second.value;
1.86 - if (v1 instanceof Number) {
1.87 - v1 = ((Number)v1).doubleValue();
1.88 + @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
1.89 + Object v1 = first.value;
1.90 + Object v2 = second.value;
1.91 + if (v1 instanceof Number) {
1.92 + v1 = ((Number)v1).doubleValue();
1.93 + }
1.94 + Assert.assertEquals(v1, v2, "Comparing results");
1.95 + }
1.96 +
1.97 + @Override
1.98 + public String getTestName() {
1.99 + return m.getName() + "[Compare]";
1.100 + }
1.101 +
1.102 + public static final class Run implements ITest {
1.103 + private final Method m;
1.104 + private final boolean js;
1.105 + Object value;
1.106 + private static Invocable code;
1.107 + private static CharSequence codeSeq;
1.108 +
1.109 + private Run(Method m, boolean js) {
1.110 + this.m = m;
1.111 + this.js = js;
1.112 + }
1.113 +
1.114 + private static void compileTheCode(Class<?> clazz) throws Exception {
1.115 + if (code != null) {
1.116 + return;
1.117 }
1.118 - Assert.assertEquals(v1, v2, "Comparing results");
1.119 - } else {
1.120 + StringBuilder sb = new StringBuilder();
1.121 + class SkipMe extends GenJS {
1.122 +
1.123 + public SkipMe(Appendable out) {
1.124 + super(out);
1.125 + }
1.126 +
1.127 + @Override
1.128 + protected boolean requireReference(String cn) {
1.129 + if (cn.contains("CompareVMs")) {
1.130 + return true;
1.131 + }
1.132 + return super.requireReference(cn);
1.133 + }
1.134 + }
1.135 + SkipMe sm = new SkipMe(sb);
1.136 + sm.doCompile(CompareVMs.class.getClassLoader(), StringArray.asList(
1.137 + clazz.getName().replace('.', '/')));
1.138 +
1.139 + ScriptEngineManager sem = new ScriptEngineManager();
1.140 + ScriptEngine js = sem.getEngineByExtension("js");
1.141 +
1.142 + Object res = js.eval(sb.toString());
1.143 + Assert.assertTrue(js instanceof Invocable, "It is invocable object: " + res);
1.144 + code = (Invocable) js;
1.145 + codeSeq = sb;
1.146 + }
1.147 +
1.148 + @Test(groups = "run") public void executeCode() throws Throwable {
1.149 if (js) {
1.150 try {
1.151 compileTheCode(m.getDeclaringClass());
1.152 @@ -136,13 +139,9 @@
1.153 value = m.invoke(m.getDeclaringClass().newInstance());
1.154 }
1.155 }
1.156 - }
1.157 -
1.158 - @Override
1.159 - public String getTestName() {
1.160 - if (first != null) {
1.161 - return m.getName() + "[Compare]";
1.162 + @Override
1.163 + public String getTestName() {
1.164 + return m.getName() + (js ? "[JavaScript]" : "[Java]");
1.165 }
1.166 - return m.getName() + (js ? "[JavaScript]" : "[Java]");
1.167 }
1.168 }