Skip to content

Commit b7dbcdc

Browse files
author
Andreas Dann
committed
update test
1 parent a0479fd commit b7dbcdc

File tree

4 files changed

+366
-0
lines changed

4 files changed

+366
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import com.github.difflib.DiffUtils;
2+
import com.github.difflib.algorithm.DiffException;
3+
import com.github.difflib.patch.Patch;
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.IOException;
8+
import java.io.PrintWriter;
9+
import java.net.URL;
10+
import java.util.ArrayList;
11+
import java.util.Collection;
12+
import java.util.List;
13+
import org.junit.Assert;
14+
import org.junit.Ignore;
15+
import org.junit.Test;
16+
import org.junit.runner.RunWith;
17+
import org.junit.runners.Parameterized;
18+
import org.objectweb.asm.ClassReader;
19+
import org.objectweb.asm.util.TraceClassVisitor;
20+
21+
/** @author Andreas Dann created on 10.12.18 */
22+
@Ignore
23+
@RunWith(Parameterized.class)
24+
public class ByteCodeCompareClassesSuccess {
25+
26+
private final String referenceFolder;
27+
private final String otherFolder;
28+
private final File filename;
29+
30+
@Parameterized.Parameters
31+
public static Collection<Object[]> generateParams() {
32+
List<Object[]> params = new ArrayList<Object[]>();
33+
34+
params.addAll(createParaList("reference", "1.5"));
35+
params.addAll(createParaList("reference", "1.6"));
36+
params.addAll(createParaList("reference", "1.7"));
37+
38+
return params;
39+
}
40+
41+
public static List<Object[]> createParaList(String refeFolder, String cmpFolder) {
42+
List<Object[]> params = new ArrayList<Object[]>();
43+
44+
URL url = ByteCodeCompareClassesSuccess.class.getResource("/" + refeFolder);
45+
File refClass = new File(url.getFile());
46+
47+
// the other class
48+
URL otherurl = ByteCodeCompareClassesSuccess.class.getResource("/" + cmpFolder);
49+
String otherFolder = new File(otherurl.getFile()).toString();
50+
51+
File[] listOfFiles = refClass.listFiles();
52+
53+
for (File filename : listOfFiles) {
54+
55+
params.add(new Object[] {refClass.toString(), otherFolder, filename});
56+
}
57+
return params;
58+
}
59+
60+
public ByteCodeCompareClassesSuccess(String referenceFolder, String otherFolder, File filename) {
61+
this.referenceFolder = referenceFolder;
62+
this.otherFolder = otherFolder;
63+
this.filename = filename;
64+
}
65+
66+
@Test
67+
public void test() throws IOException, DiffException {
68+
String qname = filename.getName().substring(0, filename.getName().indexOf("."));
69+
70+
System.out.println(
71+
"Compare " + referenceFolder + " against " + otherFolder + " using class " + qname);
72+
73+
ClassReader cr = new ClassReader(new FileInputStream(filename));
74+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
75+
76+
cr.accept(
77+
new TraceClassVisitor(new PrintWriter(byteArrayOutputStream)), ClassReader.SKIP_DEBUG);
78+
79+
String filename2 = otherFolder + "/" + qname + ".class";
80+
ClassReader cr2 = new ClassReader(new FileInputStream(filename2));
81+
ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
82+
83+
cr2.accept(
84+
new TraceClassVisitor(new PrintWriter(byteArrayOutputStream2)), ClassReader.SKIP_DEBUG);
85+
86+
Patch<String> diff =
87+
DiffUtils.diff(byteArrayOutputStream.toString(), byteArrayOutputStream2.toString(), null);
88+
System.out.println("Number of diffs: " + diff.getDeltas().size());
89+
System.out.println(diff);
90+
91+
boolean condition = diff.getDeltas().size() <= 1;
92+
93+
System.out.println(
94+
"Latex: "
95+
+ filename
96+
+ " "
97+
+ otherFolder.substring(otherFolder.lastIndexOf("/", otherFolder.length() - 1))
98+
+ " : "
99+
+ condition);
100+
101+
Assert.assertTrue(condition);
102+
}
103+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import de.upb.soot.diff.Main;
2+
import java.io.File;
3+
import java.net.URL;
4+
import java.util.ArrayList;
5+
import java.util.Collection;
6+
import java.util.List;
7+
import org.apache.commons.lang3.builder.Diff;
8+
import org.apache.commons.lang3.builder.DiffResult;
9+
import org.junit.Assert;
10+
import org.junit.Ignore;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.junit.runners.Parameterized;
14+
15+
/** @author Andreas Dann created on 10.12.18 */
16+
@Ignore
17+
@RunWith(Parameterized.class)
18+
public class JimpleCompareClassesDiffBuilderSuccess {
19+
20+
private final String referenceFolder;
21+
private final String otherFolder;
22+
private final File filename;
23+
24+
/**
25+
* THe simple jimple comparison has to fail on some classes, expected (AD)
26+
*
27+
* @return
28+
*/
29+
@Parameterized.Parameters
30+
public static Collection<Object[]> generateParams() {
31+
List<Object[]> params = new ArrayList<Object[]>();
32+
33+
params.addAll(createParaList("1.5"));
34+
params.addAll(createParaList("1.6"));
35+
params.addAll(createParaList("1.7"));
36+
37+
return params;
38+
}
39+
40+
public static List<Object[]> createParaList(String cmpFolder) {
41+
List<Object[]> params = new ArrayList<Object[]>();
42+
43+
URL url = JimpleCompareClassesDiffBuilderSuccess.class.getResource("/" + "reference");
44+
File refClass = new File(url.getFile());
45+
46+
// the other class
47+
URL otherurl =
48+
JimpleCompareClassesDiffBuilderSuccess.class.getClass().getResource("/" + cmpFolder);
49+
String otherFolder = new File(otherurl.getFile()).toString();
50+
51+
File[] listOfFiles = refClass.listFiles();
52+
53+
for (File filename : listOfFiles) {
54+
55+
params.add(new Object[] {refClass.toString(), otherFolder, filename});
56+
}
57+
return params;
58+
}
59+
60+
public JimpleCompareClassesDiffBuilderSuccess(
61+
String referenceFolder, String otherFolder, File filename) {
62+
this.referenceFolder = referenceFolder;
63+
this.otherFolder = otherFolder;
64+
this.filename = filename;
65+
}
66+
67+
@Test
68+
public void test() {
69+
String qname = filename.getName().substring(0, filename.getName().indexOf("."));
70+
71+
System.out.println(
72+
"Compare " + referenceFolder + " against " + otherFolder + " using class " + qname);
73+
74+
System.out.println("");
75+
76+
Main main = new Main(referenceFolder, otherFolder, qname, qname);
77+
DiffResult res = main.compareClasses();
78+
// AD: printout the differences for debugging
79+
for (Diff d : res.getDiffs()) {
80+
System.out.println(d.toString());
81+
}
82+
83+
int numberOfDiffs = res.getNumberOfDiffs();
84+
85+
boolean condition = numberOfDiffs == 0;
86+
System.out.println(
87+
"Latex: "
88+
+ filename
89+
+ " "
90+
+ otherFolder.substring(otherFolder.lastIndexOf("/", otherFolder.length() - 1))
91+
+ " : "
92+
+ condition);
93+
94+
Assert.assertEquals(0, numberOfDiffs);
95+
}
96+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package de.upb.soot.diff.compare;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import de.upb.soot.diff.SootDiff;
6+
import de.upb.soot.diff.Utils;
7+
import java.io.File;
8+
import java.io.PrintWriter;
9+
import java.io.StringWriter;
10+
import java.net.URL;
11+
import java.util.ArrayList;
12+
import java.util.Collection;
13+
import java.util.Collections;
14+
import java.util.List;
15+
import org.junit.Ignore;
16+
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
import org.junit.runners.Parameterized;
19+
import soot.Printer;
20+
import soot.Scene;
21+
import soot.SootClass;
22+
23+
/** @author Andreas Dann created on 10.12.18 */
24+
@RunWith(Parameterized.class)
25+
public class JimpleClassesOutputTest {
26+
27+
private final String referenceFolder;
28+
private final String otherFolder;
29+
private final File filename;
30+
31+
/**
32+
* The jimple representation is not always the same, as expected
33+
*
34+
* @return
35+
*/
36+
@Parameterized.Parameters(name = "{1}:{2}")
37+
public static Collection<Object[]> generateParams() {
38+
List<Object[]> params = new ArrayList<Object[]>();
39+
40+
/* If JDK > 8, compiling to 1.5 and 1.6 is not working anymore and the tests fail.
41+
params.addAll(createParaList("1.5"));
42+
params.addAll(createParaList("1.6"));*/
43+
params.addAll(createParaList("1.7"));
44+
// params.addAll(createParaList("1.9"));
45+
return params;
46+
}
47+
48+
public static List<Object[]> createParaList(String cmpFolder) {
49+
List<Object[]> params = new ArrayList<Object[]>();
50+
51+
URL url = JimpleClassesOutputTest.class.getResource("/" + "reference");
52+
File refClass = new File(url.getFile());
53+
54+
// the other class
55+
URL otherurl = JimpleClassesOutputTest.class.getResource("/" + cmpFolder);
56+
String otherFolder = new File(otherurl.getFile()).toString();
57+
58+
File[] listOfFiles = refClass.listFiles();
59+
60+
for (File filename : listOfFiles) {
61+
params.add(new Object[] {refClass.toString(), otherFolder, filename});
62+
}
63+
return params;
64+
}
65+
66+
public JimpleClassesOutputTest(String referenceFolder, String otherFolder, File filename) {
67+
this.referenceFolder = referenceFolder;
68+
this.otherFolder = otherFolder;
69+
this.filename = filename;
70+
}
71+
72+
@Test
73+
@Ignore
74+
public void test() {
75+
String qname = filename.getName().substring(0, filename.getName().indexOf("."));
76+
77+
System.out.println(
78+
"Compare " + referenceFolder + " against " + otherFolder + " using class " + qname);
79+
SootDiff sootDiff = new SootDiff(Collections.singletonList(referenceFolder), null, false);
80+
sootDiff.runSootDiff();
81+
82+
StringWriter s1 = new StringWriter();
83+
PrintWriter fileWriter = new PrintWriter(s1);
84+
SootClass a = Scene.v().getSootClass(Utils.className(qname));
85+
Printer.v().printTo(a, fileWriter);
86+
// the other class
87+
URL otherurl = JimpleClassesOutputTest.class.getResource("/1.9");
88+
String otherFolder = new File(otherurl.getFile()).toString();
89+
90+
SootDiff sootDiff2 = new SootDiff(Collections.singletonList(otherFolder), null, false);
91+
sootDiff2.runSootDiff();
92+
93+
SootClass b = Scene.v().getSootClass(Utils.className(qname));
94+
95+
StringWriter s2 = new StringWriter();
96+
PrintWriter fileWriter2 = new PrintWriter(s2);
97+
Printer.v().printTo(b, fileWriter2);
98+
99+
assertEquals(s1.toString(), s2.toString());
100+
}
101+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package de.upb.soot.diff.compare;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import de.upb.soot.diff.SootDiff;
6+
import java.io.File;
7+
import java.net.URL;
8+
import java.util.Collections;
9+
import org.junit.Test;
10+
import soot.Scene;
11+
import soot.SootClass;
12+
import soot.SootMethod;
13+
import targets.StringBufferConstructor;
14+
import targets.StringBuilderAndBuffer1;
15+
import targets.StringBuilderAndBuffer2;
16+
import targets.StringBuilderConstructor;
17+
import targets.StringValueOf;
18+
19+
/** @author Andreas Dann created on 10.12.18 */
20+
public class JimpleMethodsTest {
21+
22+
@Test
23+
public void stringBuilderAndBuffer1() {
24+
runTest(StringBuilderAndBuffer1.class);
25+
}
26+
27+
@Test
28+
public void stringBuilderAndBuffer2() {
29+
runTest(StringBuilderAndBuffer2.class);
30+
}
31+
32+
@Test
33+
public void stringBufferConstructor() {
34+
runTest(StringBuilderConstructor.class);
35+
}
36+
37+
@Test
38+
public void stringBuilderConstructor() {
39+
runTest(StringBufferConstructor.class);
40+
}
41+
42+
@Test
43+
public void stringValueOf() {
44+
runTest(StringValueOf.class);
45+
}
46+
47+
private void runTest(Class<?> clz) {
48+
49+
URL url = JimpleMethodsTest.class.getResource("/");
50+
File refClass = new File(url.getFile());
51+
SootDiff sootDiff = new SootDiff(Collections.singletonList(refClass.toString()), null, false);
52+
53+
sootDiff.runSootDiff();
54+
55+
SootClass sc = Scene.v().getSootClass(clz.getName());
56+
SootMethod sm1 = sc.getMethod("void targetMethod1()");
57+
SootMethod sm2 = sc.getMethod("void targetMethod2()");
58+
59+
assertEquals(
60+
String.format(
61+
"Method Bodies in Jimple format not equal \n%s\n %s",
62+
sm1.getActiveBody(), sm2.getActiveBody()),
63+
sm1.getActiveBody().toString(),
64+
sm2.getActiveBody().toString().replace("void targetMethod2()", "void targetMethod1()"));
65+
}
66+
}

0 commit comments

Comments
 (0)