Skip to content

Commit

Permalink
Merge pull request #7476 from mkouba/issue-7472
Browse files Browse the repository at this point in the history
Qute - add test for include section, a small fix for user tags
  • Loading branch information
gsmet authored Feb 28, 2020
2 parents 5939776 + 95b5ebb commit c5af2a5
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.quarkus.qute.deployment.include;

import static org.junit.jupiter.api.Assertions.assertEquals;

import javax.inject.Inject;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.qute.Template;
import io.quarkus.test.QuarkusUnitTest;

public class IncludeTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addAsResource(new StringAsset("{#insert item}NOK{/}"), "templates/base.html")
.addAsResource(new StringAsset("{#include base}{#item}OK{/}{/}"), "templates/detail.html"));

@Inject
Template detail;

@Test
public void testIncludeSection() {
assertEquals("OK", detail.render());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkus.qute.deployment.tag;

import static org.junit.jupiter.api.Assertions.assertEquals;

import javax.inject.Inject;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.qute.Engine;
import io.quarkus.test.QuarkusUnitTest;

public class UserTagNameCollisionTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addAsResource(new StringAsset("Hello from tag template!"), "templates/tags/hello.txt")
.addAsResource(new StringAsset("Hello from regular template!"), "templates/hello.txt"));

@Inject
Engine engine;

