Skip to content

Commit

Permalink
fix: Re-add Try-Catch for ConfigurationExceptions To DefaultPropertyP…
Browse files Browse the repository at this point in the history
…laceholderResolver (#10798)

Fixes #10797 
* Adding try-catch for ConfigurationExceptions which are thrown when resolving env properties
* Adding test cases for placeholder resolution, both with and without the placeholder set
* Removing irrelevant print statement and method call to vehicle for the purposes of the specified unit tests
  • Loading branch information
helios2k6 authored and sdelamo committed May 8, 2024
1 parent aeaec05 commit a6fd8c3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,18 @@ public <T> T getValue(Class<T> type) throws ConfigurationException {

@Override
public <T> Optional<T> findValue(Class<T> type) {
for (String expression: expressions) {
Optional<T> optionalValue = resolveOptionalExpression(expression, type);
if (optionalValue.isPresent()) {
return optionalValue;
try {
for (String expression: expressions) {
Optional<T> optionalValue = resolveOptionalExpression(expression, type);
if (optionalValue.isPresent()) {
return optionalValue;
}
}
}
if (defaultValue != null) {
return conversionService.convert(defaultValue, type);
if (defaultValue != null) {
return conversionService.convert(defaultValue, type);
}
} catch (ConfigurationException e) {
// Swallow exception.
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public interface Engine {
int getCylinders();

String start();

String getDescription();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

// tag::imports[]
import io.micronaut.context.annotation.Value;
import io.micronaut.core.annotation.Nullable;

import jakarta.inject.Singleton;
// end::imports[]
Expand All @@ -28,6 +29,10 @@ public class EngineImpl implements Engine {
@Value("${my.engine.cylinders:6}") // <1>
protected int cylinders;

@Nullable
@Value("${my.engine.description}")
protected String description;

@Override
public int getCylinders() {
return cylinders;
Expand All @@ -38,5 +43,10 @@ public String start() {// <2>
return "Starting V" + getCylinders() + " Engine";
}

@Override
public String getDescription() {
return description;
}

}
// end::class[]
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.LinkedHashMap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class VehicleSpec {

Expand Down Expand Up @@ -56,4 +57,34 @@ void testStartVehicleWithoutConfiguration() {
assertEquals("Starting V6 Engine", vehicle.start());
}

@Test
void testStartVehicleWithNonEmptyPlaceholder() {
// tag::start[]
ApplicationContext applicationContext = new DefaultApplicationContext("test");
LinkedHashMap<String, Object> map = new LinkedHashMap(1);
map.put("my.engine.description", "${DESCRIPTION}");
map.put("DESCRIPTION", "V8 Engine");
applicationContext.getEnvironment().addPropertySource(PropertySource.of("test", map));
applicationContext.start();

Vehicle vehicle = applicationContext.getBean(Vehicle.class);
// end::start[]

assertEquals("V8 Engine", vehicle.getEngine().getDescription());
}

@Test
void testStartVehicleWithEmptyPlaceholder() {
// tag::start[]
ApplicationContext applicationContext = new DefaultApplicationContext("test");
LinkedHashMap<String, Object> map = new LinkedHashMap(1);
map.put("my.engine.description", "${DESCRIPTION}");
applicationContext.getEnvironment().addPropertySource(PropertySource.of("test", map));
applicationContext.start();

Vehicle vehicle = applicationContext.getBean(Vehicle.class);
// end::start[]

assertNull(vehicle.getEngine().getDescription());
}
}

0 comments on commit a6fd8c3

Please sign in to comment.