package com.atlassian.stash.internal.spring;

import org.junit.Test;

import java.io.StringReader;
import java.util.Properties;

import static org.junit.Assert.*;

public class PlaceholderResolvingPropertiesTest {

    @Test
    public void testGetProperty() throws Exception {
        PlaceholderResolvingProperties properties = load("example.value=5\nexample.resolving=${example.value}");
        assertEquals("5", properties.getProperty("example.resolving"));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testGetPropertyWithFirstLevelCycle() throws Exception {
        PlaceholderResolvingProperties properties = load("example.value=${example.value}");
        properties.getProperty("example.value");
    }

    @Test(expected = IllegalArgumentException.class)
    public void testGetPropertyWithSecondLevelCycle() throws Exception {
        PlaceholderResolvingProperties properties = load("example.value=${example.resolving}\n" +
                "example.resolving=${example.value}");
        properties.getProperty("example.value");
    }

    @Test
    public void testGetPropertyIgnoresUnresolvedPlaceholders() throws Exception {
        PlaceholderResolvingProperties properties = load("example.resolving=${example.value}");
        assertEquals("${example.value}", properties.getProperty("example.resolving"));
    }

    private static PlaceholderResolvingProperties load(String value) throws Exception {
        Properties properties = new Properties();
        properties.load(new StringReader(value));

        return new PlaceholderResolvingProperties(properties);
    }
}