@Test
public void testTagDoesNotShadowRegularTemplate() {
assertEquals("Hello from regular template!", engine.getTemplate("hello").render());
assertEquals("Hello from tag template!", engine.getTemplate("tags/hello").render());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class EngineProducer {

public static final String INJECT_NAMESPACE = "inject";

private static final String TAGS = "tags/";

private static final Logger LOGGER = Logger.getLogger(EngineProducer.class);

private final Engine engine;
Expand All @@ -53,7 +55,7 @@ public class EngineProducer {
public EngineProducer(QuteContext context, Event<EngineBuilder> event) {
this.suffixes = context.getConfig().suffixes;
this.basePath = "templates/";
this.tagPath = basePath + "tags/";
this.tagPath = basePath + TAGS;
this.tags = context.getTags();

LOGGER.debugf("Initializing Qute [templates: %s, tags: %s, resolvers: %s", context.getTemplatePaths(), tags,
Expand Down Expand Up @@ -111,8 +113,9 @@ public String map(Object result, Expression expression) {
for (String tag : tags) {
// Strip suffix, item.html -> item
String tagName = tag.contains(".") ? tag.substring(0, tag.lastIndexOf('.')) : tag;
LOGGER.debugf("Registered UserTagSectionHelper for %s", tagName);
builder.addSectionHelper(new UserTagSectionHelper.Factory(tagName));
String tagTemplateId = TAGS + tagName;
LOGGER.debugf("Registered UserTagSectionHelper for %s [%s]", tagName, tagTemplateId);
builder.addSectionHelper(new UserTagSectionHelper.Factory(tagName, tagTemplateId));
}
// Add locator
builder.addLocator(this::locate);
Expand All @@ -138,10 +141,6 @@ String getTagPath() {
return tagPath;
}

List<String> getSuffixes() {
return suffixes;
}

private ValueResolver createResolver(String resolverClassName) {
try {
Class<?> resolverClazz = Thread.currentThread()
Expand All @@ -161,25 +160,21 @@ private ValueResolver createResolver(String resolverClassName) {
*/
private Optional<TemplateLocation> locate(String path) {
URL resource = null;
// First try to locate a tag template
if (tags.stream().anyMatch(tag -> tag.startsWith(path))) {
LOGGER.debugf("Locate tag for %s", path);
resource = locatePath(tagPath + path);
String templatePath = basePath + path;
LOGGER.debugf("Locate template for %s", templatePath);
resource = locatePath(templatePath);
if (resource == null) {
// Try path with suffixes
for (String suffix : suffixes) {
resource = locatePath(tagPath + path + "." + suffix);
templatePath = basePath + path + "." + suffix;
resource = locatePath(templatePath);
if (resource != null) {
break;
}
}
}
if (resource == null) {
String templatePath = basePath + path;
LOGGER.debugf("Locate template for %s", templatePath);
resource = locatePath(templatePath);
}
if (resource != null) {
return Optional.of(new ResourceTemplateLocation(resource, guessVariant(path)));
return Optional.of(new ResourceTemplateLocation(resource, guessVariant(templatePath)));
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.lang.reflect.Field;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;

import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.AnnotatedParameter;
Expand Down Expand Up @@ -44,7 +43,7 @@ Template getDefaultTemplate(InjectionPoint injectionPoint) {
}
}
// Note that engine may not be initialized and so we inject a delegating template
return new InjectableTemplate(name, engineProducer.getSuffixes());
return new InjectableTemplate(name);
}

@Produces
Expand All @@ -61,54 +60,43 @@ Template getTemplate(InjectionPoint injectionPoint) {
throw new IllegalStateException("No template reource path specified");
}
// Note that engine may not be initialized and so we inject a delegating template
return new InjectableTemplate(path.value(), engineProducer.getSuffixes());
return new InjectableTemplate(path.value());
}

class InjectableTemplate implements Template {

private final Supplier<Template> template;

public InjectableTemplate(String path, Iterable<String> suffixes) {
this.template = new Supplier<Template>() {

@Override
public Template get() {
Template template = engineProducer.getEngine().getTemplate(path);
if (template == null) {
// Try path with suffixes
for (String suffix : suffixes) {
template = engineProducer.getEngine().getTemplate(path + "." + suffix);
if (template != null) {
break;
}
}
if (template == null) {
throw new IllegalStateException("No template found for path: " + path);
}
}
return template;
}
};
private final String path;

public InjectableTemplate(String path) {
this.path = path;
}

private Template template() {
Template template = engineProducer.getEngine().getTemplate(path);
if (template == null) {
throw new IllegalStateException("No template found for path: " + path);
}
return template;
}

@Override
public TemplateInstance instance() {
return template.get().instance();
return template().instance();
}

@Override
public Set<Expression> getExpressions() {
return template.get().getExpressions();
return template().getExpressions();
}

@Override
public String getGeneratedId() {
return template.get().getGeneratedId();
return template().getGeneratedId();
}

@Override
public Optional<Variant> getVariant() {
return template.get().getVariant();
return template().getVariant();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ default Template parse(String content) {
public Template putTemplate(String id, Template template);

/**
* Obtain a template for the given identifier. The template could be registered using
* Obtain a template for the given identifier. A template may be registered using
* {@link #putTemplate(String, Template)} or loaded by a template locator.
*
* @param id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,16 @@ public CompletionStage<ResultNode> resolve(SectionResolutionContext context) {
public static class Factory implements SectionHelperFactory<UserTagSectionHelper> {

private final String name;

public Factory(String name) {
private final String templateId;

/**
*
* @param name Identifies the tag
* @param templateId Used to locate the template
*/
public Factory(String name, String templateId) {
this.name = name;
this.templateId = templateId;
}

@Override
Expand All @@ -76,9 +83,9 @@ public UserTagSectionHelper initialize(SectionInitContext context) {

@Override
public Template get() {
Template template = context.getEngine().getTemplate(name);
Template template = context.getEngine().getTemplate(templateId);
if (template == null) {
throw new IllegalStateException("Tag template not found: " + name);
throw new IllegalStateException("Tag template not found: " + templateId);
}
return template;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public class UserTagTest {
@Test
public void testUserTag() {
Engine engine = Engine.builder().addDefaultSectionHelpers().addDefaultValueResolvers()
.addSectionHelper(new UserTagSectionHelper.Factory("myTag"))
.addSectionHelper(new UserTagSectionHelper.Factory("myTag", "my-tag-id"))
.build();

Template tag = engine.parse("{#if showImage}{it.name}{#else}nope{/if}");
engine.putTemplate("myTag", tag);
engine.putTemplate("my-tag-id", tag);
Template template1 = engine.parse("{#myTag order showImage=true /}");
Template template2 = engine.parse("{#myTag order /}");
Template template3 = engine.parse("{#myTag showImage=false /}");
Expand Down

0 comments on commit c5af2a5

Please sign in to comment.