Skip to content

Commit f9baeb0

Browse files
rbygraveSentryMan
andauthored
Failing test for ModuleOrdering that skips the extra test module(s) (#760)
* Failing test for ModuleOrdering that skips the extra test module(s) Test components in src/test that are not annotated with @TestScope go into an extra module. This module is ignored/skipped by the compiled module ordering. A fix for this is desired. * Add ModuleOrdering.supportsExpected() such that we only use compiled module ordering when ... When it supports the modules that are being used. When it does not then fall back to the default ordering mechanism. * Opps, need to set factoryOrder variable there ... * [test] Add test injecting BeanScope into a test component --------- Co-authored-by: Josiah Noel <[email protected]>
1 parent 0ce53dd commit f9baeb0

File tree

6 files changed

+95
-14
lines changed

6 files changed

+95
-14
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.example.myapp.other;
2+
3+
import io.avaje.inject.test.InjectTest;
4+
import jakarta.inject.Inject;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
@InjectTest
10+
public class InjectTestOnlyComponentTest {
11+
12+
@Inject WireOther myTestOnlyComponent;
13+
@Inject WireOther2 myTestOnlyComponent2;
14+
@Inject WireOther3 withBeanScope;
15+
16+
@Test
17+
void test() {
18+
assertThat(myTestOnlyComponent).isNotNull();
19+
assertThat(myTestOnlyComponent.component).isNotNull();
20+
assertThat(myTestOnlyComponent.plugin).isNotNull();
21+
22+
assertThat(myTestOnlyComponent2).isNotNull();
23+
assertThat(myTestOnlyComponent2.component).isNotNull();
24+
assertThat(myTestOnlyComponent2.plugin).isNotNull();
25+
26+
assertThat(withBeanScope).isNotNull();
27+
assertThat(withBeanScope.beanScope).isNotNull();
28+
}
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.example.myapp.other;
2+
3+
import io.avaje.inject.PostConstruct;
4+
import jakarta.inject.Singleton;
5+
import org.example.external.aspect.PluginProvidedClass;
6+
import org.other.one.OtherComponent;
7+
8+
@Singleton
9+
public class WireOther2 {
10+
OtherComponent component;
11+
PluginProvidedClass plugin;
12+
13+
@PostConstruct
14+
public void postConstruct(OtherComponent component, PluginProvidedClass plugin) {
15+
this.component = component;
16+
this.plugin = plugin;
17+
}
18+
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.example.myapp.other;
2+
3+
import io.avaje.inject.BeanScope;
4+
import io.avaje.inject.PostConstruct;
5+
import jakarta.inject.Singleton;
6+
import org.example.external.aspect.PluginProvidedClass;
7+
import org.other.one.OtherComponent;
8+
9+
@Singleton
10+
public class WireOther3 {
11+
BeanScope beanScope;
12+
13+
@PostConstruct
14+
public void postConstruct(BeanScope beanScope) {
15+
this.beanScope = beanScope;
16+
}
17+
18+
}

inject-generator/src/main/java/io/avaje/inject/generator/SimpleOrderWriter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,17 @@ private void writeStartClass() {
7676

7777
private void writeBuildMethods() {
7878
writer.append(
79-
"\n"
79+
"\n"
80+
+ " @Override\n"
81+
+ " public boolean supportsExpected(List<AvajeModule> modules) {\n"
82+
+ " if (modules.size() != sortedModules.length) {\n"
83+
+ " return false;\n"
84+
+ " }\n"
85+
+ " return modules.stream()\n"
86+
+ " .map(m -> m.getClass().getTypeName())\n"
87+
+ " .allMatch(k -> INDEXES.containsKey(k));\n"
88+
+ " }\n"
89+
+ "\n"
8090
+ " @Override\n"
8191
+ " public List<AvajeModule> factories() {\n"
8292
+ " return List.of(sortedModules);\n"

inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@
66

77
import java.lang.System.Logger.Level;
88
import java.lang.reflect.Type;
9-
import java.util.ArrayList;
10-
import java.util.Arrays;
11-
import java.util.HashMap;
12-
import java.util.LinkedHashSet;
13-
import java.util.List;
14-
import java.util.Map;
15-
import java.util.Set;
9+
import java.util.*;
1610
import java.util.function.Consumer;
1711
import java.util.function.Supplier;
1812

@@ -257,10 +251,13 @@ public BeanScope build() {
257251

258252
// sort factories by dependsOn
259253
ModuleOrdering factoryOrder = new FactoryOrder(parent, includeModules, !suppliedBeans.isEmpty());
254+
var modules = serviceLoader.modules();
260255
if (factoryOrder.isEmpty()) {
261256
// prefer generated ModuleOrdering if provided
262-
factoryOrder = serviceLoader.moduleOrdering().orElse(factoryOrder);
263-
serviceLoader.modules().forEach(factoryOrder::add);
257+
factoryOrder = serviceLoader.moduleOrdering()
258+
.filter(o -> o.supportsExpected(modules))
259+
.orElse(factoryOrder);
260+
modules.forEach(factoryOrder::add);
264261
}
265262

266263
final var moduleNames = factoryOrder.orderModules();

inject/src/main/java/io/avaje/inject/spi/ModuleOrdering.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
*/
99
public interface ModuleOrdering extends InjectExtension {
1010

11+
/**
12+
* Return true if ordering supports the modules passed in.
13+
*/
14+
default boolean supportsExpected(List<AvajeModule> modules) {
15+
return true;
16+
}
17+
18+
/**
19+
* Accept a module for ordering
20+
*/
21+
void add(AvajeModule module);
22+
1123
/**
1224
* Order the factories, returning the ordered list of module names.
1325
*/
@@ -23,8 +35,4 @@ public interface ModuleOrdering extends InjectExtension {
2335
*/
2436
boolean isEmpty();
2537

26-
/**
27-
* Accept a module for ordering
28-
*/
29-
void add(AvajeModule module);
3038
}

0 commit comments

Comments
 (0)