org.thymeleaf.exceptions.TemplateProcessingException Java Examples

The following examples show how to use org.thymeleaf.exceptions.TemplateProcessingException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AutoRetryTest.java    From ogham with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotResendEmailIfInvalidTemplate() throws MessagingException {
	// @formatter:off
	builder
		.environment()
			.properties()
				.set("mail.smtp.host", ServerSetupTest.SMTP.getBindAddress())
				.set("mail.smtp.port", ServerSetupTest.SMTP.getPort());
	// @formatter:on
	MessagingService messagingService = builder.build();
	// @formatter:off
	MessageNotSentException e = assertThrows("should throw", MessageNotSentException.class, () -> {
		messagingService.send(new Email()
				.from("[email protected]")
				.to("[email protected]")
				.body().template("/template/thymeleaf/source/invalid.html", new SimpleBean("foo", 42)));
	});
	// @formatter:on
	assertThat("should indicate that this is due to unrecoverable error", e, hasAnyCause(UnrecoverableException.class));
	assertThat("should indicate original exceptions", e, executionFailures(UnrecoverableException.class, hasSize(1)));
	assertThat("should indicate original exceptions", e, executionFailures(UnrecoverableException.class, hasItem(hasAnyCause(instanceOf(TemplateParsingFailedException.class)))));
	assertThat("should indicate original exceptions", e, executionFailures(UnrecoverableException.class, hasItem(hasAnyCause(instanceOf(ParseException.class)))));
	assertThat("should indicate original exceptions", e, executionFailures(UnrecoverableException.class, hasItem(hasAnyCause(instanceOf(TemplateProcessingException.class)))));
}
 
Example #2
Source File: AutoRetryTest.java    From ogham with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotResendSmsIfInvalidTemplate() throws MessagingException {
	// @formatter:off
	builder
		.environment()
			.properties()
				.set("ogham.sms.smpp.host", "127.0.0.1")
				.set("ogham.sms.smpp.port", smppServer.getPort());
	// @formatter:on
	MessagingService messagingService = builder.build();
	// @formatter:off
	MessageNotSentException e = assertThrows("should throw", MessageNotSentException.class, () -> {
		messagingService.send(new Sms()
				.from("0102030405")
				.to("0203040506")
				.message().template("/template/thymeleaf/source/invalid.txt", new SimpleBean("foo", 42)));
	});
	// @formatter:on
	assertThat("should indicate that this is due to unrecoverable error", e, hasAnyCause(UnrecoverableException.class));
	assertThat("should indicate original exceptions", e, executionFailures(UnrecoverableException.class, hasSize(1)));
	assertThat("should indicate original exceptions", e, executionFailures(UnrecoverableException.class, hasItem(hasAnyCause(instanceOf(TemplateParsingFailedException.class)))));
	assertThat("should indicate original exceptions", e, executionFailures(UnrecoverableException.class, hasItem(hasAnyCause(instanceOf(ParseException.class)))));
	assertThat("should indicate original exceptions", e, executionFailures(UnrecoverableException.class, hasItem(hasAnyCause(instanceOf(TemplateProcessingException.class)))));
}
 
Example #3
Source File: SpringWebBeanResolutionTest.java    From ogham with Apache License 2.0 6 votes vote down vote up
@Test
public void emailUsingThymeleafTemplateInAsyncMethodCantResolveUrls() throws MessagingException, IOException {
	RestTemplate rt = new RestTemplate();
	// @formatter:off
	UriComponentsBuilder builder = UriComponentsBuilder.fromPath("async/email")
			.scheme("http")
			.host("localhost")
			.port(port)
			.queryParam("template", "web-beans-and-urls-resolution");
	// @formatter:on
	rt.postForEntity(builder.toUriString(), new HttpEntity<>(""), Void.class);

	await().atMost(5, SECONDS).until(() -> hasError());

	OghamAssertions.assertThat(greenMail)
		.receivedMessages()
			.count(is(0));
	ErrorDto error = getError();
	assertThat(error.getType()).isEqualTo(MessageNotSentException.class.getSimpleName());
	ErrorDto cause = getRootCause(error.getCause());
	assertThat(cause.getType()).isEqualTo(TemplateProcessingException.class.getSimpleName());
	assertThat(cause.getMessage()).contains("Link base \"/fake/resources/foo.js\" cannot be context relative (/...) unless the context used for executing the engine implements the org.thymeleaf.context.IWebContext");
}
 
Example #4
Source File: SpringWebBeanResolutionTest.java    From ogham with Apache License 2.0 6 votes vote down vote up
@Test
public void smsUsingThymeleafTemplateInAsyncMethodCantResolveUrls() throws MessagingException, IOException {
	RestTemplate rt = new RestTemplate();
	// @formatter:off
	UriComponentsBuilder builder = UriComponentsBuilder.fromPath("async/sms")
			.scheme("http")
			.host("localhost")
			.port(port)
			.queryParam("template", "web-beans-and-urls-resolution");
	// @formatter:on
	rt.postForEntity(builder.toUriString(), new HttpEntity<>(""), Void.class);

	await().atMost(5, SECONDS).until(() -> hasError());

	OghamAssertions.assertThat(smppServer)
		.receivedMessages()
			.count(is(0));
	ErrorDto error = getError();
	assertThat(error.getType()).isEqualTo(MessageNotSentException.class.getSimpleName());
	ErrorDto cause = getRootCause(error.getCause());
	assertThat(cause.getType()).isEqualTo(TemplateProcessingException.class.getSimpleName());
	assertThat(cause.getMessage()).contains("Link base \"/fake/resources/foo.js\" cannot be context relative (/...) unless the context used for executing the engine implements the org.thymeleaf.context.IWebContext");
}
 
