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

com.softicar.platform.common.testing.AssertionErrorMessageCollector Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.testing;

import com.softicar.platform.common.string.Imploder;
import java.util.ArrayList;
import java.util.Collection;

public class AssertionErrorMessageCollector {

	private final Collection errorMessages;

	public AssertionErrorMessageCollector() {

		this.errorMessages = new ArrayList<>();
	}

	public Collection getErrorMessages() {

		return errorMessages;
	}

	public AssertionErrorMessageCollector add(String message) {

		return add(message, new Object[0]);
	}

	public AssertionErrorMessageCollector addAll(Collection messages) {

		messages.forEach(this::add);
		return this;
	}

	public AssertionErrorMessageCollector addAll(AssertionErrorMessageCollector other) {

		return addAll(other.getErrorMessages());
	}

	public AssertionErrorMessageCollector add(String message, Object...args) {

		this.errorMessages.add(String.format(message, args));
		return this;
	}

	public void throwIfNecessary() {

		throwIfNecessary(null);
	}

	public void throwIfNecessary(String descriptiveMessage, Object...args) {

		if (!errorMessages.isEmpty()) {
			StringBuilder message = new StringBuilder();
			message.append(String.format("Encountered %s errors:%s\n", errorMessages.size(), createDescription(descriptiveMessage, args)));
			message.append(Imploder.implode(errorMessages, "\n"));
			throw new AssertionError(message.toString());
		}
	}

	private String createDescription(String headerMessage, Object...args) {

		if (headerMessage != null) {
			return " " + String.format(headerMessage, args);
		} else {
			return "";
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy