Skip to content

Commit

Permalink
tests: add test for previous feature
Browse files Browse the repository at this point in the history
  • Loading branch information
algomaster99 committed Aug 1, 2024
1 parent 2eceeb2 commit 55c8c60
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import static org.assertj.core.api.Assertions.assertThat;

import com.github.gumtreediff.actions.EditScript;
import com.github.gumtreediff.actions.model.Action;
import com.github.gumtreediff.actions.model.Update;
import com.github.gumtreediff.tree.TypeSet;
import java.io.IOException;
import java.lang.classfile.ClassFile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import org.junit.jupiter.api.Test;

public class ClassFileVisitorTest {
Expand All @@ -24,10 +25,7 @@ void shouldShowDiffIn_majorVersion() throws IOException {
final Builder scanner = new Builder();

// act
DiffImpl diff = new DiffImpl(
scanner.getTreeContext(),
scanner.getTree(ClassFile.of().parse(bytes1)),
scanner.getTree(ClassFile.of().parse(bytes2)));
DiffImpl diff = new DiffImpl(scanner.getTreeContext(), scanner.getTree(bytes1), scanner.getTree(bytes2));
diff.computeDiff();

// assert
Expand Down Expand Up @@ -56,10 +54,7 @@ void shouldShowDiffIn_thisClass_sourceFileAttribute() throws IOException {
final Builder scanner = new Builder();

// act
DiffImpl diff = new DiffImpl(
scanner.getTreeContext(),
scanner.getTree(ClassFile.of().parse(bytes1)),
scanner.getTree(ClassFile.of().parse(bytes2)));
DiffImpl diff = new DiffImpl(scanner.getTreeContext(), scanner.getTree(bytes1), scanner.getTree(bytes2));
diff.computeDiff();

// assert
Expand All @@ -86,4 +81,23 @@ void shouldShowDiffIn_thisClass_sourceFileAttribute() throws IOException {
assertThat(newSourceFile)
.isEqualTo("DropwizardResourceConfig$SpecificBindere75da6ac-3201-4073-bd4f-75682c761862.java");
}

@Test
void shouldShowDiffIf_orderOfFieldAndMethodsAreFlipped() throws IOException {
// it could be better if it shows that field names are replaced in methods
// arrange
Path proxy1 = RESOURCES.resolve("orderOfFieldsMethodsClint").resolve("A.class");
Path proxy2 = RESOURCES.resolve("orderOfFieldsMethodsClint").resolve("B.class");
byte[] bytes1 = Files.readAllBytes(proxy1);
byte[] bytes2 = Files.readAllBytes(proxy2);
final Builder scanner = new Builder();

// act
DiffImpl diff = new DiffImpl(scanner.getTreeContext(), scanner.getTree(bytes1), scanner.getTree(bytes2));
diff.computeDiff();

// assert
List<Action> rootOperations = diff.getSimplifiedOperations().asList();
assertThat(rootOperations).size().isEqualTo(12);
}
}
Binary file added src/test/resources/orderOfFieldsMethodsClint/A.class
Binary file not shown.
34 changes: 34 additions & 0 deletions src/test/resources/orderOfFieldsMethodsClint/A.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.lang.reflect.Method;

public class A {
private Method m1;
private Method m2;
private Method m3;

@Override
public boolean equals(Object obj) {
try {
return (boolean) m1.invoke(this, obj);
} catch (Exception e) {
return false;
}
}

@Override
public int hashCode() {
try {
return (int) m2.invoke(this);
} catch (Exception e) {
return 0;
}
}

@Override
public String toString() {
try {
return (String) m3.invoke(this);
} catch (Exception e) {
return "";
}
}
}
Binary file added src/test/resources/orderOfFieldsMethodsClint/B.class
Binary file not shown.
34 changes: 34 additions & 0 deletions src/test/resources/orderOfFieldsMethodsClint/B.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.lang.reflect.Method;

public class B {
private Method m2;
private Method m3;
private Method m1;

@Override
public boolean equals(Object obj) {
try {
return (boolean) m2.invoke(this, obj);
} catch (Exception e) {
return false;
}
}

@Override
public int hashCode() {
try {
return (int) m3.invoke(this);
} catch (Exception e) {
return 0;
}
}

@Override
public String toString() {
try {
return (String) m1.invoke(this);
} catch (Exception e) {
return "";
}
}
}

0 comments on commit 55c8c60

Please sign in to comment.