Search in sources :

Example 1 with ClassLoaderTemplateResolver

use of org.thymeleaf.templateresolver.ClassLoaderTemplateResolver in project thymeleaf-tests by thymeleaf.

the class Parsing01Test method testParsing.

@Test
public void testParsing() throws Exception {
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setTemplateMode("HTML5");
    templateResolver.setCharacterEncoding("UTF-8");
    final TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    final String result = templateEngine.process("parsing/parsingtest01.bulk", new Context());
    final String expected = ResourceUtils.read(ClassLoaderUtils.getClassLoader(Parsing01Test.class).getResourceAsStream("parsing/parsingtest01-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)

Example 2 with ClassLoaderTemplateResolver

use of org.thymeleaf.templateresolver.ClassLoaderTemplateResolver in project thymeleaf-tests by thymeleaf.

the class Parsing02Test 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/parsingtest02.bulk", new Context());
    final String expected = ResourceUtils.read(ClassLoaderUtils.getClassLoader(Parsing02Test.class).getResourceAsStream("parsing/parsingtest02-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)

Example 3 with ClassLoaderTemplateResolver

use of org.thymeleaf.templateresolver.ClassLoaderTemplateResolver in project thymeleaf-tests by thymeleaf.

the class AbstractSpring5ReactiveTest method initTemplateEngine.

@BeforeClass
public static void initTemplateEngine() {
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setPrefix(ReactiveTestUtils.TEMPLATE_PATH_BASE);
    templateResolver.setSuffix(".html");
    templateEngine = new SpringWebFluxTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    bufferFactory = new DefaultDataBufferFactory();
    htmlMediaType = MediaType.TEXT_HTML;
    sseMediaType = MediaType.TEXT_EVENT_STREAM;
    charset = Charset.forName("UTF-8");
}
Also used : ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) ISpringWebFluxTemplateEngine(org.thymeleaf.spring5.ISpringWebFluxTemplateEngine) SpringWebFluxTemplateEngine(org.thymeleaf.spring5.SpringWebFluxTemplateEngine) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) BeforeClass(org.junit.BeforeClass)

Example 4 with ClassLoaderTemplateResolver

use of org.thymeleaf.templateresolver.ClassLoaderTemplateResolver in project thymeleaf-tests by thymeleaf.

the class DecoupledGTVGTest method test.

private static void test(final String templateName, final String expectedResultLocation, final IContext context) throws Exception {
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setTemplateMode("HTML");
    templateResolver.setPrefix("templateparser/decoupled/gtvg/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setUseDecoupledLogic(true);
    final TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    templateEngine.setLinkBuilder(new StandardLinkBuilder() {

        @Override
        protected String computeContextPath(final IExpressionContext context, final String base, final Map<String, Object> parameters) {
            return "";
        }
    });
    final String result = templateEngine.process(templateName, context);
    final String expected = ResourceUtils.read(ClassLoaderUtils.getClassLoader(DecoupledGTVGTest.class).getResourceAsStream(expectedResultLocation), "ISO-8859-1", true);
    Assert.assertEquals(ResourceUtils.normalize(expected), ResourceUtils.normalize(result));
}
Also used : ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) TemplateEngine(org.thymeleaf.TemplateEngine) StandardLinkBuilder(org.thymeleaf.linkbuilder.StandardLinkBuilder) IExpressionContext(org.thymeleaf.context.IExpressionContext)

Example 5 with ClassLoaderTemplateResolver

use of org.thymeleaf.templateresolver.ClassLoaderTemplateResolver in project thymeleaf-tests by thymeleaf.

the class DecoupledSTSMTest method test.

private static void test(final String templateName, final String expectedResultLocation, final Map<String, Object> vars) throws Exception {
    final Map<String, Object> requestAttributes = new LinkedHashMap<String, Object>();
    final Map<String, Object[]> requestParameters = new LinkedHashMap<String, Object[]>();
    final HttpServletRequest mockRequest = TestMockServletUtil.createHttpServletRequest("WebVariablesMap", null, requestAttributes, requestParameters, Locale.US);
    final HttpServletResponse mockResponse = TestMockServletUtil.createHttpServletResponse();
    final Map<String, Object> servletContextAttributes = new LinkedHashMap<String, Object>();
    final ServletContext mockServletContext = TestMockServletUtil.createServletContext(servletContextAttributes);
    final XmlWebApplicationContext appCtx = new XmlWebApplicationContext();
    appCtx.setServletContext(mockServletContext);
    appCtx.setConfigLocation("classpath:templateparser/decoupled/stsm/applicationContext.xml");
    appCtx.refresh();
    mockServletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx);
    // can be null!
    final ConversionService conversionService = getConversionService(appCtx);
    mockRequest.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx);
    final ServletWebRequest servletWebRequest = new ServletWebRequest(mockRequest, mockResponse);
    RequestContextHolder.setRequestAttributes(servletWebRequest);
    final RequestContext requestContext = new RequestContext(mockRequest, mockResponse, mockServletContext, vars);
    vars.put(AbstractTemplateView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, requestContext);
    vars.put(SpringContextVariableNames.SPRING_REQUEST_CONTEXT, requestContext);
    vars.put(SpringContextVariableNames.THYMELEAF_REQUEST_CONTEXT, new SpringWebMvcThymeleafRequestContext(requestContext, mockRequest));
    final List<String> variableNames = new ArrayList<String>(vars.keySet());
    for (final String variableName : variableNames) {
        final Object bindingObject = vars.get(variableName);
        if (isBindingCandidate(variableName, bindingObject)) {
            final String bindingVariableName = BindingResult.MODEL_KEY_PREFIX + variableName;
            if (!vars.containsKey(bindingVariableName)) {
                final WebDataBinder dataBinder = new WebDataBinder(bindingObject, bindingVariableName);
                dataBinder.setConversionService(conversionService);
                vars.put(bindingVariableName, dataBinder.getBindingResult());
            }
        }
    }
    final WebContext ctx = new WebContext(mockRequest, mockResponse, mockServletContext, Locale.US);
    for (final Map.Entry<String, Object> entry : vars.entrySet()) {
        ctx.setVariable(entry.getKey(), entry.getValue());
    }
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setTemplateMode("HTML");
    templateResolver.setPrefix("templateparser/decoupled/stsm/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setUseDecoupledLogic(true);
    final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    templateEngine.setLinkBuilder(new StandardLinkBuilder() {

        @Override
        protected String computeContextPath(final IExpressionContext context, final String base, final Map<String, Object> parameters) {
            return "";
        }
    });
    appCtx.getAutowireCapableBeanFactory().initializeBean(templateEngine, "templateEngine");
    final String result = templateEngine.process(templateName, ctx);
    final String expected = ResourceUtils.read(ClassLoaderUtils.getClassLoader(DecoupledSTSMTest.class).getResourceAsStream(expectedResultLocation), "ISO-8859-1", true);
    Assert.assertEquals(ResourceUtils.normalize(expected), ResourceUtils.normalize(result));
}
Also used : SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) StandardLinkBuilder(org.thymeleaf.linkbuilder.StandardLinkBuilder) WebContext(org.thymeleaf.context.WebContext) SpringWebMvcThymeleafRequestContext(org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) LinkedHashMap(java.util.LinkedHashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) WebDataBinder(org.springframework.web.bind.WebDataBinder) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) IExpressionContext(org.thymeleaf.context.IExpressionContext) ConversionService(org.springframework.core.convert.ConversionService) ServletContext(javax.servlet.ServletContext) RequestContext(org.springframework.web.servlet.support.RequestContext) SpringWebMvcThymeleafRequestContext(org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

ClassLoaderTemplateResolver (org.thymeleaf.templateresolver.ClassLoaderTemplateResolver)50 Test (org.junit.Test)18 SpringTemplateEngine (org.thymeleaf.spring5.SpringTemplateEngine)17 ArrayList (java.util.ArrayList)13 TemplateEngine (org.thymeleaf.TemplateEngine)13 Context (org.thymeleaf.context.Context)12 ITemplateResolver (org.thymeleaf.templateresolver.ITemplateResolver)12 FileTemplateResolver (org.thymeleaf.templateresolver.FileTemplateResolver)8 DefaultTemplateResolver (org.thymeleaf.templateresolver.DefaultTemplateResolver)4 Bean (org.springframework.context.annotation.Bean)3 HashMap (java.util.HashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 IExpressionContext (org.thymeleaf.context.IExpressionContext)2 StandardLinkBuilder (org.thymeleaf.linkbuilder.StandardLinkBuilder)2 SpringWebFluxTemplateEngine (org.thymeleaf.spring5.SpringWebFluxTemplateEngine)2 AbstractConfigurableTemplateResolver (org.thymeleaf.templateresolver.AbstractConfigurableTemplateResolver)2 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1