package net.raymanoz.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.Set;
import net.raymanoz.util.PlainProperties;
import net.raymanoz.util.Properties;
import org.junit.Test;
public class ApplicationPropertiesImplTest {
@Test
public void findVariablesRequiringDialog(){
final PlainProperties properties = new PlainProperties("a=${c}\nb=${d}\nc=<ASK>\ne=f");
final ApplicationProperties applicationProperties = new ApplicationPropertiesImpl(properties);
Set<String> vars = applicationProperties.findVariablesRequiringDialog("a", "b", "e");
assertNotNull(vars);
assertEquals(2, vars.size());
assertTrue(vars.contains("c"));
assertTrue(vars.contains("d"));
}
@Test
public void shouldBeAbleToAccessPropertiesByName() throws IOException {
String key = "key";
String expectedValue = "property_value";
assertPropertyValue(new PlainProperties(key + "=" + expectedValue), expectedValue, key);
}
@Test
public void shouldBeAbleToReturnEvaluatedProperties() throws IOException {
String key = "key";
String properties =
"name=Bob\n" +
key + "=Hello ${name}";
assertPropertyValue(new PlainProperties(properties), "Hello Bob", key);
}
@Test
public void shouldBeAbleToReplaceEvaluatedPropertiesRecursively() throws IOException {
String key = "key";
String properties =
"father=Darth vader\n" +
"name=${father}\n"+
key + "=Hello ${name}";
assertPropertyValue(new PlainProperties(properties), "Hello Darth vader", key);
}
@Test
public void shouldBeAbleToReplaceNestedVariables() throws IOException {
String key = "key";
String properties =
"name5=Darth vader\n"+
"count=5\n" +
key + "=Hello ${name${count}}";
assertPropertyValue(new PlainProperties(properties), "Hello Darth vader", key);
}
@Test
public void shouldBeAbleToReturnSameStringIfVariableNotReplaced() throws IOException {
String key = "key";
String expectedValue = "${dog}";
assertPropertyValue(new PlainProperties(key + "=" + expectedValue), expectedValue, key);
}
private void assertPropertyValue(Properties properties, String expectedValue, String key) {
ApplicationProperties applicationProperties = new ApplicationPropertiesImpl(properties);
assertEquals("Property value retrieved is incorrect", expectedValue, applicationProperties.getString(key));
}
}