From b5d6e395d8026c5222b8cb1c403455335a5b2ccc Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Mon, 3 Apr 2023 12:40:40 +0200 Subject: [PATCH] Qute validation - fix the way the namespace expressions are collected - also fixes #32355 --- .../qute/deployment/QuteProcessor.java | 10 +----- .../qute/deployment/QuteProcessorTest.java | 29 ++++++++++++++++ .../i18n/MessageBundleDefaultedNameTest.java | 34 +++++++++++++++++-- 3 files changed, 61 insertions(+), 12 deletions(-) diff --git a/extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/QuteProcessor.java b/extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/QuteProcessor.java index 6735401995acf..69d769bf08e50 100644 --- a/extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/QuteProcessor.java +++ b/extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/QuteProcessor.java @@ -2940,7 +2940,7 @@ static void collectNamespaceExpressions(Expression expression, Set n if (expression.isLiteral()) { return; } - if (includeNamespaceExpression(expression, namespace)) { + if (namespace.equals(expression.getNamespace())) { // The expression itself has namespace namespaceExpressions.add(expression); } @@ -2954,14 +2954,6 @@ static void collectNamespaceExpressions(Expression expression, Set n } } - private static boolean includeNamespaceExpression(Expression expression, String namespace) { - if (namespace.equals(expression.getNamespace())) { - return true; - } - String typeInfo = expression.getParts().get(0).getTypeInfo(); - return typeInfo != null ? typeInfo.startsWith(namespace) : false; - } - public static String getName(InjectionPointInfo injectionPoint) { if (injectionPoint.isField()) { return injectionPoint.getTarget().asField().name(); diff --git a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/QuteProcessorTest.java b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/QuteProcessorTest.java index a146256da12d1..4dd266f9204d7 100644 --- a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/QuteProcessorTest.java +++ b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/QuteProcessorTest.java @@ -1,14 +1,22 @@ package io.quarkus.qute.deployment; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.Collections; import java.util.List; +import java.util.Set; import java.util.regex.Pattern; import org.junit.jupiter.api.Test; +import io.quarkus.qute.Engine; +import io.quarkus.qute.Expression; +import io.quarkus.qute.Template; +import io.quarkus.qute.deployment.TemplatesAnalysisBuildItem.TemplateAnalysis; + public class QuteProcessorTest { @Test @@ -26,4 +34,25 @@ public void testTemplateDataIgnorePattern() { .isThrownBy(() -> QuteProcessor.buildIgnorePattern(List.of())); } + @Test + public void testCollectNamespaceExpressions() { + Template template = Engine.builder().build().parse("{msg:hello} {msg2:hello_alpha} {foo:baz.get(foo:bar)}"); + TemplateAnalysis analysis = new TemplateAnalysis("foo", "1", template.getExpressions(), Collections.emptyList(), null, + Collections.emptySet()); + Set msg = QuteProcessor.collectNamespaceExpressions(analysis, "msg"); + assertEquals(1, msg.size()); + assertEquals("msg:hello", msg.iterator().next().toOriginalString()); + + Set msg2 = QuteProcessor.collectNamespaceExpressions(analysis, "msg2"); + assertEquals(1, msg2.size()); + assertEquals("msg2:hello_alpha", msg2.iterator().next().toOriginalString()); + + Set foo = QuteProcessor.collectNamespaceExpressions(analysis, "foo"); + assertEquals(2, foo.size()); + for (Expression fooExpr : foo) { + assertTrue( + fooExpr.toOriginalString().equals("foo:bar") || fooExpr.toOriginalString().equals("foo:baz.get(foo:bar)")); + } + } + } diff --git a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/i18n/MessageBundleDefaultedNameTest.java b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/i18n/MessageBundleDefaultedNameTest.java index 7602b3b75eef3..c171a5f3e1a22 100644 --- a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/i18n/MessageBundleDefaultedNameTest.java +++ b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/i18n/MessageBundleDefaultedNameTest.java @@ -4,10 +4,15 @@ import java.util.Locale; +import jakarta.inject.Inject; + import org.jboss.shrinkwrap.api.asset.StringAsset; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import io.quarkus.qute.Engine; +import io.quarkus.qute.i18n.Message; +import io.quarkus.qute.i18n.MessageBundle; import io.quarkus.qute.i18n.MessageBundles; import io.quarkus.test.QuarkusUnitTest; @@ -15,19 +20,42 @@ public class MessageBundleDefaultedNameTest { @RegisterExtension static final QuarkusUnitTest config = new QuarkusUnitTest() - .withApplicationRoot((jar) -> jar - .addClasses(Controller.class) + .withApplicationRoot(root -> root + .addClasses(Controller.class, AppMessages.class, AlphaMessages.class, Item.class) .addAsResource(new StringAsset( "{Controller_index:hello(name)}"), "templates/Controller/index.html") + .addAsResource(new StringAsset( + "{msg:hello}"), + "templates/app.html") + .addAsResource(new StringAsset( + "{alpha:hello-alpha}"), + "templates/alpha.html") + .addAsResource(new StringAsset( + "{msg2:helloQux}"), + "templates/qux.html") .addAsResource(new StringAsset("hello=Ahoj {name}!"), "messages/Controller_index_cs.properties")); + @Inject + Engine engine; + @Test - public void testBundle() { + public void testBundles() { assertEquals("Hello world!", Controller.Templates.index("world").render()); assertEquals("Ahoj svete!", Controller.Templates.index("svete") .setAttribute(MessageBundles.ATTRIBUTE_LOCALE, Locale.forLanguageTag("cs")).render()); + + assertEquals("Hello world!", engine.getTemplate("app").render()); + assertEquals("Hello alpha!", engine.getTemplate("alpha").render()); + assertEquals("Hello qux!", engine.getTemplate("qux").render()); + } + + @MessageBundle("msg2") + public interface MyAppMessages { + + @Message("Hello qux!") + String helloQux(); } }