/*
 * Copyright (c) 2013, Luigi R. Viggiano
 * All rights reserved.
 *
 * This software is distributable under the BSD license.
 * See the terms of the BSD license in the documentation provided with this software.
 */

package org.aeonbits.owner.importedprops;

import org.aeonbits.owner.Config;
import org.aeonbits.owner.ConfigFactory;
import org.junit.Test;

import java.util.Properties;

import static org.junit.Assert.assertEquals;

/**
 * @author Luigi R. Viggiano
 */
public class WithImportedPropertiesTest {

    @Test
    public void testSubstituteWithImports() {
        Properties propsFromTest = new Properties();
        propsFromTest.setProperty("external", "propsFromTest");
        WithImportedProperties conf = ConfigFactory.create
                (WithImportedProperties.class, propsFromTest);
        assertEquals("testing replacement from propsFromTest properties file.", conf.someValue());
    }

    @Test
    public void testSystemProperty() {
        String userHome = System.getProperty("user.home");
        WithImportedProperties conf = ConfigFactory.create(WithImportedProperties.class, System.getProperties());
        assertEquals(userHome, conf.userHome());
    }

    @Test
    public void testSystemEnv() {
        String envHome = System.getenv("HOME");
        WithImportedProperties conf = ConfigFactory.create(WithImportedProperties.class, System.getenv());
        assertEquals(envHome, conf.envHome());
    }

    @Test
    public void testMultipleImports() {
        Properties propsFromTest = new Properties();
        propsFromTest.setProperty("external", "propsFromTest");

	    String winPath = "C:\\windows\\path";
	    System.setProperty("value.with.backslash", winPath);
        String userHome = System.getProperty("user.home");
        String envHome = System.getenv("HOME");
        WithImportedProperties conf =
                ConfigFactory.create(WithImportedProperties.class,
                        propsFromTest, System.getProperties(), System.getenv());
        assertEquals(userHome, conf.userHome());
        assertEquals(envHome, conf.envHome());
	    assertEquals(winPath, conf.valueWithBackslash());
        assertEquals("testing replacement from propsFromTest properties file.", conf.someValue());
    }

    /**
     * @author luigi
     */
    public static interface WithImportedProperties extends Config {
        String someValue();

        @DefaultValue("${user.home}")
        String userHome();

        @DefaultValue("${HOME}")
        String envHome();

	    @DefaultValue("${value.with.backslash}")
	    String valueWithBackslash();
    }
}

@@ -1614,109 +1614,8 @@
 );%0A%0A
-%09    String winPath = %22C:%5C%5Cwindows%5C%5Cpath%22;%0A%09    System.setProperty(%22value.with.backslash%22, winPath);%0A
     
@@ -2003,63 +2003,8 @@
 ));%0A
-%09    assertEquals(winPath, conf.valueWithBackslash());%0A
     
@@ -2113,40 +2113,884 @@
     
-/**%0A     * @author luigi%0A     */
+@Test%0A    public void testBackSlash() %7B%0A        Properties propsFromTest = new Properties();%0A        propsFromTest.setProperty(%22external%22, %22propsFromTest%22);%0A        String winPath = %22C:%5C%5Cwindows%5C%5Cpath%22;%0A        propsFromTest.setProperty(%22value.with.backslash%22, winPath);%0A%0A        WithImportedProperties conf =%0A                ConfigFactory.create(WithImportedProperties.class,%0A                        propsFromTest);%0A%0A        assertEquals(winPath, conf.valueWithBackslash());%0A    %7D%0A%0A    @Test%0A    public void testPropertyComingFromExternalObject() %7B%0A        Properties propsFromTest = new Properties();%0A        propsFromTest.setProperty(%22external%22, %22propsFromTest%22);%0A%0A        WithImportedProperties conf =%0A                ConfigFactory.create(WithImportedProperties.class,%0A                        propsFromTest);%0A%0A        assertEquals(%22propsFromTest%22, conf.external());%0A    %7D%0A
 %0A   
@@ -3209,17 +3209,20 @@
 ome();%0A%0A
-%09
+    
     @Def
@@ -3262,9 +3262,12 @@
 %7D%22)%0A
-%09
+    
     
@@ -3294,16 +3294,44 @@
 slash();
+%0A%0A        String external();
 %0A    %7D%0A%7D

test refactoring