Advertisement
tpeierls

Rocoto tests

Jan 21st, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.26 KB | None | 0 0
  1. package net.peierls.rocoto.example;
  2.  
  3. import com.google.inject.Guice;
  4.  
  5. import javax.inject.Inject;
  6. import javax.inject.Named;
  7.  
  8. import org.junit.*;
  9. import static org.junit.Assert.assertEquals;
  10.  
  11. import org.nnsoft.guice.rocoto.Rocoto;
  12. import org.nnsoft.guice.rocoto.configuration.ConfigurationModule;
  13.  
  14. /**
  15.  * JUnit 4 tests to verify Rocoto behavior. Requires Guice 3.0+.
  16.  */
  17. public class RocotoTest {
  18.  
  19.     static class AModule extends ConfigurationModule {
  20.         @Override protected void bindConfigurations() {
  21.             bindProperty("aprop").toValue("avalue");
  22.         }
  23.     }
  24.  
  25.     static class BModule extends ConfigurationModule {
  26.         @Override protected void bindConfigurations() {
  27.             bindProperty("bprop").toValue("bvalue=${aprop|default}");
  28.         }
  29.     }
  30.  
  31.     static class CModule extends ConfigurationModule {
  32.         @Override protected void bindConfigurations() {
  33.             bindProperty("cprop").toValue("${nodef|${aprop}}");
  34.         }
  35.     }
  36.  
  37.     static class Injected {
  38.         @Inject @Named("aprop") String avalue;
  39.         @Inject @Named("bprop") String bvalue;
  40.     }
  41.  
  42.     static class AnotherInjected {
  43.         @Inject @Named("cprop") String cvalue;
  44.     }
  45.  
  46.     @Test public void testSimpleProperty() throws Exception {
  47.         Injected injected = Guice.createInjector(
  48.             Rocoto.expandVariables(new AModule()),
  49.             new BModule()
  50.         ).getInstance(Injected.class);
  51.         // AModule wrapped, so it expands aprop.
  52.         assertEquals("avalue", injected.avalue);
  53.     }
  54.  
  55.     @Test public void testUnexpandedProperty() throws Exception {
  56.         Injected injected = Guice.createInjector(
  57.             Rocoto.expandVariables(new AModule()),
  58.             new BModule()
  59.         ).getInstance(Injected.class);
  60.         // BModule not wrapped, so it does no expansion at all.
  61.         assertEquals("bvalue=${aprop|default}", injected.bvalue);
  62.     }
  63.  
  64.     @Test public void testDefaultProperty() throws Exception {
  65.         Injected injected = Guice.createInjector(
  66.             Rocoto.expandVariables(new AModule()),
  67.             Rocoto.expandVariables(new BModule())
  68.         ).getInstance(Injected.class);
  69.         // Each module wrapped individually, so bprop binding doesn't
  70.         // know about aprop.
  71.         assertEquals("bvalue=default", injected.bvalue);
  72.     }
  73.  
  74.     @Test public void testCrossExpandedProperty() throws Exception {
  75.         Injected injected = Guice.createInjector(Rocoto.expandVariables(
  76.             Rocoto.expandVariables(new AModule()),
  77.             Rocoto.expandVariables(new BModule())
  78.         )).getInstance(Injected.class);
  79.         // Wrapping both expanded modules in another call to expandVariables
  80.         // doesn't resolve bprop any differently.
  81.         assertEquals("bvalue=default", injected.bvalue);
  82.     }
  83.  
  84.     @Test public void testExpandedProperty() throws Exception {
  85.         Injected injected = Guice.createInjector(Rocoto.expandVariables(
  86.             new AModule(),
  87.             new BModule()
  88.         )).getInstance(Injected.class);
  89.         // Now bprop knows about aprop binding.
  90.         assertEquals("bvalue=avalue", injected.bvalue);
  91.     }
  92.  
  93.     @Test public void testSingleExpansion() throws Exception {
  94.         AnotherInjected injected = Guice.createInjector(Rocoto.expandVariables(
  95.             new AModule(),
  96.             new CModule()
  97.         )).getInstance(AnotherInjected.class);
  98.         // Default value is not expanded.
  99.         assertEquals("${aprop}", injected.cvalue);
  100.     }
  101.  
  102.     @Test public void testDoubleExpansion() throws Exception {
  103.         AnotherInjected injected = Guice.createInjector(Rocoto.expandVariables(
  104.             new AModule(),
  105.             Rocoto.expandVariables(new CModule())
  106.         )).getInstance(AnotherInjected.class);
  107.         // Default value *is* expanded.
  108.         assertEquals("avalue", injected.cvalue);
  109.     }
  110.  
  111.     @Test public void testDoubleExpansionAnotherWay() throws Exception {
  112.         AnotherInjected injected = Guice.createInjector(Rocoto.expandVariables(
  113.             Rocoto.expandVariables(
  114.                 new AModule(),
  115.                 new CModule()
  116.             )
  117.         )).getInstance(AnotherInjected.class);
  118.         // Default value is still expanded.
  119.         assertEquals("avalue", injected.cvalue);
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement