jaroslav@273
|
1 |
/**
|
jaroslav@273
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@273
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@273
|
4 |
*
|
jaroslav@273
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@273
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@273
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@273
|
8 |
*
|
jaroslav@273
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@273
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@273
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@273
|
12 |
* GNU General Public License for more details.
|
jaroslav@273
|
13 |
*
|
jaroslav@273
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@273
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@273
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@273
|
17 |
*/
|
jaroslav@372
|
18 |
package org.apidesign.bck2brwsr.vmtest.impl;
|
jaroslav@273
|
19 |
|
jaroslav@372
|
20 |
import org.apidesign.bck2brwsr.vmtest.*;
|
jaroslav@346
|
21 |
import java.io.File;
|
jaroslav@346
|
22 |
import java.io.FileWriter;
|
jaroslav@346
|
23 |
import java.io.IOException;
|
jaroslav@273
|
24 |
import java.lang.reflect.Method;
|
jaroslav@372
|
25 |
import java.util.ArrayList;
|
jaroslav@372
|
26 |
import java.util.List;
|
jaroslav@273
|
27 |
import org.testng.Assert;
|
jaroslav@273
|
28 |
import org.testng.ITest;
|
jaroslav@273
|
29 |
import org.testng.annotations.Factory;
|
jaroslav@273
|
30 |
import org.testng.annotations.Test;
|
jaroslav@273
|
31 |
|
jaroslav@273
|
32 |
/** A TestNG {@link Factory} that seeks for {@link Compare} annotations
|
jaroslav@273
|
33 |
* in provided class and builds set of tests that compare the computations
|
jaroslav@273
|
34 |
* in real as well as JavaScript virtual machines. Use as:<pre>
|
jaroslav@273
|
35 |
* {@code @}{@link Factory} public static create() {
|
jaroslav@346
|
36 |
* return @{link VMTest}.{@link #create(YourClass.class);
|
jaroslav@273
|
37 |
* }</pre>
|
jaroslav@273
|
38 |
*
|
jaroslav@273
|
39 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@273
|
40 |
*/
|
jaroslav@372
|
41 |
public final class CompareCase implements ITest {
|
jaroslav@372
|
42 |
private final Bck2BrwsrCase first, second;
|
jaroslav@273
|
43 |
private final Method m;
|
jaroslav@273
|
44 |
|
jaroslav@372
|
45 |
private CompareCase(Method m, Bck2BrwsrCase first, Bck2BrwsrCase second) {
|
jaroslav@273
|
46 |
this.first = first;
|
jaroslav@273
|
47 |
this.second = second;
|
jaroslav@273
|
48 |
this.m = m;
|
jaroslav@273
|
49 |
}
|
jaroslav@273
|
50 |
|
jaroslav@346
|
51 |
/** Inspects <code>clazz</code> and for each {@lik Compare} method creates
|
jaroslav@347
|
52 |
* instances of tests. Each instance runs the test in different virtual
|
jaroslav@346
|
53 |
* machine and at the end they compare the results.
|
jaroslav@346
|
54 |
*
|
jaroslav@346
|
55 |
* @param clazz the class to inspect
|
jaroslav@346
|
56 |
* @return the set of created tests
|
jaroslav@346
|
57 |
*/
|
jaroslav@273
|
58 |
public static Object[] create(Class<?> clazz) {
|
jaroslav@273
|
59 |
Method[] arr = clazz.getMethods();
|
jaroslav@372
|
60 |
List<Object> ret = new ArrayList<>();
|
jaroslav@372
|
61 |
|
jaroslav@372
|
62 |
final Launchers l = Launchers.INSTANCE;
|
jaroslav@372
|
63 |
|
jaroslav@372
|
64 |
ret.add(l);
|
jaroslav@372
|
65 |
|
jaroslav@273
|
66 |
for (Method m : arr) {
|
jaroslav@273
|
67 |
Compare c = m.getAnnotation(Compare.class);
|
jaroslav@273
|
68 |
if (c == null) {
|
jaroslav@273
|
69 |
continue;
|
jaroslav@273
|
70 |
}
|
jaroslav@372
|
71 |
final Bck2BrwsrCase real = new Bck2BrwsrCase(m, 0, null);
|
jaroslav@372
|
72 |
final Bck2BrwsrCase js = new Bck2BrwsrCase(m, 1, l);
|
jaroslav@372
|
73 |
final Bck2BrwsrCase brwsr = new Bck2BrwsrCase(m, 2, l);
|
jaroslav@372
|
74 |
|
jaroslav@372
|
75 |
ret.add(real);
|
jaroslav@372
|
76 |
ret.add(js);
|
jaroslav@372
|
77 |
ret.add(brwsr);
|
jaroslav@372
|
78 |
|
jaroslav@372
|
79 |
ret.add(new CompareCase(m, real, js));
|
jaroslav@372
|
80 |
ret.add(new CompareCase(m, real, brwsr));
|
jaroslav@273
|
81 |
}
|
jaroslav@372
|
82 |
return ret.toArray();
|
jaroslav@273
|
83 |
}
|
jaroslav@346
|
84 |
|
jaroslav@346
|
85 |
/** Test that compares the previous results.
|
jaroslav@346
|
86 |
* @throws Throwable
|
jaroslav@346
|
87 |
*/
|
jaroslav@280
|
88 |
@Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
|
jaroslav@280
|
89 |
Object v1 = first.value;
|
jaroslav@280
|
90 |
Object v2 = second.value;
|
jaroslav@356
|
91 |
if (v1 != null) {
|
jaroslav@356
|
92 |
v1 = v1.toString();
|
jaroslav@356
|
93 |
} else {
|
jaroslav@356
|
94 |
v1 = "null";
|
jaroslav@280
|
95 |
}
|
jaroslav@296
|
96 |
Assert.assertEquals(v2, v1, "Comparing results");
|
jaroslav@280
|
97 |
}
|
jaroslav@280
|
98 |
|
jaroslav@346
|
99 |
/** Test name.
|
jaroslav@346
|
100 |
* @return name of the tested method followed by a suffix
|
jaroslav@346
|
101 |
*/
|
jaroslav@280
|
102 |
@Override
|
jaroslav@280
|
103 |
public String getTestName() {
|
jaroslav@349
|
104 |
return m.getName() + "[Compare " + second.typeName() + "]";
|
jaroslav@280
|
105 |
}
|
jaroslav@346
|
106 |
|
jaroslav@346
|
107 |
static StringBuilder dumpJS(CharSequence sb) throws IOException {
|
jaroslav@346
|
108 |
File f = File.createTempFile("execution", ".js");
|
jaroslav@347
|
109 |
try (FileWriter w = new FileWriter(f)) {
|
jaroslav@347
|
110 |
w.append(sb);
|
jaroslav@347
|
111 |
}
|
jaroslav@346
|
112 |
return new StringBuilder(f.getPath());
|
jaroslav@346
|
113 |
}
|
jaroslav@273
|
114 |
}
|