Skip to content

Failing test for ModuleOrdering that skips the extra test module(s) #760

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

Merged
merged 7 commits into from
Jan 13, 2025
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.example.myapp.other;

import io.avaje.inject.test.InjectTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

@InjectTest
public class InjectTestOnlyComponentTest {

@Inject WireOther myTestOnlyComponent;
@Inject WireOther2 myTestOnlyComponent2;
@Inject WireOther3 withBeanScope;

@Test
void test() {
assertThat(myTestOnlyComponent).isNotNull();
assertThat(myTestOnlyComponent.component).isNotNull();
assertThat(myTestOnlyComponent.plugin).isNotNull();

assertThat(myTestOnlyComponent2).isNotNull();
assertThat(myTestOnlyComponent2.component).isNotNull();
assertThat(myTestOnlyComponent2.plugin).isNotNull();

assertThat(withBeanScope).isNotNull();
assertThat(withBeanScope.beanScope).isNotNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.example.myapp.other;

import io.avaje.inject.PostConstruct;
import jakarta.inject.Singleton;
import org.example.external.aspect.PluginProvidedClass;
import org.other.one.OtherComponent;

@Singleton
public class WireOther2 {
OtherComponent component;
PluginProvidedClass plugin;

@PostConstruct
public void postConstruct(OtherComponent component, PluginProvidedClass plugin) {
this.component = component;
this.plugin = plugin;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.example.myapp.other;

import io.avaje.inject.BeanScope;
import io.avaje.inject.PostConstruct;
import jakarta.inject.Singleton;
import org.example.external.aspect.PluginProvidedClass;
import org.other.one.OtherComponent;

@Singleton
public class WireOther3 {
BeanScope beanScope;

@PostConstruct
public void postConstruct(BeanScope beanScope) {
this.beanScope = beanScope;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,17 @@ private void writeStartClass() {

private void writeBuildMethods() {
writer.append(
"\n"
"\n"
+ " @Override\n"
+ " public boolean supportsExpected(List<AvajeModule> modules) {\n"
+ " if (modules.size() != sortedModules.length) {\n"
+ " return false;\n"
+ " }\n"
+ " return modules.stream()\n"
+ " .map(m -> m.getClass().getTypeName())\n"
+ " .allMatch(k -> INDEXES.containsKey(k));\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public List<AvajeModule> factories() {\n"
+ " return List.of(sortedModules);\n"
Expand Down
15 changes: 6 additions & 9 deletions inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@

import java.lang.System.Logger.Level;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Supplier;

Expand Down Expand Up @@ -257,10 +251,13 @@ public BeanScope build() {

// sort factories by dependsOn
ModuleOrdering factoryOrder = new FactoryOrder(parent, includeModules, !suppliedBeans.isEmpty());
var modules = serviceLoader.modules();
if (factoryOrder.isEmpty()) {
// prefer generated ModuleOrdering if provided
factoryOrder = serviceLoader.moduleOrdering().orElse(factoryOrder);
serviceLoader.modules().forEach(factoryOrder::add);
factoryOrder = serviceLoader.moduleOrdering()
.filter(o -> o.supportsExpected(modules))
.orElse(factoryOrder);
modules.forEach(factoryOrder::add);
}

final var moduleNames = factoryOrder.orderModules();
Expand Down
16 changes: 12 additions & 4 deletions inject/src/main/java/io/avaje/inject/spi/ModuleOrdering.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
*/
public interface ModuleOrdering extends InjectExtension {

/**
* Return true if ordering supports the modules passed in.
*/
default boolean supportsExpected(List<AvajeModule> modules) {
return true;
}

/**
* Accept a module for ordering
*/
void add(AvajeModule module);

/**
* Order the factories, returning the ordered list of module names.
*/
Expand All @@ -23,8 +35,4 @@ public interface ModuleOrdering extends InjectExtension {
*/
boolean isEmpty();

/**
* Accept a module for ordering
*/
void add(AvajeModule module);
}
Loading