-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow conditional components in module
Conditional component in a module, is a method annotated with @provides and @when. The factory machine for this component will only be built if the condition is satisfied.
- Loading branch information
Showing
3 changed files
with
229 additions
and
35 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
restx-factory-testing/src/test/java/restx/factory/conditional/ConditionalTest.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,89 @@ | ||
package restx.factory.conditional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static restx.factory.Factory.LocalMachines.overrideComponents; | ||
import static restx.factory.Factory.LocalMachines.threadLocal; | ||
|
||
|
||
import com.google.common.base.Function; | ||
import com.google.common.collect.Iterables; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.util.Set; | ||
import restx.factory.Factory; | ||
import restx.factory.Name; | ||
import restx.factory.conditional.components.TestModuleWithConditional; | ||
|
||
/** | ||
* Test cases for conditionals. | ||
* | ||
* @author apeyrard | ||
*/ | ||
public class ConditionalTest { | ||
|
||
/** | ||
* ElementsFromConfig component can not be build, because of module TestMandatoryDependency | ||
* which use a missing dependency. | ||
*/ | ||
@BeforeClass | ||
public static void deactivateElementsFromConfig() { | ||
System.setProperty("restx.activation::restx.factory.FactoryMachine::ElementsFromConfig", "false"); | ||
} | ||
|
||
/** | ||
* cleanup state before each test method | ||
*/ | ||
@Before | ||
public void cleanupBefore() { | ||
threadLocal().clear(); | ||
} | ||
|
||
/** | ||
* cleanup state after this test class execution | ||
*/ | ||
@AfterClass | ||
public static void cleanupAfterClass() { | ||
threadLocal().clear(); | ||
} | ||
|
||
@Test | ||
public void should_provide_component_if_condition_is_verified() { | ||
Factory factory = Factory.newInstance(); | ||
Set<TestModuleWithConditional.Pioneer> pioneers = factory.getComponents(TestModuleWithConditional.Pioneer.class); | ||
Iterable<String> names = Iterables.transform(pioneers, new Function<TestModuleWithConditional.Pioneer, String>() { | ||
@Override | ||
public String apply(TestModuleWithConditional.Pioneer pioneer) { | ||
return pioneer.name(); | ||
} | ||
}); | ||
assertThat(names).containsOnly("Marie Currie", "Charles Babbage"); | ||
|
||
overrideComponents().set("period", "all"); | ||
|
||
factory = Factory.newInstance(); | ||
pioneers = factory.getComponents(TestModuleWithConditional.Pioneer.class); | ||
names = Iterables.transform(pioneers, new Function<TestModuleWithConditional.Pioneer, String>() { | ||
@Override | ||
public String apply(TestModuleWithConditional.Pioneer pioneer) { | ||
return pioneer.name(); | ||
} | ||
}); | ||
assertThat(names).containsOnly("Marie Currie", "Charles Babbage", "Alan Turing"); | ||
} | ||
|
||
@Test | ||
public void should_provide_component_if_condition_is_verified_and_use_name_and_priorities() { | ||
Factory factory = Factory.newInstance(); | ||
TestModuleWithConditional.Pioneer pioneer = factory.getComponent(Name.of(TestModuleWithConditional.Pioneer.class, "physics")); | ||
assertThat(pioneer.name()).isEqualTo("Marie Currie"); | ||
|
||
overrideComponents().set("chauvinist", "true"); | ||
|
||
factory = Factory.newInstance(); | ||
pioneer = factory.getComponent(Name.of(TestModuleWithConditional.Pioneer.class, "physics")); | ||
assertThat(pioneer.name()).isEqualTo("Pierre Currie"); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...testing/src/test/java/restx/factory/conditional/components/TestModuleWithConditional.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,59 @@ | ||
package restx.factory.conditional.components; | ||
|
||
import javax.inject.Named; | ||
import restx.factory.Module; | ||
import restx.factory.Provides; | ||
import restx.factory.When; | ||
|
||
/** | ||
* @author apeyrard | ||
*/ | ||
@Module | ||
public class TestModuleWithConditional { | ||
|
||
public static interface Pioneer { | ||
String name(); | ||
} | ||
|
||
@Provides @Named("physics") | ||
public Pioneer currie() { | ||
return new Pioneer() { | ||
@Override | ||
public String name() { | ||
return "Marie Currie"; | ||
} | ||
}; | ||
} | ||
|
||
@Provides(priority = -100) @Named("physics") | ||
@When(name = "chauvinist", value = "true") | ||
public Pioneer pierreCurrie() { | ||
return new Pioneer() { | ||
@Override | ||
public String name() { | ||
return "Pierre Currie"; | ||
} | ||
}; | ||
} | ||
|
||
@Provides | ||
public Pioneer babbage() { | ||
return new Pioneer() { | ||
@Override | ||
public String name() { | ||
return "Charles Babbage"; | ||
} | ||
}; | ||
} | ||
|
||
@Provides | ||
@When(name = "period", value = "all") | ||
public Pioneer turing() { | ||
return new Pioneer() { | ||
@Override | ||
public String name() { | ||
return "Alan Turing"; | ||
} | ||
}; | ||
} | ||
} |
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