1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.util;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.io.BufferedInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.File;
26 import java.io.OutputStream;
27 import java.io.PrintStream;
28 import java.nio.charset.StandardCharsets;
29 import java.nio.file.Files;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33 import org.apache.bcel.AbstractTestCase;
34 import org.apache.bcel.HelloWorldCreator;
35 import org.apache.bcel.classfile.JavaClass;
36 import org.apache.bcel.classfile.Utility;
37 import org.apache.bcel.generic.BinaryOpCreator;
38 import org.junit.jupiter.api.Test;
39 import org.junit.jupiter.params.ParameterizedTest;
40 import org.junit.jupiter.params.provider.ValueSource;
41
42 public class BCELifierTestCase extends AbstractTestCase {
43
44 private static final String EOL = System.lineSeparator();
45 public static final String CLASSPATH = "." + File.pathSeparator + System.getProperty("java.class.path");
46
47
48 private String canonHashRef(String input) {
49 input = input.replaceAll("#\\d+", "#n");
50 input = input.replaceAll(" +", " ");
51 return input.replaceAll("//.+", "");
52 }
53
54 private String exec(final File workDir, final String... args) throws Exception {
55
56 final ProcessBuilder pb = new ProcessBuilder(args);
57 pb.directory(workDir);
58 pb.redirectErrorStream(true);
59 final Process proc = pb.start();
60 try (BufferedInputStream is = new BufferedInputStream(proc.getInputStream())) {
61 final byte[] buff = new byte[2048];
62 int len;
63
64 final StringBuilder sb = new StringBuilder();
65 while ((len = is.read(buff)) != -1) {
66 sb.append(new String(buff, 0, len));
67 }
68 final String output = sb.toString();
69 assertEquals(0, proc.waitFor(), output);
70 return output;
71 }
72 }
73
74 @ParameterizedTest
75 @ValueSource(strings = {
76
77 "iadd 3 2 = 5",
78 "isub 3 2 = 1",
79 "imul 3 2 = 6",
80 "idiv 3 2 = 1",
81 "irem 3 2 = 1",
82 "iand 3 2 = 2",
83 "ior 3 2 = 3",
84 "ixor 3 2 = 1",
85 "ishl 4 1 = 8",
86 "ishr 4 1 = 2",
87 "iushr 4 1 = 2",
88 "ladd 3 2 = 5",
89 "lsub 3 2 = 1",
90 "lmul 3 2 = 6",
91 "ldiv 3 2 = 1",
92 "lrem 3 2 = 1",
93 "land 3 2 = 2",
94 "lor 3 2 = 3",
95 "lxor 3 2 = 1",
96 "lshl 4 1 = 8",
97 "lshr 4 1 = 2",
98 "lushr 4 1 = 2",
99 "fadd 3 2 = 5.0",
100 "fsub 3 2 = 1.0",
101 "fmul 3 2 = 6.0",
102 "fdiv 3 2 = 1.5",
103 "frem 3 2 = 1.0",
104 "dadd 3 2 = 5.0",
105 "dsub 3 2 = 1.0",
106 "dmul 3 2 = 6.0",
107 "ddiv 3 2 = 1.5",
108 "drem 3 2 = 1.0"
109
110 })
111 public void testBinaryOp(final String exp) throws Exception {
112 BinaryOpCreator.main(new String[] {});
113 final File workDir = new File("target");
114 final Pattern pattern = Pattern.compile("([a-z]{3,5}) ([-+]?\\d*\\.?\\d+) ([-+]?\\d*\\.?\\d+) = ([-+]?\\d*\\.?\\d+)");
115 final Matcher matcher = pattern.matcher(exp);
116 assertTrue(matcher.matches());
117 final String op = matcher.group(1);
118 final String a = matcher.group(2);
119 final String b = matcher.group(3);
120 final String expected = matcher.group(4);
121 final String javaAgent = getJavaAgent();
122 if (javaAgent == null) {
123 assertEquals(expected + EOL, exec(workDir, "java", "-cp", CLASSPATH, "org.apache.bcel.generic.BinaryOp", op, a, b));
124 } else {
125 final String runtimeExecJavaAgent = javaAgent.replace("jacoco.exec", "jacoco_org.apache.bcel.generic.BinaryOp.exec");
126 assertEquals(expected + EOL, exec(workDir, "java", runtimeExecJavaAgent, "-cp", CLASSPATH, "org.apache.bcel.generic.BinaryOp", op, a, b));
127 }
128 }
129
130 private void testClassOnPath(final String javaClassFileName) throws Exception {
131 final File workDir = new File("target");
132 final File infile = new File(javaClassFileName);
133 final JavaClass javaClass = BCELifier.getJavaClass(infile.getName().replace(JavaClass.EXTENSION, ""));
134 assertNotNull(javaClass);
135
136
137 final String initial = exec(null, "javap", "-cp", CLASSPATH, "-p", "-c", javaClass.getClassName());
138 final String outFileName = javaClass.getSourceFilePath().replace(".java", "Creator.java");
139 final File outfile = new File(workDir, outFileName);
140 Files.createDirectories(outfile.getParentFile().toPath());
141 final String javaAgent = getJavaAgent();
142 String creatorSourceContents = null;
143 if (javaAgent == null) {
144 creatorSourceContents = exec(workDir, "java", "-cp", CLASSPATH, "org.apache.bcel.util.BCELifier", javaClass.getClassName());
145 } else {
146 final String runtimeExecJavaAgent = javaAgent.replace("jacoco.exec", "jacoco_" + infile.getName() + ".exec");
147 creatorSourceContents = exec(workDir, "java", runtimeExecJavaAgent, "-cp", CLASSPATH, "org.apache.bcel.util.BCELifier", javaClass.getClassName());
148 }
149 Files.write(outfile.toPath(), creatorSourceContents.getBytes(StandardCharsets.UTF_8));
150 assertEquals("", exec(workDir, "javac", "-cp", CLASSPATH, outFileName.toString()));
151 if (javaAgent == null) {
152 assertEquals("", exec(workDir, "java", "-cp", CLASSPATH, javaClass.getClassName() + "Creator"));
153 } else {
154 final String runtimeExecJavaAgent = javaAgent.replace("jacoco.exec", "jacoco_" + Utility.pathToPackage(outFileName) + ".exec");
155 assertEquals("", exec(workDir, "java", runtimeExecJavaAgent, "-cp", CLASSPATH, javaClass.getClassName() + "Creator"));
156 }
157 final String output = exec(workDir, "javap", "-p", "-c", infile.getName());
158 assertEquals(canonHashRef(initial), canonHashRef(output));
159 }
160
161 @Test
162 public void testHelloWorld() throws Exception {
163 HelloWorldCreator.main(new String[] {});
164 final File workDir = new File("target");
165 final String javaAgent = getJavaAgent();
166 if (javaAgent == null) {
167 assertEquals("Hello World!" + EOL, exec(workDir, "java", "-cp", CLASSPATH, "org.apache.bcel.HelloWorld"));
168 } else {
169 final String runtimeExecJavaAgent = javaAgent.replace("jacoco.exec", "jacoco_org.apache.bcel.HelloWorld.exec");
170 assertEquals("Hello World!" + EOL, exec(workDir, "java", runtimeExecJavaAgent, "-cp", CLASSPATH, "org.apache.bcel.HelloWorld"));
171 }
172 }
173
174
175
176
177
178
179 @ParameterizedTest
180 @ValueSource(strings = {
181
182 "org.apache.commons.lang.math.Fraction.class",
183 "org.apache.commons.lang.exception.NestableDelegate.class",
184 "org.apache.commons.lang.builder.CompareToBuilder.class",
185 "org.apache.commons.lang.builder.ToStringBuilder.class",
186 "org.apache.commons.lang.SerializationUtils.class",
187 "org.apache.commons.lang.ArrayUtils.class",
188 "target/test-classes/Java8Example.class",
189 "target/test-classes/Java8Example2.class",
190 "target/test-classes/Java4Example.class"
191
192 })
193 public void testJavapCompare(final String pathToClass) throws Exception {
194 testClassOnPath(pathToClass);
195 }
196
197 @Test
198 public void testMainNoArg() throws Exception {
199 final PrintStream sysout = System.out;
200 try {
201 final ByteArrayOutputStream out = new ByteArrayOutputStream();
202 System.setOut(new PrintStream(out));
203 BCELifier.main(new String[0]);
204 final String outputNoArgs = new String(out.toByteArray());
205 assertEquals("Usage: BCELifier className" + EOL + "\tThe class must exist on the classpath" + EOL, outputNoArgs);
206 } finally {
207 System.setOut(sysout);
208 }
209 }
210
211 @ParameterizedTest
212 @ValueSource(strings = { "StackMapExample", "StackMapExample2" })
213 public void testStackMap(final String className) throws Exception {
214 testJavapCompare(className);
215 final File workDir = new File("target");
216 assertEquals("Hello World" + EOL, exec(workDir, "java", "-cp", CLASSPATH, className, "Hello"));
217 }
218
219 @Test
220 public void testStart() throws Exception {
221 final OutputStream os = new ByteArrayOutputStream();
222 final JavaClass javaClass = BCELifier.getJavaClass("Java8Example");
223 assertNotNull(javaClass);
224 final BCELifier bcelifier = new BCELifier(javaClass, os);
225 bcelifier.start();
226 }
227 }