55import static org .junit .jupiter .api .Assertions .assertThrows ;
66import static org .junit .jupiter .api .Assertions .assertTrue ;
77
8+ import java .util .List ;
9+
810import jakarta .enterprise .context .Dependent ;
911import jakarta .inject .Inject ;
1012
1517import io .quarkus .qute .Engine ;
1618import io .quarkus .qute .Location ;
1719import io .quarkus .qute .Template ;
20+ import io .quarkus .qute .Variant ;
21+ import io .quarkus .qute .runtime .QuteRecorder .QuteContext ;
22+ import io .quarkus .qute .runtime .TemplateProducer ;
1823import io .quarkus .test .QuarkusUnitTest ;
1924
2025public class InjectionTest {
@@ -23,20 +28,33 @@ public class InjectionTest {
2328 static final QuarkusUnitTest config = new QuarkusUnitTest ()
2429 .withApplicationRoot ((jar ) -> jar
2530 .addClasses (SimpleBean .class )
26- .addAsResource (new StringAsset ("quarkus.qute.suffixes=txt" ), "application.properties" )
2731 .addAsResource (new StringAsset ("{this}" ), "templates/foo.txt" )
2832 .addAsResource (new StringAsset ("<strong>{this}</strong>" ), "templates/foo.qute.html" )
29- .addAsResource (new StringAsset ("{@String foo}{this}" ), "templates/bars/bar.txt" ));
33+ .addAsResource (new StringAsset ("{@String foo}{this}" ), "templates/bars/bar.txt" )
34+ .addAsResource (new StringAsset ("Hello {name}!" ), "templates/foo.1.html" )
35+ .addAsResource (new StringAsset ("Hello {name}!" ), "templates/foo.1.txt" ));
3036
3137 @ Inject
3238 SimpleBean simpleBean ;
3339
40+ @ Inject
41+ QuteContext quteContext ;
42+
43+ @ Inject
44+ TemplateProducer templateProducer ;
45+
3446 @ Test
3547 public void testInjection () {
3648 assertNotNull (simpleBean .engine );
3749 assertTrue (simpleBean .engine .locate ("foo.txt" ).isPresent ());
50+ // foo.qute.html takes precedence
51+ assertTrue (simpleBean .engine .locate ("foo" ).orElseThrow ().getVariant ().get ().getContentType ().equals (Variant .TEXT_HTML ));
3852 assertTrue (simpleBean .engine .locate ("foo.html" ).isEmpty ());
39- assertEquals ("bar" , simpleBean .foo .render ("bar" ));
53+ assertEquals ("bar" ,
54+ simpleBean .foo .instance ()
55+ .setVariant (Variant .forContentType (Variant .TEXT_PLAIN ))
56+ .data ("bar" )
57+ .render ());
4058 assertEquals ("<strong>bar</strong>" , simpleBean .foo2 .render ("bar" ));
4159 assertEquals ("bar" , simpleBean .bar .render ("bar" ));
4260
@@ -54,6 +72,25 @@ public void testInjection() {
5472 assertEquals ("UTF-8" , simpleBean .bar .getVariant ().get ().getEncoding ());
5573 assertNotNull (simpleBean .bar .getGeneratedId ());
5674 assertEquals ("foo.qute.html" , simpleBean .foo2 .getId ());
75+ assertEquals (Variant .TEXT_HTML , simpleBean .foo2 .getVariant ().get ().getContentType ());
76+ List <String > fooVariants = quteContext .getVariants ().get ("foo" );
77+ // foo -> foo.txt, foo.qute.html
78+ assertEquals (2 , fooVariants .size ());
79+ assertTrue (fooVariants .contains ("foo.txt" ));
80+ assertTrue (fooVariants .contains ("foo.qute.html" ));
81+ List <String > fooQuteVariants = quteContext .getVariants ().get ("foo.qute" );
82+ // foo.qute -> foo.qute.html
83+ assertEquals (1 , fooQuteVariants .size ());
84+ assertTrue (fooVariants .contains ("foo.qute.html" ));
85+
86+ assertEquals ("Hello <strong>Foo</strong>!" , templateProducer .getInjectableTemplate ("foo.1" ).instance ()
87+ .setVariant (Variant .forContentType (Variant .TEXT_HTML ))
88+ .data ("name" , "<strong>Foo</strong>" )
89+ .render ());
90+ assertEquals ("Hello <strong>Foo</strong>!" , templateProducer .getInjectableTemplate ("foo.1" ).instance ()
91+ .setVariant (Variant .forContentType (Variant .TEXT_PLAIN ))
92+ .data ("name" , "<strong>Foo</strong>" )
93+ .render ());
5794 }
5895
5996 @ Dependent
@@ -73,4 +110,4 @@ public static class SimpleBean {
73110
74111 }
75112
76- }
113+ }
0 commit comments