PebbleEngineProducer { @Produces @ViewEngineConfig public PebbleEngine pebbleEngine() { PebbleEngine.Builder engine = new PebbleEngine.Builder(); pebbleConfiguration .entrySet() .stream() .filter(e -> String.valueOf(e.getValue()).trim().length() > 0) .forEach((e) -> { String val = String.valueOf(e.getValue()); switch (PebbleProperty.fromKey(String.valueOf(e.getKey()))) { case AUTO_ESCAPING: engine.autoEscaping(Boolean.valueOf(val)); break; case CACHE_ACTIVE: engine.cacheActive(Boolean.valueOf(val)); break; case ESCAPING_STRATEGY: try { String escapingStrategyKey = "userDefinedEscapingStrategy"; engine.addEscapingStrategy(escapingStrategyKey, (EscapingStrategy) Class.forName(val).newInstance()); engine.defaultEscapingStrategy(escapingStrategyKey); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException ex) { String msg = String.format("Pebble initialization error: Could not register escaping strategy '%s' of type %s", String.valueOf(e.getKey()), val); throw new IllegalArgumentException(msg, ex); } break; case DEFAULT_LOCALE: engine.defaultLocale(Locale.forLanguageTag(val)); break; case NEW_LINE_TRIMMING: engine.newLineTrimming(Boolean.valueOf(val)); break; case STRICT_VARIABLES: engine.strictVariables(Boolean.valueOf(val)); break; case EXECUTOR_SERVICE: try { engine.executorService((ExecutorService) Class.forName(val).newInstance()); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException ex) { String msg = String.format("Pebble initialization error: Could not register executor service type %s", val); throw new IllegalArgumentException(msg, ex); } break; case EXTENSION: String[] extensions = val.split(","); Extension[] extensionArray = Stream.of(extensions) .map(clazzName -> { try { return (Extension) Class.forName(clazzName.trim()).newInstance(); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException ex) { String msg = String.format("Pebble initialization error: Could not register extension of type %s", clazzName); throw new IllegalArgumentException(msg, ex); } }).toArray(Extension[]::new); engine.extension(extensionArray); break; case TAG_CACHE_MAX: engine.tagCache(CacheBuilder.newBuilder().maximumSize(Integer.valueOf(val)).build()); break; case TEMPLATE_CACHE_MAX: engine.templateCache(CacheBuilder.newBuilder().maximumSize(Integer.valueOf(val)).build()); break; case UNKNOWN: break; default: break; } }); engine.loader(new ServletLoader(servletContext)); return engine.build(); } @Inject  PebbleEngineProducer(Properties pebbleConfiguration, ServletContext servletContext); @Produces @ViewEngineConfig PebbleEngine pebbleEngine();  }
@Test public void shouldCorrectlySetTagCache() { properties.put(PebbleProperty.TAG_CACHE_MAX.key(), "123"); Cache<BaseTagCacheKey, Object> tagCache = pebbleEngineProducer.pebbleEngine().getTagCache(); IntStream.range(0, 200).forEach(i -> tagCache.put(new BaseTagCacheKey(String.valueOf(i)) { }, i)); assertEquals(123, tagCache.size()); }
@Test public void shouldCorrectlySetTemplateCache() { properties.put(PebbleProperty.TEMPLATE_CACHE_MAX.key(), "126"); Cache<Object, PebbleTemplate> templateCache = pebbleEngineProducer.pebbleEngine().getTemplateCache(); IntStream.range(0, 200).forEach(i -> templateCache.put(i, new PebbleTemplateImpl(null, null, String.valueOf(i)))); assertEquals(126, templateCache.size()); }
@Test public void shouldNotSetCachesWhenCachingIsOff() { properties.put(PebbleProperty.CACHE_ACTIVE.key(), "false"); properties.put(PebbleProperty.TAG_CACHE_MAX.key(), "123"); properties.put(PebbleProperty.TEMPLATE_CACHE_MAX.key(), "126"); Cache<BaseTagCacheKey, Object> tagCache = pebbleEngineProducer.pebbleEngine().getTagCache(); IntStream.range(0, 200).forEach(i -> tagCache.put(new BaseTagCacheKey(String.valueOf(i)) { }, i)); Cache<Object, PebbleTemplate> templateCache = pebbleEngineProducer.pebbleEngine().getTemplateCache(); IntStream.range(0, 200).forEach(i -> templateCache.put(i, new PebbleTemplateImpl(null, null, String.valueOf(i)))); assertEquals(0, tagCache.size()); assertEquals(0, templateCache.size()); }
@Test public void shouldCorrectlySetStrictVariables() { properties.put(PebbleProperty.STRICT_VARIABLES.key(), "true"); assertTrue(pebbleEngineProducer.pebbleEngine().isStrictVariables()); properties.put(PebbleProperty.STRICT_VARIABLES.key(), "false"); assertFalse(pebbleEngineProducer.pebbleEngine().isStrictVariables()); }
@Test public void shouldCorrectlySetExecutorService() { properties.put(PebbleProperty.EXECUTOR_SERVICE.key(), CustomExecutorService.class.getCanonicalName()); assertTrue(pebbleEngineProducer.pebbleEngine().getExecutorService() instanceof CustomExecutorService); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhileSettingExecutorService() { properties.put(PebbleProperty.EXECUTOR_SERVICE.key(), "org.dummy.DummyExecutorService"); pebbleEngineProducer.pebbleEngine(); }
@Test public void shouldCorrectlySetExtensions() { properties.put(PebbleProperty.EXTENSION.key(), String.format("%s,%s", CustomExtensionOne.class.getCanonicalName(), CustomExtensionTwo.class.getCanonicalName())); pebbleEngineProducer.pebbleEngine(); properties.put(PebbleProperty.EXTENSION.key(), String.format("%s , %s", CustomExtensionOne.class.getCanonicalName(), CustomExtensionTwo.class.getCanonicalName())); pebbleEngineProducer.pebbleEngine(); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhileSettingExtensions() { properties.put(PebbleProperty.EXTENSION.key(), String.format("%s-%s", CustomExtensionOne.class.getCanonicalName(), CustomExtensionTwo.class.getCanonicalName())); pebbleEngineProducer.pebbleEngine(); }
@Test public void shouldCorrectlySetEscapingStrategy() { properties.put(PebbleProperty.ESCAPING_STRATEGY.key(), CustomEscapingStrategy.class.getCanonicalName()); PebbleEngine pebbleEngine = pebbleEngineProducer.pebbleEngine(); EscapeFilter ef = (EscapeFilter) pebbleEngine.getExtensionRegistry().getFilter("escape"); assertEquals(ef.getDefaultStrategy(), "userDefinedEscapingStrategy"); }
@Test public void shouldCreateAnInstanceOfPebbleEngine() { assertTrue(pebbleEngineProducer.pebbleEngine() instanceof PebbleEngine); }
@Test public void shouldIgnoreEmptyValueAndUseDefaultValue() { properties.put(PebbleProperty.TAG_CACHE_MAX.key(), ""); properties.put(PebbleProperty.CACHE_ACTIVE.key(), "true"); Cache<BaseTagCacheKey, Object> tagCache = pebbleEngineProducer.pebbleEngine().getTagCache(); IntStream.range(0, 250).forEach(i -> tagCache.put(new BaseTagCacheKey(String.valueOf(i)) { }, i)); assertEquals(200, tagCache.size()); }
@Test public void shouldUseServletContextLoader() { assertTrue(pebbleEngineProducer.pebbleEngine().getLoader() instanceof ServletLoader); }
@Test public void shouldSetCorrectLocale() { properties.put(PebbleProperty.DEFAULT_LOCALE.key(), "de"); assertEquals(Locale.GERMAN, pebbleEngineProducer.pebbleEngine().getDefaultLocale()); }