com.teamscale.jacoco.agent.commandline.Validator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-jacoco-agent Show documentation
Show all versions of teamscale-jacoco-agent Show documentation
JVM profiler that simplifies various aspects around recording and uploading test coverage
/*-------------------------------------------------------------------------+
| |
| Copyright (c) 2009-2017 CQSE GmbH |
| |
+-------------------------------------------------------------------------*/
package com.teamscale.jacoco.agent.commandline;
import org.conqat.lib.commons.assertion.CCSMAssert;
import org.conqat.lib.commons.string.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* Helper class to allow for multiple validations to occur.
*/
public class Validator {
/** The found validation problems in the form of error messages for the user. */
private final List messages = new ArrayList<>();
/** Runs the given validation routine. */
public void ensure(ExceptionBasedValidation validation) {
try {
validation.validate();
} catch (Exception | AssertionError e) {
messages.add(e.getMessage());
}
}
/**
* Interface for a validation routine that throws an exception when it fails.
*/
@FunctionalInterface
public interface ExceptionBasedValidation {
/**
* Throws an {@link Exception} or {@link AssertionError} if the validation
* fails.
*/
void validate() throws Exception, AssertionError;
}
/**
* Checks that the given condition is true
or adds the given error
* message.
*/
public void isTrue(boolean condition, String message) {
ensure(() -> CCSMAssert.isTrue(condition, message));
}
/**
* Checks that the given condition is false
or adds the given error
* message.
*/
public void isFalse(boolean condition, String message) {
ensure(() -> CCSMAssert.isFalse(condition, message));
}
/** Returns true
if the validation succeeded. */
public boolean isValid() {
return messages.isEmpty();
}
/** Returns an error message with all validation problems that were found. */
public String getErrorMessage() {
return "- " + StringUtils.concat(messages, StringUtils.LINE_FEED + "- ");
}
}