use of io.micronaut.context.env.PropertySource in project micronaut-test by micronaut-projects.
the class TestExecutableEmbeddedServer method start.
@Override
public EmbeddedServer start() {
if (process == null) {
PropertySource testPropertySource = environment.getPropertySources().stream().filter(ps -> ps.getName().equals(AbstractMicronautExtension.TEST_PROPERTY_SOURCE)).findFirst().orElse(null);
CompletableFuture<Process> processFuture = new CompletableFuture<>();
Integer p = httpServerConfiguration.getPort().orElse(null);
int port;
if (p == null) {
if (environment.getActiveNames().contains(Environment.TEST)) {
port = findAvailableTcpPort();
} else {
port = 8080;
}
} else {
if (p == -1) {
port = findAvailableTcpPort();
} else {
port = p;
}
}
new Thread(() -> {
ProcessBuilder processBuilder = new ProcessBuilder();
List<String> commandArgs = new ArrayList<>(Arrays.asList("-Dmicronaut.environments=test", "-Dmicronaut.server.host=localhost", "-Dmicronaut.server.port=" + port));
if (executable.endsWith(".jar")) {
commandArgs.addAll(0, Arrays.asList("java", "-jar", executable));
} else {
commandArgs.add(0, executable);
}
if (testPropertySource != null) {
for (String prop : testPropertySource) {
commandArgs.add("-D" + prop + "=" + testPropertySource.get(prop));
}
}
processBuilder.command(commandArgs);
processBuilder.inheritIO();
try {
Process start = processBuilder.start();
processFuture.complete(start);
} catch (IOException e) {
processFuture.completeExceptionally(new RuntimeException("Error starting native image server: " + e.getMessage(), e));
}
}).start();
try {
this.process = processFuture.get();
int max = 10000;
int timeout = 0;
while (timeout < max) {
try {
URLConnection urlConnection = new URL("http://localhost:" + port).openConnection();
urlConnection.setConnectTimeout(max);
urlConnection.setReadTimeout(max);
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
in.readLine();
in.close();
} catch (IOException e) {
if (!(e instanceof FileNotFoundException)) {
timeout += 100;
if (timeout < max) {
Thread.sleep(100);
} else {
throw new ServerStartupException("Timeout occurred starting Micronaut process server");
}
} else {
// response from server
break;
}
}
}
this.port = port;
} catch (InterruptedException | ExecutionException e) {
throw new ServerStartupException(e.getMessage(), e);
}
}
return this;
}
use of io.micronaut.context.env.PropertySource in project micronaut-test by micronaut-projects.
the class AbstractMicronautExtension method beforeClass.
/**
* Executed before tests within a class are run.
*
* @param context The test context
* @param testClass The test class
* @param testAnnotationValue The test annotation values
*/
protected void beforeClass(C context, Class<?> testClass, @Nullable MicronautTestValue testAnnotationValue) {
if (testAnnotationValue != null) {
Class<? extends ApplicationContextBuilder>[] cb = testAnnotationValue.contextBuilder();
if (ArrayUtils.isNotEmpty(cb)) {
this.builder = InstantiationUtils.instantiate(cb[0]);
}
this.testAnnotationValue = testAnnotationValue;
final Package aPackage = testClass.getPackage();
builder.packages(aPackage.getName());
final List<Property> ps = AnnotationUtils.findRepeatableAnnotations(testClass, Property.class);
for (Property property : ps) {
testProperties.put(property.name(), property.value());
}
String[] propertySources = testAnnotationValue.propertySources();
if (ArrayUtils.isNotEmpty(propertySources)) {
Map<String, PropertySourceLoader> loaderMap = readPropertySourceLoaderMap();
ResourceResolver resourceResolver = new ResourceResolver();
for (String propertySource : propertySources) {
String ext = NameUtils.extension(propertySource);
if (StringUtils.isNotEmpty(ext)) {
String filename = NameUtils.filename(propertySource);
PropertySourceLoader loader = loaderMap.get(ext);
if (loader != null) {
Optional<InputStream> resourceAsStream = resourceResolver.getResourceAsStream(propertySource);
InputStream inputStream = resourceAsStream.orElse(testClass.getResourceAsStream(propertySource));
if (inputStream != null) {
Map<String, Object> properties;
try {
properties = loader.read(filename, inputStream);
builder.propertySources(PropertySource.of(filename, properties));
} catch (IOException e) {
throw new RuntimeException("Error loading property source reference for @MicronautTest: " + filename);
} finally {
try {
inputStream.close();
} catch (IOException e) {
// ignore
}
}
}
}
}
}
}
if (TestPropertyProvider.class.isAssignableFrom(testClass)) {
resolveTestProperties(context, testAnnotationValue, testProperties);
}
testProperties.put(TestActiveCondition.ACTIVE_SPEC_CLAZZ, testClass);
testProperties.put(TEST_ROLLBACK, String.valueOf(testAnnotationValue.rollback()));
testProperties.put(TEST_TRANSACTIONAL, String.valueOf(testAnnotationValue.transactional()));
testProperties.put(TEST_TRANSACTION_MODE, String.valueOf(testAnnotationValue.transactionMode()));
final Class<?> application = testAnnotationValue.application();
if (application != void.class) {
builder.mainClass(application);
}
String[] environments = testAnnotationValue.environments();
if (environments.length == 0) {
environments = new String[] { "test" };
}
builder.packages(testAnnotationValue.packages()).environments(environments);
PropertySource testPropertySource = PropertySource.of(TEST_PROPERTY_SOURCE, testProperties);
builder.propertySources(testPropertySource);
postProcessBuilder(builder);
this.applicationContext = builder.build();
startApplicationContext();
specDefinition = applicationContext.findBeanDefinition(testClass).orElse(null);
if (testAnnotationValue.startApplication() && applicationContext.containsBean(EmbeddedApplication.class)) {
embeddedApplication = applicationContext.getBean(EmbeddedApplication.class);
embeddedApplication.start();
}
refreshScope = applicationContext.findBean(RefreshScope.class).orElse(null);
}
}
Aggregations