parent
6feb7f87dc
commit
6a3d7f4f42
@ -0,0 +1,55 @@
|
|||||||
|
package space.kklochko.simple_jbdd.tests.commands.decorators;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
import space.kklochko.simple_jbdd.tests.commands.AbstractTestCommand;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public abstract class AbstractDecorator<T extends Test> extends AbstractTestCommand<T> {
|
||||||
|
protected AbstractTestCommand<T> command;
|
||||||
|
|
||||||
|
protected Method method;
|
||||||
|
|
||||||
|
public AbstractDecorator(String label, AbstractTestCommand<T> command) {
|
||||||
|
super(label);
|
||||||
|
setCommand(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract boolean test();
|
||||||
|
|
||||||
|
public Map<String, String> getLabels() {
|
||||||
|
Map<String, String> labels = getCommand().getLabels();
|
||||||
|
labels.put(getType(), this.getLabel());
|
||||||
|
return labels;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract boolean runMethod();
|
||||||
|
|
||||||
|
public void setMethod(Method method) {
|
||||||
|
this.method = method;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Method getMethod() {
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setObject(T object) {
|
||||||
|
command.setObject(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T getObject() {
|
||||||
|
return command.getObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
public AbstractTestCommand<T> getCommand() {
|
||||||
|
return command;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCommand(AbstractTestCommand<T> command) {
|
||||||
|
this.command = command;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,39 @@
|
|||||||
|
package space.kklochko.simple_jbdd.tests.commands.decorators;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
import space.kklochko.simple_jbdd.tests.commands.AbstractTestCommand;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
public class BlockDecorator<T extends Test> extends AbstractDecorator<T> {
|
||||||
|
public BlockDecorator(String label, AbstractTestCommand<T> command, Method method, String type) {
|
||||||
|
super(label, command);
|
||||||
|
setType(type);
|
||||||
|
setMethod(method);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean test() {
|
||||||
|
boolean result = runMethod();
|
||||||
|
|
||||||
|
if(!result)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return getCommand().test();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean runMethod() {
|
||||||
|
try {
|
||||||
|
method.setAccessible(true);
|
||||||
|
method.invoke(getCommand().getObject());
|
||||||
|
return true;
|
||||||
|
} catch (InvocationTargetException e) {
|
||||||
|
System.err.println(e.getMessage());
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
System.err.println("Check access modifiers.");
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in new issue