Tries to avoid injecting fields that are not defined in controller

This commit is contained in:
Guillaume Tâche
2024-11-30 19:04:01 +01:00
parent 17d112fb41
commit 7ec52cd175
62 changed files with 280 additions and 1537 deletions

View File

@@ -0,0 +1,32 @@
package com.github.gtache.fxml.compiler;
import java.util.List;
/**
* Info about a controller field
*/
public interface ControllerFieldInfo {
/**
* Returns the field name
*
* @return The name
*/
String name();
/**
* Returns whether the field is generic
*
* @return True if the field is generic, false if not (or raw)
*/
default boolean isGeneric() {
return !genericTypes().isEmpty();
}
/**
* Returns the generic types for the field
*
* @return The generic types as a list, empty if not generic or raw
*/
List<String> genericTypes();
}

View File

@@ -1,6 +1,5 @@
package com.github.gtache.fxml.compiler;
import java.util.List;
import java.util.Map;
/**
@@ -26,19 +25,19 @@ public interface ControllerInfo {
}
/**
* Returns a mapping of property name -> generic types
* Returns a mapping of property name -> field info
*
* @return A mapping of property name to generic types
* @return the mapping
*/
Map<String, List<String>> propertyGenericTypes();
Map<String, ControllerFieldInfo> fieldInfo();
/**
* Returns the generic types for the given property (null if not generic or raw)
* Returns the field information for the given property
*
* @param property The property
* @return The generic types
* @return The info, or null if not found
*/
default List<String> propertyGenericTypes(final String property) {
return propertyGenericTypes().get(property);
default ControllerFieldInfo fieldInfo(final String property) {
return fieldInfo().get(property);
}
}

View File

@@ -0,0 +1,31 @@
package com.github.gtache.fxml.compiler;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
class TestControllerFieldInfo {
private final ControllerFieldInfo info;
TestControllerFieldInfo() {
this.info = spy(ControllerFieldInfo.class);
}
@Test
void testIsGenericFalse() {
when(info.genericTypes()).thenReturn(List.of());
assertFalse(info.isGeneric());
}
@Test
void testIsGenericTrue() {
when(info.genericTypes()).thenReturn(List.of("A", "B", "C"));
assertTrue(info.isGeneric());
}
}

View File

@@ -2,35 +2,39 @@ package com.github.gtache.fxml.compiler;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class TestControllerInfo {
private final String string;
private final Map<String, Boolean> handlerHasArgument;
private final Map<String, List<String>> propertyGenericTypes;
private final List<String> genericTypes;
private final ControllerFieldInfo fieldInfo;
private final Map<String, ControllerFieldInfo> fieldInfoMap;
private final ControllerInfo controllerInfo;
TestControllerInfo() {
TestControllerInfo(@Mock final ControllerFieldInfo fieldInfo) {
this.string = "string";
this.handlerHasArgument = new HashMap<>();
this.propertyGenericTypes = new HashMap<>();
this.genericTypes = List.of("a", "b");
this.fieldInfoMap = new HashMap<>();
this.fieldInfo = Objects.requireNonNull(fieldInfo);
this.controllerInfo = spy(ControllerInfo.class);
}
@BeforeEach
void beforeEach() {
when(controllerInfo.handlerHasArgument()).thenReturn(handlerHasArgument);
when(controllerInfo.propertyGenericTypes()).thenReturn(propertyGenericTypes);
when(controllerInfo.fieldInfo()).thenReturn(fieldInfoMap);
}
@Test
@@ -51,13 +55,13 @@ class TestControllerInfo {
}
@Test
void testPropertyGenericTypesNull() {
assertNull(controllerInfo.propertyGenericTypes(string));
void testFieldInfoNull() {
assertNull(controllerInfo.fieldInfo(string));
}
@Test
void testPropertyGenericTypes() {
propertyGenericTypes.put(string, genericTypes);
assertEquals(genericTypes, controllerInfo.propertyGenericTypes(string));
void testFieldInfo() {
fieldInfoMap.put(string, fieldInfo);
assertEquals(fieldInfo, controllerInfo.fieldInfo(string));
}
}