From 6a3d7f4f42409e48b9ee916ab6a57a778d529cc3 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sat, 23 Sep 2023 21:41:42 +0300 Subject: [PATCH] Add the test block decorator. --- .../decorators/AbstractDecorator.java | 55 +++++++++++++++++++ .../commands/decorators/BlockDecorator.java | 39 +++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 src/main/java/space/kklochko/simple_jbdd/tests/commands/decorators/AbstractDecorator.java create mode 100644 src/main/java/space/kklochko/simple_jbdd/tests/commands/decorators/BlockDecorator.java diff --git a/src/main/java/space/kklochko/simple_jbdd/tests/commands/decorators/AbstractDecorator.java b/src/main/java/space/kklochko/simple_jbdd/tests/commands/decorators/AbstractDecorator.java new file mode 100644 index 0000000..7b6505e --- /dev/null +++ b/src/main/java/space/kklochko/simple_jbdd/tests/commands/decorators/AbstractDecorator.java @@ -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 extends AbstractTestCommand { + protected AbstractTestCommand command; + + protected Method method; + + public AbstractDecorator(String label, AbstractTestCommand command) { + super(label); + setCommand(command); + } + + public abstract boolean test(); + + public Map getLabels() { + Map 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 getCommand() { + return command; + } + + public void setCommand(AbstractTestCommand command) { + this.command = command; + } +} + diff --git a/src/main/java/space/kklochko/simple_jbdd/tests/commands/decorators/BlockDecorator.java b/src/main/java/space/kklochko/simple_jbdd/tests/commands/decorators/BlockDecorator.java new file mode 100644 index 0000000..379e302 --- /dev/null +++ b/src/main/java/space/kklochko/simple_jbdd/tests/commands/decorators/BlockDecorator.java @@ -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 extends AbstractDecorator { + public BlockDecorator(String label, AbstractTestCommand 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); + } + }; +} +