-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 #38954 from mkouba/qute-test-recording
Qute: introduce RenderedResults assert API for tests
- Loading branch information
Showing
14 changed files
with
664 additions
and
6 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
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
12 changes: 12 additions & 0 deletions
12
extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/test/FooTemplates.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,12 @@ | ||
package io.quarkus.qute.deployment.test; | ||
|
||
import io.quarkus.qute.CheckedTemplate; | ||
import io.quarkus.qute.TemplateInstance; | ||
|
||
@CheckedTemplate | ||
public class FooTemplates { | ||
|
||
static native TemplateInstance foo(String name); | ||
|
||
static native TemplateInstance foo$bar(); | ||
} |
33 changes: 33 additions & 0 deletions
33
...deployment/src/test/java/io/quarkus/qute/deployment/test/RenderedResultsDisabledTest.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,33 @@ | ||
package io.quarkus.qute.deployment.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import jakarta.enterprise.inject.Instance; | ||
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.RenderedResults; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class RenderedResultsDisabledTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root | ||
.addClasses(SimpleBean.class) | ||
.addAsResource(new StringAsset("quarkus.qute.test-mode.record-rendered-results=false"), | ||
"application.properties") | ||
.addAsResource(new StringAsset("{name}"), "templates/foo.txt")); | ||
|
||
@Inject | ||
Instance<RenderedResults> renderedResults; | ||
|
||
@Test | ||
public void testRenderedResultsNotRegistered() { | ||
assertTrue(renderedResults.isUnsatisfied()); | ||
} | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
...ns/qute/deployment/src/test/java/io/quarkus/qute/deployment/test/RenderedResultsTest.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,98 @@ | ||
package io.quarkus.qute.deployment.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Supplier; | ||
|
||
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.RenderedResults; | ||
import io.quarkus.qute.RenderedResults.RenderedResult; | ||
import io.quarkus.qute.TemplateInstance; | ||
import io.quarkus.qute.Variant; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class RenderedResultsTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root | ||
.addClasses(SimpleBean.class, FooTemplates.class) | ||
.addAsResource(new StringAsset("quarkus.qute.suffixes=txt,html"), "application.properties") | ||
.addAsResource(new StringAsset("{name}{#fragment id=bar rendered=false}bar{/fragment}"), | ||
"templates/foo.txt") | ||
.addAsResource(new StringAsset("<h1>{name}{#fragment id=bar rendered=false}bar{/fragment}</h1>"), | ||
"templates/foo.html")); | ||
|
||
@Inject | ||
RenderedResults renderedResults; | ||
|
||
@Inject | ||
SimpleBean bean; | ||
|
||
@Test | ||
public void testInjectedTemplate() throws InterruptedException { | ||
assertResults(() -> bean.fooInstance().data("name", "oof").render(), "foo.txt", "oof"); | ||
} | ||
|
||
@Test | ||
public void testInjectedTemplateSelectedVariant() throws InterruptedException { | ||
assertResults(() -> bean.fooInstance() | ||
.setAttribute(TemplateInstance.SELECTED_VARIANT, Variant.forContentType(Variant.TEXT_HTML)) | ||
.data("name", "oof") | ||
.render(), "foo.html", "<h1>oof</h1>"); | ||
} | ||
|
||
@Test | ||
public void testTypesafeTemplate() throws InterruptedException { | ||
assertResults(() -> FooTemplates.foo("oof").render(), "foo.txt", "oof"); | ||
} | ||
|
||
@Test | ||
public void testTypesafeFragment() throws InterruptedException { | ||
assertResults(() -> FooTemplates.foo$bar().render(), "foo.txt$bar", "bar"); | ||
} | ||
|
||
@Test | ||
public void testTypesafeTemplateSelectedVariant() throws InterruptedException { | ||
assertResults( | ||
() -> FooTemplates.foo("oof") | ||
.setAttribute(TemplateInstance.SELECTED_VARIANT, Variant.forContentType(Variant.TEXT_HTML)).render(), | ||
"foo.html", "<h1>oof</h1>"); | ||
} | ||
|
||
@Test | ||
public void testTypesafeFragmentSelectedVariant() throws InterruptedException { | ||
assertResults( | ||
() -> FooTemplates.foo$bar() | ||
.setAttribute(TemplateInstance.SELECTED_VARIANT, Variant.forContentType(Variant.TEXT_HTML)).render(), | ||
"foo.html$bar", "bar"); | ||
} | ||
|
||
private void assertResults(Supplier<String> renderAction, String templateId, String expectedResult) | ||
throws InterruptedException { | ||
renderedResults.clear(); | ||
assertEquals(expectedResult, renderAction.get()); | ||
// Wait a little so that we can test the RenderedResult#timeout() | ||
// Note that LocalDateTime.now() has precision of the system clock and it seems that windows has millisecond precision | ||
TimeUnit.MILLISECONDS.sleep(50); | ||
List<RenderedResult> results = renderedResults.getResults(templateId); | ||
assertEquals(1, results.size(), renderedResults.toString()); | ||
assertEquals(expectedResult, results.get(0).result()); | ||
assertEquals(expectedResult, renderAction.get()); | ||
results = renderedResults.getResults(templateId); | ||
assertEquals(2, results.size(), renderedResults.toString()); | ||
assertEquals(expectedResult, results.get(1).result()); | ||
assertTrue(results.get(0).timestamp().isBefore(results.get(1).timestamp())); | ||
renderedResults.clear(); | ||
assertTrue(renderedResults.getResults(templateId).isEmpty()); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/test/SimpleBean.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,19 @@ | ||
package io.quarkus.qute.deployment.test; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
|
||
import io.quarkus.qute.Template; | ||
import io.quarkus.qute.TemplateInstance; | ||
|
||
@Singleton | ||
public class SimpleBean { | ||
|
||
@Inject | ||
Template foo; | ||
|
||
public TemplateInstance fooInstance() { | ||
return foo.instance(); | ||
} | ||
|
||
} |
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
17 changes: 17 additions & 0 deletions
17
extensions/qute/runtime/src/main/java/io/quarkus/qute/runtime/QuteTestModeConfig.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,17 @@ | ||
package io.quarkus.qute.runtime; | ||
|
||
import io.quarkus.qute.RenderedResults; | ||
import io.quarkus.runtime.annotations.ConfigGroup; | ||
import io.quarkus.runtime.annotations.ConfigItem; | ||
|
||
@ConfigGroup | ||
public class QuteTestModeConfig { | ||
|
||
/** | ||
* By default, the rendering results of injected and type-safe templates are recorded in the managed | ||
* {@link RenderedResults} which is registered as a CDI bean. | ||
*/ | ||
@ConfigItem(defaultValue = "true") | ||
public boolean recordRenderedResults; | ||
|
||
} |
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
14 changes: 14 additions & 0 deletions
14
...sions/qute/runtime/src/main/java/io/quarkus/qute/runtime/test/RenderedResultsCreator.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,14 @@ | ||
package io.quarkus.qute.runtime.test; | ||
|
||
import io.quarkus.arc.BeanCreator; | ||
import io.quarkus.arc.SyntheticCreationalContext; | ||
import io.quarkus.qute.RenderedResults; | ||
|
||
public class RenderedResultsCreator implements BeanCreator<RenderedResults> { | ||
|
||
@Override | ||
public RenderedResults create(SyntheticCreationalContext<RenderedResults> context) { | ||
return new RenderedResults(); | ||
} | ||
|
||
} |
Oops, something went wrong.