@Test
 public void testDetectsAbstractMethod() throws Exception {
   ConfigurationObjectFactory c = new ConfigurationObjectFactory(new Properties());
   try {
     c.build(Config4.class);
     fail("Expected exception due to abstract method without @Config annotation");
   } catch (AbstractMethodError e) {
   }
 }
 @Test
 public void testMissingDefault() throws Exception {
   ConfigurationObjectFactory c = new ConfigurationObjectFactory(new Properties());
   try {
     c.build(Config3.class);
     fail("Expected exception due to missing value");
   } catch (Exception e) {
   }
 }
  @Test
  public void testProvidedOverridesDefault() throws Exception {
    ConfigurationObjectFactory c =
        new ConfigurationObjectFactory(
            new Properties() {
              {
                setProperty("option", "provided");
              }
            });

    Config2 config = c.build(Config2.class);
    assertEquals(config.getOption(), "provided");
  }
 @Test
 public void testFoo() throws Exception {
   ConfigurationObjectFactory c =
       new ConfigurationObjectFactory(
           new Properties() {
             {
               setProperty("hello", "world");
               setProperty("theValue", "value");
             }
           });
   Thing t = c.build(Thing.class);
   assertEquals(t.getName(), "world");
 }
 @Test
 public void testMultiParameters() throws Exception {
   ConfigurationObjectFactory c =
       new ConfigurationObjectFactory(
           new Properties() {
             {
               setProperty("another-option.one.a", "1-x");
               setProperty("another-option.two.b", "2-y");
             }
           });
   EnumeratedConfig1 t = c.build(EnumeratedConfig1.class);
   assertEquals(t.getStringOption2Types(ConfigEnum.ONE, "a"), "1-x");
   assertEquals(t.getStringOption2Types(ConfigEnum.TWO, "b"), "2-y");
   assertEquals(t.getStringOption2Types(ConfigEnum.ONE, "dummy"), "default");
 }
 @Test
 public void testEnum() throws Exception {
   ConfigurationObjectFactory c =
       new ConfigurationObjectFactory(
           new Properties() {
             {
               setProperty("option.one", "1");
               setProperty("option.two", "2");
             }
           });
   EnumeratedConfig1 t = c.build(EnumeratedConfig1.class);
   assertEquals(t.getStringOption(ConfigEnum.ONE), "1");
   assertEquals(t.getStringOption(ConfigEnum.TWO), "2");
   assertEquals(t.getStringOption(ConfigEnum.THREE), "default");
 }
  @Test
  public void testDays() {
    ClassWithDays ec = cof.build(ClassWithDays.class);

    Assert.assertEquals(5, ec.getValue().getPeriod());
    Assert.assertEquals(TimeUnit.DAYS, ec.getValue().getUnit());
    Assert.assertEquals(new TimeSpan(5, TimeUnit.DAYS), ec.getValue());
    Assert.assertEquals(TimeUnit.MILLISECONDS.convert(5, TimeUnit.DAYS), ec.getValue().getMillis());
  }
  @Test
  public void testMilliSeconds() {
    ClassWithMilliseconds ec = cof.build(ClassWithMilliseconds.class);

    Assert.assertEquals(5, ec.getValue().getPeriod());
    Assert.assertEquals(TimeUnit.MILLISECONDS, ec.getValue().getUnit());
    Assert.assertEquals(new TimeSpan(5, TimeUnit.MILLISECONDS), ec.getValue());
    Assert.assertEquals(5, ec.getValue().getMillis());
  }
  @Test
  public void testCompleteReplacement() throws Exception {
    Map<String, String> replacementsMap = new HashMap<String, String>();
    replacementsMap.put("type", "first");
    ConfigurationObjectFactory c =
        new ConfigurationObjectFactory(
            new Properties() {
              {
                setProperty("option.first", "/parent/${type}");
                setProperty("option.second", "/parent/${type}");
              }
            });
    ReplacementConfig1 r = c.buildWithReplacements(ReplacementConfig1.class, replacementsMap);
    assertEquals(r.getStringOption(), "/parent/first");

    replacementsMap.put("type", "second");
    r = c.buildWithReplacements(ReplacementConfig1.class, replacementsMap);
    assertEquals(r.getStringOption(), "/parent/second");
  }
  @Test
  public void testTypes() {
    ConfigurationObjectFactory c =
        new ConfigurationObjectFactory(
            new Properties() {
              {
                setProperty("stringOption", "a string");
                setProperty("booleanOption", "true");
                setProperty("boxedBooleanOption", "true");
                setProperty("byteOption", Byte.toString(Byte.MAX_VALUE));
                setProperty("boxedByteOption", Byte.toString(Byte.MAX_VALUE));
                setProperty("shortOption", Short.toString(Short.MAX_VALUE));
                setProperty("boxedShortOption", Short.toString(Short.MAX_VALUE));
                setProperty("integerOption", Integer.toString(Integer.MAX_VALUE));
                setProperty("boxedIntegerOption", Integer.toString(Integer.MAX_VALUE));
                setProperty("longOption", Long.toString(Long.MAX_VALUE));
                setProperty("boxedLongOption", Long.toString(Long.MAX_VALUE));
                setProperty("floatOption", Float.toString(Float.MAX_VALUE));
                setProperty("boxedFloatOption", Float.toString(Float.MAX_VALUE));
                setProperty("doubleOption", Double.toString(Double.MAX_VALUE));
                setProperty("boxedDoubleOption", Double.toString(Double.MAX_VALUE));
              }
            });

    Config1 config = c.build(Config1.class);
    assertEquals("a string", config.getStringOption());
    assertEquals(true, config.getBooleanOption());
    assertEquals(Boolean.TRUE, config.getBoxedBooleanOption());
    assertEquals(Byte.MAX_VALUE, config.getByteOption());
    assertEquals(Byte.valueOf(Byte.MAX_VALUE), config.getBoxedByteOption());
    assertEquals(Short.MAX_VALUE, config.getShortOption());
    assertEquals(Short.valueOf(Short.MAX_VALUE), config.getBoxedShortOption());
    assertEquals(Integer.MAX_VALUE, config.getIntegerOption());
    assertEquals(Integer.valueOf(Integer.MAX_VALUE), config.getBoxedIntegerOption());
    assertEquals(Long.MAX_VALUE, config.getLongOption());
    assertEquals(Long.valueOf(Long.MAX_VALUE), config.getBoxedLongOption());
    assertEquals(Float.MAX_VALUE, config.getFloatOption(), 0);
    assertEquals(Float.valueOf(Float.MAX_VALUE), config.getBoxedFloatOption());
    assertEquals(Double.MAX_VALUE, config.getDoubleOption(), 0);
    assertEquals(Double.valueOf(Double.MAX_VALUE), config.getBoxedDoubleOption());
  }
Example #11
0
  // for [Issue-6]
  @Test
  public void testWhitespace() {
    ClassWithTimespanWithWhitespace ec = cof.build(ClassWithTimespanWithWhitespace.class);
    // "5 h"
    Assert.assertEquals(5, ec.getValue().getPeriod());
    Assert.assertEquals(TimeUnit.HOURS, ec.getValue().getUnit());
    Assert.assertEquals(new TimeSpan(5, TimeUnit.HOURS), ec.getValue());
    Assert.assertEquals(
        TimeUnit.MILLISECONDS.convert(5, TimeUnit.HOURS), ec.getValue().getMillis());

    Assert.assertEquals(new TimeSpan("5ms"), new TimeSpan("5 milliseconds"));
    Assert.assertEquals(new TimeSpan("5s"), new TimeSpan("5 seconds"));
    Assert.assertEquals(new TimeSpan("5m"), new TimeSpan("5 minutes"));
    Assert.assertEquals(new TimeSpan("5d"), new TimeSpan("5 days"));
  }
  @Test
  public void testMultiplePropertyNameReplacements() throws Exception {
    ConfigurationObjectFactory c =
        new ConfigurationObjectFactory(
            new Properties() {
              {
                setProperty("another-option.a.1", "A1");
                setProperty("another-option.a.2", "A2");
                setProperty("another-option.b.1", "B1");
                setProperty("another-option.b.2", "B2");
              }
            });

    ReplacementConfig1 r;
    Map<String, String> replacementsMap = new HashMap<String, String>();

    replacementsMap.put("type", "a");
    replacementsMap.put("s", "1");
    r = c.buildWithReplacements(ReplacementConfig1.class, replacementsMap);
    assertEquals(r.getStringOption2Types(), "A1");

    replacementsMap.put("type", "a");
    replacementsMap.put("s", "2");
    r = c.buildWithReplacements(ReplacementConfig1.class, replacementsMap);
    assertEquals(r.getStringOption2Types(), "A2");

    replacementsMap.put("type", "b");
    replacementsMap.put("s", "1");
    r = c.buildWithReplacements(ReplacementConfig1.class, replacementsMap);
    assertEquals(r.getStringOption2Types(), "B1");

    replacementsMap.put("type", "b");
    replacementsMap.put("s", "2");
    r = c.buildWithReplacements(ReplacementConfig1.class, replacementsMap);
    assertEquals(r.getStringOption2Types(), "B2");
  }
Example #13
0
 @Test(expected = IllegalArgumentException.class)
 public void testIllegalUnit() {
   cof.build(ClassWithTimespanWithIllegalUnit.class);
 }
 @Test
 public void testDefaultViaImpl() throws Exception {
   ConfigurationObjectFactory c = new ConfigurationObjectFactory(new Properties());
   Config2 config = c.build(Config2.class);
   assertEquals(config.getOption(), "default");
 }
 @Test
 public void testDefaultValue() throws Exception {
   ConfigurationObjectFactory c = new ConfigurationObjectFactory(new Properties());
   Thing t = c.build(Thing.class);
   assertEquals(t.getName(), "woof");
 }