Created
May 2, 2019 22:32
-
-
Save acdcjunior/b5b45aa0e691aa7946873388c298346d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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