-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08c80a9
commit 633879b
Showing
9 changed files
with
226 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/main/java/de/oliver/fancynpcs/tests/FNTestClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package de.oliver.fancynpcs.tests; | ||
|
||
import de.oliver.fancynpcs.tests.annotations.FNAfterEach; | ||
import de.oliver.fancynpcs.tests.annotations.FNBeforeEach; | ||
import de.oliver.fancynpcs.tests.annotations.FNTest; | ||
import org.bukkit.entity.Player; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public record FNTestClass( | ||
Class<?> testClass, | ||
Method beforeEach, | ||
Method afterEach, | ||
List<Method> testMethods | ||
) { | ||
|
||
public static FNTestClass fromClass(Class<?> testClass) { | ||
Method beforeEach = null; | ||
Method afterEach = null; | ||
List<Method> testMethods = new ArrayList<>(); | ||
|
||
for (Method method : testClass.getDeclaredMethods()) { | ||
if (method.isAnnotationPresent(FNTest.class)) { | ||
if (method.getParameterCount() != 1) continue; | ||
if (method.getParameterTypes()[0] != Player.class) continue; | ||
|
||
testMethods.add(method); | ||
continue; | ||
} | ||
|
||
if (method.isAnnotationPresent(FNBeforeEach.class)) { | ||
if (method.getParameterCount() != 1) continue; | ||
if (method.getParameterTypes()[0] != Player.class) continue; | ||
|
||
beforeEach = method; | ||
continue; | ||
} | ||
|
||
if (method.isAnnotationPresent(FNAfterEach.class)) { | ||
if (method.getParameterCount() != 1) continue; | ||
if (method.getParameterTypes()[0] != Player.class) continue; | ||
|
||
afterEach = method; | ||
} | ||
} | ||
|
||
return new FNTestClass(testClass, beforeEach, afterEach, testMethods); | ||
} | ||
|
||
public boolean runTests(Player player) { | ||
for (Method testMethod : testMethods) { | ||
Object testClassObj; | ||
try { | ||
testClassObj = testClass.getDeclaredConstructor().newInstance(); | ||
} catch (Exception e) { | ||
System.out.println("Failed to create test class instance: " + e.getMessage()); | ||
player.sendMessage("Failed to create test class instance: " + e.getMessage()); | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
|
||
FNTest fnTest = testMethod.getAnnotation(FNTest.class); | ||
if (fnTest.skip()) { | ||
System.out.println("Skipping test " + displayName(testMethod)); | ||
player.sendMessage("Skipping test " + displayName(testMethod)); | ||
continue; | ||
} | ||
|
||
|
||
long testStart = System.currentTimeMillis(); | ||
|
||
try { | ||
if (beforeEach != null) beforeEach.invoke(testClassObj, player); | ||
|
||
testMethod.invoke(testClassObj, player); | ||
|
||
if (afterEach != null) afterEach.invoke(testClassObj, player); | ||
} catch (InvocationTargetException e) { | ||
System.out.println("Test " + displayName(testMethod) + " failed with exception: " + e.getCause().getMessage()); | ||
player.sendMessage("Test " + displayName(testMethod) + " failed with exception: " + e.getCause().getMessage()); | ||
e.getCause().printStackTrace(); | ||
return false; | ||
} catch (Exception e) { | ||
System.out.println("Unexpected exception in test " + fnTest.name() + ": " + e.getMessage()); | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
|
||
long testEnd = System.currentTimeMillis(); | ||
System.out.println("Test " + displayName(testMethod) + " took " + (testEnd - testStart) + "ms"); | ||
player.sendMessage("Test " + displayName(testMethod) + " took " + (testEnd - testStart) + "ms"); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public String displayName(Method m) { | ||
if (!m.isAnnotationPresent(FNTest.class)) { | ||
return testClass.getSimpleName() + "#" + m.getName(); | ||
} | ||
|
||
FNTest fnTest = m.getAnnotation(FNTest.class); | ||
return testClass.getSimpleName() + "#" + m.getName() + " (" + fnTest.name() + ")"; | ||
} | ||
|
||
} |
13 changes: 0 additions & 13 deletions
13
src/main/java/de/oliver/fancynpcs/tests/FancyNpcsTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/de/oliver/fancynpcs/tests/annotations/FNAfterEach.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package de.oliver.fancynpcs.tests.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface FNAfterEach { | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/de/oliver/fancynpcs/tests/annotations/FNBeforeEach.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package de.oliver.fancynpcs.tests.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface FNBeforeEach { | ||
} | ||
|
14 changes: 14 additions & 0 deletions
14
src/main/java/de/oliver/fancynpcs/tests/annotations/FNTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package de.oliver.fancynpcs.tests.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface FNTest { | ||
String name(); | ||
|
||
boolean skip() default false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.