Search in sources :

Example 1 with TemplateProcessingException

use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf-tests by thymeleaf.

the class AbstractSpring5ReactiveTest method testTemplateDirectExecution.

private static void testTemplateDirectExecution(final String template, final Set<String> markupSelectors, final IContext context, final String result, final boolean sse, final int responseMaxChunkSizeBytes) throws Exception {
    final String dataDriverVariableName = detectDataDriver(context);
    final boolean isDataDriven = dataDriverVariableName != null;
    List<DataBuffer> resultBuffers = null;
    try {
        final Publisher<DataBuffer> resultStream = templateEngine.processStream(template, markupSelectors, context, bufferFactory, (sse ? sseMediaType : htmlMediaType), charset, responseMaxChunkSizeBytes);
        resultBuffers = Flux.from(resultStream).collectList().block();
    } catch (final Exception e) {
        throw new TemplateProcessingException("Error happened while executing reactive test for template " + template + " with markup " + "selectors " + markupSelectors + ", context with variables " + context.getVariableNames() + " and " + "response chunk size of " + responseMaxChunkSizeBytes + " bytes.", e);
    }
    if (responseMaxChunkSizeBytes != Integer.MAX_VALUE) {
        for (final DataBuffer resultBuffer : resultBuffers) {
            Assert.assertTrue("Buffer returned by stream is of size larger than " + responseMaxChunkSizeBytes, resultBuffer.readableByteCount() <= responseMaxChunkSizeBytes);
        }
    } else {
        if (!isDataDriven) {
            final int bufferCount = resultBuffers.size();
            Assert.assertTrue("No limit set on buffer size, and non-data-driven: there should only be one result buffer instead of " + bufferCount, bufferCount == 1);
        }
    }
    final String resultStr = resultBuffers.stream().map((buffer) -> ReactiveTestUtils.bufferAsString(buffer, charset)).map(// Note we NORMALIZE before joining it all
    ReactiveTestUtils::normalizeResult).collect(Collectors.joining());
    final String expected = ReactiveTestUtils.readExpectedNormalizedResults(result, charset);
    Assert.assertEquals(expected, resultStr);
}
Also used : TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 2 with TemplateProcessingException

use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf-tests by thymeleaf.

the class SpringSpecificVersionUtils method createSpringStandardDialectInstance.

public static IDialect createSpringStandardDialectInstance(final boolean compiledSpEL) {
    if (dialectClass == null) {
        throw new ConfigurationException("Cannot create instance of SpringStandardDialect. The testing system was not able to determine " + "that the Spring version being used is a supported one.");
    }
    try {
        final IDialect dialect = dialectClass.newInstance();
        if (!compiledSpEL) {
            return dialect;
        }
        try {
            final Method enableSpELMethod = dialect.getClass().getMethod("setEnableSpringELCompiler", new Class[] { boolean.class });
            enableSpELMethod.invoke(dialect, true);
            return dialect;
        } catch (final NoSuchMethodException e) {
            if (!SPRING3_STANDARD_DIALECT_CLASS.equals(dialectClass.getName())) {
                throw new TemplateProcessingException("Could not activate SpEL Compiler in SpringStandardDialect for Spring >= v4");
            }
            return dialect;
        }
    } catch (final Exception e) {
        throw new ConfigurationException("Cannot create instance of SpringStandardDialect", e);
    }
}
Also used : ConfigurationException(org.thymeleaf.exceptions.ConfigurationException) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) Method(java.lang.reflect.Method) IDialect(org.thymeleaf.dialect.IDialect) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) ConfigurationException(org.thymeleaf.exceptions.ConfigurationException)

Example 3 with TemplateProcessingException

use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf-tests by thymeleaf.

the class Spring5Reactive12Test method testDataDrivenEmpty02.

@Test
public void testDataDrivenEmpty02() throws Exception {
    final List<Album> albums = AlbumRepository.findAllAlbums();
    final Context ctx1 = new Context();
    try {
        testTemplate("reactive12", null, ctx1, "reactive12-01", true);
        Assert.assertTrue(false);
    } catch (final TemplateProcessingException e) {
        // When there is no data-driver variable, an exception should be thrown
        Assert.assertTrue(true);
    }
}
Also used : Context(org.thymeleaf.context.Context) Album(org.thymeleaf.spring5.reactive.data.Album) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) Test(org.junit.Test)

Example 4 with TemplateProcessingException

use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf-tests by thymeleaf.

the class Spring5Reactive13Test method testDataDrivenEmpty02.

@Test
public void testDataDrivenEmpty02() throws Exception {
    final List<Album> albums = AlbumRepository.findAllAlbums();
    final Context ctx1 = new Context();
    try {
        testTemplate("reactive13", null, ctx1, "reactive13-01", true);
        Assert.assertTrue(false);
    } catch (final TemplateProcessingException e) {
        // When there is no data-driver variable, an exception should be thrown
        Assert.assertTrue(true);
    }
}
Also used : Context(org.thymeleaf.context.Context) Album(org.thymeleaf.spring5.reactive.data.Album) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) Test(org.junit.Test)

Example 5 with TemplateProcessingException

