-
-
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
8 changed files
with
113 additions
and
17 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
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
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
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
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
78 changes: 78 additions & 0 deletions
78
di/src/test/java/org/panda_lang/utilities/inject/DependencyInjectionHandlerTest.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,78 @@ | ||
package org.panda_lang.utilities.inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.panda_lang.utilities.inject.annotations.Inject; | ||
import org.panda_lang.utilities.inject.annotations.Injectable; | ||
import org.panda_lang.utilities.inject.annotations.PostConstruct; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class DependencyInjectionHandlerTest { | ||
|
||
@Injectable | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface Custom { | ||
} | ||
|
||
@Injectable | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface AwesomeRandom { | ||
} | ||
|
||
public static class Service1 { | ||
@Inject | ||
@Custom | ||
public String fieldOne; | ||
|
||
@Inject | ||
public int fieldTwo; | ||
|
||
@Inject | ||
@AwesomeRandom | ||
public int fieldThree; | ||
|
||
@PostConstruct | ||
public void construct() { | ||
assertEquals("HelloWorld", this.fieldOne); | ||
|
||
assertEquals(7, this.fieldTwo); | ||
assertTrue(this.fieldTwo >= 2 && this.fieldTwo <= 10); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldCreateInstance() { | ||
Injector injector = DependencyInjection.createInjector(resources -> { | ||
resources.annotatedWith(Custom.class).assignHandler((type, annotation, args) -> "HelloWorld"); | ||
resources.on(int.class).assignHandler((type, annotation, args) -> { | ||
AwesomeRandom randomAnnotation = type.getAnnotation(AwesomeRandom.class); | ||
if (randomAnnotation == null) { | ||
return 7; | ||
} | ||
return ThreadLocalRandom.current().nextInt(2, 10); | ||
}); | ||
}); | ||
|
||
injector.newInstanceWithFields(Service1.class); | ||
} | ||
|
||
public static class Service2 { | ||
@Inject | ||
public String fieldOne; | ||
} | ||
|
||
@Test | ||
void shouldNotCreateInstance() { | ||
Injector injector = DependencyInjection.createInjector(resources -> { | ||
resources.on(String.class).assignThrowingHandler((type, annotation, args) -> { | ||
throw new Exception("Failed"); | ||
}); | ||
}); | ||
assertThrows(DependencyInjectionException.class, () -> injector.newInstanceWithFields(Service2.class)); | ||
} | ||
|
||
} |
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
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