Vorläufige Testsuite für ByteCode-Testing

This commit is contained in:
KingJulian 2024-06-16 12:28:27 +02:00
parent fa430301bb
commit c44ed5446d
4 changed files with 85 additions and 6 deletions

View File

@ -50,12 +50,14 @@ public class CompareByteCodeBehaviour {
}
}
public void compareMethodBehaviour(Class<?> class1, Class<?> class2){
public boolean compareMethodBehaviour(Class<?> class1, Class<?> class2){
try {
this.sortMethods(class1, class2);
// Create instances
Constructor<?> constructor1 = class1.getDeclaredConstructor();
Constructor<?> constructor2 = class2.getDeclaredConstructor();
constructor1.setAccessible(true);
constructor2.setAccessible(true);
Object obj1 = constructor1.newInstance();
Object obj2 = constructor2.newInstance();
@ -71,6 +73,9 @@ public class CompareByteCodeBehaviour {
Method method1 = methods1[i];
Method method2 = methods2[i];
method1.setAccessible(true);
method2.setAccessible(true);
// Test with some sample inputs
Object[] testInputs = getTestInputs(method1.getParameterTypes());
@ -78,19 +83,17 @@ public class CompareByteCodeBehaviour {
Object result1 = method1.invoke(obj1, testInputs);
Object result2 = method2.invoke(obj2, testInputs);
/*
if (!result1.equals(result2)) {
System.out.println("Methods " + method1.getName() + " do not produce the same result");
} else {
System.out.println("Methods " + method1.getName() + " produce the same result");
return false;
}
*/
}
} catch (InstantiationException | IllegalAccessException |
NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
return true;
}
private static Object[] getTestInputs(Class<?>[] parameterTypes) {

View File

@ -39,8 +39,10 @@ public class CompareByteCodeSyntax {
return false;
}
for (Method method1 : methods1) {
method1.setAccessible(true);
boolean found = false;
for (Method method2 : methods2) {
method2.setAccessible(true);
if (compareMethod(method1, method2)) {
found = true;
break;
@ -76,8 +78,10 @@ public class CompareByteCodeSyntax {
return false;
}
for (Field field1 : fields1) {
field1.setAccessible(true);
boolean found = false;
for (Field field2 : fields2) {
field2.setAccessible(true);
if (compareField(field1, field2)) {
found = true;
break;

View File

@ -0,0 +1,37 @@
package ByteCode;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FileClassLoader extends ClassLoader {
private final String classFilePath;
public FileClassLoader(String classFilePath) {
this.classFilePath = classFilePath;
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
try {
byte[] classData = loadClassData();
return defineClass(name, classData, 0, classData.length);
} catch (IOException e) {
throw new ClassNotFoundException("Class not found", e);
}
}
private byte[] loadClassData() throws IOException {
File file = new File(classFilePath);
try (FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
return baos.toByteArray();
}
}
}

View File

@ -1,4 +1,39 @@
package ByteCode;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestAll {
CompareByteCodeBehaviour byteCodeBehaviourComparer;
public TestAll(){
byteCodeBehaviourComparer = new CompareByteCodeBehaviour();
}
public void testByteCode(String classPath1, String classPath2, String className){
try {
FileClassLoader classLoader1 = new FileClassLoader(classPath1);
FileClassLoader classLoader2 = new FileClassLoader(classPath2);
Class<?> class1 = classLoader1.findClass(className);
Class<?> class2 = classLoader2.findClass(className);
assertTrue(CompareByteCodeSyntax.haveSameBehavior(class1, class2));
assertTrue(byteCodeBehaviourComparer.compareMethodBehaviour(class1, class2));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Test
public void testEmptyClass() throws Exception {
String classPath1 = "src/test/resources/basicClasses/emptyClass.class";
String classPath2 = "src/test/resources/basicClasses/emptyClass.class";
String className = "emptyClass";
testByteCode(classPath1, classPath2, "emptyClass");
}
}