-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from ia3andy/link-support
Add link support to QuteWebTemplateBuildItem
- Loading branch information
Showing
10 changed files
with
244 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
deployment/src/main/java/io/quarkiverse/qute/web/deployment/QuteWebTemplateBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.quarkiverse.qute.web.deployment; | ||
|
||
import static io.quarkiverse.qute.web.runtime.PathUtils.removeExtension; | ||
import static io.quarkiverse.qute.web.runtime.PathUtils.removeLeadingSlash; | ||
import static io.quarkiverse.qute.web.runtime.PathUtils.removeTrailingSlash; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
|
||
public final class QuteWebTemplateBuildItem extends MultiBuildItem { | ||
|
||
/** | ||
* templatePath is used also as path if link is null (e.g. "my-blog-post") | ||
*/ | ||
private final String templatePath; | ||
|
||
/** | ||
* The link to use for this template or null to use the template path (e.g "posts/my-blog-post") | ||
* | ||
* If two links are identical, an exception is thrown | ||
* If a link and a path are identical for different items, the link has priority | ||
*/ | ||
private final String link; | ||
|
||
public QuteWebTemplateBuildItem(String templatePath, String link) { | ||
this.templatePath = removeExtension(templatePath); | ||
this.link = normalizeLink(link); | ||
} | ||
|
||
public String templatePath() { | ||
return templatePath; | ||
} | ||
|
||
public String link() { | ||
return link; | ||
} | ||
|
||
public boolean hasLink() { | ||
return link != null; | ||
} | ||
|
||
private static String normalizeLink(String link) { | ||
if (link == null) { | ||
return null; | ||
} | ||
return removeTrailingSlash(removeLeadingSlash(link)); | ||
} | ||
|
||
} |
17 changes: 0 additions & 17 deletions
17
...oyment/src/main/java/io/quarkiverse/qute/web/deployment/QuteWebTemplatePathBuildItem.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
deployment/src/test/java/io/quarkiverse/qute/web/test/LinkedDuplicateTemplateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.quarkiverse.qute.web.test; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkiverse.qute.web.deployment.QuteWebTemplateBuildItem; | ||
import io.quarkus.builder.BuildContext; | ||
import io.quarkus.builder.BuildStep; | ||
import io.quarkus.builder.BuildStepBuilder; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class LinkedDuplicateTemplateTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest().withApplicationRoot(root -> { | ||
root.addAsResource(new StringAsset( | ||
"Hello {name ?: 'world'}!"), | ||
"templates/pub/hello.txt"); | ||
}).addBuildChainCustomizer(buildChainBuilder -> { | ||
final BuildStepBuilder stepBuilder = buildChainBuilder.addBuildStep(new BuildStep() { | ||
@Override | ||
public void execute(BuildContext context) { | ||
context.produce(new QuteWebTemplateBuildItem("pub/hello.txt", "/foo/bar")); | ||
context.produce(new QuteWebTemplateBuildItem("pub/hello.txt", "/foo/bar")); | ||
} | ||
}); | ||
stepBuilder.produces(QuteWebTemplateBuildItem.class).build(); | ||
}).assertException(throwable -> { | ||
Assertions.assertInstanceOf(IllegalStateException.class, throwable); | ||
}); | ||
|
||
@Test | ||
public void testDuplicateLink() { | ||
|
||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
deployment/src/test/java/io/quarkiverse/qute/web/test/LinkedTemplateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.quarkiverse.qute.web.test; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.Matchers.containsString; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkiverse.qute.web.deployment.QuteWebTemplateBuildItem; | ||
import io.quarkus.builder.BuildContext; | ||
import io.quarkus.builder.BuildStep; | ||
import io.quarkus.builder.BuildStepBuilder; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class LinkedTemplateTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest().withApplicationRoot(root -> { | ||
root.addAsResource(new StringAsset( | ||
"Hello {name ?: 'world'}!"), | ||
"templates/pub/hello.txt"); | ||
root.addAsResource(new StringAsset( | ||
"Linked Hello {name ?: 'world'}!"), | ||
"templates/linked.txt"); | ||
}).addBuildChainCustomizer(buildChainBuilder -> { | ||
final BuildStepBuilder stepBuilder = buildChainBuilder.addBuildStep(new BuildStep() { | ||
@Override | ||
public void execute(BuildContext context) { | ||
context.produce(new QuteWebTemplateBuildItem("pub/hello.txt", "/foo/bar")); | ||
context.produce(new QuteWebTemplateBuildItem("pub/hello.txt", "/")); | ||
context.produce(new QuteWebTemplateBuildItem("linked.txt", "/hello.txt")); | ||
} | ||
}); | ||
stepBuilder.produces(QuteWebTemplateBuildItem.class).build(); | ||
}); | ||
|
||
@Test | ||
public void testFixedLink() { | ||
given() | ||
.when().get("/hello") | ||
.then() | ||
.statusCode(200) | ||
.body(containsString("Hello world!")); | ||
given() | ||
.when().get("/foo/bar") | ||
.then() | ||
.statusCode(200) | ||
.body(containsString("Hello world!")); | ||
given() | ||
.when().get("/hello.txt") | ||
.then() | ||
.statusCode(200) | ||
.body(containsString("Linked Hello world!")); | ||
|
||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
runtime/src/main/java/io/quarkiverse/qute/web/runtime/PathUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.quarkiverse.qute.web.runtime; | ||
|
||
public final class PathUtils { | ||
|
||
public static String toUnixPath(String path) { | ||
return path.replaceAll("\\\\", "/"); | ||
} | ||
|
||
public static String prefixWithSlash(String path) { | ||
return path.startsWith("/") ? path : "/" + path; | ||
} | ||
|
||
public static String surroundWithSlashes(String path) { | ||
return prefixWithSlash(addTrailingSlash(path)); | ||
} | ||
|
||
public static String addTrailingSlash(String path) { | ||
return path.endsWith("/") ? path : path + "/"; | ||
} | ||
|
||
public static String join(String path1, String path2) { | ||
return addTrailingSlash(path1) + removeLeadingSlash(path2); | ||
} | ||
|
||
public static String removeLeadingSlash(String path) { | ||
return path.startsWith("/") ? path.substring(1) : path; | ||
} | ||
|
||
public static String removeTrailingSlash(String path) { | ||
return path.endsWith("/") ? path.substring(0, path.length() - 1) : path; | ||
} | ||
|
||
public static String removeExtension(String path) { | ||
final int i = path.lastIndexOf("."); | ||
return i > 0 ? path.substring(0, i) : path; | ||
} | ||
} |
Oops, something went wrong.