From e8bdc5908124822c7cd786e11970c7832fd08c07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Mon, 25 Apr 2016 19:27:29 +0200 Subject: [PATCH] implemented Tester.runAllTests() and @Test annotation --- cicaprojekt/Tester.java | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/cicaprojekt/Tester.java b/cicaprojekt/Tester.java index d3f5d98..7b336bd 100644 --- a/cicaprojekt/Tester.java +++ b/cicaprojekt/Tester.java @@ -3,11 +3,18 @@ package cicaprojekt; import java.io.Console; import java.io.File; import java.io.IOException; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; public class Tester { - Player oneill; - Player jaffa; - PlayerBase replicator; + private Player oneill; + private Player jaffa; + private PlayerBase replicator; + Dungeon loadMap(String param) throws IOException{ Dungeon dungeon = new Dungeon(); @@ -21,5 +28,27 @@ public class Tester { System.out.println(replicator); } + /* custom Test annotation */ + @Target(ElementType.METHOD) // it's for methods + @Retention(RetentionPolicy.RUNTIME) // we want to retain annotations in runtime + private @interface Test {} + /* run all methods annotated with @Test */ + public boolean runAllTests() throws InvocationTargetException, IllegalAccessException + { + boolean testspassed = true; + + for (Method m : this.getClass().getMethods()) // iterate over all methods of this + { + if (m.isAnnotationPresent(Test.class)) // if its annotated with @Test... + { + Boolean testresult = (Boolean) m.invoke(null); // call it! + if (!testresult) + { + testspassed = false; + } + } + } + return testspassed; + } }