Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.qute.deployment.contents;

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

import java.util.concurrent.ExecutionException;

Expand All @@ -13,6 +14,7 @@
import io.quarkus.qute.Engine;
import io.quarkus.qute.TemplateContents;
import io.quarkus.qute.TemplateInstance;
import io.quarkus.qute.Variant;
import io.quarkus.test.QuarkusUnitTest;

public class TemplateContentsRecordTest {
Expand All @@ -26,12 +28,18 @@ public class TemplateContentsRecordTest {

@Test
public void testTemplateContents() throws InterruptedException, ExecutionException {
// Clear preloaded templates to simulate quarkus.qute.dev-mode.no-restart-templates in the dev mode
engine.clearTemplates();

assertEquals("Hello 42!", new helloInt(42).render());
assertEquals("Hello 1!", engine.getTemplate("TemplateContentsRecordTest/helloInt").data("val", 1).render());
assertEquals("Hello 42!", new helloLong(42).render());
assertEquals("Hello 1!", engine.getTemplate("foo/helloLong").data("val", 1).render());

assertEquals("<p>Hello &quot;Martin&quot;!</p>", new helloHtml("\"Martin\"").render());
TemplateInstance helloHtml = new helloHtml("\"Martin\"");
assertNotNull(helloHtml.getTemplate().getVariant());
assertEquals(Variant.TEXT_HTML, helloHtml.getTemplate().getVariant().get().getContentType());
assertEquals("<p>Hello &quot;Martin&quot;!</p>", helloHtml.render());
assertEquals("<p>Hello Lu!</p>",
engine.getTemplate("TemplateContentsRecordTest/helloHtml.html").data("name", "Lu").render());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,21 +377,20 @@ private Optional<TemplateLocation> locate(String path) {
// First try the template contents, i.e. templates not backed by files
LOGGER.debugf("Locate template contents for %s", path);
String content = templateContents.get(path);
if (content == null) {
// Try path with suffixes
for (String suffix : suffixes) {
String pathWithSuffix = path + "." + suffix;
if (isExcluded(pathWithSuffix)) {
continue;
}
content = templateContents.get(pathWithSuffix);
if (content != null) {
break;
}
}
}
if (content != null) {
return Optional.of(new ContentTemplateLocation(content, createVariant(path)));
return getTemplateLocation(content, path);
}

// Try path with suffixes
for (String suffix : suffixes) {
String pathWithSuffix = path + "." + suffix;
if (isExcluded(pathWithSuffix)) {
continue;
}
content = templateContents.get(pathWithSuffix);
if (content != null) {
return getTemplateLocation(content, pathWithSuffix);
}
}

// Then try to locate file-based templates
Expand All @@ -400,28 +399,34 @@ private Optional<TemplateLocation> locate(String path) {
String templatePath = templateRoot + path;
LOGGER.debugf("Locate template file for %s", templatePath);
resource = locatePath(templatePath);
if (resource == null) {
// Try path with suffixes
for (String suffix : suffixes) {
String pathWithSuffix = path + "." + suffix;
if (isExcluded(pathWithSuffix)) {
continue;
}
templatePath = templateRoot + pathWithSuffix;
resource = locatePath(templatePath);
if (resource != null) {
break;
}
}
}
if (resource != null) {
return Optional.of(new ResourceTemplateLocation(resource, createVariant(templatePath)));
return getTemplateLocation(resource, templatePath);
}
// Try path with suffixes
for (String suffix : suffixes) {
String pathWithSuffix = path + "." + suffix;
if (isExcluded(pathWithSuffix)) {
continue;
}
templatePath = templateRoot + pathWithSuffix;
resource = locatePath(templatePath);
if (resource != null) {
return getTemplateLocation(resource, templatePath);
}
}
}

return Optional.empty();
}

private Optional<TemplateLocation> getTemplateLocation(String content, String pathWithSuffix) {
return Optional.of(new ContentTemplateLocation(content, createVariant(pathWithSuffix)));
}

private Optional<TemplateLocation> getTemplateLocation(URL resource, String templatePath) {
return Optional.of(new ResourceTemplateLocation(resource, createVariant(templatePath)));
}

private URL locatePath(String path) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
Expand Down
Loading