-
Notifications
You must be signed in to change notification settings - Fork 29
Add the Ability to Pick Test Modules #1063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SentryMan
wants to merge
5
commits into
master
Choose a base branch
from
picking-test-modules
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7cfe900
Add the ability to pick test modules
SentryMan ae491bb
Merge branch 'master' into picking-test-modules
SentryMan abe3d4e
Merge branch 'master' into picking-test-modules
SentryMan 6c01849
Pr comments
SentryMan 216d5b7
Merge branch 'master' into picking-test-modules
SentryMan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
blackbox-multi-scope/src/test/java/org/multi/main/BoundedCustomModuleTest.java
This file contains hidden or 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,24 @@ | ||
| package org.multi.main; | ||
|
|
||
| import io.avaje.inject.test.InjectTest; | ||
| import jakarta.inject.Inject; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.multi.parents.BeanIn1; | ||
| import org.multi.parents.Mod1Module; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
|
||
| /** | ||
| * Bound the wiring to a custom scope module. Custom scope modules are not registered as an {@code | ||
| * InjectExtension} service, so this covers the reflection fallback in {@code SelectedModules}. | ||
| */ | ||
| @InjectTest(modules = Mod1Module.class) | ||
| class BoundedCustomModuleTest { | ||
|
|
||
| @Inject BeanIn1 beanIn1; | ||
|
|
||
| @Test | ||
| void onlyTheSelectedModuleIsWired() { | ||
| assertNotNull(beanIn1); | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
blackbox-test-inject/src/test/java/org/example/myapp/modules/BoundedModulesStaticTest.java
This file contains hidden or 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,31 @@ | ||
| package org.example.myapp.modules; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import org.example.external.aspect.sub.ExampleExternalAspectModule; | ||
| import org.example.myapp.HelloService; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import io.avaje.inject.aop.MethodInterceptor; | ||
| import io.avaje.inject.test.InjectTest; | ||
| import jakarta.inject.Inject; | ||
|
|
||
| /** | ||
| * The selected modules must also bound the class level BeanScope, which is built without a test | ||
| * instance - so {@code MetaInfo} has to read them from the test class. | ||
| */ | ||
| @InjectTest(modules = ExampleExternalAspectModule.class) | ||
| class BoundedModulesStaticTest { | ||
|
|
||
| @Inject static MethodInterceptor interceptor; | ||
|
|
||
| @Inject static Optional<HelloService> helloService; | ||
|
|
||
| @Test | ||
| void classLevelScopeIsAlsoBounded() { | ||
| assertThat(interceptor).isNotNull(); | ||
| assertThat(helloService).isEmpty(); | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
blackbox-test-inject/src/test/java/org/example/myapp/modules/BoundedModulesTest.java
This file contains hidden or 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,51 @@ | ||
| package org.example.myapp.modules; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import org.example.external.aspect.sub.ExampleExternalAspectModule; | ||
| import org.example.myapp.HelloData; | ||
| import org.example.myapp.HelloService; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.other.one.OtherComponent; | ||
|
|
||
| import io.avaje.inject.aop.MethodInterceptor; | ||
| import io.avaje.inject.test.InjectTest; | ||
| import jakarta.inject.Inject; | ||
|
|
||
| /** | ||
| * Bound the wiring to a single module. {@code ExampleExternalAspectModule} is registered as an | ||
| * {@code InjectExtension} service so this covers the ServiceLoader path in {@code SelectedModules}. | ||
| */ | ||
| @InjectTest(modules = ExampleExternalAspectModule.class) | ||
| class BoundedModulesTest { | ||
|
|
||
| /** Provided by the selected module. */ | ||
| @Inject MethodInterceptor interceptor; | ||
|
|
||
| /** Provided by the global test scope (parent), so still available. */ | ||
| @Inject HelloData helloData; | ||
|
|
||
| /** Provided by MyappModule, which was not selected. */ | ||
| @Inject Optional<HelloService> helloService; | ||
|
|
||
| /** Provided by OneModule (blackbox-other), which was not selected. */ | ||
| @Inject Optional<OtherComponent> otherComponent; | ||
|
|
||
| @Test | ||
| void selectedModuleIsWired() { | ||
| assertThat(interceptor).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void parentTestScopeIsStillInherited() { | ||
| assertThat(helloData.helloData()).isEqualTo("TestHelloData"); | ||
| } | ||
|
|
||
| @Test | ||
| void unselectedModulesAreNotWired() { | ||
| assertThat(helloService).isEmpty(); | ||
| assertThat(otherComponent).isEmpty(); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
43 changes: 43 additions & 0 deletions
43
inject-test/src/main/java/io/avaje/inject/test/SelectedModules.java
This file contains hidden or 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,43 @@ | ||
| package io.avaje.inject.test; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.ServiceLoader; | ||
| import io.avaje.inject.spi.AvajeModule; | ||
| import io.avaje.inject.spi.InjectExtension; | ||
|
|
||
| /** Resolves the {@code AvajeModule} instances for {@code @InjectTest(modules = ...)}. */ | ||
| final class SelectedModules { | ||
|
|
||
| private static final AvajeModule[] EMPTY = {}; | ||
|
|
||
| private SelectedModules() {} | ||
|
|
||
| /** Return the module instances for the given module classes. */ | ||
| static AvajeModule[] instances(Class<? extends AvajeModule>[] moduleClasses) { | ||
| final var wanted = new LinkedHashSet<>(List.of(moduleClasses)); | ||
| final List<AvajeModule> modules = new ArrayList<>(wanted.size()); | ||
| ServiceLoader.load(InjectExtension.class).stream() | ||
| .filter(provider -> wanted.contains(provider.type())) | ||
| .forEach( | ||
| provider -> { | ||
| wanted.remove(provider.type()); | ||
| modules.add((AvajeModule) provider.get()); | ||
| }); | ||
| // fall back to reflection if the module is not registered as a service. | ||
| for (var cls : wanted) { | ||
| modules.add(newInstance(cls)); | ||
| } | ||
| return modules.toArray(EMPTY); | ||
| } | ||
|
|
||
| private static AvajeModule newInstance(Class<? extends AvajeModule> cls) { | ||
| try { | ||
| return cls.getDeclaredConstructor().newInstance(); | ||
| } catch (ReflectiveOperationException e) { | ||
| throw new IllegalStateException( | ||
| "Failed to create @InjectTest(modules = " + cls.getTypeName() + ")", e); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.