Skip to content

Instantly share code, notes, and snippets.

@acdcjunior
Created May 2, 2019 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acdcjunior/b5b45aa0e691aa7946873388c298346d to your computer and use it in GitHub Desktop.
Save acdcjunior/b5b45aa0e691aa7946873388c298346d to your computer and use it in GitHub Desktop.
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(SpringExtension.class)
@DataJpaTest
@ActiveProfiles("testes")
@Import(FlywayAutoConfiguration.class)
class ImprimirPropertiesTest {
@Autowired
private ApplicationContext appContext;
@Autowired
private InstrucaoParaAssinarRepository repo;
@Test
void imprimirProps() {
Iterable<?> es = repo.findAll();
assertThat(es).isNotEmpty();
PropertiesLogger.printProperties(appContext.getEnvironment());
}
}
class PropertiesLogger {
public static void printProperties(Environment environment) {
System.out.println("\n\n\n");
for (EnumerablePropertySource propertySource : findPropertiesPropertySources(((ConfigurableEnvironment) environment))) {
System.out.println("@### ******* " + propertySource.getName() + " *******");
String[] propertyNames = propertySource.getPropertyNames();
Arrays.sort(propertyNames);
for (String propertyName : propertyNames) {
String resolvedProperty = environment.getProperty(propertyName);
String sourceProperty = propertySource.getProperty(propertyName).toString();
if (resolvedProperty.equals(sourceProperty)) {
System.out.println("@### " + propertyName + "=" + valor(resolvedProperty));
} else {
System.out.println("@### " + propertyName + "=" + valor(sourceProperty) + " OVERRIDDEN to " + valor(resolvedProperty));
}
}
}
System.out.println("\n\n\n");
}
private static String valor(String prop) {
return StringUtils.abbreviate(prop, 100) + (prop.length() > 100 ? "(TRUNCADA)" : "");
}
private static List<EnumerablePropertySource> findPropertiesPropertySources(ConfigurableEnvironment environment) {
List<EnumerablePropertySource> propertiesPropertySources = new LinkedList<>();
for (PropertySource<?> propertySource : environment.getPropertySources()) {
if (propertySource instanceof EnumerablePropertySource) {
propertiesPropertySources.add((EnumerablePropertySource) propertySource);
}
}
return propertiesPropertySources;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment