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

com.g2forge.gearbox.command.test.CommandTester Maven / Gradle / Ivy

The newest version!
package com.g2forge.gearbox.command.test;

import java.util.List;

import com.g2forge.alexandria.command.invocation.CommandInvocation;
import com.g2forge.alexandria.java.core.helpers.HCollection;
import com.g2forge.alexandria.java.function.IFunction1;
import com.g2forge.alexandria.test.HAssert;
import com.g2forge.gearbox.command.converter.ICommandConverterR_;
import com.g2forge.gearbox.command.process.IProcess;
import com.g2forge.gearbox.command.process.IRunner;
import com.g2forge.gearbox.command.process.redirect.IRedirect;
import com.g2forge.gearbox.command.proxy.CommandProxyFactory;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;

public class CommandTester {
	@AllArgsConstructor
	@Getter
	protected static class CommandException extends Error {
		private static final long serialVersionUID = -8389195474206822767L;

		protected final CommandInvocation commandInvocation;
	}

	protected class ThrowingRunner implements IRunner {
		@Override
		public IProcess apply(CommandInvocation commandInvocation) {
			throw new CommandException(commandInvocation);
		}
	}

	@Getter(AccessLevel.PROTECTED)
	protected final Command command;

	public CommandTester(ICommandConverterR_ converter, Class commandClass) {
		this.command = new CommandProxyFactory(converter, new ThrowingRunner()).apply(commandClass);
	}

	public void assertArguments(IFunction1 actual, String... expected) {
		assertArguments(HCollection.asList(expected), actual);
	}

	public void assertArguments(List expected, IFunction1 actual) {
		try {
			actual.apply(getCommand());
		} catch (CommandException exception) {
			HAssert.assertArrayEquals(expected.toArray(new String[0]), exception.getCommandInvocation().getArguments().toArray(new String[0]));
			return;
		}
		HAssert.fail(String.format("Command invocation did not throw the expected exception!"));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy