package org.computelab.config;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.List;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.junit.Test;

public class DefaultConfigBuilderTest {

    @Test
    public void testConstructor() throws IllegalAccessException {
        final String appName = getClass().getSimpleName();
        final DefaultConfigBuilder builder = new DefaultConfigBuilder(appName);
        assertEquals(appName, FieldUtils.readField(
                builder, "appName", true).toString());
        assertEquals("app.properties", FieldUtils.readField(
                builder, "configFile", true).toString());
    }

    @Test(expected=NullPointerException.class)
    public void constructorAppNameCannotBeNull() {
        new DefaultConfigBuilder(null, "configFile");
    }

    @Test(expected=IllegalArgumentException.class)
    public void constructorAppNameCannotBeEmpty() {
        new DefaultConfigBuilder("", "configFile");
    }

    @Test(expected=NullPointerException.class)
    public void constructorConfigFileCannotBeNull() {
        new DefaultConfigBuilder("app", null);
    }

    @Test(expected=IllegalArgumentException.class)
    public void constructorConfigFileCannotBeEmpty() {
        new DefaultConfigBuilder("app", "");
    }

    @SuppressWarnings("unchecked")
    @Test
    public void testBuild() throws IllegalAccessException {
        final String appName = getClass().getSimpleName();
        final DefaultConfigBuilder builder = new DefaultConfigBuilder(appName);
        final Config config = builder.build();
        assertTrue(config instanceof ConfigChain);
        final List<Config> configs = (List<Config>)FieldUtils.readField(config, "configs", true);
        assertNotNull(configs);
        assertEquals(2, configs.size());
        assertTrue(configs.get(0) instanceof SystemPropertyConfig);
        assertTrue(configs.get(1) instanceof SystemEnvConfig);
    }
}

@@ -260,16 +260,47 @@
 .Test;%0A%0A
+@SuppressWarnings(%22unchecked%22)%0A
 public c
@@ -1418,43 +1418,8 @@
  %7D%0A%0A
-    @SuppressWarnings(%22unchecked%22)%0A
     
@@ -1956,24 +1956,24 @@
 rtyConfig);%0A
-
         asse
@@ -2028,11 +2028,904 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testBuildWithResource() throws IllegalAccessException %7B%0A        final String appName = getClass().getSimpleName();%0A        final DefaultConfigBuilder builder = new DefaultConfigBuilder(appName, %22test.properties%22);%0A        assertEquals(%22test.properties%22, FieldUtils.readField(%0A                builder, %22configFile%22, true).toString());%0A        final Config config = builder.build();%0A        assertTrue(config instanceof ConfigChain);%0A        final List%3CConfig%3E configs = (List%3CConfig%3E)FieldUtils.readField(config, %22configs%22, true);%0A        assertNotNull(configs);%0A        assertEquals(3, configs.size());%0A        assertTrue(configs.get(0) instanceof SystemPropertyConfig);%0A        assertTrue(configs.get(1) instanceof SystemEnvConfig);%0A        assertTrue(configs.get(2) instanceof PropertiesConfig);%0A        assertEquals(123, config.getAsInt(%22sp.provider.id%22));%0A    %7D
 %0A%7D%0A

Test config from resource