All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.epam.jdi.light.asserts.core.SoftAssert Maven / Gradle / Ivy

There is a newer version: 1.6.0
Show newest version
package com.epam.jdi.light.asserts.core;

import com.epam.jdi.tools.Safe;
import org.hamcrest.Matcher;

import java.util.ArrayList;
import java.util.List;

import static com.epam.jdi.light.settings.WebSettings.logger;
import static com.epam.jdi.tools.PrintUtils.print;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * Created by Roman Iovlev on 26.09.2019
 * Email: [email protected]; Skype: roman.iovlev
 */
public class SoftAssert {
    private static Safe> listOfErrors = new Safe<>(new ArrayList<>());
    private static boolean IS_SOFT_ASSERT = false;

    public static void setAssertType(String type) {
        IS_SOFT_ASSERT = type.equalsIgnoreCase("soft");
        clearResults();
    }
    public static void assertSoft() {
        setAssertType("soft");
    }
    public static void assertStrict() {
        setAssertType("strict");
    }
    public static  void jdiAssert(List actual, Matcher> matcher) {
        try {
            assertThat(actual, matcher);
        } catch (Throwable error) {
            if (IS_SOFT_ASSERT) {
                addError(error);
            } else
                throw new AssertionError(error);
        }
    }
    public static  void jdiAssert(T actual, Matcher matcher) {
        try {
            assertThat(actual, matcher);
            logger.debug(">>> " + actual);
        } catch (Throwable error) {
            if (IS_SOFT_ASSERT) {
                addError(error);
            } else
                throw new AssertionError(error);
        }
    }
    private static void addError(Throwable error) {
        listOfErrors.get().add(error.getMessage().replace("java.lang.AssertionError: ", ""));
    }
    public static List getErrors() {
        List errors = new ArrayList<>(listOfErrors.get());
        clearResults();
        return errors;
    }

    public static void assertResults() {
        List errors = new ArrayList<>(listOfErrors.get());
        clearResults();
        if (!errors.isEmpty())
            throw new AssertionError(print(errors));
    }

    public static void clearResults(){
        listOfErrors.get().clear();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy