diff --git a/src/main/java/space/kklochko/simple_jbdd/tests/factories/TestCommandFactory.java b/src/main/java/space/kklochko/simple_jbdd/tests/factories/TestCommandFactory.java index 4bf3b69..6af4934 100644 --- a/src/main/java/space/kklochko/simple_jbdd/tests/factories/TestCommandFactory.java +++ b/src/main/java/space/kklochko/simple_jbdd/tests/factories/TestCommandFactory.java @@ -11,37 +11,48 @@ import space.kklochko.simple_jbdd.tests.commands.decorators.BlockDecorator; import java.lang.annotation.Annotation; import java.lang.reflect.Method; -import java.util.Map; -import java.util.TreeMap; +import java.util.*; public class TestCommandFactory { public AbstractTestCommand create(Test input) { Class aClass = input.getClass(); - Map test_methods = this.getTestMethods(aClass); + Map> test_methods = this.getTestMethods(aClass); AbstractTestCommand aTestCommand = createTest(aClass, input); if(test_methods.containsKey("Then")) { - Method then = test_methods.get("Then"); - String label = then.getAnnotation(Then.class).value(); - aTestCommand = new BlockDecorator(label, aTestCommand, then, "Then"); + ArrayList thens = getMethodsInReverseOrder(test_methods, "Then"); + for(Method then : thens) { + String label = then.getAnnotation(Then.class).value(); + aTestCommand = new BlockDecorator(label, aTestCommand, then, "Then"); + } } if(test_methods.containsKey("When")) { - Method when = test_methods.get("When"); - String label = when.getAnnotation(When.class).value(); - aTestCommand = new BlockDecorator(label, aTestCommand, when, "When"); + ArrayList whens = getMethodsInReverseOrder(test_methods, "When"); + for(Method when : whens) { + String label = when.getAnnotation(When.class).value(); + aTestCommand = new BlockDecorator(label, aTestCommand, when, "When"); + } } if(test_methods.containsKey("Given")) { - Method when = test_methods.get("Given"); - String label = when.getAnnotation(Given.class).value(); - aTestCommand = new BlockDecorator(label, aTestCommand, when, "Given"); + ArrayList givens = getMethodsInReverseOrder(test_methods, "Given"); + for(Method given : givens) { + String label = given.getAnnotation(Given.class).value(); + aTestCommand = new BlockDecorator(label, aTestCommand, given, "Given"); + } } return aTestCommand; } + private ArrayList getMethodsInReverseOrder(Map> test_methods, String key) { + ArrayList methods = test_methods.get(key); + Collections.reverse(methods); + return methods; + } + protected AbstractTestCommand createTest(Class aClass, Test input) { Title aTitle = aClass.getAnnotation(Title.class); String title = aClass.getName(); @@ -52,8 +63,8 @@ public class TestCommandFactory { return new SimpleTestCommand(title, input); } - protected Map getTestMethods(Class aClass) { - Map test_methods = new TreeMap(); + protected Map> getTestMethods(Class aClass) { + Map> test_methods = new TreeMap<>(); for(Method method : aClass.getDeclaredMethods()) { Annotation[] annotations = method.getAnnotations(); @@ -62,15 +73,21 @@ public class TestCommandFactory { continue; if(method.isAnnotationPresent(Given.class)) - test_methods.put("Given", method); + addMethodTo(test_methods, "Given", method); if(method.isAnnotationPresent(When.class)) - test_methods.put("When", method); + addMethodTo(test_methods, "When", method); if(method.isAnnotationPresent(Then.class)) - test_methods.put("Then", method); + addMethodTo(test_methods, "Then", method); } return test_methods; } + + private void addMethodTo(Map> test_methods, String key, Method method) { + ArrayList methods = test_methods.getOrDefault(key, new ArrayList<>()); + methods.add(method); + test_methods.put(key, methods); + } }