Skip to content

Commit

Permalink
Support computed Properties in @containers
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgitario committed May 10, 2021
1 parent 8bc2dd7 commit 94c5f38
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.quarkus.test.bootstrap.ManagedResourceBuilder;
import io.quarkus.test.bootstrap.ServiceContext;
import io.quarkus.test.services.Container;
import io.quarkus.test.utils.PropertiesUtils;

public class ContainerManagedResourceBuilder implements ManagedResourceBuilder {

Expand Down Expand Up @@ -42,9 +43,9 @@ protected ServiceContext getContext() {
@Override
public void init(Annotation annotation) {
Container metadata = (Container) annotation;
this.image = metadata.image();
this.image = PropertiesUtils.resolveProperty(metadata.image());
this.command = metadata.command();
this.expectedLog = metadata.expectedLog();
this.expectedLog = PropertiesUtils.resolveProperty(metadata.expectedLog());
this.port = metadata.port();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,42 @@
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

public final class PropertiesUtils {

private static final String AUTOGENERATED_COMMENT = "This properties file has been auto generated "
+ "by Quarkus QE Test Framework";
private static final String PROPERTY_START_TAG = "${";
private static final String PROPERTY_END_TAG = "}";
private static final String PROPERTY_WITH_OPTIONAL = ":";

private PropertiesUtils() {

}

/**
* Try to resolve the value property from the value if the content is contained between ${xxx}.
*
* @param value
* @return
*/
public static String resolveProperty(String value) {
if (StringUtils.startsWith(value, PROPERTY_START_TAG)) {
String propertyKey = StringUtils.substringBetween(value, PROPERTY_START_TAG, PROPERTY_END_TAG);
String defaultValue = StringUtils.EMPTY;
if (StringUtils.contains(propertyKey, PROPERTY_WITH_OPTIONAL)) {
String[] propertyKeySplit = propertyKey.split(PROPERTY_WITH_OPTIONAL);
propertyKey = propertyKeySplit[0];
defaultValue = propertyKeySplit[1];
}

return System.getProperty(propertyKey, defaultValue);
}

return value;
}

public static Map<String, String> toMap(String propertiesFile) {
try (InputStream in = ClassLoader.getSystemResourceAsStream(propertiesFile)) {
return toMap(in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.quarkus.test.bootstrap.ServiceContext;
import io.quarkus.test.services.AmqContainer;
import io.quarkus.test.services.containers.model.AmqProtocol;
import io.quarkus.test.utils.PropertiesUtils;

public class AmqContainerManagedResourceBuilder extends ContainerManagedResourceBuilder {

Expand Down Expand Up @@ -45,8 +46,8 @@ protected AmqProtocol getProtocol() {
@Override
public void init(Annotation annotation) {
AmqContainer metadata = (AmqContainer) annotation;
this.image = metadata.image();
this.expectedLog = metadata.expectedLog();
this.image = PropertiesUtils.resolveProperty(metadata.image());
this.expectedLog = PropertiesUtils.resolveProperty(metadata.expectedLog());
this.protocol = metadata.protocol();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.quarkus.test.bootstrap.ServiceContext;
import io.quarkus.test.services.KafkaContainer;
import io.quarkus.test.services.containers.model.KafkaVendor;
import io.quarkus.test.utils.PropertiesUtils;

public class KafkaContainerManagedResourceBuilder implements ManagedResourceBuilder {

Expand Down Expand Up @@ -44,8 +45,8 @@ protected boolean isWithRegistry() {
public void init(Annotation annotation) {
KafkaContainer metadata = (KafkaContainer) annotation;
this.vendor = metadata.vendor();
this.image = metadata.image();
this.version = metadata.version();
this.image = PropertiesUtils.resolveProperty(metadata.image());
this.version = PropertiesUtils.resolveProperty(metadata.version());
this.withRegistry = metadata.withRegistry();
}

Expand Down

0 comments on commit 94c5f38

Please sign in to comment.