-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
di/src/test/java/org/panda_lang/utilities/inject/MethodsInvocationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.panda_lang.utilities.inject; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class MethodsInvocationTest { | ||
|
||
private static class TestClass { | ||
|
||
private String testString; | ||
private int testInt = 0; | ||
|
||
public void testMethod() { | ||
this.testString = "test"; | ||
} | ||
|
||
@TestAnnotation | ||
public void annotatedMethod() { | ||
this.testInt += 2; | ||
} | ||
|
||
@TestAnnotation | ||
public void annotatedMethod(int value) { | ||
this.testInt *= value; | ||
} | ||
|
||
@TestAnnotation2 | ||
public void annotatedMethod2() { | ||
throw new IllegalStateException("This method should not be invoked"); | ||
} | ||
|
||
public void failMethod() { | ||
throw new RuntimeException(); | ||
} | ||
|
||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
private @interface TestAnnotation { } | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
private @interface TestAnnotation2 {} | ||
|
||
@Test | ||
void shouldInvokeMethods() throws NoSuchMethodException { | ||
Injector injector = DependencyInjection.createInjector(resources -> { | ||
resources.on(int.class).assignInstance(3); | ||
}); | ||
|
||
TestClass testClass = new TestClass(); | ||
injector.invokeMethod(TestClass.class.getMethod("testMethod"), testClass); | ||
assertEquals("test", testClass.testString); | ||
|
||
injector.invokeAnnotatedMethods(TestAnnotation.class, testClass); | ||
assertEquals(6, testClass.testInt); | ||
} | ||
|
||
@Test | ||
void shouldNotInvokeMethods() throws NoSuchMethodException { | ||
Injector injector = DependencyInjection.createInjector(resources -> { | ||
resources.on(int.class).assignInstance(3); | ||
}); | ||
|
||
TestClass testClass = new TestClass(); | ||
assertThrows(DependencyInjectionException.class, () -> injector.invokeMethod(TestClass.class.getMethod("failMethod"), testClass)); | ||
} | ||
|
||
} |