From 2c46bccc2459541319ca908cb90b11c101eb7240 Mon Sep 17 00:00:00 2001 From: Rohitesh Date: Sun, 8 Aug 2021 22:18:51 +0530 Subject: [PATCH 1/4] add test for createReference --- .../spoon/test/factory/MethodFactoryTest.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/test/java/spoon/test/factory/MethodFactoryTest.java diff --git a/src/test/java/spoon/test/factory/MethodFactoryTest.java b/src/test/java/spoon/test/factory/MethodFactoryTest.java new file mode 100644 index 00000000000..b699ddebf15 --- /dev/null +++ b/src/test/java/spoon/test/factory/MethodFactoryTest.java @@ -0,0 +1,34 @@ +package spoon.test.factory; + +import org.junit.jupiter.api.Test; +import spoon.Launcher; +import spoon.reflect.declaration.CtClass; +import spoon.reflect.declaration.CtMethod; +import spoon.reflect.factory.MethodFactory; +import spoon.reflect.reference.CtExecutableReference; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.CoreMatchers.is; + +public class MethodFactoryTest { + + private static final String TEST_CLASS_STRING = + "class TestClass { " + + "private void foo() { }" + + "}"; + + @Test + public void testCreateReference() { + // contract: createReference creates a method reference of the foo method + + CtClass testClass = Launcher.parseClass(TEST_CLASS_STRING); + CtMethod foo = testClass.getMethodsByName("foo").get(0); + CtExecutableReference expectedReference = testClass.getMethod("foo").getReference(); + MethodFactory methodFactory = testClass.getFactory().Method(); + CtExecutableReference actualCreatedReference = null; + + actualCreatedReference = methodFactory.createReference(foo); + + assertThat(actualCreatedReference, is(expectedReference)); + } +} From de8288dafff435d055635a81e92f25c3220e353c Mon Sep 17 00:00:00 2001 From: Rohitesh Date: Mon, 9 Aug 2021 11:23:37 +0530 Subject: [PATCH 2/4] add another test --- .../spoon/test/factory/MethodFactoryTest.java | 41 +++++++++++++++---- .../spoon/test/factory/testclasses/Bar.java | 6 +++ 2 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 src/test/java/spoon/test/factory/testclasses/Bar.java diff --git a/src/test/java/spoon/test/factory/MethodFactoryTest.java b/src/test/java/spoon/test/factory/MethodFactoryTest.java index b699ddebf15..61470f9ea6c 100644 --- a/src/test/java/spoon/test/factory/MethodFactoryTest.java +++ b/src/test/java/spoon/test/factory/MethodFactoryTest.java @@ -4,24 +4,25 @@ import spoon.Launcher; import spoon.reflect.declaration.CtClass; import spoon.reflect.declaration.CtMethod; +import spoon.reflect.factory.Factory; import spoon.reflect.factory.MethodFactory; import spoon.reflect.reference.CtExecutableReference; +import spoon.reflect.reference.CtTypeReference; +import spoon.test.factory.testclasses.Bar; + +import java.lang.reflect.Method; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.CoreMatchers.is; public class MethodFactoryTest { - private static final String TEST_CLASS_STRING = - "class TestClass { " + - "private void foo() { }" + - "}"; - @Test public void testCreateReference() { // contract: createReference creates a method reference of the foo method - CtClass testClass = Launcher.parseClass(TEST_CLASS_STRING); + Factory factory = new Launcher().getFactory(); + CtClass testClass = factory.Class().get(Bar.class); CtMethod foo = testClass.getMethodsByName("foo").get(0); CtExecutableReference expectedReference = testClass.getMethod("foo").getReference(); MethodFactory methodFactory = testClass.getFactory().Method(); @@ -31,4 +32,30 @@ public void testCreateReference() { assertThat(actualCreatedReference, is(expectedReference)); } -} + + @Test + public void testCreateReferenceWithActualMethod() throws ClassNotFoundException, NoSuchMethodException { + // contract: createReference creates a method reference of a actual method foo + + // arrange + Factory factory = new Launcher().getFactory(); + Class testClass = Class.forName("spoon.test.factory.testclasses.Bar"); + Method testMethod = testClass.getMethod("foo"); + + CtExecutableReference expectedReference = factory.createExecutableReference(); + expectedReference.setSimpleName("foo"); + CtTypeReference ctTypeReference = factory.Type().createReference(Bar.class); + expectedReference.setDeclaringType(ctTypeReference); + CtTypeReference typeReference = factory.Type().createReference(testMethod.getReturnType()); +// expectedReference.setType(typeReference); + + MethodFactory methodFactory = factory.Method(); + CtExecutableReference actualCreatedReference = null; + + // act + actualCreatedReference = methodFactory.createReference(testMethod); + + // assert + assertThat(actualCreatedReference, is(expectedReference)); + } +} \ No newline at end of file diff --git a/src/test/java/spoon/test/factory/testclasses/Bar.java b/src/test/java/spoon/test/factory/testclasses/Bar.java new file mode 100644 index 00000000000..d4988534afa --- /dev/null +++ b/src/test/java/spoon/test/factory/testclasses/Bar.java @@ -0,0 +1,6 @@ +package spoon.test.factory.testclasses; + +public class Bar { + + public void foo() { } +} \ No newline at end of file From 7e74ce50f5afc9aeb2969ee4437e30c3907c3ede Mon Sep 17 00:00:00 2001 From: Rohitesh Date: Mon, 9 Aug 2021 16:53:13 +0530 Subject: [PATCH 3/4] setType of the expectedRerence --- .../java/spoon/test/factory/MethodFactoryTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/spoon/test/factory/MethodFactoryTest.java b/src/test/java/spoon/test/factory/MethodFactoryTest.java index 61470f9ea6c..029ee359707 100644 --- a/src/test/java/spoon/test/factory/MethodFactoryTest.java +++ b/src/test/java/spoon/test/factory/MethodFactoryTest.java @@ -38,23 +38,23 @@ public void testCreateReferenceWithActualMethod() throws ClassNotFoundException, // contract: createReference creates a method reference of a actual method foo // arrange - Factory factory = new Launcher().getFactory(); + Launcher launcher = new Launcher(); + Factory factory = launcher.getFactory(); Class testClass = Class.forName("spoon.test.factory.testclasses.Bar"); Method testMethod = testClass.getMethod("foo"); - CtExecutableReference expectedReference = factory.createExecutableReference(); + CtExecutableReference expectedReference = factory.createExecutableReference(); expectedReference.setSimpleName("foo"); CtTypeReference ctTypeReference = factory.Type().createReference(Bar.class); expectedReference.setDeclaringType(ctTypeReference); - CtTypeReference typeReference = factory.Type().createReference(testMethod.getReturnType()); -// expectedReference.setType(typeReference); + expectedReference.setType(launcher.getFactory().Type().voidPrimitiveType()); MethodFactory methodFactory = factory.Method(); CtExecutableReference actualCreatedReference = null; // act actualCreatedReference = methodFactory.createReference(testMethod); - + // assert assertThat(actualCreatedReference, is(expectedReference)); } From ec2d3170a6ec1e32c9e98d1d4be3e35f61a32c21 Mon Sep 17 00:00:00 2001 From: Rohitesh Date: Mon, 9 Aug 2021 21:32:08 +0530 Subject: [PATCH 4/4] move test resource from conflicting directory --- src/test/java/spoon/test/factory/MethodFactoryTest.java | 4 ++-- .../spoon/test/factory/{testclasses => testclasses4}/Bar.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename src/test/java/spoon/test/factory/{testclasses => testclasses4}/Bar.java (53%) diff --git a/src/test/java/spoon/test/factory/MethodFactoryTest.java b/src/test/java/spoon/test/factory/MethodFactoryTest.java index 029ee359707..f7147235f43 100644 --- a/src/test/java/spoon/test/factory/MethodFactoryTest.java +++ b/src/test/java/spoon/test/factory/MethodFactoryTest.java @@ -8,7 +8,7 @@ import spoon.reflect.factory.MethodFactory; import spoon.reflect.reference.CtExecutableReference; import spoon.reflect.reference.CtTypeReference; -import spoon.test.factory.testclasses.Bar; +import spoon.test.factory.testclasses4.Bar; import java.lang.reflect.Method; @@ -40,7 +40,7 @@ public void testCreateReferenceWithActualMethod() throws ClassNotFoundException, // arrange Launcher launcher = new Launcher(); Factory factory = launcher.getFactory(); - Class testClass = Class.forName("spoon.test.factory.testclasses.Bar"); + Class testClass = Class.forName("spoon.test.factory.testclasses4.Bar"); Method testMethod = testClass.getMethod("foo"); CtExecutableReference expectedReference = factory.createExecutableReference(); diff --git a/src/test/java/spoon/test/factory/testclasses/Bar.java b/src/test/java/spoon/test/factory/testclasses4/Bar.java similarity index 53% rename from src/test/java/spoon/test/factory/testclasses/Bar.java rename to src/test/java/spoon/test/factory/testclasses4/Bar.java index d4988534afa..b3d6520a052 100644 --- a/src/test/java/spoon/test/factory/testclasses/Bar.java +++ b/src/test/java/spoon/test/factory/testclasses4/Bar.java @@ -1,4 +1,4 @@ -package spoon.test.factory.testclasses; +package spoon.test.factory.testclasses4; public class Bar {