freemarker.core.InvalidReferenceException Java Examples

The following examples show how to use freemarker.core.InvalidReferenceException. 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 doNotResendEmailIfParsingFailed() 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/freemarker/source/simple.html.ftl", null));
	});
	// @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(InvalidReferenceException.class)))));
}
 
Example #2
Source File: AutoRetryTest.java    From ogham with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotResendSmsIfParsingFailed() 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/freemarker/source/simple.html.ftl", null));
	});
	// @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(InvalidReferenceException.class)))));
}
 
Example #3
Source File: StaticMethodAccessTest.java    From ogham with Apache License 2.0 6 votes vote down vote up
@Test
public void emailUsingFreemarkerTemplateAndStaticMethodAccessDisabledShouldFail() throws MessagingException, IOException {
	contextRunner = contextRunner.withPropertyValues(
			"ogham.freemarker.static-method-access.enable=false");
	contextRunner.run((context) -> {
		MessagingService messagingService = context.getBean(MessagingService.class);
		// @formatter:off
		MessagingException e = assertThrows("should throw", MessagingException.class, () -> {
			messagingService.send(new Email()
					.from("[email protected]")
					.to("[email protected]")
					.content(new TemplateContent("/freemarker/source/static-methods.html.ftl", new SimpleBean("world", 0))));
		});
		assertThat("should report missing statics", e, allOf(
				instanceOf(MessagingException.class),
				hasAnyCause(InvalidReferenceException.class, hasMessage(containsString("The following has evaluated to null or missing:\n==> statics")))));
		// @formatter:on
	});
}
 
Example #4
Source File: StaticMethodAccessTest.java    From ogham with Apache License 2.0 6 votes vote down vote up
@Test
public void smsUsingFreemarkerTemplateAndStaticMethodAccessDisabledShouldFail() throws MessagingException, IOException {
	contextRunner = contextRunner.withPropertyValues(
			"ogham.freemarker.static-method-access.enable=false");
	contextRunner.run((context) -> {
		MessagingService messagingService = context.getBean(MessagingService.class);
		// @formatter:off
		MessagingException e = assertThrows("should throw", MessagingException.class, () -> {
			messagingService.send(new Sms()
					.from("+33102030405")
					.to("+33123456789")
					.content(new TemplateContent("/freemarker/source/static-methods.txt.ftl", new SimpleBean("world", 0))));
		});
		// @formatter:on
		assertThat("should report missing statics", e, allOf(
				instanceOf(MessagingException.class),
				hasAnyCause(InvalidReferenceException.class, hasMessage(containsString("The following has evaluated to null or missing:\n==> statics")))));
	});
}
 
Example #5
Source File: ScriptUtils.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
/** @return script variables */
public static Set<String> getScriptVariables(Script script) {
  // based on https://stackoverflow.com/a/48024379
  Set<String> scriptExpressions = new LinkedHashSet<>();
  Configuration configuration = new Configuration(VERSION);
  configuration.setTemplateExceptionHandler(
      (te, env, out) -> {
        if (te instanceof InvalidReferenceException) {
          scriptExpressions.add(te.getBlamedExpressionString());
          return;
        }
        throw te;
      });

  try {
    Template template = new Template(null, new StringReader(script.getContent()), configuration);
    template.process(emptyMap(), new StringWriter());
  } catch (TemplateException | IOException e) {
    throw new GenerateScriptException(
        "Error processing parameters for script [" + script.getName() + "]. " + e.getMessage());
  }
  return scriptExpressions;
}
 
Example #6
Source File: StaticMethodAccessDisabledTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void emailUsingFreemarkerTemplateAndStaticMethodAccessDisabledShouldFail() throws MessagingException, IOException {
	// @formatter:off
	MessagingException e = assertThrows("should throw", MessagingException.class, () -> {
		messagingService.send(new Email()
				.from("[email protected]")
				.to("[email protected]")
				.content(new TemplateContent("/template/freemarker/source/static-methods.html.ftl", new SimpleBean("world", 0))));
	});
	assertThat("should report missing statics", e, allOf(
			instanceOf(MessagingException.class),
			hasAnyCause(InvalidReferenceException.class, hasMessage(containsString("The following has evaluated to null or missing:\n==> statics")))));
	// @formatter:on
}
 
Example #7
Source File: StaticMethodAccessDisabledTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void smsUsingFreemarkerTemplateAndStaticMethodAccessDisabledShouldFail() throws MessagingException, IOException {
	// @formatter:off
	MessagingException e = assertThrows("should throw", MessagingException.class, () -> {
		messagingService.send(new Sms()
				.from("+33102030405")
				.to("+33123456789")
				.content(new TemplateContent("/template/freemarker/source/static-methods.txt.ftl", new SimpleBean("world", 0))));
	});
	// @formatter:on
	assertThat("should report missing statics", e, allOf(
			instanceOf(MessagingException.class),
			hasAnyCause(InvalidReferenceException.class, hasMessage(containsString("The following has evaluated to null or missing:\n==> statics")))));
}
 
Example #8
Source File: SpringBeanResolutionTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void missingBeanErrorUsingFreemarker() throws MessagingException, IOException {
	MessagingException e = assertThrows("should throw", MessagingException.class, () -> {
		contextRunner.run((context) -> {
			MessagingService messagingService = context.getBean(MessagingService.class);
			messagingService.send(new Email()
					.from("[email protected]")
					.to("[email protected]")
					.content(new TemplateContent("/freemarker/source/missing-bean.html.ftl", new SimpleBean("world", 0))));
		});
	});
	assertThat("should indicate missing bean", e, hasAnyCause(InvalidReferenceException.class, hasMessage(containsString("The following has evaluated to null or missing:\n==> @missingService"))));
}
 
Example #9
Source File: SpringBeanResolutionTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void missingBeanErrorUsingFreemarker() throws MessagingException, IOException {
	MessagingException e = assertThrows("should throw", MessagingException.class, () -> {
		messagingService.send(new Email()
				.from("[email protected]")
				.to("[email protected]")
				.content(new TemplateContent("/freemarker/source/missing-bean.html.ftl", new SimpleBean("world", 0))));
	});
	assertThat("should indicate missing bean", e, hasAnyCause(InvalidReferenceException.class, hasMessage(containsString("The following has evaluated to null or missing:\n==> @missingService"))));
}