Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,5 @@ node_modules

# JENV
.java-version

.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.keycloak.test.framework.config;

import org.keycloak.test.framework.injection.ValueTypeAlias;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class Config {

private final static Config instance = new Config();

private Properties localEnv = new Properties();

private Config() {
File envFile = new File(".env");
if (envFile.isFile()) {
try {
localEnv.load(new FileInputStream(envFile));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

public static Config getInstance() {
return instance;
}

public String getSelectedSupplier(Class valueType) {
return getString("kc-test-" + ValueTypeAlias.getAlias(valueType));
}

public String getString(String key) {
String propKey = key.replace('-', '.');
String envKey = key.replace('-', '_').toUpperCase();

String value = System.getProperty(propKey);
if (value != null) {
return value;
}

value = System.getenv(envKey);
if (value != null) {
return value;
}

value = localEnv.getProperty(envKey);
if (value != null) {
return value;
}

return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

public class DevFileDatabaseSupplier extends AbstractDatabaseSupplier {

public static final String VENDOR = "dev-file";

@Override
TestDatabase getTestDatabase() {
DatabaseConfig databaseConfig = new DatabaseConfig().vendor("dev-file");
DatabaseConfig databaseConfig = new DatabaseConfig().vendor(VENDOR);
return new TestDatabase(databaseConfig);
}

@Override
public String getAlias() {
return VENDOR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

public class DevMemDatabaseSupplier extends AbstractDatabaseSupplier {

public static final String VENDOR = "dev-mem";

@Override
TestDatabase getTestDatabase() {
DatabaseConfig databaseConfig = new DatabaseConfig().vendor("dev-mem");
DatabaseConfig databaseConfig = new DatabaseConfig().vendor(VENDOR);
return new TestDatabase(databaseConfig);
}

@Override
public String getAlias() {
return VENDOR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

public class PostgresDatabaseSupplier extends AbstractDatabaseSupplier {

public static final String VENDOR = "postgres";

@Override
TestDatabase getTestDatabase() {
DatabaseConfig databaseConfig = new DatabaseConfig()
.vendor("postgres")
.vendor(VENDOR)
.username("keycloak")
.password("keycloak")
.containerImage("the-postgres-container:the-version");
return new TestDatabase(databaseConfig);
}

@Override
public String getAlias() {
return VENDOR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import org.jboss.logging.Logger;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.keycloak.test.framework.config.Config;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -209,10 +211,48 @@ private InstanceWrapper getDeployedInstance(Supplier supplier) {
}

private void loadSuppliers() {
ServiceLoader.load(Supplier.class).iterator().forEachRemaining(suppliers::add);
Iterator<Supplier> supplierIterator = ServiceLoader.load(Supplier.class).iterator();
Set<Class> loadedValueTypes = new HashSet<>();
Set<Supplier> skippedSuppliers = new HashSet<>();

while (supplierIterator.hasNext()) {
Supplier supplier = supplierIterator.next();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supplier.getValueType() is called 4 times in this loop, perhaps re-use it in a variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

boolean shouldAdd = false;
Class supplierValueType = supplier.getValueType();

if (!loadedValueTypes.contains(supplierValueType)) {
String requestedSupplier = Config.getInstance().getSelectedSupplier(supplierValueType);
if (requestedSupplier != null) {
if (requestedSupplier.equals(supplier.getAlias())) {
shouldAdd = true;
}
} else {
shouldAdd = true;
}
}

if (shouldAdd) {
suppliers.add(supplier);
loadedValueTypes.add(supplierValueType);
} else {
skippedSuppliers.add(supplier);
}
}

if (LOGGER.isTraceEnabled()) {
LOGGER.tracev("Suppliers: {0}", suppliers.stream().map(s -> s.getClass().getSimpleName()).collect(Collectors.joining(", ")));
StringBuilder loaded = new StringBuilder();
loaded.append("Loaded suppliers:");
for (Supplier s : suppliers) {
loaded.append("\n - " + ValueTypeAlias.getAlias(s.getValueType()) + " --> " + s.getAlias());
}
LOGGER.trace(loaded.toString());

StringBuilder skipped = new StringBuilder();
skipped.append("Skipped suppliers:");
for (Supplier s : skippedSuppliers) {
skipped.append("\n - " + ValueTypeAlias.getAlias(s.getValueType()) + " --> " + s.getAlias());
}
LOGGER.trace(skipped.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ public interface Supplier<T, S extends Annotation> {

default void close(T instance) {}

default String getAlias() {
return getClass().getSimpleName();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.keycloak.test.framework.injection;

import org.keycloak.test.framework.database.TestDatabase;
import org.keycloak.test.framework.server.KeycloakTestServer;
import org.openqa.selenium.WebDriver;

import java.util.Map;

public class ValueTypeAlias {

private static final Map<Class, String> aliases = Map.of(
WebDriver.class, "browser",
KeycloakTestServer.class, "server",
TestDatabase.class, "database"
);

public static String getAlias(Class clazz) {
String alias = aliases.get(clazz);
if (alias == null) {
alias = clazz.getSimpleName();
}
return alias;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ public boolean requiresDatabase() {
return true;
}

@Override
public String getAlias() {
return "distribution";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ public boolean requiresDatabase() {
return true;
}

@Override
public String getAlias() {
return "embedded";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ public boolean requiresDatabase() {
return false;
}

@Override
public String getAlias() {
return "remote";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ public boolean compatible(InstanceWrapper<WebDriver, TestWebDriver> a, InstanceW
public void close(WebDriver instance) {
instance.quit();
}

@Override
public String getAlias() {
return "chrome";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ public void close(WebDriver instance) {
instance.quit();
}

@Override
public String getAlias() {
return "firefox";
}
}