Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.peierls.rocoto.example;
- import com.google.inject.Guice;
- import javax.inject.Inject;
- import javax.inject.Named;
- import org.junit.*;
- import static org.junit.Assert.assertEquals;
- import org.nnsoft.guice.rocoto.Rocoto;
- import org.nnsoft.guice.rocoto.configuration.ConfigurationModule;
- /**
- * JUnit 4 tests to verify Rocoto behavior. Requires Guice 3.0+.
- */
- public class RocotoTest {
- static class AModule extends ConfigurationModule {
- @Override protected void bindConfigurations() {
- bindProperty("aprop").toValue("avalue");
- }
- }
- static class BModule extends ConfigurationModule {
- @Override protected void bindConfigurations() {
- bindProperty("bprop").toValue("bvalue=${aprop|default}");
- }
- }
- static class CModule extends ConfigurationModule {
- @Override protected void bindConfigurations() {
- bindProperty("cprop").toValue("${nodef|${aprop}}");
- }
- }
- static class Injected {
- @Inject @Named("aprop") String avalue;
- @Inject @Named("bprop") String bvalue;
- }
- static class AnotherInjected {
- @Inject @Named("cprop") String cvalue;
- }
- @Test public void testSimpleProperty() throws Exception {
- Injected injected = Guice.createInjector(
- Rocoto.expandVariables(new AModule()),
- new BModule()
- ).getInstance(Injected.class);
- // AModule wrapped, so it expands aprop.
- assertEquals("avalue", injected.avalue);
- }
- @Test public void testUnexpandedProperty() throws Exception {
- Injected injected = Guice.createInjector(
- Rocoto.expandVariables(new AModule()),
- new BModule()
- ).getInstance(Injected.class);
- // BModule not wrapped, so it does no expansion at all.
- assertEquals("bvalue=${aprop|default}", injected.bvalue);
- }
- @Test public void testDefaultProperty() throws Exception {
- Injected injected = Guice.createInjector(
- Rocoto.expandVariables(new AModule()),
- Rocoto.expandVariables(new BModule())
- ).getInstance(Injected.class);
- // Each module wrapped individually, so bprop binding doesn't
- // know about aprop.
- assertEquals("bvalue=default", injected.bvalue);
- }
- @Test public void testCrossExpandedProperty() throws Exception {
- Injected injected = Guice.createInjector(Rocoto.expandVariables(
- Rocoto.expandVariables(new AModule()),
- Rocoto.expandVariables(new BModule())
- )).getInstance(Injected.class);
- // Wrapping both expanded modules in another call to expandVariables
- // doesn't resolve bprop any differently.
- assertEquals("bvalue=default", injected.bvalue);
- }
- @Test public void testExpandedProperty() throws Exception {
- Injected injected = Guice.createInjector(Rocoto.expandVariables(
- new AModule(),
- new BModule()
- )).getInstance(Injected.class);
- // Now bprop knows about aprop binding.
- assertEquals("bvalue=avalue", injected.bvalue);
- }
- @Test public void testSingleExpansion() throws Exception {
- AnotherInjected injected = Guice.createInjector(Rocoto.expandVariables(
- new AModule(),
- new CModule()
- )).getInstance(AnotherInjected.class);
- // Default value is not expanded.
- assertEquals("${aprop}", injected.cvalue);
- }
- @Test public void testDoubleExpansion() throws Exception {
- AnotherInjected injected = Guice.createInjector(Rocoto.expandVariables(
- new AModule(),
- Rocoto.expandVariables(new CModule())
- )).getInstance(AnotherInjected.class);
- // Default value *is* expanded.
- assertEquals("avalue", injected.cvalue);
- }
- @Test public void testDoubleExpansionAnotherWay() throws Exception {
- AnotherInjected injected = Guice.createInjector(Rocoto.expandVariables(
- Rocoto.expandVariables(
- new AModule(),
- new CModule()
- )
- )).getInstance(AnotherInjected.class);
- // Default value is still expanded.
- assertEquals("avalue", injected.cvalue);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement