From a4d0f817bc6aa848d5bcd2e1e6d77d742fe6deea Mon Sep 17 00:00:00 2001 From: Kamil Patryk Kozakowski Date: Sat, 19 Jul 2025 23:02:54 +0200 Subject: [PATCH] tests --- .../KamilEnumStateMachineConfiguration.java | 2 +- .../ast/in/AstFileFinderTest.java | 303 ++++++++++++++++++ 2 files changed, 304 insertions(+), 1 deletion(-) create mode 100644 src/test/java/click/kamil/springstatemachineexporter/ast/in/AstFileFinderTest.java diff --git a/src/main/java/click/kamil/springstatemachineexporter/statemachine/KamilEnumStateMachineConfiguration.java b/src/main/java/click/kamil/springstatemachineexporter/statemachine/KamilEnumStateMachineConfiguration.java index 395a97a..12392e3 100644 --- a/src/main/java/click/kamil/springstatemachineexporter/statemachine/KamilEnumStateMachineConfiguration.java +++ b/src/main/java/click/kamil/springstatemachineexporter/statemachine/KamilEnumStateMachineConfiguration.java @@ -14,7 +14,7 @@ import org.springframework.statemachine.listener.StateMachineListenerAdapter; import org.springframework.statemachine.state.State; @Configuration -@EnableStateMachineFactory(name = "simpleEnumStateMachineFactory") +@EnableStateMachineFactory(name = "kamilEnumStateMachineFactory") @Slf4j class KamilEnumStateMachineConfiguration extends StateMachineConfigurerAdapter { @Override diff --git a/src/test/java/click/kamil/springstatemachineexporter/ast/in/AstFileFinderTest.java b/src/test/java/click/kamil/springstatemachineexporter/ast/in/AstFileFinderTest.java new file mode 100644 index 0000000..a916044 --- /dev/null +++ b/src/test/java/click/kamil/springstatemachineexporter/ast/in/AstFileFinderTest.java @@ -0,0 +1,303 @@ +package click.kamil.springstatemachineexporter.ast.in; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; + +class AstFileFinderTest { + + private static final String CLASS_WITH_ANNOTATION = """ + @Service + public class TestService { + public void doSomething() { + // implementation + } + } + """; + + private static final String CLASS_WITH_MULTIPLE_ANNOTATIONS = """ + @Service + @Component + public class TestService { + public void doSomething() { + // implementation + } + } + """; + + private static final String INTERFACE_WITH_ANNOTATION = """ + @FunctionalInterface + public interface TestInterface { + void doSomething(); + } + """; + + private static final String CLASS_WITHOUT_ANNOTATION = """ + public class TestService { + public void doSomething() { + // implementation + } + } + """; + + private static final String CLASS_WITH_DIFFERENT_ANNOTATION = """ + @Repository + public class TestRepository { + public void doSomething() { + // implementation + } + } + """; + + private static final String CLASS_WITH_FULLY_QUALIFIED_ANNOTATION = """ + @org.springframework.stereotype.Service + public class TestService { + public void doSomething() { + // implementation + } + } + """; + + private static final String MULTIPLE_CLASSES = """ + @Service + public class TestService { + public void doSomething() { + // implementation + } + } + + public class AnotherClass { + // no annotation + } + """; + + private static final String CLASS_WITH_ANNOTATION_AND_FIELDS = """ + @Service + public class TestService { + private String name; + + public void doSomething() { + // implementation + } + } + """; + + private static final String EMPTY_SOURCE = ""; + + private static final String INVALID_JAVA_SYNTAX = """ + @Service + public class TestService { + // missing closing brace + """; + + @Test + void shouldReturnTrue_whenClassHasMatchingAnnotation() { + AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); + List annotationNames = Arrays.asList("Service"); + + boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames); + + assertThat(result).isTrue(); + } + + @Test + void shouldReturnTrue_whenClassHasMultipleMatchingAnnotations() { + AstFileFinder finder = new AstFileFinder(CLASS_WITH_MULTIPLE_ANNOTATIONS); + List annotationNames = Arrays.asList("Service", "Component"); + + boolean result = finder.hasClassWithAnnotation(CLASS_WITH_MULTIPLE_ANNOTATIONS, annotationNames); + + assertThat(result).isTrue(); + } + + @Test + void shouldReturnFalse_whenClassHasNoAnnotation() { + AstFileFinder finder = new AstFileFinder(CLASS_WITHOUT_ANNOTATION); + List annotationNames = Arrays.asList("Service"); + + boolean result = finder.hasClassWithAnnotation(CLASS_WITHOUT_ANNOTATION, annotationNames); + + assertThat(result).isFalse(); + } + + @Test + void shouldReturnFalse_whenClassHasDifferentAnnotation() { + AstFileFinder finder = new AstFileFinder(CLASS_WITH_DIFFERENT_ANNOTATION); + List annotationNames = Arrays.asList("Service"); + + boolean result = finder.hasClassWithAnnotation(CLASS_WITH_DIFFERENT_ANNOTATION, annotationNames); + + assertThat(result).isFalse(); + } + + @Test + void shouldReturnTrue_whenClassHasFullyQualifiedAnnotation() { + AstFileFinder finder = new AstFileFinder(CLASS_WITH_FULLY_QUALIFIED_ANNOTATION); + List annotationNames = Arrays.asList("org.springframework.stereotype.Service"); + + boolean result = finder.hasClassWithAnnotation(CLASS_WITH_FULLY_QUALIFIED_ANNOTATION, annotationNames); + + assertThat(result).isTrue(); + } + + @Test + void shouldReturnTrue_whenMultipleClassesAndOneHasAnnotation() { + AstFileFinder finder = new AstFileFinder(MULTIPLE_CLASSES); + List annotationNames = Arrays.asList("Service"); + + boolean result = finder.hasClassWithAnnotation(MULTIPLE_CLASSES, annotationNames); + + assertThat(result).isTrue(); + } + + @Test + void shouldReturnFalse_whenInterfaceHasAnnotation() { + AstFileFinder finder = new AstFileFinder(INTERFACE_WITH_ANNOTATION); + List annotationNames = Arrays.asList("FunctionalInterface"); + + boolean result = finder.hasClassWithAnnotation(INTERFACE_WITH_ANNOTATION, annotationNames); + + assertThat(result).isFalse(); + } + + @Test + void shouldReturnFalse_whenEmptyAnnotationList() { + AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); + List annotationNames = Collections.emptyList(); + + boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames); + + assertThat(result).isFalse(); + } + + @Test + void shouldReturnFalse_whenEmptySource() { + AstFileFinder finder = new AstFileFinder(EMPTY_SOURCE); + List annotationNames = Arrays.asList("Service"); + + boolean result = finder.hasClassWithAnnotation(EMPTY_SOURCE, annotationNames); + + assertThat(result).isFalse(); + } + + @Test + void shouldThrowException_whenNullAnnotationList() { + AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); + + // when & then + assertThatThrownBy(() -> + finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, null) + ).isInstanceOf(NullPointerException.class); + } + + @Test + void shouldReturnTrue_whenClassHasAnnotationAndFields() { + AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION_AND_FIELDS); + List annotationNames = Arrays.asList("Service"); + + boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION_AND_FIELDS, annotationNames); + + assertThat(result).isTrue(); + } + + @Test + void shouldHandleInvalidJavaSyntax() { + AstFileFinder finder = new AstFileFinder(INVALID_JAVA_SYNTAX); + List annotationNames = Arrays.asList("Service"); + + boolean result = finder.hasClassWithAnnotation(INVALID_JAVA_SYNTAX, annotationNames); + + // AST parser might still create a valid AST even with syntax errors + // The result depends on how the parser handles the invalid syntax + assertThat(result).isNotNull(); + } + + @Test + void shouldReturnTrue_whenAnnotationNameMatchesCaseInsensitive() { + AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); + List annotationNames = Arrays.asList("service"); // lowercase + + boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames); + + assertThat(result).isFalse(); // AST parser is case-sensitive + } + + @Test + void shouldReturnTrue_whenMultipleAnnotationNamesProvided() { + AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); + List annotationNames = Arrays.asList("Component", "Service", "Repository"); + + boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames); + + assertThat(result).isTrue(); + } + + @Test + void shouldReturnFalse_whenNoMatchingAnnotationNames() { + AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); + List annotationNames = Arrays.asList("Component", "Repository", "Controller"); + + boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames); + + assertThat(result).isFalse(); + } + + @Test + void shouldReturnFalse_whenSourceContainsOnlyComments() { + String sourceWithComments = """ + // This is a comment + /* Another comment */ + /** JavaDoc comment */ + """; + AstFileFinder finder = new AstFileFinder(sourceWithComments); + List annotationNames = Arrays.asList("Service"); + + boolean result = finder.hasClassWithAnnotation(sourceWithComments, annotationNames); + + assertThat(result).isFalse(); + } + + @Test + void shouldReturnFalse_whenSourceContainsOnlyPackageDeclaration() { + String sourceWithPackage = "package com.example;"; + AstFileFinder finder = new AstFileFinder(sourceWithPackage); + List annotationNames = Arrays.asList("Service"); + + boolean result = finder.hasClassWithAnnotation(sourceWithPackage, annotationNames); + + assertThat(result).isFalse(); + } + + @Test + void shouldReturnTrue_whenClassHasAnnotationWithParameters() { + String classWithAnnotatedParams = """ + @Service("testService") + public class TestService { + public void doSomething() { + // implementation + } + } + """; + AstFileFinder finder = new AstFileFinder(classWithAnnotatedParams); + List annotationNames = Arrays.asList("Service"); + + boolean result = finder.hasClassWithAnnotation(classWithAnnotatedParams, annotationNames); + + assertThat(result).isTrue(); + } + + @Test + void shouldReturnFalse_whenAnnotationListContainsNull() { + AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); + List annotationNames = Arrays.asList("Service", null, "Component"); + + boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames); + + assertThat(result).isTrue(); // Should still find "Service" annotation + } +} \ No newline at end of file