Skip to content

Commit

Permalink
Qute validation - fix the way the namespace expressions are collected
Browse files Browse the repository at this point in the history
- also fixes quarkusio#32355
  • Loading branch information
mkouba authored and holly-cummins committed Apr 20, 2023
1 parent 3906531 commit 59cd6af
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2940,7 +2940,7 @@ static void collectNamespaceExpressions(Expression expression, Set<Expression> n
if (expression.isLiteral()) {
return;
}
if (includeNamespaceExpression(expression, namespace)) {
if (namespace.equals(expression.getNamespace())) {
// The expression itself has namespace
namespaceExpressions.add(expression);
}
Expand All @@ -2954,14 +2954,6 @@ static void collectNamespaceExpressions(Expression expression, Set<Expression> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Expression> msg = QuteProcessor.collectNamespaceExpressions(analysis, "msg");
assertEquals(1, msg.size());
assertEquals("msg:hello", msg.iterator().next().toOriginalString());

Set<Expression> msg2 = QuteProcessor.collectNamespaceExpressions(analysis, "msg2");
assertEquals(1, msg2.size());
assertEquals("msg2:hello_alpha", msg2.iterator().next().toOriginalString());

Set<Expression> 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)"));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,58 @@

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;

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();
}

}

0 comments on commit 59cd6af

Please sign in to comment.