Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ private <T> void injectFields(T objectToConfigure) {
try {
if (field.isAnnotationPresent(Configuration.class)) {
Configuration configurationAnnotation = field.getAnnotation(Configuration.class);
String expression = "".equals(configurationAnnotation.value()) ? field.getName() : configurationAnnotation.value();
String expression = getExpression(objectToConfigure, field, configurationAnnotation);
field.setAccessible(true);
Class<?> fieldType = field.getType();
if (hasValue(expression)) {
Expand Down Expand Up @@ -514,6 +514,29 @@ private <T> void injectFields(T objectToConfigure) {
} while ((objectToConfigureClass = objectToConfigureClass.getSuperclass()) != null);
}

/**
* If @Configuration() has value, this is used;
* if field is non-empty String, its value is used;
* otherwise, field name is used.
*
* @return Property key
*/
private static <T> String getExpression(T objectToConfigure, Field field, Configuration configurationAnnotation) {
String expression = configurationAnnotation.value();
if ("".equals(configurationAnnotation.value())) {
expression = field.getName();
if (String.class.equals(field.getType())) {
try {
String value = (String) field.get(objectToConfigure);
if (value != null && value.length() > 0) {
expression = value;
}
} catch (IllegalAccessException ile) { }
}
}
return expression;
}

private boolean hasAnnotationDefaults(Configuration configurationAnnotation) {
return !("N/A".equals(configurationAnnotation.defaultValue()) && configurationAnnotation.defaultValueFactory().equals(Configuration.EmptyValueFactory.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void constrettoShouldWorkWithNonExistingResources() {
assertNotNull(configuration);
}

@Test(expected = ConstrettoExpressionException.class) //TODO perhaps a more specific expeption
@Test(expected = ConstrettoExpressionException.class) //TODO perhaps a more specific exception
public void shouldThrowExceptionIfTryingToMapWithoutNeededAllTagsProvided() throws Exception {
new ConstrettoBuilder(false)
.createPropertiesStore()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void prepareTests() {
setProperty("password", "password");
setProperty("vendor", "derby");
setProperty("version", "10");
setProperty("derby.system.home", "C:\\home\\Derby\\");
setProperty("array", "[\"one\",\"two\",\"three\"]");
setProperty("map", "{\"1\":\"10\",\"2\":\"20\"}");
configuration = new ConstrettoBuilder().createSystemPropertiesStore().getConfiguration();
Expand All @@ -69,6 +70,7 @@ public void applyConfigrationToAnnotatedConfigurationObject() {
assertEquals("username", customerDataSource.getUsername());
assertEquals("jdbc://url", customerDataSource.getUrl());
assertEquals("password", customerDataSource.getPassword());
assertEquals("C:\\home\\Derby\\", customerDataSource.homeDir);
assertEquals(new Integer(10), customerDataSource.getVersion());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class DataSourceConfiguration {
@Configuration("username")
private String myUsername;

@Configuration
public String homeDir = "derby.system.home";

@Configure
public void configureMe(@Configuration String url, @Configuration("password") String secret) {
Expand Down