use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf by thymeleaf.

the class TemplateManager method createTemplateProcessingHandlerChain.

private static ITemplateHandler createTemplateProcessingHandlerChain(final IEngineContext context, final boolean setPreProcessors, final boolean setPostProcessors, final ITemplateHandler handler, final Writer writer) {
    final IEngineConfiguration configuration = context.getConfiguration();
    /*
         * Declare the pair of pointers that will allow us to build the chain of template handlers
         */
    ITemplateHandler firstHandler = null;
    ITemplateHandler lastHandler = null;
    /*
         * First type of handlers to be added: pre-processors (if any)
         */
    if (setPreProcessors) {
        final Set<IPreProcessor> preProcessors = configuration.getPreProcessors(context.getTemplateMode());
        if (preProcessors != null && preProcessors.size() > 0) {
            for (final IPreProcessor preProcessor : preProcessors) {
                final Class<? extends ITemplateHandler> preProcessorClass = preProcessor.getHandlerClass();
                final ITemplateHandler preProcessorHandler;
                try {
                    preProcessorHandler = preProcessorClass.newInstance();
                } catch (final Exception e) {
                    // This should never happen - class was already checked during configuration to contain a zero-arg constructor
                    throw new TemplateProcessingException("An exception happened during the creation of a new instance of pre-processor " + preProcessorClass.getClass().getName(), e);
                }
                // Initialize the pre-processor
                preProcessorHandler.setContext(context);
                if (firstHandler == null) {
                    firstHandler = preProcessorHandler;
                    lastHandler = preProcessorHandler;
                } else {
                    lastHandler.setNext(preProcessorHandler);
                    lastHandler = preProcessorHandler;
                }
            }
        }
    }
    /*
         * Initialize and add to the chain te Processor Handler itself, the central piece of the chain
         */
    handler.setContext(context);
    if (firstHandler == null) {
        firstHandler = handler;
        lastHandler = handler;
    } else {
        lastHandler.setNext(handler);
        lastHandler = handler;
    }
    /*
         * After the Processor Handler, we now must add the post-processors (if any)
         */
    if (setPostProcessors) {
        final Set<IPostProcessor> postProcessors = configuration.getPostProcessors(context.getTemplateMode());
        if (postProcessors != null && postProcessors.size() > 0) {
            for (final IPostProcessor postProcessor : postProcessors) {
                final Class<? extends ITemplateHandler> postProcessorClass = postProcessor.getHandlerClass();
                final ITemplateHandler postProcessorHandler;
                try {
                    postProcessorHandler = postProcessorClass.newInstance();
                } catch (final Exception e) {
                    // This should never happen - class was already checked during configuration to contain a zero-arg constructor
                    throw new TemplateProcessingException("An exception happened during the creation of a new instance of post-processor " + postProcessorClass.getClass().getName(), e);
                }
                // Initialize the pre-processor
                postProcessorHandler.setContext(context);
                if (firstHandler == null) {
                    firstHandler = postProcessorHandler;
                    lastHandler = postProcessorHandler;
                } else {
                    lastHandler.setNext(postProcessorHandler);
                    lastHandler = postProcessorHandler;
                }
            }
        }
    }
    /*
         * Last step: the OUTPUT HANDLER
         */
    if (writer != null) {
        final OutputTemplateHandler outputHandler = new OutputTemplateHandler(writer);
        outputHandler.setContext(context);
        if (firstHandler == null) {
            firstHandler = outputHandler;
        } else {
            lastHandler.setNext(outputHandler);
        }
    }
    return firstHandler;
}
Also used : IEngineConfiguration(org.thymeleaf.IEngineConfiguration) IPostProcessor(org.thymeleaf.postprocessor.IPostProcessor) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) IPreProcessor(org.thymeleaf.preprocessor.IPreProcessor) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) TemplateInputException(org.thymeleaf.exceptions.TemplateInputException)

Aggregations

TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)45 BigDecimal (java.math.BigDecimal)12 IEngineConfiguration (org.thymeleaf.IEngineConfiguration)8 IStandardExpression (org.thymeleaf.standard.expression.IStandardExpression)6 IOException (java.io.IOException)5 ITemplateContext (org.thymeleaf.context.ITemplateContext)4 Writer (java.io.Writer)3 Test (org.junit.Test)3 Context (org.thymeleaf.context.Context)3 AttributeName (org.thymeleaf.engine.AttributeName)3 TemplateEngineException (org.thymeleaf.exceptions.TemplateEngineException)3 TemplateInputException (org.thymeleaf.exceptions.TemplateInputException)3 TemplateOutputException (org.thymeleaf.exceptions.TemplateOutputException)3 FastStringWriter (org.thymeleaf.util.FastStringWriter)3 DataBuffer (org.springframework.core.io.buffer.DataBuffer)2 IEngineContext (org.thymeleaf.context.IEngineContext)2 TemplateManager (org.thymeleaf.engine.TemplateManager)2 IAttribute (org.thymeleaf.model.IAttribute)2 IOpenElementTag (org.thymeleaf.model.IOpenElementTag)2 IProcessableElementTag (org.thymeleaf.model.IProcessableElementTag)2