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);
}
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);
}
}
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);
}
}
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);
}
}
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;
}
Aggregations