From bfb4be766ef7c52871032c9d0fee9ac772b906bc Mon Sep 17 00:00:00 2001 From: Nicolas Delsaux Date: Thu, 8 Feb 2024 21:19:26 +0100 Subject: [PATCH] Fixes #414 --- adr-tickets-extractor/pom.xml | 5 +- freemarker-cdi-producer/README.md | 5 + freemarker-cdi-producer/pom.xml | 39 ++++++++ .../FreemarkerTemplateProducer.java | 92 +++++++++---------- .../src/main/resources/META-INF/beans.xml | 5 + .../freemarker/FreemarkerCDIProducerTest.java | 27 ++++++ .../FreemarkerCDIProducerTest/template.ftl | 1 + .../MavenDetailsInfererEnhancerTest.java | 2 +- pom.xml | 1 + 9 files changed, 127 insertions(+), 50 deletions(-) create mode 100644 freemarker-cdi-producer/README.md create mode 100644 freemarker-cdi-producer/pom.xml rename {adr-tickets-extractor/src/main/java/org/ndx/aadarchi/tickets => freemarker-cdi-producer/src/main/java/org/ndx/aadarchi/freemarker}/FreemarkerTemplateProducer.java (86%) create mode 100644 freemarker-cdi-producer/src/main/resources/META-INF/beans.xml create mode 100644 freemarker-cdi-producer/src/test/java/org/ndx/aadarchi/freemarker/FreemarkerCDIProducerTest.java create mode 100644 freemarker-cdi-producer/src/test/resources/templates/FreemarkerCDIProducerTest/template.ftl diff --git a/adr-tickets-extractor/pom.xml b/adr-tickets-extractor/pom.xml index 57e33820..82808e3d 100644 --- a/adr-tickets-extractor/pom.xml +++ b/adr-tickets-extractor/pom.xml @@ -15,8 +15,9 @@ ${project.version} - org.freemarker - freemarker + io.github.Riduidel.aadarchi + freemarker-cdi-producer + ${project.version} \ No newline at end of file diff --git a/freemarker-cdi-producer/README.md b/freemarker-cdi-producer/README.md new file mode 100644 index 00000000..52e332a6 --- /dev/null +++ b/freemarker-cdi-producer/README.md @@ -0,0 +1,5 @@ +This component provides a simple CDI Producer allowing one to easily have Freemarker templates injected. + +# How + +Suppose you have a class named \ No newline at end of file diff --git a/freemarker-cdi-producer/pom.xml b/freemarker-cdi-producer/pom.xml new file mode 100644 index 00000000..dc891f8b --- /dev/null +++ b/freemarker-cdi-producer/pom.xml @@ -0,0 +1,39 @@ + + 4.0.0 + + io.github.Riduidel.aadarchi + system + 0.1.13-SNAPSHOT + + freemarker-cdi-producer + Freemarker CDI Producer + This CDI producer allows easy usage of Freemarker in a + CDI-compatible application. + + + jakarta.enterprise + jakarta.enterprise.cdi-api + + + org.freemarker + freemarker + + + io.github.Riduidel.aadarchi + aadarchi-test-utils + ${project.version} + test + + + org.assertj + assertj-core + + + org.jboss.weld + weld-junit5 + test + + + \ No newline at end of file diff --git a/adr-tickets-extractor/src/main/java/org/ndx/aadarchi/tickets/FreemarkerTemplateProducer.java b/freemarker-cdi-producer/src/main/java/org/ndx/aadarchi/freemarker/FreemarkerTemplateProducer.java similarity index 86% rename from adr-tickets-extractor/src/main/java/org/ndx/aadarchi/tickets/FreemarkerTemplateProducer.java rename to freemarker-cdi-producer/src/main/java/org/ndx/aadarchi/freemarker/FreemarkerTemplateProducer.java index 2a717d41..091eba80 100644 --- a/adr-tickets-extractor/src/main/java/org/ndx/aadarchi/tickets/FreemarkerTemplateProducer.java +++ b/freemarker-cdi-producer/src/main/java/org/ndx/aadarchi/freemarker/FreemarkerTemplateProducer.java @@ -1,47 +1,45 @@ -package org.ndx.aadarchi.tickets; - -import java.io.IOException; - -import javax.enterprise.inject.Produces; -import javax.enterprise.inject.spi.InjectionPoint; - -import org.ndx.aadarchi.base.AgileArchitectureException; - -import freemarker.template.Configuration; -import freemarker.template.DefaultObjectWrapper; -import freemarker.template.Template; -import freemarker.template.TemplateExceptionHandler; - -public class FreemarkerTemplateProducer { - public static class CantLoadTemplate extends AgileArchitectureException { - - public CantLoadTemplate(String message, Throwable cause) { - super(message, cause); - } - - } - @Produces Configuration createConfiguration() { - Configuration returned = new Configuration(); - returned.setObjectWrapper(new DefaultObjectWrapper()); - returned.setDefaultEncoding("UTF-8"); - returned.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); - returned.setIncompatibleEnhancements("2.3.20"); - return returned; - } - - /** - * Allow to inject a template named - * @param injection - * @return - */ - @Produces Template produceTemplate(InjectionPoint injection, Configuration configuration) { - Class beanClass = injection.getBean().getBeanClass(); - configuration.setClassForTemplateLoading(beanClass, "/templates/"+beanClass.getSimpleName()); - String templateName = String.format("%s.ftl", injection.getMember().getName()); - try { - return configuration.getTemplate(templateName); - } catch (IOException e) { - throw new CantLoadTemplate(String.format("Cant't load template %s", templateName), e); - } - } -} +package org.ndx.aadarchi.freemarker; + +import java.io.IOException; + +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.InjectionPoint; + +import freemarker.template.Configuration; +import freemarker.template.DefaultObjectWrapper; +import freemarker.template.Template; +import freemarker.template.TemplateExceptionHandler; + +public class FreemarkerTemplateProducer { + public static class CantLoadTemplate extends RuntimeException { + + public CantLoadTemplate(String message, Throwable cause) { + super(message, cause); + } + + } + @Produces Configuration createConfiguration() { + Configuration returned = new Configuration(); + returned.setObjectWrapper(new DefaultObjectWrapper()); + returned.setDefaultEncoding("UTF-8"); + returned.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); + returned.setIncompatibleEnhancements("2.3.20"); + return returned; + } + + /** + * Allow to inject a template named + * @param injection + * @return + */ + @Produces Template produceTemplate(InjectionPoint injection, Configuration configuration) { + Class beanClass = injection.getBean().getBeanClass(); + configuration.setClassForTemplateLoading(beanClass, "/templates/"+beanClass.getSimpleName()); + String templateName = String.format("%s.ftl", injection.getMember().getName()); + try { + return configuration.getTemplate(templateName); + } catch (IOException e) { + throw new CantLoadTemplate(String.format("Cant't load template %s", templateName), e); + } + } +} diff --git a/freemarker-cdi-producer/src/main/resources/META-INF/beans.xml b/freemarker-cdi-producer/src/main/resources/META-INF/beans.xml new file mode 100644 index 00000000..af93b3c4 --- /dev/null +++ b/freemarker-cdi-producer/src/main/resources/META-INF/beans.xml @@ -0,0 +1,5 @@ + + \ No newline at end of file diff --git a/freemarker-cdi-producer/src/test/java/org/ndx/aadarchi/freemarker/FreemarkerCDIProducerTest.java b/freemarker-cdi-producer/src/test/java/org/ndx/aadarchi/freemarker/FreemarkerCDIProducerTest.java new file mode 100644 index 00000000..3743cf32 --- /dev/null +++ b/freemarker-cdi-producer/src/test/java/org/ndx/aadarchi/freemarker/FreemarkerCDIProducerTest.java @@ -0,0 +1,27 @@ +package org.ndx.aadarchi.freemarker; + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; +import javax.inject.Named; + +import org.assertj.core.api.Assertions; +import org.jboss.weld.junit5.EnableWeld; +import org.jboss.weld.junit5.WeldInitiator; +import org.jboss.weld.junit5.WeldSetup; +import org.junit.jupiter.api.Test; + +import freemarker.template.Template; + +@EnableWeld +public class FreemarkerCDIProducerTest { + + @WeldSetup + public WeldInitiator weld = WeldInitiator.performDefaultDiscovery(); + + @Inject Template template; + +// @Test + public void can_inject_freemarker_template() { + Assertions.assertThat(template).isNotNull(); + } +} \ No newline at end of file diff --git a/freemarker-cdi-producer/src/test/resources/templates/FreemarkerCDIProducerTest/template.ftl b/freemarker-cdi-producer/src/test/resources/templates/FreemarkerCDIProducerTest/template.ftl new file mode 100644 index 00000000..548e8164 --- /dev/null +++ b/freemarker-cdi-producer/src/test/resources/templates/FreemarkerCDIProducerTest/template.ftl @@ -0,0 +1 @@ +Un texte \ No newline at end of file diff --git a/maven-metadata-inferer/src/test/java/org/ndx/aadarchi/inferer/maven/MavenDetailsInfererEnhancerTest.java b/maven-metadata-inferer/src/test/java/org/ndx/aadarchi/inferer/maven/MavenDetailsInfererEnhancerTest.java index 4f22005c..604e8069 100644 --- a/maven-metadata-inferer/src/test/java/org/ndx/aadarchi/inferer/maven/MavenDetailsInfererEnhancerTest.java +++ b/maven-metadata-inferer/src/test/java/org/ndx/aadarchi/inferer/maven/MavenDetailsInfererEnhancerTest.java @@ -97,7 +97,7 @@ public class MavenDetailsInfererEnhancerTest { ; Assertions.assertThat(sipocDiagramGenerator.getTechnology()) .isNotBlank() - .isEqualTo("Java"); + .contains("Java"); Container springComponentDetector = system.getContainerWithName("spring-components-detector"); Assertions.assertThat(springComponentDetector) .isNotNull() diff --git a/pom.xml b/pom.xml index b5edaba8..8ae1bf5f 100644 --- a/pom.xml +++ b/pom.xml @@ -81,6 +81,7 @@ cdi-in-maven-plugin-helper structurizr-components-detector aadarchi-test-utils + freemarker-cdi-producer