Skip to content

Commit

Permalink
Merge pull request #253 from reportportal/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
HardNorth authored Nov 1, 2024
2 parents fcc700c + 33e4d01 commit 497a258
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## [Unreleased]
### Added
- Issue [#210](https://github.com/reportportal/client-java/issues/210) Ability to call methods without parameters in templates, by @HardNorth

## [5.2.17]
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@
String arrayEndSymbol() default TemplateConfiguration.ARRAY_END_PATTERN;

String arrayElementDelimiter() default TemplateConfiguration.ARRAY_ELEMENT_DELIMITER;

String methodCallStartSymbol() default TemplateConfiguration.METHOD_CALL_START_PATTERN;

String methodCallEndSymbol() default TemplateConfiguration.METHOD_CALL_END_PATTERN;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class LaunchIdLockFile extends AbstractLaunchIdLock implements LaunchIdLo

public static final Charset LOCK_FILE_CHARSET = StandardCharsets.ISO_8859_1;
public static final String TIME_SEPARATOR = ":";
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private static final String LINE_SEPARATOR = System.lineSeparator();

private final File lockFile;
private final File syncFile;
Expand Down Expand Up @@ -178,7 +178,7 @@ private <T> T executeOperation(@Nonnull final IoOperation<T> operation, @Nonnull
return operation.execute(fileIo);
} catch (IOException e) {
// operations failed with IOException will be retried according to timeout and retries number
LOGGER.error("Unable to read/write a file after obtaining mainLock: " + e.getMessage(), e);
LOGGER.error("Unable to read/write a file after obtaining mainLock: {}", e.getMessage(), e);
}
return null;
}
Expand Down Expand Up @@ -247,8 +247,8 @@ private String obtainLaunch(@Nonnull final String instanceUuid) {
/**
* Returns a Launch UUID for many Clients launched on one machine.
*
* @param instanceUuid a Client instance UUID, which will be written to lock and sync files and, if it the first thread which managed to
* obtain lock on '.lock' file, returned to every client instance.
* @param instanceUuid a Client instance UUID, which will be written to lock and sync files and, if it is the first thread which managed
* to obtain lock on '.lock' file, returned to every client instance.
* @return either a Client instance UUID, either the first UUID which thread managed to place a lock on a '.lock' file.
*/
@Override
Expand Down Expand Up @@ -371,14 +371,14 @@ public void finishInstanceUuid(@Nonnull final String instanceUuid) {
Boolean isLast = executeBlockingOperation(uuidRemove, syncFile);
if (isLast != null && isLast) {
if (!syncFile.delete()) {
LOGGER.warn("Unable to delete synchronization file: " + syncFile.getPath());
LOGGER.warn("Unable to delete synchronization file: {}", syncFile.getPath());
}
}

if (mainLock != null && lockUuid.equals(instanceUuid)) {
reset();
if (!lockFile.delete()) {
LOGGER.warn("Unable to delete locking file: " + lockFile.getPath());
LOGGER.warn("Unable to delete locking file: {}", lockFile.getPath());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class TemplateConfiguration {
public static final String ARRAY_START_PATTERN = "{";
public static final String ARRAY_END_PATTERN = "}";
public static final String ARRAY_ELEMENT_DELIMITER = ", ";
public static final String METHOD_CALL_START_PATTERN = "(";
public static final String METHOD_CALL_END_PATTERN = ")";

private String className;
private String classRef;
Expand All @@ -48,6 +50,8 @@ public class TemplateConfiguration {
private String arrayStart;
private String arrayEnd;
private String arrayDelimiter;
private String methodCallStart;
private String methodCallEnd;

public TemplateConfiguration() {
className = CLASS_SIMPLE_NAME_TEMPLATE;
Expand All @@ -61,6 +65,8 @@ public TemplateConfiguration() {
arrayStart = ARRAY_START_PATTERN;
arrayEnd = ARRAY_END_PATTERN;
arrayDelimiter = ARRAY_ELEMENT_DELIMITER;
methodCallStart = METHOD_CALL_START_PATTERN;
methodCallEnd = METHOD_CALL_END_PATTERN;
}

@Override
Expand Down Expand Up @@ -92,7 +98,9 @@ public int hashCode() {
iterableDelimiter,
arrayStart,
arrayEnd,
arrayDelimiter
arrayDelimiter,
methodCallStart,
methodCallEnd
);
}

Expand All @@ -108,6 +116,8 @@ public TemplateConfiguration(TemplateConfig config) {
arrayStart = config.arrayStartSymbol();
arrayEnd = config.arrayEndSymbol();
arrayDelimiter = config.arrayElementDelimiter();
methodCallStart = config.methodCallStartSymbol();
methodCallEnd = config.methodCallEndSymbol();
}

public String getClassName() {
Expand Down Expand Up @@ -208,4 +218,22 @@ public TemplateConfiguration setArrayDelimiter(String arrayDelimiter) {
this.arrayDelimiter = arrayDelimiter;
return this;
}

public String getMethodCallStart() {
return methodCallStart;
}

public TemplateConfiguration setMethodCallStart(String methodCallStart) {
this.methodCallStart = methodCallStart;
return this;
}

public String getMethodCallEnd() {
return methodCallEnd;
}

public TemplateConfiguration setMethodCallEnd(String methodCallEnd) {
this.methodCallEnd = methodCallEnd;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
import javax.annotation.Nullable;
import java.lang.reflect.Array;
import java.lang.reflect.Executable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static java.util.Optional.ofNullable;

Expand Down Expand Up @@ -96,14 +98,14 @@ private static String getReplacement(@Nonnull String templatePart, @Nonnull Map<
String[] fields = templatePart.split(Pattern.quote(templateConfig.getFieldDelimiter()));
String variableName = fields[0];
if (!parametersMap.containsKey(variableName)) {
LOGGER.error("Param - " + variableName + " was not found");
LOGGER.error("Param - {} was not found", variableName);
return null;
}
Object param = parametersMap.get(variableName);
try {
return retrieveValue(templateConfig, 1, fields, param);
} catch (NoSuchFieldException e) {
LOGGER.error("Unable to parse: " + templatePart);
} catch (Throwable e) {
LOGGER.error("Unable to parse: {}", templatePart);
return null;
}
}
Expand All @@ -129,7 +131,7 @@ private static String getReplacement(@Nonnull String templatePart, @Nonnull Map<
* @throws NoSuchFieldException if field not found
*/
public static String retrieveValue(@Nonnull TemplateConfiguration templateConfig, int index, @Nonnull String[] fields,
@Nullable Object object) throws NoSuchFieldException {
@Nullable Object object) throws Throwable {
for (int i = index; i < fields.length; i++) {
if (object == null) {
return NULL_VALUE;
Expand All @@ -142,7 +144,23 @@ public static String retrieveValue(@Nonnull TemplateConfiguration templateConfig
return parseCollection(templateConfig, (Iterable<?>) object, i, fields);
}

object = Accessible.on(object).field(fields[i]).getValue();
String field = fields[i];
int methodCallStartIndex = field.indexOf(templateConfig.getMethodCallStart());
if (methodCallStartIndex > 0 && field.endsWith(templateConfig.getMethodCallEnd())) {
String method = field.substring(0, methodCallStartIndex);
String parameters = field.substring(methodCallStartIndex + 1, field.length() - 1);
if (!parameters.isEmpty()) {
LOGGER.warn(
"Method parameters are not supported. Method: {} Parameters: {}. Ignoring the call.",
method,
parameters
);
return Arrays.stream(fields).collect(Collectors.joining(templateConfig.getFieldDelimiter()));
}
object = Accessible.on(object).method(method).invoke();
} else {
object = Accessible.on(object).field(field).getValue();
}

}
return parseDescendant(templateConfig, object);
Expand All @@ -155,8 +173,7 @@ public static String retrieveValue(@Nonnull TemplateConfiguration templateConfig
* @param fields Fields of the template part
* @return {@link String} representation of the parsed Array
*/
private static String parseArray(TemplateConfiguration templateConfig, Object[] array, int index, String[] fields)
throws NoSuchFieldException {
private static String parseArray(TemplateConfiguration templateConfig, Object[] array, int index, String[] fields) throws Throwable {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(templateConfig.getArrayStart());

Expand All @@ -179,7 +196,7 @@ private static String parseArray(TemplateConfiguration templateConfig, Object[]
* @return {@link String} representation of the parsed Collection
*/
private static String parseCollection(TemplateConfiguration templateConfig, Iterable<?> iterable, int index, String[] fields)
throws NoSuchFieldException {
throws Throwable {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(templateConfig.getIterableStart());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static String processTemplate(@Nonnull String pattern, @Nullable Map<Stri
* @throws NoSuchFieldException if field not found
*/
public static String retrieveValue(TemplateConfiguration templateConfig, int index, String[] fields, Object object)
throws NoSuchFieldException {
throws Throwable {
return com.epam.reportportal.utils.formatting.templating.TemplateProcessing.retrieveValue(templateConfig.getDelegate(),
index,
fields,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void annotationProvider() {

@ParameterizedTest
@MethodSource("data")
public void retrieveValue(String template, String expected) throws NoSuchMethodException, NoSuchFieldException {
public void retrieveValue(String template, String expected) throws Throwable {

Outer.Inner someObject = createInnerObject();

Expand All @@ -58,7 +58,8 @@ public static Object[][] data() {
{"someObject.outers", "[OUTER]"},
{"someObject.outers.outerStrings", "[[{first, second, third}, {fourth, fifth, sixth}]]"},
{"someObject.outers.outerName", "[outer]"}, {"someObject.innerNullString", ParameterUtils.NULL_VALUE},
{"someObject.innerNullList", ParameterUtils.NULL_VALUE}};
{"someObject.innerNullList", ParameterUtils.NULL_VALUE}, {"someObject.toString()", "INNER"},
{"someObject.echo(\"test\")", "someObject.echo(\"test\")"}};
}

private Outer.Inner createInnerObject() {
Expand Down Expand Up @@ -122,6 +123,10 @@ public Inner(String outerName, List<String[]> outerStrings, String innerName, Li
public String toString() {
return "INNER";
}

public String echo(String param) {
return param;
}
}

}
Expand Down

0 comments on commit 497a258

Please sign in to comment.