test(annotations): New tests for annotations

Added some new test for annotations @AutoTransient and @ThreadProvider.
These tests ensure that @AutoTransient is only used on transient fields
and @ThreadProvider is used on class that extend Thread.
This commit is contained in:
Jakub Žitník 2025-03-16 21:02:03 +01:00
parent 53a3ae43f3
commit 391cda0550
Signed by: jzitnik
GPG Key ID: C577A802A6AF4EF3
2 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package cz.jzitnik.game.core.autotransient;
import cz.jzitnik.game.annotations.AutoTransient;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.reflections.Reflections;
import org.reflections.scanners.Scanners;
import org.reflections.util.ConfigurationBuilder;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
class AutoTransientSupportTest {
@Test
@DisplayName("All fields annotated with @AutoTransient must be transient.")
void checkAutoTransientAnnotation() {
Reflections reflections = new Reflections(
new ConfigurationBuilder()
.forPackages("cz.jzitnik.game")
.addScanners(Scanners.FieldsAnnotated) // Explicitly add the scanner for fields
);
Set<Field> fields = reflections.getFieldsAnnotatedWith(AutoTransient.class);
for (Field field : fields) {
if (!Modifier.isTransient(field.getModifiers())) {
fail("Field '" + field.getName() + "' in class " + field.getClass().getName() +
" is annotated with @AutoTransient but is not transient.");
}
}
}
}

View File

@ -0,0 +1,25 @@
package cz.jzitnik.game.threads;
import cz.jzitnik.game.annotations.ThreadRegistry;
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 ThreadProviderTest {
@Test
@DisplayName("All classes annotated with @ThreadRegistry must extend Thread")
void threadRegistryTest() {
Reflections reflections = new Reflections("cz.jzitnik.game.threads.list");
Set<Class<?>> classes = reflections.getTypesAnnotatedWith(ThreadRegistry.class);
for (Class<?> clazz : classes) {
if (!Thread.class.isAssignableFrom(clazz)) {
fail("Class " + clazz.getName() + " is annotated with @ThreadRegistry but does not extend Thread.");
}
}
}
}