Search in sources :

Example 31 with TemplateEngine

use of org.thymeleaf.TemplateEngine in project thymeleaf-tests by thymeleaf.

the class ScriptInlineTest method testInlineResult.

private static void testInlineResult(final String script, final String expectedResult, final Map<String, Object> variables) {
    final String completeScript = "<script th:inline=\"javascript\">\n/*<![CDATA[ */\n" + script + "\n/* ]]> */\n</script>";
    final ITemplateResolver templateResolver = new StringTemplateResolver();
    final TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    final Context ctx = new Context();
    ctx.setVariables(variables);
    final StringWriter stringWriter = new StringWriter();
    templateEngine.process(completeScript, ctx, stringWriter);
    final String result = stringWriter.toString();
    final String extractedResult = result.substring(0, result.indexOf("\n/* ]]> */\n</script>")).substring(24);
    Assert.assertEquals(expectedResult, extractedResult);
}
Also used : Context(org.thymeleaf.context.Context) TemplateEngine(org.thymeleaf.TemplateEngine) StringWriter(java.io.StringWriter) StringTemplateResolver(org.thymeleaf.templateresolver.StringTemplateResolver) ITemplateResolver(org.thymeleaf.templateresolver.ITemplateResolver)

Example 32 with TemplateEngine

use of org.thymeleaf.TemplateEngine in project thymeleaf-tests by thymeleaf.

the class LinkBuilderTest method testLinkBuilderSpring01.

@Test
public void testLinkBuilderSpring01() throws Exception {
    final Context ctx = new Context();
    ctx.setVariable("one", "This is one");
    final TemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(new StringTemplateResolver());
    templateEngine.setLinkBuilder(new TestLinkBuilder());
    final String result = templateEngine.process("<a th:href='@{/something}'>", ctx);
    final String expected = "<a href='[/fromthebuilder/something]'>";
    Assert.assertEquals(expected, result);
}
Also used : Context(org.thymeleaf.context.Context) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) TemplateEngine(org.thymeleaf.TemplateEngine) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) StringTemplateResolver(org.thymeleaf.templateresolver.StringTemplateResolver) Test(org.junit.Test)

Example 33 with TemplateEngine

use of org.thymeleaf.TemplateEngine in project thymeleaf-tests by thymeleaf.

the class LinkBuilderTest method testLinkBuilderWithECFactory01.

@Test
public void testLinkBuilderWithECFactory01() throws Exception {
    final Context ctx = new Context();
    ctx.setVariable("one", "This is one");
    final TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(new StringTemplateResolver());
    templateEngine.setLinkBuilder(new TestLinkBuilder());
    templateEngine.setEngineContextFactory(new TestEngineContextFactory());
    final String result = templateEngine.process("<a th:href='@{/something}'>", ctx);
    final String expected = "<a href='[ENGINE: /fromthebuilder/something]'>";
    Assert.assertEquals(expected, result);
}
Also used : Context(org.thymeleaf.context.Context) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) TemplateEngine(org.thymeleaf.TemplateEngine) StringTemplateResolver(org.thymeleaf.templateresolver.StringTemplateResolver) Test(org.junit.Test)

Example 34 with TemplateEngine

use of org.thymeleaf.TemplateEngine in project thymeleaf-tests by thymeleaf.

the class OfflineTest method testOffline01.

@Test
public void testOffline01() throws Exception {
    final Context ctx = new Context();
    ctx.setVariable("one", "This is one");
    final TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(new ClassLoaderTemplateResolver());
    final String result = templateEngine.process("offline/offline01.html", ctx);
    final String expected = ResourceUtils.read(ClassLoaderUtils.getClassLoader(OfflineTest.class).getResourceAsStream("offline/offlinespring01-result.html"), "UTF-8", true);
    Assert.assertEquals(expected, ResourceUtils.normalize(result));
}
Also used : Context(org.thymeleaf.context.Context) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) TemplateEngine(org.thymeleaf.TemplateEngine) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) Test(org.junit.Test)

Example 35 with TemplateEngine

use of org.thymeleaf.TemplateEngine in project thymeleaf-tests by thymeleaf.

the class Parsing03Test method testParsing.

@Test
public void testParsing() throws Exception {
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setTemplateMode("HTML5");
    templateResolver.setCharacterEncoding("UTF-16");
    final TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    final String result = templateEngine.process("parsing/parsingtest03.bulk", new Context());
    final String expected = ResourceUtils.read(ClassLoaderUtils.getClassLoader(Parsing03Test.class).getResourceAsStream("parsing/parsingtest03-result.bulk"), "ISO-8859-1", true);
    Assert.assertEquals(expected, ResourceUtils.normalize(result));
}
Also used : Context(org.thymeleaf.context.Context) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) TemplateEngine(org.thymeleaf.TemplateEngine) Test(org.junit.Test)

Aggregations

TemplateEngine (org.thymeleaf.TemplateEngine)49 Context (org.thymeleaf.context.Context)28 Test (org.junit.Test)17 ClassLoaderTemplateResolver (org.thymeleaf.templateresolver.ClassLoaderTemplateResolver)13 MockServletContext (org.springframework.mock.web.MockServletContext)11 RequestContext (org.springframework.web.servlet.support.RequestContext)10 Test (org.junit.jupiter.api.Test)7 WebContext (org.thymeleaf.context.WebContext)7 SpringTemplateEngine (org.thymeleaf.spring5.SpringTemplateEngine)6 SpringTemplateEngine (org.thymeleaf.spring6.SpringTemplateEngine)6 ServletContext (jakarta.servlet.ServletContext)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)5 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)5 StringTemplateResolver (org.thymeleaf.templateresolver.StringTemplateResolver)5 ServletContextTemplateResolver (org.thymeleaf.templateresolver.ServletContextTemplateResolver)4 IEngineConfiguration (org.thymeleaf.IEngineConfiguration)3 TemplateSpec (org.thymeleaf.TemplateSpec)3 Provides (com.google.inject.Provides)2 Singleton (com.google.inject.Singleton)2