test: Added some tests for annotations

This commit is contained in:
Jakub Žitník 2025-03-08 12:50:38 +01:00
parent e9f753da3d
commit 70c77cb5fb
Signed by: jzitnik
GPG Key ID: C577A802A6AF4EF3
10 changed files with 218 additions and 1 deletions

View File

@ -81,7 +81,7 @@ public class ItemBlockSupplier {
if (dropsList.get(key).equals(item.getId())) { if (dropsList.get(key).equals(item.getId())) {
block.setDrops(List.of(item)); block.setDrops(List.of(item));
} else { } else {
block.setDrops(List.of(getItem(dropsList.get(key), block))); // Todo: Add block reference block.setDrops(List.of(getItem(dropsList.get(key), block)));
} }
return block; return block;

View File

@ -0,0 +1,50 @@
package cz.jzitnik.game.entities.items;
import cz.jzitnik.game.annotations.BlockRegistry;
import cz.jzitnik.game.annotations.EntityRegistry;
import cz.jzitnik.game.annotations.ItemRegistry;
import cz.jzitnik.game.entities.Block;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.reflections.Reflections;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
class ItemBlockSupplierTest {
@Test
@DisplayName("All classes annotated with @BlockRegistry must extend class Block")
void allBlocksMustExtendBlock() {
Reflections reflections = new Reflections("cz.jzitnik.game.entities.items.registry");
Set<Class<?>> blockClasses = reflections.getTypesAnnotatedWith(BlockRegistry.class);
for (Class<?> clazz : blockClasses) {
var extendsClass = Block.class.isAssignableFrom(clazz);
assertTrue(extendsClass, "Class " + clazz.getName() + " annotated with @BlockRegistry does not extend " + Block.class.getName());
}
}
@Test
@DisplayName("All classes annotated with @EntityRegistry must extend class Block")
void allEntitiesMustExtendBlock() {
Reflections reflections = new Reflections("cz.jzitnik.game.entities.items.registry");
Set<Class<?>> blockClasses = reflections.getTypesAnnotatedWith(EntityRegistry.class);
for (Class<?> clazz : blockClasses) {
var extendsClass = Block.class.isAssignableFrom(clazz);
assertTrue(extendsClass, "Class " + clazz.getName() + " annotated with @EntityRegistry does not extend " + Block.class.getName());
}
}
@Test
@DisplayName("All classes annotated with @ItemRegistry must extend class Item")
void allItemsMustExtendItem() {
Reflections reflections = new Reflections("cz.jzitnik.game.entities.items.registry");
Set<Class<?>> blockClasses = reflections.getTypesAnnotatedWith(ItemRegistry.class);
for (Class<?> clazz : blockClasses) {
var extendsClass = Item.class.isAssignableFrom(clazz);
assertTrue(extendsClass, "Class " + clazz.getName() + " annotated with @ItemRegistry does not extend " + Item.class.getName());
}
}
}

View File

@ -0,0 +1,34 @@
package cz.jzitnik.game.handlers.pickup;
import cz.jzitnik.game.annotations.PickupHandler;
import cz.jzitnik.game.entities.items.ItemBlockSupplier;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.reflections.Reflections;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
class PickupHandlerProviderTest {
@Test
@DisplayName("All classes annotated with @PickupHandler must implement CustomPickupHandler")
void allClassesPickupImplementHandler() {
Reflections reflections = new Reflections("cz.jzitnik.game.handlers.pickup.handlers");
Set<Class<?>> handlerClasses = reflections.getTypesAnnotatedWith(PickupHandler.class);
for (Class<?> clazz : handlerClasses) {
boolean implementsClass = CustomPickupHandler.class.isAssignableFrom(clazz);
assertTrue(implementsClass, "Class " + clazz.getName() + " annotated with @PickupHandler does not implement CustomPickupHandler.");
}
}
@Test
@DisplayName("All items in annotation @PickupHandler must exist")
void allBlocksMustExist() {
PickupHandlerProvider pickupHandlerProvider = new PickupHandlerProvider();
for (String id : pickupHandlerProvider.pickupList.keySet()) {
assertDoesNotThrow(() -> ItemBlockSupplier.getItem(id), "Item " + id + " does not exist but is in annotation @PickupHandler");
}
}
}

View File

@ -14,6 +14,19 @@ import java.util.Set;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class PlaceHandlerTest { class PlaceHandlerTest {
@Test
@DisplayName("All classes annotated with @PlaceHandler must implement CustomPlaceHandler")
void allClassesPickupImplementHandler() {
Reflections reflections = new Reflections("cz.jzitnik.game.handlers.pickup.handlers");
Set<Class<?>> handlerClasses = reflections.getTypesAnnotatedWith(cz.jzitnik.game.annotations.PlaceHandler.class);
for (Class<?> clazz : handlerClasses) {
boolean implementsClass = CustomPlaceHandler.class.isAssignableFrom(clazz);
assertTrue(implementsClass, "Class " + clazz.getName() + " annotated with @PlaceHandler does not implement CustomPlaceHandler.");
}
}
@Test @Test
@DisplayName("All blocks in annotation @PlaceHandler must exist") @DisplayName("All blocks in annotation @PlaceHandler must exist")
void allBlocksMustExist() { void allBlocksMustExist() {

View File

@ -0,0 +1,23 @@
package cz.jzitnik.game.handlers.rightclick;
import cz.jzitnik.game.annotations.RightClickLogic;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.reflections.Reflections;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
class RightClickHandlerProviderTest {
@Test
@DisplayName("All classes annotated with @RightClickLogic must implement RightClickHandler")
void allClassesPickupImplementHandler() {
Reflections reflections = new Reflections("cz.jzitnik.game");
Set<Class<?>> handlerClasses = reflections.getTypesAnnotatedWith(RightClickLogic.class);
for (Class<?> clazz : handlerClasses) {
boolean implementsClass = RightClickHandler.class.isAssignableFrom(clazz);
assertTrue(implementsClass, "Class " + clazz.getName() + " annotated with @RightClickLogic does not implement RightClickHandler.");
}
}
}

View File

@ -0,0 +1,24 @@
package cz.jzitnik.game.logic;
import cz.jzitnik.game.annotations.CustomLogic;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.reflections.Reflections;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
class CustomLogicProviderTest {
@Test
@DisplayName("All classes annotated with @CustomLogic must implement CustomLogicInterface")
void allClassesLogicImplementLogic() {
Reflections reflections = new Reflections("cz.jzitnik.game.logic.services");
Set<Class<?>> handlerClasses = reflections.getTypesAnnotatedWith(CustomLogic.class);
for (Class<?> clazz : handlerClasses) {
boolean implementsClass = CustomLogicInterface.class.isAssignableFrom(clazz);
assertTrue(implementsClass, "Class " + clazz.getName() + " annotated with @CustomLogic does not implement CustomLogicInterface.");
}
}
}

View File

@ -0,0 +1,24 @@
package cz.jzitnik.game.logic.services.saplings;
import cz.jzitnik.game.annotations.BlockRegistry;
import cz.jzitnik.game.annotations.Sapling;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.reflections.Reflections;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
class SaplingLogicTest {
@Test
@DisplayName("All blocks annoteted with @Sapling must be annotated with @BlockRegistry")
void allBlocksMustExist() {
Reflections reflections = new Reflections("cz.jzitnik.game.entities.items.registry.blocks");
Set<Class<?>> handlerClasses = reflections.getTypesAnnotatedWith(Sapling.class);
for (Class<?> clazz : handlerClasses) {
var hasClass = clazz.isAnnotationPresent(BlockRegistry.class);
assertTrue(hasClass, "Class " + clazz.getName() + " annotated with @Sapling is not annotated with @BlockRegistry.");
}
}
}

View File

@ -1,12 +1,28 @@
package cz.jzitnik.game.mobs; package cz.jzitnik.game.mobs;
import cz.jzitnik.game.annotations.EntityHurtAnimationHandler;
import cz.jzitnik.game.entities.items.ItemBlockSupplier; import cz.jzitnik.game.entities.items.ItemBlockSupplier;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.reflections.Reflections;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class EntityHurtAnimationTest { class EntityHurtAnimationTest {
@Test
@DisplayName("All classes annotated with @EntityHurtAnimationHandler must implement EntityHurtAnimationChanger")
void allEntityHurtImplement() {
Reflections reflections = new Reflections("cz.jzitnik.game.mobs.services");
Set<Class<?>> handlerClasses = reflections.getTypesAnnotatedWith(EntityHurtAnimationHandler.class);
for (Class<?> clazz : handlerClasses) {
boolean implementsClass = EntityHurtAnimationChanger.class.isAssignableFrom(clazz);
assertTrue(implementsClass, "Class " + clazz.getName() + " annotated with @EntityHurtAnimationHandler does not implement EntityHurtAnimationChanger.");
}
}
@Test @Test
@DisplayName("Test that entities exists for all entity spawn handlers") @DisplayName("Test that entities exists for all entity spawn handlers")
void allEntitiesExists() { void allEntitiesExists() {

View File

@ -1,12 +1,28 @@
package cz.jzitnik.game.mobs; package cz.jzitnik.game.mobs;
import cz.jzitnik.game.annotations.EntityKillHandler;
import cz.jzitnik.game.entities.items.ItemBlockSupplier; import cz.jzitnik.game.entities.items.ItemBlockSupplier;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.reflections.Reflections;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class EntityKillTest { class EntityKillTest {
@Test
@DisplayName("All classes annotated with @EntityKillHandler must implement EntityKillInterface")
void allEntityKillImplement() {
Reflections reflections = new Reflections("cz.jzitnik.game.mobs.services");
Set<Class<?>> handlerClasses = reflections.getTypesAnnotatedWith(EntityKillHandler.class);
for (Class<?> clazz : handlerClasses) {
boolean implementsClass = EntityKillInterface.class.isAssignableFrom(clazz);
assertTrue(implementsClass, "Class " + clazz.getName() + " annotated with @EntityKillHandler does not implement EntityKillInterface.");
}
}
@Test @Test
@DisplayName("Test that entities exists for all entity spawn handlers") @DisplayName("Test that entities exists for all entity spawn handlers")
void allEntitiesExists() { void allEntitiesExists() {

View File

@ -1,11 +1,28 @@
package cz.jzitnik.game.mobs; package cz.jzitnik.game.mobs;
import cz.jzitnik.game.annotations.EntityLogic;
import cz.jzitnik.game.entities.items.ItemBlockSupplier; import cz.jzitnik.game.entities.items.ItemBlockSupplier;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.reflections.Reflections;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class EntityLogicProviderTest { class EntityLogicProviderTest {
@Test
@DisplayName("All classes annotated with @EntityLogic must implement EntityLogicInterface")
void allEntityKillImplement() {
Reflections reflections = new Reflections("cz.jzitnik.game.mobs.services");
Set<Class<?>> handlerClasses = reflections.getTypesAnnotatedWith(EntityLogic.class);
for (Class<?> clazz : handlerClasses) {
boolean implementsClass = EntityLogicInterface.class.isAssignableFrom(clazz);
assertTrue(implementsClass, "Class " + clazz.getName() + " annotated with @EntityLogic does not implement EntityLogicInterface.");
}
}
@Test @Test
@DisplayName("Test that entities exists for all entity spawn handlers") @DisplayName("Test that entities exists for all entity spawn handlers")
void allEntitiesExists() { void allEntitiesExists() {