Adds maven wrapper ; rework internal a bit ; Adds all tests for core

This commit is contained in:
Guillaume Tâche
2024-12-25 22:40:01 +01:00
parent 38056c43f7
commit 58fbbff7cb
99 changed files with 5028 additions and 1649 deletions

View File

@@ -1,11 +1,11 @@
package com.github.gtache.fxml.compiler.maven;
import com.github.gtache.fxml.compiler.ControllerFieldInjectionType;
import com.github.gtache.fxml.compiler.ControllerInjectionType;
import com.github.gtache.fxml.compiler.ControllerMethodsInjectionType;
import com.github.gtache.fxml.compiler.ResourceBundleInjectionType;
import com.github.gtache.fxml.compiler.compatibility.impl.GenerationCompatibilityImpl;
import com.github.gtache.fxml.compiler.impl.ControllerFieldInjectionTypes;
import com.github.gtache.fxml.compiler.impl.ControllerInjectionTypes;
import com.github.gtache.fxml.compiler.impl.ControllerMethodsInjectionType;
import com.github.gtache.fxml.compiler.impl.GenerationParametersImpl;
import com.github.gtache.fxml.compiler.impl.ResourceBundleInjectionTypes;
import com.github.gtache.fxml.compiler.maven.internal.CompilationInfo;
import com.github.gtache.fxml.compiler.maven.internal.CompilationInfoProvider;
import com.github.gtache.fxml.compiler.maven.internal.Compiler;
@@ -21,6 +21,9 @@ import org.apache.maven.project.MavenProject;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Main mojo for FXML compiler
@@ -41,31 +44,45 @@ public class FXMLCompilerMojo extends AbstractMojo {
private boolean useImageInputStreamConstructor;
@Parameter(property = "controller-injection", defaultValue = "INSTANCE", required = true)
private ControllerInjectionTypes controllerInjectionType;
private ControllerInjectionType controllerInjectionType;
@Parameter(property = "field-injection", defaultValue = "REFLECTION", required = true)
private ControllerFieldInjectionTypes fieldInjectionType;
private ControllerFieldInjectionType fieldInjectionType;
@Parameter(property = "method-injection", defaultValue = "REFLECTION", required = true)
private ControllerMethodsInjectionType methodInjectionType;
@Parameter(property = "resource-injection", defaultValue = "CONSTRUCTOR", required = true)
private ResourceBundleInjectionTypes resourceInjectionType;
private ResourceBundleInjectionType resourceInjectionType;
@Parameter(property = "resource-map")
private Map<String, String> resourceMap;
@Parameter(property = "parallelism", defaultValue = "1", required = true)
private int parallelism;
@Override
public void execute() throws MojoExecutionException {
if (fieldInjectionType == ControllerFieldInjectionTypes.FACTORY && controllerInjectionType != ControllerInjectionTypes.FACTORY) {
if (fieldInjectionType == ControllerFieldInjectionType.FACTORY && controllerInjectionType != ControllerInjectionType.FACTORY) {
getLog().warn("Field injection is set to FACTORY : Forcing controller injection to FACTORY");
controllerInjectionType = ControllerInjectionTypes.FACTORY;
controllerInjectionType = ControllerInjectionType.FACTORY;
}
final var fxmls = FXMLProvider.getFXMLs(project);
final var controllerMapping = createControllerMapping(fxmls);
final var compilationInfoMapping = createCompilationInfoMapping(fxmls, controllerMapping);
compile(compilationInfoMapping);
if (parallelism < 1) {
parallelism = Runtime.getRuntime().availableProcessors();
}
if (parallelism > 1) {
try (final var executor = Executors.newFixedThreadPool(parallelism)) {
final var controllerMapping = createControllerMapping(fxmls, executor);
final var compilationInfoMapping = createCompilationInfoMapping(fxmls, controllerMapping, executor);
compile(compilationInfoMapping, executor);
}
} else {
final var controllerMapping = createControllerMapping(fxmls);
final var compilationInfoMapping = createCompilationInfoMapping(fxmls, controllerMapping);
compile(compilationInfoMapping);
}
}
private static Map<Path, String> createControllerMapping(final Map<? extends Path, ? extends Path> fxmls) throws MojoExecutionException {
@@ -76,7 +93,8 @@ public class FXMLCompilerMojo extends AbstractMojo {
return mapping;
}
private Map<Path, CompilationInfo> createCompilationInfoMapping(final Map<? extends Path, ? extends Path> fxmls, final Map<? extends Path, String> controllerMapping) throws MojoExecutionException {
private Map<Path, CompilationInfo> createCompilationInfoMapping(final Map<? extends Path, ? extends Path> fxmls,
final Map<? extends Path, String> controllerMapping) throws MojoExecutionException {
final var mapping = new HashMap<Path, CompilationInfo>();
for (final var entry : fxmls.entrySet()) {
final var info = CompilationInfoProvider.getCompilationInfo(entry.getValue(), entry.getKey(), controllerMapping, outputDirectory, project);
@@ -91,4 +109,48 @@ public class FXMLCompilerMojo extends AbstractMojo {
Compiler.compile(mapping, parameters);
project.addCompileSourceRoot(outputDirectory.toAbsolutePath().toString());
}
private static Map<Path, String> createControllerMapping(final Map<? extends Path, ? extends Path> fxmls,
final ExecutorService executor) {
final var mapping = new ConcurrentHashMap<Path, String>();
for (final var fxml : fxmls.keySet()) {
executor.submit(() -> {
try {
mapping.put(fxml, ControllerProvider.getController(fxml));
} catch (final MojoExecutionException e) {
throw new RuntimeException(e);
}
});
}
return mapping;
}
private Map<Path, CompilationInfo> createCompilationInfoMapping(final Map<? extends Path, ? extends Path> fxmls,
final Map<? extends Path, String> controllerMapping, final ExecutorService executor) {
final var mapping = new ConcurrentHashMap<Path, CompilationInfo>();
for (final var entry : fxmls.entrySet()) {
executor.submit(() -> {
try {
final var info = CompilationInfoProvider.getCompilationInfo(entry.getValue(), entry.getKey(), controllerMapping, outputDirectory, project);
mapping.put(entry.getKey(), info);
} catch (final MojoExecutionException e) {
throw new RuntimeException(e);
}
});
}
return mapping;
}
private void compile(final Map<Path, CompilationInfo> mapping, final ExecutorService executor) throws MojoExecutionException {
final var parameters = new GenerationParametersImpl(new GenerationCompatibilityImpl(targetVersion), useImageInputStreamConstructor, resourceMap,
controllerInjectionType, fieldInjectionType, methodInjectionType, resourceInjectionType);
mapping.forEach((p, i) -> executor.submit(() -> {
try {
Compiler.compile(p, i, mapping, parameters);
} catch (final MojoExecutionException e) {
throw new RuntimeException(e);
}
}));
project.addCompileSourceRoot(outputDirectory.toAbsolutePath().toString());
}
}

View File

@@ -12,7 +12,6 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.maven.plugin.MojoExecutionException;
import javax.inject.Named;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -21,7 +20,6 @@ import java.util.Map;
/**
* Creates compiled Java code
*/
@Named
public final class Compiler {
private static final Logger logger = LogManager.getLogger(Compiler.class);
@@ -45,7 +43,7 @@ public final class Compiler {
}
}
private static void compile(final Path inputPath, final CompilationInfo info, final Map<Path, CompilationInfo> mapping, final GenerationParameters parameters) throws MojoExecutionException {
public static void compile(final Path inputPath, final CompilationInfo info, final Map<Path, CompilationInfo> mapping, final GenerationParameters parameters) throws MojoExecutionException {
try {
logger.info("Parsing {} with {}", inputPath, PARSER.getClass().getSimpleName());
final var root = PARSER.parse(inputPath);

View File

@@ -3,7 +3,6 @@ package com.github.gtache.fxml.compiler.maven.internal;
import org.apache.maven.plugin.MojoExecutionException;
import org.xml.sax.SAXException;
import javax.inject.Named;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
@@ -13,7 +12,6 @@ import java.nio.file.Path;
/**
* Extracts controller class from FXMLs
*/
@Named
public final class ControllerProvider {
private static final DocumentBuilder DOCUMENT_BUILDER;

View File

@@ -5,7 +5,6 @@ import org.apache.logging.log4j.Logger;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import javax.inject.Named;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
@@ -17,7 +16,6 @@ import java.util.Map;
/**
* Extracts FXML paths from Maven project
*/
@Named
public final class FXMLProvider {
private static final Logger logger = LogManager.getLogger(FXMLProvider.class);

View File

@@ -25,7 +25,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.*;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
@@ -41,7 +40,6 @@ class TestCompiler {
private final SourceInfo sourceInfo;
private final String content;
private final GenerationParameters parameters;
private final Compiler compiler;
TestCompiler(@Mock final ControllerInfoProvider controllerInfoProvider, @Mock final SourceInfoProvider sourceInfoProvider,
@Mock final FXMLParser fxmlParser, @Mock final CompilationInfo compilationInfo, @Mock final ParsedObject object,
@@ -57,7 +55,6 @@ class TestCompiler {
this.content = "content";
this.parameters = Objects.requireNonNull(parameters);
this.generator = Objects.requireNonNull(generator);
this.compiler = new Compiler(controllerInfoProvider, sourceInfoProvider, fxmlParser, generator);
}
@BeforeEach
@@ -77,7 +74,7 @@ class TestCompiler {
when(compilationInfo.outputClass()).thenReturn(outputClass);
final var mapping = Map.of(path, compilationInfo);
final var request = new GenerationRequestImpl(parameters, controllerInfo, sourceInfo, object, outputClass);
compiler.compile(mapping, parameters);
Compiler.compile(mapping, parameters);
verify(fxmlParser).parse(path);
ControllerInfoProvider.getControllerInfo(compilationInfo);
SourceInfoProvider.getSourceInfo(compilationInfo, mapping);
@@ -94,7 +91,7 @@ class TestCompiler {
when(compilationInfo.outputClass()).thenReturn(outputClass);
final var mapping = Map.of(path, compilationInfo);
final var request = new GenerationRequestImpl(parameters, controllerInfo, sourceInfo, object, outputClass);
assertThrows(MojoExecutionException.class, () -> compiler.compile(mapping, parameters));
assertThrows(MojoExecutionException.class, () -> Compiler.compile(mapping, parameters));
verify(fxmlParser).parse(path);
ControllerInfoProvider.getControllerInfo(compilationInfo);
SourceInfoProvider.getSourceInfo(compilationInfo, mapping);
@@ -111,7 +108,7 @@ class TestCompiler {
final var mapping = Map.of(path, compilationInfo);
final var request = new GenerationRequestImpl(parameters, controllerInfo, sourceInfo, object, outputClass);
when(generator.generate(request)).thenThrow(RuntimeException.class);
assertThrows(MojoExecutionException.class, () -> compiler.compile(mapping, parameters));
assertThrows(MojoExecutionException.class, () -> Compiler.compile(mapping, parameters));
verify(fxmlParser).parse(path);
ControllerInfoProvider.getControllerInfo(compilationInfo);
SourceInfoProvider.getSourceInfo(compilationInfo, mapping);
@@ -123,7 +120,7 @@ class TestCompiler {
when(fxmlParser.parse((Path) any())).thenThrow(ParseException.class);
final var path = tempDir.resolve("fxml1.fxml");
final var mapping = Map.of(path, compilationInfo);
assertThrows(MojoExecutionException.class, () -> compiler.compile(mapping, parameters));
assertThrows(MojoExecutionException.class, () -> Compiler.compile(mapping, parameters));
verify(fxmlParser).parse(path);
verifyNoInteractions(controllerInfoProvider, sourceInfoProvider, generator);
}
@@ -138,18 +135,10 @@ class TestCompiler {
final var mapping = Map.of(path, compilationInfo);
final var request = new GenerationRequestImpl(parameters, controllerInfo, sourceInfo, object, outputClass);
when(generator.generate(request)).thenThrow(GenerationException.class);
assertThrows(MojoExecutionException.class, () -> compiler.compile(mapping, parameters));
assertThrows(MojoExecutionException.class, () -> Compiler.compile(mapping, parameters));
verify(fxmlParser).parse(path);
ControllerInfoProvider.getControllerInfo(compilationInfo);
SourceInfoProvider.getSourceInfo(compilationInfo, mapping);
verify(generator).generate(request);
}
@Test
void testIllegal() {
assertThrows(NullPointerException.class, () -> new Compiler(null, sourceInfoProvider, fxmlParser, generator));
assertThrows(NullPointerException.class, () -> new Compiler(controllerInfoProvider, null, fxmlParser, generator));
assertThrows(NullPointerException.class, () -> new Compiler(controllerInfoProvider, sourceInfoProvider, null, generator));
assertThrows(NullPointerException.class, () -> new Compiler(controllerInfoProvider, sourceInfoProvider, fxmlParser, null));
}
}

View File

@@ -16,12 +16,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
@ExtendWith(MockitoExtension.class)
class TestControllerProvider {
private final ControllerProvider provider;
TestControllerProvider() {
this.provider = new ControllerProvider();
}
@Test
void testGetController(@TempDir final Path tempDir) throws Exception {
final var fxml = tempDir.resolve("fxml.fxml");

View File

@@ -22,11 +22,9 @@ import static org.mockito.Mockito.when;
class TestFXMLProvider {
private final MavenProject project;
private final FXMLProvider provider;
TestFXMLProvider(@Mock final MavenProject project) {
this.project = Objects.requireNonNull(project);
this.provider = new FXMLProvider();
}
@Test