5
5
import static org .junit .jupiter .api .Assertions .assertThrows ;
6
6
import static org .junit .jupiter .api .Assertions .assertTrue ;
7
7
8
+ import java .util .List ;
9
+
8
10
import jakarta .enterprise .context .Dependent ;
9
11
import jakarta .inject .Inject ;
10
12
15
17
import io .quarkus .qute .Engine ;
16
18
import io .quarkus .qute .Location ;
17
19
import 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 ;
18
23
import io .quarkus .test .QuarkusUnitTest ;
19
24
20
25
public class InjectionTest {
@@ -23,20 +28,33 @@ public class InjectionTest {
23
28
static final QuarkusUnitTest config = new QuarkusUnitTest ()
24
29
.withApplicationRoot ((jar ) -> jar
25
30
.addClasses (SimpleBean .class )
26
- .addAsResource (new StringAsset ("quarkus.qute.suffixes=txt" ), "application.properties" )
27
31
.addAsResource (new StringAsset ("{this}" ), "templates/foo.txt" )
28
32
.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" ));
30
36
31
37
@ Inject
32
38
SimpleBean simpleBean ;
33
39
40
+ @ Inject
41
+ QuteContext quteContext ;
42
+
43
+ @ Inject
44
+ TemplateProducer templateProducer ;
45
+
34
46
@ Test
35
47
public void testInjection () {
36
48
assertNotNull (simpleBean .engine );
37
49
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 ));
38
52
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 ());
40
58
assertEquals ("<strong>bar</strong>" , simpleBean .foo2 .render ("bar" ));
41
59
assertEquals ("bar" , simpleBean .bar .render ("bar" ));
42
60
@@ -54,6 +72,25 @@ public void testInjection() {
54
72
assertEquals ("UTF-8" , simpleBean .bar .getVariant ().get ().getEncoding ());
55
73
assertNotNull (simpleBean .bar .getGeneratedId ());
56
74
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 ());
57
94
}
58
95
59
96
@ Dependent
@@ -73,4 +110,4 @@ public static class SimpleBean {
73
110
74
111
}
75
112
76
- }
113
+ }
0 commit comments