From 55c8c6017106da78828d2ea81edbc069f0ad9239 Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Thu, 1 Aug 2024 13:43:59 +0200 Subject: [PATCH] tests: add test for previous feature --- .../diff/visitor/ClassFileVisitorTest.java | 32 ++++++++++++----- .../orderOfFieldsMethodsClint/A.class | Bin 0 -> 903 bytes .../orderOfFieldsMethodsClint/A.java | 34 ++++++++++++++++++ .../orderOfFieldsMethodsClint/B.class | Bin 0 -> 903 bytes .../orderOfFieldsMethodsClint/B.java | 34 ++++++++++++++++++ 5 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 src/test/resources/orderOfFieldsMethodsClint/A.class create mode 100644 src/test/resources/orderOfFieldsMethodsClint/A.java create mode 100644 src/test/resources/orderOfFieldsMethodsClint/B.class create mode 100644 src/test/resources/orderOfFieldsMethodsClint/B.java diff --git a/src/test/java/io/github/algomaster99/bytecode/diff/visitor/ClassFileVisitorTest.java b/src/test/java/io/github/algomaster99/bytecode/diff/visitor/ClassFileVisitorTest.java index 0f5cc717ad..04e7df99f5 100644 --- a/src/test/java/io/github/algomaster99/bytecode/diff/visitor/ClassFileVisitorTest.java +++ b/src/test/java/io/github/algomaster99/bytecode/diff/visitor/ClassFileVisitorTest.java @@ -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 { @@ -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 @@ -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 @@ -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 rootOperations = diff.getSimplifiedOperations().asList(); + assertThat(rootOperations).size().isEqualTo(12); + } } diff --git a/src/test/resources/orderOfFieldsMethodsClint/A.class b/src/test/resources/orderOfFieldsMethodsClint/A.class new file mode 100644 index 0000000000000000000000000000000000000000..897568000b57ba6a59647931618d0dd04e2c93b2 GIT binary patch literal 903 zcmZvaU2hUW7=_QUz`}O1Ed{JtTI(lJE!I{OQ)5z%jWtkBFg5DcLPywkyIbkf_@lU} ziNVCAjo1Dte|4>jLp7 zwrl%O1+-#mPayi-+m`~}S$cCcqTU80yC6HeH!9|&0H z2iN$QXo#p-IE$!$f|1TSMro9(v1Yj{DBB!kU~6iBGyN|SJQgItsL*@d{%KnGgDMda#FYwvft^*^XyeD4A`#%!$6Mg zL|G#{;3oSpdes3pP1}>)43#Wf$ql*nkXyjwfctX5&5%1a;+kjGbm}Z{t3sNj7N{&T Qw51{SO-Nm$_Xw{20kTP^c>n+a literal 0 HcmV?d00001 diff --git a/src/test/resources/orderOfFieldsMethodsClint/A.java b/src/test/resources/orderOfFieldsMethodsClint/A.java new file mode 100644 index 0000000000..224bf12c51 --- /dev/null +++ b/src/test/resources/orderOfFieldsMethodsClint/A.java @@ -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 ""; + } + } +} diff --git a/src/test/resources/orderOfFieldsMethodsClint/B.class b/src/test/resources/orderOfFieldsMethodsClint/B.class new file mode 100644 index 0000000000000000000000000000000000000000..3cd482b4cbb5676a42ae1d1646dda85e2dee356c GIT binary patch literal 903 zcmZvaU2hUW7=_QUz`}O1Ed{JtT5J6Ps>RxBVrsm=Xsm&1LQ|t&Ep&u!x4V@tjX#Qu znixz>G+z6ojAwQsfO_-J%$b>I&Ug?z&0Kl(6E}sC0y6BX5a>H>bPYfhTA%d zN-yas8%SbZE$-;Jo4|&Sia_M#zCgZqWSv_T$8y`1uKeK0mS3q$zvCV5s1sYNYzo95 z+pg_D5zvaI1A*vM?@$WN)@)bq^-h|yyKgleDslO-XE|pA+2REFRr844ozgpjRKvGg z$93y8xIl{niH>#F34x^VQt-Wo-?iPgK&Dul;DjoT84a)3ZOP|$@Md*G^7~@dG;!nhU}MtVIW6# zqO6e}aFYWV{o0V5rtN8NhDw&L