Example #5
Source File: SpringWebBeanResolutionTest.java    From ogham with Apache License 2.0 6 votes vote down vote up
@Test
public void emailUsingThymeleafTemplateInAsyncMethodCantResolveUrls() throws MessagingException, IOException {
	RestTemplate rt = new RestTemplate();
	// @formatter:off
	UriComponentsBuilder builder = UriComponentsBuilder.fromPath("async/email")
			.scheme("http")
			.host("localhost")
			.port(port)
			.queryParam("template", "web-beans-and-urls-resolution");
	// @formatter:on
	rt.postForEntity(builder.toUriString(), new HttpEntity<>(""), Void.class);

	await().atMost(5, SECONDS).until(() -> hasError());

	OghamAssertions.assertThat(greenMail)
		.receivedMessages()
			.count(is(0));
	ErrorDto error = getError();
	assertThat(error.getType()).isEqualTo(MessageNotSentException.class.getSimpleName());
	ErrorDto cause = getRootCause(error.getCause());
	assertThat(cause.getType()).isEqualTo(TemplateProcessingException.class.getSimpleName());
	assertThat(cause.getMessage()).contains("Link base \"/fake/resources/foo.js\" cannot be context relative (/) or page relative unless you implement the org.thymeleaf.context.IWebContext");
}
 
Example #6
Source File: SpringWebBeanResolutionTest.java    From ogham with Apache License 2.0 6 votes vote down vote up
@Test
public void smsUsingThymeleafTemplateInAsyncMethodCantResolveUrls() throws MessagingException, IOException {
	RestTemplate rt = new RestTemplate();
	// @formatter:off
	UriComponentsBuilder builder = UriComponentsBuilder.fromPath("async/sms")
			.scheme("http")
			.host("localhost")
			.port(port)
			.queryParam("template", "web-beans-and-urls-resolution");
	// @formatter:on
	rt.postForEntity(builder.toUriString(), new HttpEntity<>(""), Void.class);

	await().atMost(5, SECONDS).until(() -> hasError());

	OghamAssertions.assertThat(smppServer)
		.receivedMessages()
			.count(is(0));
	ErrorDto error = getError();
	assertThat(error.getType()).isEqualTo(MessageNotSentException.class.getSimpleName());
	ErrorDto cause = getRootCause(error.getCause());
	assertThat(cause.getType()).isEqualTo(TemplateProcessingException.class.getSimpleName());
	assertThat(cause.getMessage()).contains("Link base \"/fake/resources/foo.js\" cannot be context relative (/) or page relative unless you implement the org.thymeleaf.context.IWebContext");
}
 
Example #7
Source File: ThymeleafServiceImpl.java    From purplejs with Apache License 2.0 5 votes vote down vote up
static RuntimeException handleError( final RuntimeException e )
{
    if ( e instanceof TemplateProcessingException )
    {
        return handleError( (TemplateProcessingException) e );
    }

    return e;
}
 
Example #8
Source File: ThymeleafServiceImpl.java    From purplejs with Apache License 2.0 5 votes vote down vote up
private static RuntimeException handleError( final TemplateProcessingException e )
{
    final int lineNumber = e.getLine() != null ? e.getLine() : 0;
    final ResourcePath path = e.getTemplateName() != null ? ResourcePath.from( e.getTemplateName() ) : null;

    return ProblemException.newBuilder().
        lineNumber( lineNumber ).
        path( path ).
        cause( e ).
        message( e.getMessage() ).
        build();
}
 
Example #9
Source File: ExternalFileTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void fileNotFound() throws ParseException {
	TemplateParser parser = builder.build();
	
	ParseException e = assertThrows("should throw", ParseException.class, () -> {
		parser.parse(new UnresolvedPath("file:unexisting.html"), new BeanContext(new SimpleBean("foo", 42)));
	});
	assertThat("should indicate cause", e.getCause(), instanceOf(TemplateProcessingException.class));
	assertThat("should indicate cause", e.getCause(), hasAnyCause(TemplateResolutionException.class, hasMessage(containsString("Failed to find template file:unexisting.html"))));
}
 
Example #10
Source File: ThymeleafFacade.java    From thymeleaf-extras-shiro with Apache License 2.0 5 votes vote down vote up
public static List<Object> evaluateAsIterable(ITemplateContext arguments, String rawValue) throws TemplateProcessingException {
    notNull(arguments, "arguments must not be null");
    notEmpty(rawValue, "rawValue must not be empty");

    final Object evaluatedExpression = evaluateExpression(arguments, rawValue);

    return EvaluationUtils.evaluateAsList(evaluatedExpression);
}
 
Example #11
Source File: ThymeleafFacade.java    From thymeleaf-extras-shiro with Apache License 2.0 5 votes vote down vote up
public static List<Object> evaluateAsIterableOrRawValue(ITemplateContext arguments, String rawValue) {
    notNull(arguments, "arguments must not be null");
    notEmpty(rawValue, "rawValue must not be empty");

    final List<Object> result = new ArrayList<Object>();
    try {
        result.addAll(evaluateAsIterable(arguments, rawValue));
    } catch (TemplateProcessingException ex) {
        result.add(rawValue);
    }

    return unmodifiableList(result);
}
 
Example #12
Source File: ThymeleafFacade.java    From thymeleaf-extras-shiro with Apache License 2.0 3 votes vote down vote up
public static Object evaluateExpression(ITemplateContext arguments, String expression) throws TemplateProcessingException {
    notNull(arguments, "arguments must not be null");
    notEmpty(expression, "expression must not be empty");

    final IStandardExpressionParser parser = new StandardExpressionParser();

    final IStandardExpression evaluableExpression = parser.parseExpression(arguments, expression);

    return evaluableExpression.execute(arguments);
}