com.hubspot.jinjava.JinjavaConfig Java Examples
The following examples show how to use
com.hubspot.jinjava.JinjavaConfig.
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: ExpressionResolverTest.java From jinjava with Apache License 2.0 | 6 votes |
@Test public void itBlocksDisabledFunctions() { Map<Context.Library, Set<String>> disabled = ImmutableMap.of( Library.FUNCTION, ImmutableSet.of(":range") ); String template = "hi {% for i in range(1, 3) %}{{i}} {% endfor %}"; String rendered = jinjava.render(template, context); assertEquals("hi 1 2 ", rendered); final JinjavaConfig config = JinjavaConfig .newBuilder() .withDisabled(disabled) .build(); final RenderResult renderResult = jinjava.renderForResult(template, context, config); assertEquals("hi ", renderResult.getOutput()); TemplateError e = renderResult.getErrors().get(0); assertThat(e.getItem()).isEqualTo(ErrorItem.FUNCTION); assertThat(e.getReason()).isEqualTo(ErrorReason.DISABLED); assertThat(e.getMessage()).contains("':range' is disabled in this context"); }
Example #2
Source File: ValidationModeTest.java From jinjava with Apache License 2.0 | 6 votes |
@Before public void setup() { validationFilter = new ValidationFilter(); ELFunctionDefinition validationFunction = new ELFunctionDefinition( "", "validation_test", ValidationModeTest.class, "validationTestFunction" ); jinjava = new Jinjava(); jinjava.getGlobalContext().registerFilter(validationFilter); jinjava.getGlobalContext().registerFunction(validationFunction); interpreter = jinjava.newInterpreter(); context = interpreter.getContext(); validatingInterpreter = new JinjavaInterpreter( jinjava, context, JinjavaConfig.newBuilder().withValidationMode(true).build() ); JinjavaInterpreter.pushCurrent(interpreter); }
Example #3
Source File: TokenScanner.java From jinjava with Apache License 2.0 | 6 votes |
public TokenScanner(String input, JinjavaConfig config) { this.config = config; is = input.toCharArray(); length = is.length; currPost = 0; tokenStart = 0; tokenKind = -1; lastStart = 0; inComment = 0; inRaw = 0; inBlock = 0; inQuote = 0; currLine = 1; lastNewlinePos = 0; symbols = config.getTokenScannerSymbols(); }
Example #4
Source File: MacroTagTest.java From jinjava with Apache License 2.0 | 6 votes |
@Test public void itEnforcesMacroRecursionWithMaxDepth() throws IOException { interpreter = new Jinjava( JinjavaConfig .newBuilder() .withEnableRecursiveMacroCalls(true) .withMaxMacroRecursionDepth(2) .build() ) .newInterpreter(); JinjavaInterpreter.pushCurrent(interpreter); try { String template = fixtureText("ending-recursion"); String out = interpreter.render(template); assertThat(interpreter.getErrorsCopy().get(0).getMessage()) .contains("Max recursion limit of 2 reached for macro 'hello'"); assertThat(out).contains("Hello Hello"); } finally { JinjavaInterpreter.popCurrent(); } }
Example #5
Source File: MacroTagTest.java From jinjava with Apache License 2.0 | 6 votes |
@Test public void itAllowsMacroRecursionWithMaxDepthInValidationMode() throws IOException { interpreter = new Jinjava( JinjavaConfig .newBuilder() .withEnableRecursiveMacroCalls(true) .withMaxMacroRecursionDepth(10) .withValidationMode(true) .build() ) .newInterpreter(); JinjavaInterpreter.pushCurrent(interpreter); try { String template = fixtureText("ending-recursion"); String out = interpreter.render(template); assertThat(interpreter.getErrorsCopy()).isEmpty(); assertThat(out).contains("Hello Hello Hello Hello Hello"); } finally { JinjavaInterpreter.popCurrent(); } }
Example #6
Source File: MacroTagTest.java From jinjava with Apache License 2.0 | 6 votes |
@Test public void itAllowsMacroRecursionWithMaxDepth() throws IOException { interpreter = new Jinjava( JinjavaConfig .newBuilder() .withEnableRecursiveMacroCalls(true) .withMaxMacroRecursionDepth(10) .build() ) .newInterpreter(); JinjavaInterpreter.pushCurrent(interpreter); try { String template = fixtureText("ending-recursion"); String out = interpreter.render(template); assertThat(interpreter.getErrorsCopy()).isEmpty(); assertThat(out).contains("Hello Hello Hello Hello Hello"); } finally { JinjavaInterpreter.popCurrent(); } }
Example #7
Source File: MacroTagTest.java From jinjava with Apache License 2.0 | 6 votes |
@Test public void itAllowsMacroRecursionWhenEnabledInConfiguration() throws IOException { // I need a different configuration here therefore interpreter = new Jinjava(JinjavaConfig.newBuilder().withEnableRecursiveMacroCalls(true).build()) .newInterpreter(); JinjavaInterpreter.pushCurrent(interpreter); try { String template = fixtureText("ending-recursion"); String out = interpreter.render(template); assertThat(interpreter.getErrorsCopy()).isEmpty(); assertThat(out).contains("Hello Hello Hello Hello Hello"); } finally { // and I need to cleanup my mess... JinjavaInterpreter.popCurrent(); } }
Example #8
Source File: JinjavaInterpreterTest.java From jinjava with Apache License 2.0 | 6 votes |
@Test public void itLimitsOutputSizeWhenSumOfNodeSizesExceedsMax() { JinjavaConfig outputSizeLimitedConfig = JinjavaConfig .newBuilder() .withMaxOutputSize(19) .build(); String input = "1234567890{% block testchild %}1234567890{% endblock %}"; String output = "12345678901234567890"; // Note that this exceeds the max size RenderResult renderResult = new Jinjava().renderForResult(input, new HashMap<>()); assertThat(renderResult.getOutput()).isEqualTo(output); assertThat(renderResult.hasErrors()).isFalse(); renderResult = new Jinjava(outputSizeLimitedConfig).renderForResult(input, new HashMap<>()); assertThat(renderResult.hasErrors()).isTrue(); assertThat(renderResult.getErrors().get(0).getMessage()) .contains("OutputTooBigException"); }
Example #9
Source File: JinjavaInterpreterTest.java From jinjava with Apache License 2.0 | 6 votes |
@Test public void itLimitsOutputSize() { JinjavaConfig outputSizeLimitedConfig = JinjavaConfig .newBuilder() .withMaxOutputSize(20) .build(); String output = "123456789012345678901234567890"; RenderResult renderResult = new Jinjava().renderForResult(output, new HashMap<>()); assertThat(renderResult.getOutput()).isEqualTo(output); assertThat(renderResult.hasErrors()).isFalse(); renderResult = new Jinjava(outputSizeLimitedConfig).renderForResult(output, new HashMap<>()); assertThat(renderResult.getErrors().get(0).getMessage()) .contains("OutputTooBigException"); }
Example #10
Source File: DeferredTest.java From jinjava with Apache License 2.0 | 6 votes |
@Before public void setup() { JinjavaConfig config = JinjavaConfig .newBuilder() .withRandomNumberGeneratorStrategy(RandomNumberGeneratorStrategy.DEFERRED) .build(); JinjavaInterpreter parentInterpreter = new JinjavaInterpreter( jinjava, globalContext, config ); interpreter = new JinjavaInterpreter(parentInterpreter); localContext = interpreter.getContext(); localContext.put("deferred", DeferredValue.instance()); localContext.put("resolved", "resolvedValue"); JinjavaInterpreter.pushCurrent(interpreter); }
Example #11
Source File: TokenScannerTest.java From jinjava with Apache License 2.0 | 5 votes |
@Test public void testLstripBlocks() { config = JinjavaConfig.newBuilder().withLstripBlocks(true).withTrimBlocks(true).build(); List<Token> tokens = tokens("tag-with-trim-chars"); assertThat(tokens).isNotEmpty(); }
Example #12
Source File: TreeParserTest.java From jinjava with Apache License 2.0 | 5 votes |
@Test public void trimAndLstripBlocks() { interpreter = new Jinjava( JinjavaConfig.newBuilder().withLstripBlocks(true).withTrimBlocks(true).build() ) .newInterpreter(); assertThat(interpreter.render(parse("parse/tokenizer/whitespace-tags.jinja"))) .isEqualTo("<div>\n" + " yay\n" + "</div>\n"); }
Example #13
Source File: TokenWhitespaceTest.java From jinjava with Apache License 2.0 | 5 votes |
private List<Token> scanTokens(String srcPath, JinjavaConfig config) { try { return Lists.newArrayList( new TokenScanner( Resources.toString(Resources.getResource(srcPath), StandardCharsets.UTF_8), config ) ); } catch (IOException e) { throw new RuntimeException(e); } }
Example #14
Source File: CustomTokenScannerSymbolsTest.java From jinjava with Apache License 2.0 | 5 votes |
@Before public void setup() { config = JinjavaConfig.newBuilder().withTokenScannerSymbols(new CustomTokens()).build(); jinjava = new Jinjava(config); jinjava.getGlobalContext().put("numbers", Lists.newArrayList(1L, 2L, 3L, 4L, 5L)); }
Example #15
Source File: FailOnUnknownTokensTest.java From jinjava with Apache License 2.0 | 5 votes |
@Before public void setUp() { JinjavaConfig.Builder builder = JinjavaConfig.newBuilder(); builder.withFailOnUnknownTokens(true); JinjavaConfig config = builder.build(); jinjava = new Jinjava(config); }
Example #16
Source File: FailOnUnknownTokensTest.java From jinjava with Apache License 2.0 | 5 votes |
@Test public void itReplacesTokensInContextButThrowsExceptionForOthers() { final JinjavaConfig config = JinjavaConfig .newBuilder() .withFailOnUnknownTokens(true) .build(); JinjavaInterpreter jinjavaInterpreter = new Jinjava(config).newInterpreter(); String template = "{{ name }} has a {{ animal }}"; Node node = new TreeParser(jinjavaInterpreter, template).buildTree(); assertThatThrownBy(() -> jinjavaInterpreter.render(node)) .isInstanceOf(UnknownTokenException.class) .hasMessageContaining("Unknown token found: name"); }
Example #17
Source File: ExtendsTagTest.java From jinjava with Apache License 2.0 | 5 votes |
@Before public void setup() { locator = new ExtendsTagTestResourceLocator(); JinjavaConfig conf = new JinjavaConfig(); jinjava = new Jinjava(conf); jinjava.setResourceLocator(locator); }
Example #18
Source File: JoinFilterTest.java From jinjava with Apache License 2.0 | 5 votes |
@Test public void itTruncatesStringToConfigLimit() { jinjava = new Jinjava(JinjavaConfig.newBuilder().withMaxStringLength(5).build()); RenderResult result = jinjava.renderForResult( "{{ [1, 2, 3, 4, 5]|join('|') }}", new HashMap<>() ); assertThat(result.getOutput()).isEqualTo("1|2|3"); assertThat(result.getErrors().size()).isEqualTo(1); assertThat(result.getErrors().get(0).getMessage()) .contains("filter has been truncated to the max String length"); }
Example #19
Source File: ExpressionResolverTest.java From jinjava with Apache License 2.0 | 5 votes |
@Test public void itStoresResolvedFunctions() { context.put("datetime", 12345); final JinjavaConfig config = JinjavaConfig.newBuilder().build(); String template = "{% for i in range(1, 5) %}{{i}} {% endfor %}\n{{ unixtimestamp(datetime) }}"; final RenderResult renderResult = jinjava.renderForResult(template, context, config); assertThat(renderResult.getOutput()).isEqualTo("1 2 3 4 \n12345"); assertThat(renderResult.getContext().getResolvedFunctions()) .hasSameElementsAs(ImmutableSet.of(":range", ":unixtimestamp")); }
Example #20
Source File: ExpressionNodeTest.java From jinjava with Apache License 2.0 | 5 votes |
@Test public void itFailsOnUnknownTokensOfIf() throws Exception { final JinjavaConfig config = JinjavaConfig .newBuilder() .withFailOnUnknownTokens(true) .build(); JinjavaInterpreter jinjavaInterpreter = new Jinjava(config).newInterpreter(); String jinja = "{% if bad %} BAD {% endif %}"; Node node = new TreeParser(jinjavaInterpreter, jinja).buildTree(); assertThatThrownBy(() -> jinjavaInterpreter.render(node)) .isInstanceOf(UnknownTokenException.class) .hasMessageContaining("Unknown token found: bad"); }
Example #21
Source File: ExpressionNodeTest.java From jinjava with Apache License 2.0 | 5 votes |
@Test public void itFailsOnUnknownTokensOfLoops() throws Exception { final JinjavaConfig config = JinjavaConfig .newBuilder() .withFailOnUnknownTokens(true) .build(); JinjavaInterpreter jinjavaInterpreter = new Jinjava(config).newInterpreter(); String jinja = "{% for v in values %} {{ v }} {% endfor %}"; Node node = new TreeParser(jinjavaInterpreter, jinja).buildTree(); assertThatThrownBy(() -> jinjavaInterpreter.render(node)) .isInstanceOf(UnknownTokenException.class) .hasMessage("Unknown token found: values"); }
Example #22
Source File: ExpressionNodeTest.java From jinjava with Apache License 2.0 | 5 votes |
@Test public void itFailsOnUnknownTokensVariables() throws Exception { final JinjavaConfig config = JinjavaConfig .newBuilder() .withFailOnUnknownTokens(true) .build(); JinjavaInterpreter jinjavaInterpreter = new Jinjava(config).newInterpreter(); String jinja = "{{ UnknownToken }}"; Node node = new TreeParser(jinjavaInterpreter, jinja).buildTree(); assertThatThrownBy(() -> jinjavaInterpreter.render(node)) .isInstanceOf(UnknownTokenException.class) .hasMessage("Unknown token found: UnknownToken"); }
Example #23
Source File: ExpressionNodeTest.java From jinjava with Apache License 2.0 | 5 votes |
@Test public void itRendersNestedTags() throws Exception { final JinjavaConfig config = JinjavaConfig.newBuilder().build(); JinjavaInterpreter jinjava = new Jinjava(config).newInterpreter(); Context context = jinjava.getContext(); context.put("myvar", "hello {% if (true) %}nasty{% endif %}"); ExpressionNode node = fixture("simplevar"); assertThat(node.render(jinjava).toString()).isEqualTo("hello nasty"); }
Example #24
Source File: ExpressionNodeTest.java From jinjava with Apache License 2.0 | 5 votes |
@Test public void itRendersWithNestedExpressionInterpretationByDefault() throws Exception { final JinjavaConfig config = JinjavaConfig.newBuilder().build(); JinjavaInterpreter noNestedInterpreter = new Jinjava(config).newInterpreter(); Context contextNoNestedInterpretation = noNestedInterpreter.getContext(); contextNoNestedInterpretation.put("myvar", "hello {{ place }}"); contextNoNestedInterpretation.put("place", "world"); ExpressionNode node = fixture("simplevar"); assertThat(node.render(noNestedInterpreter).toString()).isEqualTo("hello world"); }
Example #25
Source File: ExpressionNodeTest.java From jinjava with Apache License 2.0 | 5 votes |
@Test public void itRendersResultWithoutNestedExpressionInterpretation() throws Exception { final JinjavaConfig config = JinjavaConfig .newBuilder() .withNestedInterpretationEnabled(false) .build(); JinjavaInterpreter noNestedInterpreter = new Jinjava(config).newInterpreter(); Context contextNoNestedInterpretation = noNestedInterpreter.getContext(); contextNoNestedInterpretation.put("myvar", "hello {{ place }}"); contextNoNestedInterpretation.put("place", "world"); ExpressionNode node = fixture("simplevar"); assertThat(node.render(noNestedInterpreter).toString()) .isEqualTo("hello {{ place }}"); }
Example #26
Source File: ExpressionNodeTest.java From jinjava with Apache License 2.0 | 5 votes |
@Test public void itFailsOnUnknownTokensWithFilter() throws Exception { final JinjavaConfig config = JinjavaConfig .newBuilder() .withFailOnUnknownTokens(true) .build(); JinjavaInterpreter jinjavaInterpreter = new Jinjava(config).newInterpreter(); String jinja = "{{ UnknownToken }}"; Node node = new TreeParser(jinjavaInterpreter, jinja).buildTree(); assertThatThrownBy(() -> jinjavaInterpreter.render(node)) .isInstanceOf(UnknownTokenException.class) .hasMessage("Unknown token found: UnknownToken"); }
Example #27
Source File: JinjavaInterpreterFactory.java From jinjava with Apache License 2.0 | 5 votes |
@Override public JinjavaInterpreter newInstance( Jinjava application, Context context, JinjavaConfig renderConfig ) { return new JinjavaInterpreter(application, context, renderConfig); }
Example #28
Source File: JinjavaInterpreter.java From jinjava with Apache License 2.0 | 5 votes |
public JinjavaInterpreter( Jinjava application, Context context, JinjavaConfig renderConfig ) { this.context = context; this.config = renderConfig; this.application = application; switch (config.getRandomNumberGeneratorStrategy()) { case THREAD_LOCAL: random = ThreadLocalRandom.current(); break; case CONSTANT_ZERO: random = new ConstantZeroRandomNumberGenerator(); break; case DEFERRED: random = new DeferredRandomNumberGenerator(); break; default: throw new IllegalStateException( "No random number generator with strategy " + config.getRandomNumberGeneratorStrategy() ); } this.expressionResolver = new ExpressionResolver(this, application.getExpressionFactory()); }
Example #29
Source File: Functions.java From jinjava with Apache License 2.0 | 4 votes |
@JinjavaDoc( value = "formats a date to a string", params = { @JinjavaParam(value = "var", type = "date", defaultValue = "current time"), @JinjavaParam( value = "format", defaultValue = StrftimeFormatter.DEFAULT_DATE_FORMAT ), @JinjavaParam( value = "timezone", defaultValue = "utc", desc = "Time zone of output date" ) } ) public static String dateTimeFormat(Object var, String... format) { ZoneId zoneOffset = ZoneOffset.UTC; if (format.length > 1) { String timezone = format[1]; try { zoneOffset = ZoneId.of(timezone); } catch (DateTimeException e) { throw new InvalidDateFormatException(timezone, e); } } else if (var instanceof ZonedDateTime) { zoneOffset = ((ZonedDateTime) var).getZone(); } else if (var instanceof PyishDate) { zoneOffset = ((PyishDate) var).toDateTime().getZone(); } ZonedDateTime d = getDateTimeArg(var, zoneOffset); if (d == null) { return ""; } Locale locale; if (format.length > 2 && format[2] != null) { locale = Locale.forLanguageTag(format[2]); } else { locale = JinjavaInterpreter .getCurrentMaybe() .map(JinjavaInterpreter::getConfig) .map(JinjavaConfig::getLocale) .orElse(Locale.ENGLISH); } if (format.length > 0) { return StrftimeFormatter.format(d, format[0], locale); } else { return StrftimeFormatter.format(d, locale); } }
Example #30
Source File: KerberosUserMapper.java From gcp-token-broker with Apache License 2.0 | 4 votes |
public String evaluateThenExpression(Context context) { JinjavaInterpreter interpreter = new JinjavaInterpreter(new Jinjava(), context, new JinjavaConfig()); Object rendered = interpreter.resolveELExpression(then, 0); return Objects.toString(rendered, ""); }