Skip to content

Commit

Permalink
chore(citrus-base): review and code cleanup
Browse files Browse the repository at this point in the history
pr: #1224

citrus-base` module.
  • Loading branch information
bbortt committed Oct 24, 2024
1 parent 0f41890 commit 800d702
Show file tree
Hide file tree
Showing 114 changed files with 715 additions and 826 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

package org.citrusframework;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.citrusframework.actions.NoopTestAction;
import org.citrusframework.container.TestActionContainer;
import org.citrusframework.spi.ReferenceResolver;
import org.citrusframework.spi.ReferenceResolverAware;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Abstract container builder takes care on calling the container runner when actions are placed in the container.
*/
Expand Down Expand Up @@ -129,7 +129,7 @@ public T doBuild() {

@Override
public T build() {
if (container.getActions().size() > 0) {
if (!container.getActions().isEmpty()) {
return container;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

package org.citrusframework;

import java.util.Map;
import java.util.Optional;

import org.citrusframework.exceptions.CitrusRuntimeException;
import org.citrusframework.spi.ResourcePathTypeResolver;
import org.citrusframework.spi.TypeResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Optional;

@FunctionalInterface
public interface CitrusContextProvider {

Expand Down Expand Up @@ -85,7 +85,7 @@ static Optional<CitrusContextProvider> lookup(String name) {
CitrusContextProvider instance = TYPE_RESOLVER.resolve(name);
return Optional.of(instance);
} catch (CitrusRuntimeException e) {
logger.warn(String.format("Failed to resolve Citrus context provider from resource '%s/%s'", RESOURCE_PATH, name));
logger.warn("Failed to resolve Citrus context provider from resource '{}/{}'", RESOURCE_PATH, name);
}

return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* Citrus' built-in runner, but it also offers the flexibility to replace the default runner with a
* custom implementation. To do this, it leverages the Citrus {@link ResourcePathTypeResolver}
* mechanism.
*
* <p>
* To provide a custom runner, the following file needs to be added to the classpath:
* <p>
* <code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@

package org.citrusframework.actions;

import java.io.IOException;
import java.io.PrintStream;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Stack;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildListener;
import org.apache.tools.ant.DefaultLogger;
Expand All @@ -38,10 +32,16 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.PrintStream;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Stack;

/**
* Action calls Apache ANT with given build file and runs ANT targets
* as separate build. User can set and overwrite properties for the build.
*
* <p>
* Build logging output is forwarded to test run logger.
*
* @since 1.3
Expand Down Expand Up @@ -95,7 +95,7 @@ public void doExecute(TestContext context) {

for (Entry<Object, Object> entry : properties.entrySet()) {
String propertyValue = entry.getValue() != null ? context.replaceDynamicContentInString(entry.getValue().toString()) : "";
logger.debug("Set build property: " + entry.getKey() + "=" + propertyValue);
logger.debug("Set build property: {}={}", entry.getKey(), propertyValue);
project.setProperty(entry.getKey().toString(), propertyValue);
}

Expand All @@ -109,20 +109,20 @@ public void doExecute(TestContext context) {

project.addBuildListener(consoleLogger);

logger.info("Executing ANT build: " + buildFileResource);
logger.info("Executing ANT build: {}", buildFileResource);

if (StringUtils.hasText(targets)) {
logger.info("Executing ANT targets: " + targets);
logger.info("Executing ANT targets: {}", targets);
project.executeTargets(parseTargets());
} else {
logger.info("Executing ANT target: " + target);
logger.info("Executing ANT target: {}", target);
project.executeTarget(target);
}
} catch (BuildException e) {
throw new CitrusRuntimeException("Failed to run ANT build file", e);
}

logger.info("Executed ANT build: " + buildFileResource);
logger.info("Executed ANT build: {}", buildFileResource);
}

private static DefaultLogger getDefaultConsoleLogger() {
Expand Down Expand Up @@ -166,18 +166,15 @@ private Stack<String> parseTargets() {
private void loadBuildPropertyFile(Project project, TestContext context) {
if (StringUtils.hasText(propertyFilePath)) {
String propertyFileResource = context.replaceDynamicContentInString(propertyFilePath);
logger.info("Reading build property file: " + propertyFileResource);
logger.info("Reading build property file: {}", propertyFileResource);
Properties fileProperties = new Properties();
try {
Resource propertyResource = Resources.fromClasspath(propertyFileResource);
fileProperties.load(propertyResource.getInputStream());

for (Entry<Object, Object> entry : fileProperties.entrySet()) {
String propertyValue = entry.getValue() != null ? context.replaceDynamicContentInString(entry.getValue().toString()) : "";

if (logger.isDebugEnabled()) {
logger.debug("Set build property from file resource: " + entry.getKey() + "=" + propertyValue);
}
logger.debug("Set build property from file resource: {}={}", entry.getKey(), propertyValue);
project.setProperty(entry.getKey().toString(), propertyValue);
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

package org.citrusframework.actions;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.citrusframework.AbstractTestActionBuilder;
import org.citrusframework.context.TestContext;
import org.citrusframework.variable.VariableUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
* Action creating new test variables during a test. Existing test variables are overwritten
* by new values.
Expand Down Expand Up @@ -64,7 +64,7 @@ public void doExecute(TestContext context) {
//check if value is variable or function (and resolve it if yes)
value = context.replaceDynamicContentInString(value);

logger.info("Setting variable: " + key + " to value: " + value);
logger.info("Setting variable: {} to value: {}", key, value);

context.setVariable(key, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

package org.citrusframework.actions;

import java.util.Date;

import org.citrusframework.AbstractTestActionBuilder;
import org.citrusframework.context.TestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
* Prints messages to the console/logger during test execution.
*
Expand All @@ -49,7 +49,7 @@ private EchoAction(Builder builder) {
@Override
public void doExecute(TestContext context) {
if (message == null) {
logger.info("Citrus test " + new Date(System.currentTimeMillis()));
logger.info("Citrus test {}", new Date(System.currentTimeMillis()));
} else {
logger.info(context.getLogModifier().mask(context.replaceDynamicContentInString(message)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@

package org.citrusframework.actions;

import org.citrusframework.AbstractTestActionBuilder;
import org.citrusframework.context.TestContext;
import org.citrusframework.exceptions.CitrusRuntimeException;
import org.citrusframework.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Expand All @@ -24,17 +31,10 @@
import java.util.StringTokenizer;
import java.util.stream.Stream;

import org.citrusframework.AbstractTestActionBuilder;
import org.citrusframework.context.TestContext;
import org.citrusframework.exceptions.CitrusRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.citrusframework.util.StringUtils;

/**
* Test action prompts user data from standard input stream. The input data is then stored as new
* test variable. Test workflow stops until user input is complete.
*
* <p>
* Action can declare a set of valid answers, so user will be prompted until a valid
* answer was returned.
*
Expand Down Expand Up @@ -77,7 +77,7 @@ public void doExecute(TestContext context) {

if (context.getVariables().containsKey(variable)) {
input = context.getVariable(variable);
logger.info("Variable " + variable + " is already set (='" + input + "'). Skip waiting for user input");
logger.info("Variable {} is already set (='{}'). Skip waiting for user input", variable, input);

return;
}
Expand Down Expand Up @@ -123,7 +123,7 @@ private boolean checkAnswer(String input) {
}
}

logger.info("User input is not valid - must be one of " + validAnswers);
logger.info("User input is not valid - must be one of {}", validAnswers);

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@

package org.citrusframework.actions;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.citrusframework.AbstractTestActionBuilder;
import org.citrusframework.context.TestContext;
import org.citrusframework.exceptions.CitrusRuntimeException;
Expand All @@ -32,6 +24,14 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* Action to enable class invocation through java reflection
*
Expand Down Expand Up @@ -125,7 +125,7 @@ private void invokeMethod(Object instance, Class<?>[] methodTypes, Object[] meth
Arrays.stream(methodTypes).map(Class::getSimpleName).collect(Collectors.joining(",")) + ")' for class '" + instance.getClass() + "'");
}

logger.info("Invoking method '" + methodToRun.toString() + "' on instance '" + instance.getClass() + "'");
logger.info("Invoking method '{}' on instance '{}'", methodToRun, instance.getClass());

methodToRun.invoke(instance, methodObjects);
}
Expand All @@ -152,7 +152,7 @@ private Object getObjectInstanceFromClass(TestContext context) throws ClassNotFo
"is set for Java reflection call");
}

logger.info("Instantiating class for name '" + className + "'");
logger.info("Instantiating class for name '{}'", className);

Class<?> classToRun = Class.forName(className);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@

package org.citrusframework.actions;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.citrusframework.AbstractTestActionBuilder;
import org.citrusframework.context.TestContext;
import org.citrusframework.exceptions.CitrusRuntimeException;
Expand All @@ -29,6 +24,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

/**
* Action reads property files and creates test variables for every property entry. File
* resource path can define a resource located on classpath or file system.
Expand All @@ -55,23 +55,18 @@ public LoadPropertiesAction(Builder builder) {
public void doExecute(TestContext context) {
Resource resource = FileUtils.getFileResource(filePath, context);

if (logger.isDebugEnabled()) {
logger.debug("Reading property file " + FileUtils.getFileName(resource.getLocation()));
}
logger.debug("Reading property file {}", FileUtils.getFileName(resource.getLocation()));

Properties props = FileUtils.loadAsProperties(resource);

Map<String, Object> unresolved = new LinkedHashMap<>();
for (Entry<Object, Object> entry : props.entrySet()) {
String key = entry.getKey().toString();

if (logger.isDebugEnabled()) {
logger.debug("Loading property: " + key + "=" + props.getProperty(key) + " into variables");
}
logger.debug("Loading property: {}={} into variables", key, props.getProperty(key));

if (logger.isDebugEnabled() && context.getVariables().containsKey(key)) {
logger.debug("Overwriting property " + key + " old value:" + context.getVariable(key)
+ " new value:" + props.getProperty(key));
logger.debug("Overwriting property {} old value:{} new value:{}", key, context.getVariable(key), props.getProperty(key));
}

try {
Expand All @@ -83,7 +78,7 @@ public void doExecute(TestContext context) {

context.resolveDynamicValuesInMap(unresolved).forEach(context::setVariable);

logger.info("Loaded property file " + FileUtils.getFileName(resource.getLocation()));
logger.info("Loaded property file {}", FileUtils.getFileName(resource.getLocation()));
}

/**
Expand Down
Loading

0 comments on commit 800d702

Please sign in to comment.