DefaultVelocityEngine implements VelocityEngine { @Override public void initialize(Properties overridingProperties) throws XWikiVelocityException { RuntimeInstance runtime = new RuntimeInstance(); runtime.setApplicationAttribute(ComponentManager.class.getName(), this.componentManager); initializeProperties(runtime, this.velocityConfiguration.getProperties(), overridingProperties); runtime.loadDirective(TryCatchDirective.class.getName()); try { runtime.init(); } catch (Exception e) { throw new XWikiVelocityException("Cannot start the Velocity engine", e); } this.runtimeInstance = runtime; this.globalEntry = new TemplateEntry(""); }  @Override void initialize(Properties overridingProperties); @Override boolean evaluate(Context context, Writer out, String templateName, String source); @Override boolean evaluate(Context context, Writer out, String namespace, Reader source); @Override @Deprecated void clearMacroNamespace(String namespace); @Override void startedUsingMacroNamespace(String namespace); @Override void stoppedUsingMacroNamespace(String namespace);  }
@Test void evaluateWithString() throws Exception { this.engine.initialize(new Properties()); assertEvaluate("hello World", "#set($foo='hello')$foo World", DEFAULT_TEMPLATE_NAME); }
@Test void evaluateWithSecureUberspectorActiveByDefault() throws Exception { this.engine.initialize(new Properties()); String content = "#set($foo = 'test')#set($object = $foo.class.forName('java.util.ArrayList')" + ".newInstance())$object.size()"; assertEvaluate("$object.size()", content, DEFAULT_TEMPLATE_NAME); assertEquals( "Cannot retrieve method forName from object of class java.lang.Class due to security restrictions.", logCapture.getMessage(0)); }
@Test void evaluateMacroScope() throws XWikiVelocityException { this.engine.initialize(new Properties()); assertEvaluate("{}", "#macro (testMacro)$macro#end#testMacro()"); }
@Test void evaluateMacroWithMissingParameter() throws XWikiVelocityException { this.engine.initialize(new Properties()); assertEvaluate("$param", "#macro (testMacro $param)$param#end#testMacro()"); }
@Test void evaluateMacroWithLiteralBooleanParameter() throws XWikiVelocityException { this.engine.initialize(new Properties()); assertEvaluate("true", "#macro (testMacro $param)$param#end#testMacro(true)"); }
@Test void evaluateMacroWithLiteralMapParameter() throws XWikiVelocityException { this.engine.initialize(new Properties()); assertEvaluate("{}", "#macro (testMacro $param)$param#end#testMacro({})"); }
@Test void evaluateMacroWithLiteralArrayParameter() throws XWikiVelocityException { this.engine.initialize(new Properties()); assertEvaluate("[]", "#macro (testMacro $param)$param#end#testMacro([])"); }
@Test void evaluateWithncludeMacro() throws XWikiVelocityException { this.engine.initialize(new Properties()); Context context = new XWikiVelocityContext(); context.put("test", new TestClass(context)); assertEvaluate("included", "$test.evaluate('#macro(includedmacro)included#end')#includedmacro()", context); }
@Test void evaluateWithOverwrittenIncludeMacro() throws XWikiVelocityException { this.engine.initialize(new Properties()); Context context = new XWikiVelocityContext(); context.put("test", new TestClass(context)); assertEvaluate("included", "$test.evaluate('#macro(includedmacro)included#end')#macro(includedmacro)ovewritten#end#includedmacro()", context); }
@Test void evauateTemplateScope() throws XWikiVelocityException { this.engine.initialize(new Properties()); assertEvaluate("{}", "$template"); }
@Test void evaluateWithStopCommand() throws Exception { this.engine.initialize(new Properties()); assertEvaluate("hello world", "hello world#stop", DEFAULT_TEMPLATE_NAME); }
@Test void evaluateVelocityCount() throws XWikiVelocityException { this.engine.initialize(new Properties()); assertEvaluate("1true2true3true4true5false", "#foreach($nb in [1,2,3,4,5])$velocityCount$velocityHasNext#end", DEFAULT_TEMPLATE_NAME); assertEquals("Deprecated binding [$velocityCount] used in [mytemplate]", logCapture.getMessage(0)); assertEquals("Deprecated binding [$velocityHasNext] used in [mytemplate]", logCapture.getMessage(1)); assertEquals("Deprecated binding [$velocityCount] used in [mytemplate]", logCapture.getMessage(2)); assertEquals("Deprecated binding [$velocityHasNext] used in [mytemplate]", logCapture.getMessage(3)); assertEquals("Deprecated binding [$velocityCount] used in [mytemplate]", logCapture.getMessage(4)); assertEquals("Deprecated binding [$velocityHasNext] used in [mytemplate]", logCapture.getMessage(5)); assertEquals("Deprecated binding [$velocityCount] used in [mytemplate]", logCapture.getMessage(6)); assertEquals("Deprecated binding [$velocityHasNext] used in [mytemplate]", logCapture.getMessage(7)); assertEquals("Deprecated binding [$velocityCount] used in [mytemplate]", logCapture.getMessage(8)); assertEquals("Deprecated binding [$velocityHasNext] used in [mytemplate]", logCapture.getMessage(9)); }
@Test void evaluateMacroParameters() throws Exception { this.engine.initialize(new Properties()); assertEvaluate("$caller", "#macro (testMacro $called)$called#end#testMacro($caller)"); assertEvaluate("$caller", "#macro (testMacro $called)#set($called = $NULL)$called#set($called = 'value')#end#testMacro($caller)"); assertEvaluate("$caller", "#macro (testMacro $called)#set($called = $NULL)$called#set($called = 'value')#end#set($caller = 'value')#testMacro($caller)"); assertEvaluate("$caller$caller2$caller", "#macro(macro1 $called)$called#macro2($caller2)$called#end#macro(macro2 $called)$called#end#macro1($caller)"); Context context = new XWikiVelocityContext(); context.put("test", new TestClass(context)); context.put("other", new TestClass(context, "othername")); assertEvaluate("namename", "#macro (testMacro $test $name)$name#end$test.name#testMacro($other, $test.name)", context); }
@Test void evaluateMissingMacroParameter() throws XWikiVelocityException { this.engine.initialize(new Properties()); assertEvaluate("global", "#macro(mymacro $var)$var#end#set($var = 'global')#mymacro()"); }
@Test void evaluateSetSharpString() throws Exception { this.engine.initialize(new Properties()); assertEvaluate("#", "#set($var = \"#\")$var", DEFAULT_TEMPLATE_NAME); assertEvaluate("test#", "#set($var = \"test#\")$var", DEFAULT_TEMPLATE_NAME); }
@Test void evaluateSetDollarString() throws Exception { this.engine.initialize(new Properties()); assertEvaluate("$", "#set($var = \"$\")$var", DEFAULT_TEMPLATE_NAME); assertEvaluate("test$", "#set($var = \"test$\")$var", DEFAULT_TEMPLATE_NAME); }
@Test void evaluateExpressionFollowedByTwoPipes() throws XWikiVelocityException { this.engine.initialize(new Properties()); assertEvaluate("$var||", "$var||"); }
@Test void evaluateClassMethods() throws XWikiVelocityException { this.engine.initialize(new Properties()); Context context = new XWikiVelocityContext(); context.put("var", new TestClass()); assertEvaluate("org.xwiki.velocity.internal.DefaultVelocityEngineTest$TestClass name", "$var.class.getName() $var.getName()", context); }
@Test void evaluateWithSubEvaluate() throws XWikiVelocityException { this.engine.initialize(new Properties()); Context context = new XWikiVelocityContext(); context.put("test", new TestClass(context)); assertEvaluate("top", "#set($var = 'top')$test.evaluate('$var')", context); assertEvaluate("sub", "$test.evaluate('#set($var = \"sub\")')$var", context); assertEvaluate("sub", "#set($var = 'top')$test.evaluate('#set($var = \"sub\")')$var", context); }
@Test void evaluateSpaceGobling() throws Exception { this.engine.initialize(new Properties()); assertEvaluate("value\nvalueline", "#macro (testMacro)value#end#testMacro\n#testMacro()\nline"); }