Finishes testing, cleanup, adds license

This commit is contained in:
Guillaume Tâche
2024-12-28 17:25:33 +01:00
parent 58fbbff7cb
commit b5c38bea54
73 changed files with 1259 additions and 455 deletions

View File

@@ -5,7 +5,6 @@ import java.net.URI;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@@ -43,12 +42,12 @@ public final class ClassesFinder {
final var file = resource.getFile();
if (file.contains(".jar!")) {
final var jarFile = file.substring(0, file.indexOf(".jar!") + 4);
try (final var fs = FileSystems.newFileSystem(Paths.get(URI.create(jarFile)), classLoader)) {
try (final var fs = FileSystems.newFileSystem(Path.of(URI.create(jarFile)), classLoader)) {
classes.addAll(findClasses(fs.getPath(path), packageName));
}
} else {
final var filepath = START_FILE_PATTERN.matcher(file).replaceAll("");
classes.addAll(findClasses(Paths.get(filepath), packageName));
classes.addAll(findClasses(Path.of(filepath), packageName));
}
}
return classes;

View File

@@ -20,6 +20,7 @@ public record ControllerInfoImpl(String className, Map<String, Boolean> handlerH
/**
* Instantiates a new controller info
*
* @param className The controller class name
* @param handlerHasArgument The mapping of method name to true if the method has an argument
* @param fieldInfo The mapping of property name to controller field info

View File

@@ -22,6 +22,7 @@ public record GenerationRequestImpl(GenerationParameters parameters, ControllerI
String outputClassName) implements GenerationRequest {
/**
* Instantiates a new request
*
* @param parameters The generation parameters
* @param controllerInfo The controller info
* @param sourceInfo The source info

View File

@@ -9,8 +9,6 @@ import com.github.gtache.fxml.compiler.impl.internal.HelperProvider;
import java.util.Objects;
import java.util.function.Function;
//TODO handle binding (${})
/**
* Implementation of {@link Generator}
*/
@@ -28,6 +26,7 @@ public class GeneratorImpl implements Generator {
/**
* Used for testing
*
* @param helperProviderFactory The helper provider factory
*/
GeneratorImpl(final Function<GenerationProgress, HelperProvider> helperProviderFactory) {

View File

@@ -24,6 +24,7 @@ public record SourceInfoImpl(String generatedClassName, String controllerClassNa
/**
* Instantiates a new source info
*
* @param generatedClassName The generated class name
* @param controllerClassName The controller class name
* @param sourceFile The source file

View File

@@ -8,6 +8,7 @@ import com.github.gtache.fxml.compiler.parsing.ParsedProperty;
import java.util.SequencedCollection;
import static com.github.gtache.fxml.compiler.impl.internal.GenerationHelper.INDENT_8;
import static java.util.Objects.requireNonNull;
/**
@@ -40,14 +41,15 @@ final class ControllerInjector {
void injectControllerField(final String id, final String variable) {
switch (fieldInjectionType) {
case FACTORY ->
sb.append(" fieldMap.put(\"").append(id).append("\", ").append(variable).append(");\n");
case ASSIGN -> sb.append(" controller.").append(id).append(" = ").append(variable).append(";\n");
sb.append(INDENT_8).append("fieldMap.put(\"").append(id).append("\", ").append(variable).append(");\n");
case ASSIGN ->
sb.append(INDENT_8).append("controller.").append(id).append(" = ").append(variable).append(";\n");
case SETTERS -> {
final var setMethod = GenerationHelper.getSetMethod(id);
sb.append(" controller.").append(setMethod).append("(").append(variable).append(");\n");
sb.append(INDENT_8).append("controller.").append(setMethod).append("(").append(variable).append(");\n");
}
case REFLECTION ->
sb.append(" injectField(\"").append(id).append("\", ").append(variable).append(");\n");
sb.append(INDENT_8).append("injectField(\"").append(id).append("\", ").append(variable).append(");\n");
}
}
@@ -98,13 +100,13 @@ final class ControllerInjector {
case REFERENCE -> {
final var hasArgument = controllerInfo.handlerHasArgument(controllerMethod);
if (hasArgument) {
yield " " + parentVariable + "." + setMethod + "(controller::" + controllerMethod + ");\n";
yield INDENT_8 + parentVariable + "." + setMethod + "(controller::" + controllerMethod + ");\n";
} else {
yield " " + parentVariable + "." + setMethod + "(e -> controller." + controllerMethod + "());\n";
yield INDENT_8 + parentVariable + "." + setMethod + "(e -> controller." + controllerMethod + "());\n";
}
}
case REFLECTION ->
" " + parentVariable + "." + setMethod + "(e -> callEventHandlerMethod(\"" + controllerMethod + "\", e));\n";
INDENT_8 + parentVariable + "." + setMethod + "(e -> callEventHandlerMethod(\"" + controllerMethod + "\", e));\n";
};
}
@@ -120,10 +122,9 @@ final class ControllerInjector {
final var setMethod = GenerationHelper.getSetMethod(property.name());
final var controllerMethod = property.value().replace("#", "");
return switch (methodInjectionType) {
case REFERENCE ->
" " + parentVariable + "." + setMethod + "(controller::" + controllerMethod + ");\n";
case REFERENCE -> INDENT_8 + parentVariable + "." + setMethod + "(controller::" + controllerMethod + ");\n";
case REFLECTION ->
" " + parentVariable + "." + setMethod + "(e -> callCallbackMethod(\"" + controllerMethod + "\", e, " + argumentClazz + "));\n";
INDENT_8 + parentVariable + "." + setMethod + "(e -> callCallbackMethod(\"" + controllerMethod + "\", e, " + argumentClazz + "));\n";
};
}
}

View File

@@ -9,6 +9,7 @@ import java.util.Objects;
import java.util.SequencedCollection;
import static com.github.gtache.fxml.compiler.impl.internal.GenerationHelper.EXPRESSION_PREFIX;
import static com.github.gtache.fxml.compiler.impl.internal.GenerationHelper.INDENT_8;
import static java.util.Objects.requireNonNull;
/**
@@ -16,6 +17,7 @@ import static java.util.Objects.requireNonNull;
*/
final class FieldSetter {
private static final String CONTROLLER = "controller";
private final HelperProvider helperProvider;
private final ControllerFieldInjectionType fieldInjectionType;
private final StringBuilder sb;
@@ -63,10 +65,10 @@ final class FieldSetter {
final var value = property.value().replace(EXPRESSION_PREFIX, "");
final var split = value.split("\\.");
final var holderName = split[0];
if (Objects.equals(holderName, "controller")) {
sb.append(" ").append(parentVariable).append(".").append(methodName).append("(").append(value).append(");\n");
if (Objects.equals(holderName, CONTROLLER)) {
sb.append(INDENT_8).append(parentVariable).append(".").append(methodName).append("(").append(value).append(");\n");
} else {
throw new GenerationException("Unexpected variable holder : " + holderName + " ; expected : controller");
throw unexpectedHolderException(holderName);
}
}
@@ -83,11 +85,11 @@ final class FieldSetter {
final var value = property.value().replace(EXPRESSION_PREFIX, "");
final var split = value.split("\\.");
final var holderName = split[0];
if (Objects.equals(holderName, "controller")) {
if (Objects.equals(holderName, CONTROLLER)) {
final var getterName = GenerationHelper.getGetMethod(split[1]);
return " " + parentVariable + "." + methodName + "(controller." + getterName + "());\n";
return INDENT_8 + parentVariable + "." + methodName + "(" + CONTROLLER + "." + getterName + "());\n";
} else {
throw new GenerationException("Unexpected variable holder : " + holderName + " ; expected : controller");
throw unexpectedHolderException(holderName);
}
}
@@ -96,20 +98,24 @@ final class FieldSetter {
final var value = property.value().replace(EXPRESSION_PREFIX, "");
final var split = value.split("\\.");
final var holderName = split[0];
if (Objects.equals(holderName, "controller")) {
if (Objects.equals(holderName, CONTROLLER)) {
final var fieldName = split[1];
sb.append(" try {\n");
sb.append(" ").append(helperProvider.getCompatibilityHelper().getStartVar("java.lang.reflect.Field", 0))
.append("field = controller.getClass().getDeclaredField(\"").append(fieldName).append("\");\n");
.append("field = ").append(CONTROLLER).append(".getClass().getDeclaredField(\"").append(fieldName).append("\");\n");
sb.append(" field.setAccessible(true);\n");
sb.append(" final var value = (").append(fieldType).append(") field.get(controller);\n");
sb.append(" final var value = (").append(fieldType).append(") field.get(").append(CONTROLLER).append(");\n");
sb.append(" ").append(parentVariable).append(".").append(methodName).append("(value);\n");
sb.append(" } catch (final NoSuchFieldException | IllegalAccessException e) {\n");
sb.append(" throw new RuntimeException(e);\n");
sb.append(" }\n");
} else {
throw new GenerationException("Unexpected variable holder : " + holderName + " ; expected : controller");
throw unexpectedHolderException(holderName);
}
}
private static GenerationException unexpectedHolderException(final String holderName) {
return new GenerationException("Unexpected variable holder : " + holderName + " ; expected : " + CONTROLLER);
}
}

View File

@@ -3,8 +3,6 @@ package com.github.gtache.fxml.compiler.impl.internal;
import com.github.gtache.fxml.compiler.impl.GeneratorImpl;
import com.github.gtache.fxml.compiler.parsing.ParsedObject;
import com.github.gtache.fxml.compiler.parsing.ParsedProperty;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
import java.util.Map;
@@ -14,7 +12,8 @@ import java.util.Map;
*/
final class GenerationHelper {
private static final Logger logger = LogManager.getLogger(GenerationHelper.class);
static final String INDENT_8 = " ";
static final String INDENT_12 = " ";
static final String FX_ID = "fx:id";
static final String FX_VALUE = "fx:value";
static final String VALUE = "value";

View File

@@ -21,6 +21,9 @@ import static java.util.Objects.requireNonNull;
*/
public final class InitializationFormatter {
private static final String RESOURCE_BUNDLE_TYPE = "java.util.ResourceBundle";
private static final String RESOURCE_BUNDLE = "resourceBundle";
private final HelperProvider helperProvider;
private final GenerationRequest request;
private final StringBuilder sb;
@@ -106,7 +109,7 @@ public final class InitializationFormatter {
private ResourceBundleInfo getResourceBundleInfo() {
final var injectionType = request.parameters().resourceInjectionType();
return switch (injectionType) {
case CONSTRUCTOR -> new ResourceBundleInfo("java.util.ResourceBundle", "resourceBundle");
case CONSTRUCTOR -> new ResourceBundleInfo(RESOURCE_BUNDLE_TYPE, RESOURCE_BUNDLE);
case CONSTRUCTOR_FUNCTION ->
new ResourceBundleInfo("java.util.function.Function<String, String>", "resourceBundleFunction");
case CONSTRUCTOR_NAME -> new ResourceBundleInfo("String", "resourceBundleName");
@@ -140,7 +143,7 @@ public final class InitializationFormatter {
return hasDuplicateControllerClass(request.sourceInfo(), set);
}
private static boolean hasDuplicateControllerClass(final SourceInfo info, final Set<String> controllers) {
private static boolean hasDuplicateControllerClass(final SourceInfo info, final Set<? super String> controllers) {
final var controllerClass = info.controllerClassName();
if (controllers.contains(controllerClass)) {
return true;
@@ -202,15 +205,15 @@ public final class InitializationFormatter {
yield bundleVariable;
}
case CONSTRUCTOR_FUNCTION -> {
final var bundleVariable = variableProvider.getNextVariableName("resourceBundle");
sb.append(compatibilityHelper.getStartVar("java.util.ResourceBundle")).append(bundleVariable).append(" = java.util.ResourceBundle.getBundle(\"").append(include.resources()).append("\");\n");
final var bundleVariable = variableProvider.getNextVariableName(RESOURCE_BUNDLE);
sb.append(compatibilityHelper.getStartVar(RESOURCE_BUNDLE_TYPE)).append(bundleVariable).append(" = java.util.ResourceBundle.getBundle(\"").append(include.resources()).append("\");\n");
final var bundleFunctionVariable = variableProvider.getNextVariableName("resourceBundleFunction");
sb.append(compatibilityHelper.getStartVar("java.util.function.Function<String, String>")).append(bundleFunctionVariable).append(" = (java.util.function.Function<String, String>) s -> ").append(bundleVariable).append(".getString(s);\n");
yield bundleFunctionVariable;
}
case CONSTRUCTOR -> {
final var bundleVariable = variableProvider.getNextVariableName("resourceBundle");
sb.append(compatibilityHelper.getStartVar("java.util.ResourceBundle")).append(bundleVariable).append(" = java.util.ResourceBundle.getBundle(\"").append(include.resources()).append("\");\n");
final var bundleVariable = variableProvider.getNextVariableName(RESOURCE_BUNDLE);
sb.append(compatibilityHelper.getStartVar(RESOURCE_BUNDLE_TYPE)).append(bundleVariable).append(" = java.util.ResourceBundle.getBundle(\"").append(include.resources()).append("\");\n");
yield bundleVariable;
}
};

View File

@@ -325,7 +325,7 @@ final class ObjectFormatter {
if (!notDefinedChildren.isEmpty()) {
final var defaultProperty = ReflectionHelper.getDefaultProperty(parsedObject.className());
if (!constructorArgs.namedArgs().containsKey(defaultProperty)) {
final var property = new ParsedPropertyImpl(defaultProperty, null, "");
final var property = new ParsedPropertyImpl(defaultProperty, null, null);
helperProvider.getPropertyFormatter().formatProperty(property, notDefinedChildren, parsedObject, variableName);
}
}

View File

@@ -60,7 +60,7 @@ final class PropertyFormatter {
* The values should not contain any ParsedDefine
*
* @param property The property to format
* @param values The property's values
* @param values The property's values
* @param parent The property's parent object
* @param parentVariable The parent variable
* @throws GenerationException if an error occurs or if the values contain a ParsedDefine

View File

@@ -22,7 +22,7 @@ import static java.util.Objects.requireNonNull;
*/
final class SceneFormatter {
private static final ParsedProperty ROOT_PROPERTY = new ParsedPropertyImpl("root", null, "");
private static final ParsedProperty ROOT_PROPERTY = new ParsedPropertyImpl("root", null, null);
private final HelperProvider helperProvider;
private final StringBuilder sb;

View File

@@ -14,8 +14,7 @@ import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static com.github.gtache.fxml.compiler.impl.internal.GenerationHelper.FX_ID;
import static com.github.gtache.fxml.compiler.impl.internal.GenerationHelper.getSortedAttributes;
import static com.github.gtache.fxml.compiler.impl.internal.GenerationHelper.*;
import static java.util.Objects.requireNonNull;
/**
@@ -93,37 +92,37 @@ final class TriangleMeshFormatter {
private void setPoints(final String variableName, final Collection<Float> points) {
if (!points.isEmpty()) {
sb.append(" ").append(variableName).append(".getPoints().setAll(new float[]{").append(formatList(points)).append("});\n");
sb.append(INDENT_8).append(variableName).append(".getPoints().setAll(new float[]{").append(formatList(points)).append("});\n");
}
}
private void setTexCoords(final String variableName, final Collection<Float> texCoords) {
if (!texCoords.isEmpty()) {
sb.append(" ").append(variableName).append(".getTexCoords().setAll(new float[]{").append(formatList(texCoords)).append("});\n");
sb.append(INDENT_8).append(variableName).append(".getTexCoords().setAll(new float[]{").append(formatList(texCoords)).append("});\n");
}
}
private void setNormals(final String variableName, final Collection<Float> normals) {
if (!normals.isEmpty()) {
sb.append(" ").append(variableName).append(".getNormals().setAll(new float[]{").append(formatList(normals)).append("});\n");
sb.append(INDENT_8).append(variableName).append(".getNormals().setAll(new float[]{").append(formatList(normals)).append("});\n");
}
}
private void setFaces(final String variableName, final Collection<Integer> faces) {
if (!faces.isEmpty()) {
sb.append(" ").append(variableName).append(".getFaces().setAll(new int[]{").append(formatList(faces)).append("});\n");
sb.append(INDENT_8).append(variableName).append(".getFaces().setAll(new int[]{").append(formatList(faces)).append("});\n");
}
}
private void setFaceSmoothingGroups(final String variableName, final Collection<Integer> faceSmoothingGroups) {
if (!faceSmoothingGroups.isEmpty()) {
sb.append(" ").append(variableName).append(".getFaceSmoothingGroups().setAll(new int[]{").append(formatList(faceSmoothingGroups)).append("});\n");
sb.append(INDENT_8).append(variableName).append(".getFaceSmoothingGroups().setAll(new int[]{").append(formatList(faceSmoothingGroups)).append("});\n");
}
}
private void setVertexFormat(final String variableName, final VertexFormat vertexFormat) {
if (vertexFormat != null) {
sb.append(" ").append(variableName).append(".setVertexFormat(javafx.scene.shape.VertexFormat.").append(vertexFormat).append(");\n");
sb.append(INDENT_8).append(variableName).append(".setVertexFormat(javafx.scene.shape.VertexFormat.").append(vertexFormat).append(");\n");
}
}

View File

@@ -16,6 +16,7 @@ record VariableInfo(String id, ParsedObject parsedObject, String variableName, S
/**
* Instantiates a new variable info
*
* @param id The fx:id of the variable
* @param parsedObject The parsed object of the variable
* @param variableName The variable name

View File

@@ -33,7 +33,8 @@ class VariableProvider {
/**
* Adds a variable info
* @param id The variable id
*
* @param id The variable id
* @param variableInfo The variable info
*/
void addVariableInfo(final String id, final VariableInfo variableInfo) {
@@ -42,6 +43,7 @@ class VariableProvider {
/**
* Gets the variable info
*
* @param id The variable id
* @return The variable info
*/

View File

@@ -14,6 +14,7 @@ public record ParsedDefineImpl(List<ParsedObject> children) implements ParsedDef
/**
* Instantiates the define
*
* @param children The children
* @throws NullPointerException If the children are null
*/

View File

@@ -6,6 +6,8 @@ import com.github.gtache.fxml.compiler.parsing.ParsedProperty;
import java.util.HashMap;
import java.util.Map;
import static java.util.Objects.requireNonNull;
/**
* Implementation of {@link ParsedInclude}
*
@@ -42,6 +44,7 @@ public record ParsedIncludeImpl(Map<String, ParsedProperty> attributes) implemen
}
private static Map<String, ParsedProperty> createAttributes(final String source, final String resources, final String fxId) {
requireNonNull(source);
final var map = HashMap.<String, ParsedProperty>newHashMap(3);
map.put(SOURCE, new ParsedPropertyImpl(SOURCE, null, source));
if (resources != null) {

View File

@@ -25,6 +25,7 @@ public record ParsedObjectImpl(String className, Map<String, ParsedProperty> att
/**
* Instantiates a new object
*
* @param className The object class
* @param attributes The object attributes
* @param properties The object properties

View File

@@ -15,13 +15,13 @@ public record ParsedPropertyImpl(String name, String sourceType, String value) i
/**
* Instantiates a property
*
* @param name The property name
* @param sourceType The property source type
* @param value The property value
* @throws NullPointerException If the name or value is null
* @throws NullPointerException If the name is null
*/
public ParsedPropertyImpl {
requireNonNull(name);
requireNonNull(value);
}
}

View File

@@ -7,7 +7,6 @@ import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -30,7 +29,7 @@ class TestSourceInfoImpl {
TestSourceInfoImpl(@Mock final SourceInfo subInfo) {
this.generatedClassName = "class";
this.controllerClassName = "controller";
this.sourceFile = Paths.get("path");
this.sourceFile = Path.of("path");
this.includedSources = new ArrayList<>(List.of(subInfo));
this.sourceToSourceInfo = new HashMap<>(Map.of("source", subInfo));
this.requiresResourceBundle = false;

View File

@@ -26,6 +26,7 @@ import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class TestConstructorHelper {
private static final Annotation[][] EMPTY_ANNOTATIONS = new Annotation[0][];
private final ConstructorArgs args;
private final Constructor<Object>[] constructors;
private final ParsedObject parsedObject;
@@ -83,7 +84,7 @@ class TestConstructorHelper {
namedArgs.put("p1", new Parameter("p1", int.class, "0"));
namedArgs.put("p2", new Parameter("p2", String.class, "value2"));
when(constructors[0].getParameterAnnotations()).thenReturn(new Annotation[0][]);
when(constructors[0].getParameterAnnotations()).thenReturn(EMPTY_ANNOTATIONS);
when(constructors[1].getParameterAnnotations()).thenReturn(new Annotation[][]{{new NamedArgImpl("p1", "")}, {
new NamedArgImpl("p2", "value2")}});
when(constructors[1].getParameterTypes()).thenReturn(new Class[]{int.class, String.class});
@@ -95,8 +96,8 @@ class TestConstructorHelper {
void testGetMatchingConstructorArgsEmpty() {
final var namedArgs = new LinkedHashMap<String, Parameter>();
when(constructors[0].getParameterAnnotations()).thenReturn(new Annotation[0][]);
when(constructors[1].getParameterAnnotations()).thenReturn(new Annotation[0][]);
when(constructors[0].getParameterAnnotations()).thenReturn(EMPTY_ANNOTATIONS);
when(constructors[1].getParameterAnnotations()).thenReturn(EMPTY_ANNOTATIONS);
when(constructors[0].getParameterCount()).thenReturn(0);
when(constructors[1].getParameterCount()).thenReturn(1);
final var expectedArgs = new ConstructorArgs(constructors[0], namedArgs);
@@ -105,8 +106,8 @@ class TestConstructorHelper {
@Test
void testGetMatchingConstructorArgsNull() {
when(constructors[0].getParameterAnnotations()).thenReturn(new Annotation[0][]);
when(constructors[1].getParameterAnnotations()).thenReturn(new Annotation[0][]);
when(constructors[0].getParameterAnnotations()).thenReturn(EMPTY_ANNOTATIONS);
when(constructors[1].getParameterAnnotations()).thenReturn(EMPTY_ANNOTATIONS);
when(constructors[0].getParameterCount()).thenReturn(1);
when(constructors[1].getParameterCount()).thenReturn(1);
assertNull(ConstructorHelper.getMatchingConstructorArgs(constructors, propertyNames));

View File

@@ -50,7 +50,7 @@ class TestFontFormatter {
}
@BeforeEach
void beforeEach() throws GenerationException {
void beforeEach() {
when(helperProvider.getCompatibilityHelper()).thenReturn(compatibilityHelper);
when(helperProvider.getURLFormatter()).thenReturn(urlFormatter);
when(parsedObject.attributes()).thenReturn(attributes);
@@ -70,7 +70,7 @@ class TestFontFormatter {
@Test
void testHasProperties() {
final var properties = new LinkedHashMap<ParsedProperty, SequencedCollection<ParsedObject>>();
properties.put(new ParsedPropertyImpl("str", null, ""), List.of(parsedObject));
properties.put(new ParsedPropertyImpl("str", null, null), List.of(parsedObject));
when(parsedObject.properties()).thenReturn(properties);
assertThrows(GenerationException.class, () -> fontFormatter.formatFont(parsedObject, variableName));
}

View File

@@ -19,7 +19,6 @@ import java.util.SequencedCollection;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
@@ -50,7 +49,7 @@ class TestImageFormatter {
}
@BeforeEach
void beforeEach() throws GenerationException {
void beforeEach() {
when(helperProvider.getCompatibilityHelper()).thenReturn(compatibilityHelper);
when(helperProvider.getURLFormatter()).thenReturn(urlFormatter);
when(helperProvider.getVariableProvider()).thenReturn(variableProvider);
@@ -72,7 +71,7 @@ class TestImageFormatter {
@Test
void testHasProperties() {
final var properties = new LinkedHashMap<ParsedProperty, SequencedCollection<ParsedObject>>();
properties.put(new ParsedPropertyImpl("str", null, ""), List.of(parsedObject));
properties.put(new ParsedPropertyImpl("str", null, null), List.of(parsedObject));
when(parsedObject.properties()).thenReturn(properties);
assertThrows(GenerationException.class, () -> imageFormatter.formatImage(parsedObject, variableName));
}

View File

@@ -22,7 +22,6 @@ import java.util.Objects;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)

View File

@@ -28,8 +28,6 @@ import java.util.SequencedCollection;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
@@ -146,49 +144,49 @@ class TestObjectFormatter {
}
@Test
void testHandleIdPropertyTooManyChildren(@Mock final ControllerFieldInfo fieldInfo, @Mock PropertyFormatter propertyFormatter) {
void testHandleIdPropertyTooManyChildren(@Mock final ControllerFieldInfo fieldInfo, @Mock final PropertyFormatter propertyFormatter) {
when(helperProvider.getPropertyFormatter()).thenReturn(propertyFormatter);
final var className = "javafx.scene.control.Label";
final var value = "id";
final var properties = new LinkedHashMap<ParsedProperty, SequencedCollection<ParsedObject>>();
properties.put(new ParsedPropertyImpl("fx:id", null, ""), List.of(new ParsedTextImpl("value"), new ParsedTextImpl("value2")));
properties.put(new ParsedPropertyImpl("fx:id", null, null), List.of(new ParsedTextImpl("value"), new ParsedTextImpl("value2")));
final var parsedObject = new ParsedObjectImpl(className, Map.of(), properties, List.of());
when(controllerInfo.fieldInfo(value)).thenReturn(fieldInfo);
assertThrows(GenerationException.class, () -> objectFormatter.format(parsedObject, variableName));
}
@Test
void testHandleIdPropertyNoChildren(@Mock final ControllerFieldInfo fieldInfo, @Mock PropertyFormatter propertyFormatter) {
void testHandleIdPropertyNoChildren(@Mock final ControllerFieldInfo fieldInfo, @Mock final PropertyFormatter propertyFormatter) {
when(helperProvider.getPropertyFormatter()).thenReturn(propertyFormatter);
final var className = "javafx.scene.control.Label";
final var value = "id";
final var properties = new LinkedHashMap<ParsedProperty, SequencedCollection<ParsedObject>>();
properties.put(new ParsedPropertyImpl("fx:id", null, ""), List.of());
properties.put(new ParsedPropertyImpl("fx:id", null, null), List.of());
final var parsedObject = new ParsedObjectImpl(className, Map.of(), properties, List.of());
when(controllerInfo.fieldInfo(value)).thenReturn(fieldInfo);
assertThrows(GenerationException.class, () -> objectFormatter.format(parsedObject, variableName));
}
@Test
void testHandleIdPropertyNotText(@Mock final ControllerFieldInfo fieldInfo, @Mock PropertyFormatter propertyFormatter) {
void testHandleIdPropertyNotText(@Mock final ControllerFieldInfo fieldInfo, @Mock final PropertyFormatter propertyFormatter) {
when(helperProvider.getPropertyFormatter()).thenReturn(propertyFormatter);
final var className = "javafx.scene.control.Label";
final var value = "id";
final var properties = new LinkedHashMap<ParsedProperty, SequencedCollection<ParsedObject>>();
properties.put(new ParsedPropertyImpl("fx:id", null, ""), List.of(mock(ParsedObject.class)));
properties.put(new ParsedPropertyImpl("fx:id", null, null), List.of(mock(ParsedObject.class)));
final var parsedObject = new ParsedObjectImpl(className, Map.of(), properties, List.of());
when(controllerInfo.fieldInfo(value)).thenReturn(fieldInfo);
assertThrows(GenerationException.class, () -> objectFormatter.format(parsedObject, variableName));
}
@Test
void testHandleIdProperty(@Mock final ControllerFieldInfo fieldInfo, @Mock PropertyFormatter propertyFormatter) throws GenerationException {
void testHandleIdProperty(@Mock final ControllerFieldInfo fieldInfo, @Mock final PropertyFormatter propertyFormatter) throws GenerationException {
when(helperProvider.getPropertyFormatter()).thenReturn(propertyFormatter);
final var className = "javafx.scene.control.Label";
final var value = "id";
final var properties = new LinkedHashMap<ParsedProperty, SequencedCollection<ParsedObject>>();
final var define = mock(ParsedDefine.class);
properties.put(new ParsedPropertyImpl("fx:id", null, ""), List.of(define, new ParsedTextImpl(value)));
properties.put(new ParsedPropertyImpl("fx:id", null, null), List.of(define, new ParsedTextImpl(value)));
final var parsedObject = new ParsedObjectImpl(className, Map.of(), properties, List.of());
when(controllerInfo.fieldInfo(value)).thenReturn(fieldInfo);
doNothing().when(objectFormatter).format(define, "define");
@@ -317,8 +315,10 @@ class TestObjectFormatter {
when(sourceInfo.sourceToSourceInfo()).thenReturn(sourceToSourceInfo);
final var include = new ParsedIncludeImpl("source", "resources", "id");
objectFormatter.format(include, variableName);
final var expected = "include(source, resources) final javafx.scene.Parent variable = view.load();\n" +
"controllerClassNamecontroller = view.controller();\n";
final var expected = """
include(source, resources) final javafx.scene.Parent variable = view.load();
controllerClassNamecontroller = view.controller();
""";
assertEquals(expected, sb.toString());
verify(initializationFormatter).formatSubViewConstructorCall(include);
}
@@ -334,8 +334,10 @@ class TestObjectFormatter {
when(sourceInfo.sourceToSourceInfo()).thenReturn(sourceToSourceInfo);
final var include = new ParsedIncludeImpl(source, "resources", "id");
objectFormatter.format(include, variableName);
final var expected = "include(source, resources) final javafx.scene.Parent variable = view.load();\n" +
"controllerClassNamecontroller = view.controller();\ninject(idController, controller)inject(id, variable)";
final var expected = """
include(source, resources) final javafx.scene.Parent variable = view.load();
controllerClassNamecontroller = view.controller();
inject(idController, controller)inject(id, variable)""";
assertEquals(expected, sb.toString());
verify(initializationFormatter).formatSubViewConstructorCall(include);
verify(controllerInjector).injectControllerField("id", "variable");
@@ -441,7 +443,7 @@ class TestObjectFormatter {
@Test
void testFormatSimpleClassProperties() {
final var properties = new LinkedHashMap<ParsedProperty, SequencedCollection<ParsedObject>>();
properties.put(new ParsedPropertyImpl("str", null, ""), List.of(mock(ParsedObject.class)));
properties.put(new ParsedPropertyImpl("str", null, null), List.of(mock(ParsedObject.class)));
final var parsedObject = new ParsedObjectImpl("java.lang.String", Map.of(), properties, List.of());
assertThrows(GenerationException.class, () -> objectFormatter.format(parsedObject, variableName));
}
@@ -568,7 +570,7 @@ class TestObjectFormatter {
"initialValue", new ParsedPropertyImpl("initialValue", null, "3"));
final var label = new ParsedObjectImpl("javafx.scene.control.Label", Map.of(), new LinkedHashMap<>(), List.of());
final var properties = new LinkedHashMap<ParsedProperty, SequencedCollection<ParsedObject>>();
properties.put(new ParsedPropertyImpl("childrenUnmodifiable", null, ""), List.of(label));
properties.put(new ParsedPropertyImpl("childrenUnmodifiable", null, null), List.of(label));
final var define = mock(ParsedDefine.class);
doAnswer(i -> sb.append("define")).when(objectFormatter).format(define, "define");
final var parsedObject = new ParsedObjectImpl(className, attributes, properties, List.of(define));
@@ -577,7 +579,7 @@ class TestObjectFormatter {
final var expected = "definestartVarvariable = new javafx.scene.control.Spinner<bla>(1, 2, 3);\nproperty";
assertEquals(expected, sb.toString());
verify(propertyFormatter).formatProperty(new ParsedPropertyImpl("editable", null, "false"), parsedObject, variableName);
verify(propertyFormatter).formatProperty(new ParsedPropertyImpl("childrenUnmodifiable", null, ""), List.of(label), parsedObject, variableName);
verify(propertyFormatter).formatProperty(new ParsedPropertyImpl("childrenUnmodifiable", null, null), List.of(label), parsedObject, variableName);
verify(objectFormatter).format(define, "define");
verify(compatibilityHelper).getStartVar(parsedObject);
verify(reflectionHelper).getGenericTypes(parsedObject);
@@ -591,7 +593,7 @@ class TestObjectFormatter {
final var className = "javafx.scene.control.Spinner";
final var attributes = Map.<String, ParsedProperty>of();
final var label = new ParsedObjectImpl("javafx.scene.control.Label", Map.of(), new LinkedHashMap<>(), List.of());
final var property = new ParsedPropertyImpl("childrenUnmodifiable", null, "");
final var property = new ParsedPropertyImpl("childrenUnmodifiable", null, null);
final var properties = new LinkedHashMap<ParsedProperty, SequencedCollection<ParsedObject>>();
properties.put(property, List.of(label, label));
final var parsedObject = new ParsedObjectImpl(className, attributes, properties, List.of());
@@ -617,7 +619,7 @@ class TestObjectFormatter {
objectFormatter.format(parsedObject, variableName);
final var expected = "startVarvariable = new javafx.scene.layout.StackPane();\nproperty";
assertEquals(expected, sb.toString());
verify(propertyFormatter).formatProperty(new ParsedPropertyImpl("children", null, ""), children, parsedObject, variableName);
verify(propertyFormatter).formatProperty(new ParsedPropertyImpl("children", null, null), children, parsedObject, variableName);
verify(compatibilityHelper).getStartVar(parsedObject);
verify(reflectionHelper).getGenericTypes(parsedObject);
}

View File

@@ -28,8 +28,6 @@ import java.util.Objects;
import java.util.SequencedCollection;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
@@ -211,7 +209,7 @@ class TestPropertyFormatter {
void testFormatListSingleValue() throws GenerationException {
final var text = new ParsedTextImpl("text");
final var values = List.of(text);
final var emptyProperty = new ParsedPropertyImpl("name", null, "");
final var emptyProperty = new ParsedPropertyImpl("name", null, null);
final var newProperty = new ParsedPropertyImpl("name", null, text.text());
doNothing().when(propertyFormatter).formatProperty(newProperty, rootObject, variableName);
propertyFormatter.formatProperty(emptyProperty, values, rootObject, variableName);
@@ -222,7 +220,7 @@ class TestPropertyFormatter {
void testFormatListSingleValueStatic() throws GenerationException {
final var text = new ParsedTextImpl("text");
final var values = List.of(text);
final var emptyProperty = new ParsedPropertyImpl("name", String.class.getName(), "");
final var emptyProperty = new ParsedPropertyImpl("name", String.class.getName(), null);
final var newProperty = new ParsedPropertyImpl("name", String.class.getName(), text.text());
doNothing().when(propertyFormatter).formatProperty(newProperty, rootObject, variableName);
propertyFormatter.formatProperty(emptyProperty, values, rootObject, variableName);
@@ -267,7 +265,7 @@ class TestPropertyFormatter {
final var className = "javafx.scene.layout.BorderPane";
final var properties = new LinkedHashMap<ParsedProperty, SequencedCollection<ParsedObject>>();
final var parsedObject = new ParsedObjectImpl(className, Map.of(), properties, List.of());
when(property.name()).thenReturn("blabla");
when(property.name()).thenReturn("abc");
final var child = new ParsedObjectImpl("javafx.scene.control.Label", Map.of(), new LinkedHashMap<>(), List.of());
final var objects = List.of(child);
assertThrows(GenerationException.class, () -> propertyFormatter.formatProperty(property, objects, parsedObject, variableName));
@@ -296,7 +294,7 @@ class TestPropertyFormatter {
final var className = "javafx.scene.layout.HBox";
final var properties = new LinkedHashMap<ParsedProperty, SequencedCollection<ParsedObject>>();
final var parsedObject = new ParsedObjectImpl(className, Map.of(), properties, List.of());
when(property.name()).thenReturn("blabla");
when(property.name()).thenReturn("abc");
final var child = new ParsedObjectImpl("javafx.scene.control.Label", Map.of(), new LinkedHashMap<>(), List.of());
final var objects = List.of(child, child);
assertThrows(GenerationException.class, () -> propertyFormatter.formatProperty(property, objects, parsedObject, variableName));
@@ -326,7 +324,7 @@ class TestPropertyFormatter {
final var className = "javafx.scene.layout.BorderPane";
final var properties = new LinkedHashMap<ParsedProperty, SequencedCollection<ParsedObject>>();
final var parsedObject = new ParsedObjectImpl(className, Map.of(), properties, List.of());
when(property.name()).thenReturn("blabla");
when(property.name()).thenReturn("abc");
when(property.sourceType()).thenReturn("javafx.scene.layout.GridPane");
final var child = new ParsedObjectImpl("javafx.scene.control.Label", Map.of(), new LinkedHashMap<>(), List.of());
final var objects = List.of(child);

View File

@@ -22,8 +22,6 @@ import java.util.SequencedMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
@@ -81,33 +79,33 @@ class TestSceneFormatter {
@Test
void testUnknownAttribute() {
properties.put(new ParsedPropertyImpl("root", null, ""), List.of(parsedObject));
properties.put(new ParsedPropertyImpl("root", null, null), List.of(parsedObject));
attributes.put("unknown", new ParsedPropertyImpl("unknown", null, "value"));
assertThrows(GenerationException.class, () -> sceneFormatter.formatScene(parsedObject, variableName));
}
@Test
void testNonRootProperty() {
properties.put(new ParsedPropertyImpl("property", null, ""), List.of(parsedObject));
properties.put(new ParsedPropertyImpl("property", null, null), List.of(parsedObject));
assertThrows(GenerationException.class, () -> sceneFormatter.formatScene(parsedObject, variableName));
}
@Test
void testRootPropertyNonEmptyChildren() {
properties.put(new ParsedPropertyImpl("root", null, ""), List.of(parsedObject));
properties.put(new ParsedPropertyImpl("root", null, null), List.of(parsedObject));
children.add(parsedObject);
assertThrows(GenerationException.class, () -> sceneFormatter.formatScene(parsedObject, variableName));
}
@Test
void testRootPropertyEmptyChild() {
properties.put(new ParsedPropertyImpl("root", null, ""), List.of());
properties.put(new ParsedPropertyImpl("root", null, null), List.of());
assertThrows(GenerationException.class, () -> sceneFormatter.formatScene(parsedObject, variableName));
}
@Test
void testRootPropertyNonOneChild() {
properties.put(new ParsedPropertyImpl("root", null, ""), List.of(parsedObject, parsedObject));
properties.put(new ParsedPropertyImpl("root", null, null), List.of(parsedObject, parsedObject));
assertThrows(GenerationException.class, () -> sceneFormatter.formatScene(parsedObject, variableName));
}
@@ -127,7 +125,7 @@ class TestSceneFormatter {
void testDefaultAttributesProperty() throws GenerationException {
final var rootObject = mock(ParsedObject.class);
final var define = mock(ParsedDefine.class);
properties.put(new ParsedPropertyImpl("root", null, ""), List.of(define, rootObject));
properties.put(new ParsedPropertyImpl("root", null, null), List.of(define, rootObject));
sceneFormatter.formatScene(parsedObject, variableName);
final var expected = "definerootjavafx.scene.Scenevariable = new javafx.scene.Scene(root, -1.0, -1.0, javafx.scene.paint.Color.valueOf(\"0xffffffff\"));\n";
assertEquals(expected, sb.toString());
@@ -151,15 +149,17 @@ class TestSceneFormatter {
@Test
void testAllAttributesProperty() throws GenerationException {
final var rootObject = mock(ParsedObject.class);
properties.put(new ParsedPropertyImpl("root", null, ""), List.of(rootObject));
properties.put(new ParsedPropertyImpl("root", null, null), List.of(rootObject));
attributes.put("width", new ParsedPropertyImpl("width", null, "100"));
attributes.put("height", new ParsedPropertyImpl("height", null, "200"));
attributes.put("fill", new ParsedPropertyImpl("fill", null, "#FF0000"));
attributes.put("stylesheets", new ParsedPropertyImpl("stylesheets", null, "style.css"));
sceneFormatter.formatScene(parsedObject, variableName);
final var expected = "rootjavafx.scene.Scenevariable = new javafx.scene.Scene(root, 100.0, 200.0, javafx.scene.paint.Color.valueOf(\"#FF0000\"));\n" +
"style.cssjava.util.List<String>stylesheets = variable.getStyleSheets();\n" +
" stylesheets.addAll(listof(1, 2));\n";
final var expected = """
rootjavafx.scene.Scenevariable = new javafx.scene.Scene(root, 100.0, 200.0, javafx.scene.paint.Color.valueOf("#FF0000"));
style.cssjava.util.List<String>stylesheets = variable.getStyleSheets();
stylesheets.addAll(listof(1, 2));
""";
assertEquals(expected, sb.toString());
verify(objectFormatter).format(rootObject, "root");
}

View File

@@ -45,7 +45,7 @@ class TestTriangleMeshFormatter {
}
@BeforeEach
void beforeEach() throws GenerationException {
void beforeEach() {
when(helperProvider.getCompatibilityHelper()).thenReturn(compatibilityHelper);
when(parsedObject.attributes()).thenReturn(attributes);
when(parsedObject.children()).thenReturn(List.of());
@@ -62,7 +62,7 @@ class TestTriangleMeshFormatter {
@Test
void testFormatProperties() {
final var map = new LinkedHashMap<ParsedProperty, SequencedCollection<ParsedObject>>();
map.put(new ParsedPropertyImpl("str", null, ""), List.of());
map.put(new ParsedPropertyImpl("str", null, null), List.of());
when(parsedObject.properties()).thenReturn(map);
assertThrows(GenerationException.class, () -> triangleMeshFormatter.formatTriangleMesh(parsedObject, variableName));
}

View File

@@ -44,7 +44,7 @@ class TestURLFormatter {
}
@BeforeEach
void beforeEach() throws GenerationException {
void beforeEach() {
when(helperProvider.getCompatibilityHelper()).thenReturn(compatibilityHelper);
when(helperProvider.getVariableProvider()).thenReturn(variableProvider);
when(variableProvider.getNextVariableName("url")).thenReturn("url1", "url2");
@@ -78,7 +78,7 @@ class TestURLFormatter {
@Test
void testFormatURLObjectProperties() {
final var map = new LinkedHashMap<ParsedProperty, SequencedCollection<ParsedObject>>();
map.put(new ParsedPropertyImpl("str", null, ""), List.of());
map.put(new ParsedPropertyImpl("str", null, null), List.of());
when(parsedObject.properties()).thenReturn(map);
assertThrows(GenerationException.class, () -> urlFormatter.formatURL(parsedObject, variableName));
}

View File

@@ -84,7 +84,7 @@ class TestWebViewFormatter {
@Test
void testFormatHasProperty() {
final var properties = new LinkedHashMap<ParsedProperty, SequencedCollection<ParsedObject>>();
properties.put(new ParsedPropertyImpl("str", null, ""), List.of(parsedObject));
properties.put(new ParsedPropertyImpl("str", null, null), List.of(parsedObject));
when(parsedObject.properties()).thenReturn(properties);
assertThrows(GenerationException.class, () -> webViewFormatter.formatWebView(parsedObject, variableName));
}

View File

@@ -58,7 +58,7 @@ class TestParsedIncludeImpl {
@Test
void testIllegal() {
assertThrows(NullPointerException.class, () -> new ParsedIncludeImpl(null));
assertThrows(NullPointerException.class, () -> new ParsedIncludeImpl(null, "", ""));
assertThrows(NullPointerException.class, () -> new ParsedIncludeImpl(null, "", null));
assertDoesNotThrow(() -> new ParsedIncludeImpl("", null, null));
final var emptyMap = Map.<String, ParsedProperty>of();
assertThrows(IllegalArgumentException.class, () -> new ParsedIncludeImpl(emptyMap));

View File

@@ -31,7 +31,7 @@ class TestParsedObjectImpl {
this.attributes = new LinkedHashMap<>();
this.attributes.put("name", property);
this.properties = new LinkedHashMap<>();
this.properties.put(new ParsedPropertyImpl("property", null, ""), List.of(object));
this.properties.put(new ParsedPropertyImpl("property", null, null), List.of(object));
this.objects = new ArrayList<>(List.of(define));
this.parsedObject = new ParsedObjectImpl(clazz, attributes, properties, objects);
}

View File

@@ -30,6 +30,6 @@ class TestParsedPropertyImpl {
void testIllegal() {
assertThrows(NullPointerException.class, () -> new ParsedPropertyImpl(null, sourceType, value));
assertDoesNotThrow(() -> new ParsedPropertyImpl(name, null, value));
assertThrows(NullPointerException.class, () -> new ParsedPropertyImpl(name, sourceType, null));
assertDoesNotThrow(() -> new ParsedPropertyImpl(name, sourceType, null));
}
}