aboutsummaryrefslogtreecommitdiff
path: root/velocity-engine-core/src/test
diff options
context:
space:
mode:
authorClaude Brisson <cbrisson@apache.org>2020-01-28 10:48:29 +0000
committerClaude Brisson <cbrisson@apache.org>2020-01-28 10:48:29 +0000
commit1cdc4eb1a0d01dfc9d34d5e0b5acc6bf616c2a99 (patch)
treec2e62965bf349db0aef30e3217fc3d68cd9bd84c /velocity-engine-core/src/test
parent97ffb37b7c9bbe1b724dea9de57092e0eb655fd8 (diff)
downloadapache-velocity-engine-1cdc4eb1a0d01dfc9d34d5e0b5acc6bf616c2a99.tar.gz
[engine] Review VELOCITY-926 fix:
deprecate velocimacro.arguments.preserve_literals in favor of the new velocimacro.enable_bc_mode flag, which mimics 1.7 velocimacro behavior: - preserve arguments literals: #macro(m $arg) $arg #end m($null) will displays $arg w/o bc mode and $null with bc mode - use global defaults for missing arguments: #macro(m $foo) $foo #end #set($foo='foo') #m() will display $foo w/o bc mode and 'foo' with bc mode The following use cases have been left aside from backward compatibility: - preserving local macro scope values #macro(test) #set($foo = 'foo') $some_tool.change_foo_value_in_global_context_to_bar() $foo #end #test() will always display 'bar', while 1.7 displayed 'foo' - setting a null argument to null, while argument name exists in context, #macro(setnull $foo) #set($foo = $null) #end #set($foo='foo') #setnull($null) $foo Will always display 'foo' (w or w/o bc mode), while 1.7 did display $foo git-svn-id: https://svn.apache.org/repos/asf/velocity/engine/trunk@1873244 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'velocity-engine-core/src/test')
-rw-r--r--velocity-engine-core/src/test/java/org/apache/velocity/test/PreserveArgumentsLiteralsTestCase.java45
-rw-r--r--velocity-engine-core/src/test/java/org/apache/velocity/test/VelocimacroBCModeTestCase.java99
-rwxr-xr-xvelocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity904TestCase.java1
-rw-r--r--velocity-engine-core/src/test/resources/bc_mode/compare/test_bc_mode.bc_mode_disabled40
-rw-r--r--velocity-engine-core/src/test/resources/bc_mode/compare/test_bc_mode.bc_mode_enabled40
-rw-r--r--velocity-engine-core/src/test/resources/bc_mode/test_bc_mode.vtl39
6 files changed, 219 insertions, 45 deletions
diff --git a/velocity-engine-core/src/test/java/org/apache/velocity/test/PreserveArgumentsLiteralsTestCase.java b/velocity-engine-core/src/test/java/org/apache/velocity/test/PreserveArgumentsLiteralsTestCase.java
deleted file mode 100644
index ff627e18..00000000
--- a/velocity-engine-core/src/test/java/org/apache/velocity/test/PreserveArgumentsLiteralsTestCase.java
+++ /dev/null
@@ -1,45 +0,0 @@
-package org.apache.velocity.test;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import org.apache.velocity.app.VelocityEngine;
-import org.apache.velocity.runtime.RuntimeConstants;
-
-/**
- * This class tests the mode where velocimacros do preserve arguments literals
- */
-
-public class PreserveArgumentsLiteralsTestCase extends BaseTestCase
-{
- public PreserveArgumentsLiteralsTestCase(final String name)
- {
- super(name);
- }
-
- protected void setUpEngine(VelocityEngine engine)
- {
- engine.setProperty(RuntimeConstants.VM_PRESERVE_ARGUMENTS_LITERALS, true);
- }
-
- public void testPreserveLiterals()
- {
- assertEvalEquals("$bar","#macro(m $foo)$foo#end#m($bar)");
- }
-}
diff --git a/velocity-engine-core/src/test/java/org/apache/velocity/test/VelocimacroBCModeTestCase.java b/velocity-engine-core/src/test/java/org/apache/velocity/test/VelocimacroBCModeTestCase.java
new file mode 100644
index 00000000..72a27c15
--- /dev/null
+++ b/velocity-engine-core/src/test/java/org/apache/velocity/test/VelocimacroBCModeTestCase.java
@@ -0,0 +1,99 @@
+package org.apache.velocity.test;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.runtime.RuntimeConstants;
+
+import java.io.BufferedWriter;
+import java.io.FileOutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+
+/**
+ * This class tests the mode where velocimacros do preserve arguments literals
+ */
+
+public class VelocimacroBCModeTestCase extends BaseTestCase
+{
+ private static final String BASE_DIR = TEST_COMPARE_DIR + "/bc_mode";
+ private static final String CMP_DIR = BASE_DIR + "/compare";
+ private static final String RESULTS_DIR = TEST_RESULT_DIR + "/bc_mode";
+
+ public VelocimacroBCModeTestCase(final String name)
+ {
+ super(name);
+ }
+
+ protected void setUpEngine(VelocityEngine engine)
+ {
+ boolean bcMode = !getName().contains("NoPreserve");
+ engine.setProperty(RuntimeConstants.VM_ENABLE_BC_MODE, bcMode);
+ engine.setProperty("file.resource.loader.path", TEST_COMPARE_DIR + "/bc_mode");
+ }
+
+ public void testPreserveLiterals()
+ {
+ assertEvalEquals("$bar","#macro(m $foo)$foo#end#m($bar)");
+ }
+
+ public void testGlobalDefaults()
+ {
+ assertEvalEquals("foo","#macro(m $foo)$foo#end#set($foo='foo')#m()");
+ }
+
+ public void testVariousCasesPreserve() throws Exception
+ {
+ doTestVariousCases("bc_mode_enabled");
+ }
+
+ public void testVariousCasesNoPreserve() throws Exception
+ {
+ doTestVariousCases("bc_mode_disabled");
+ }
+
+ private void doTestVariousCases(String compare_ext) throws Exception
+ {
+ assureResultsDirectoryExists(RESULTS_DIR);
+ String basefilename = "test_bc_mode";
+ Template template = engine.getTemplate( getFileName(null, basefilename, "vtl") );
+ context = new VelocityContext();
+ FileOutputStream fos;
+ Writer fwriter;
+
+ fos = new FileOutputStream (getFileName(RESULTS_DIR, basefilename, RESULT_FILE_EXT));
+
+ fwriter = new BufferedWriter( new OutputStreamWriter(fos) );
+
+ template.merge(context, fwriter);
+ fwriter.flush();
+ fwriter.close();
+
+ if (!isMatch(RESULTS_DIR, CMP_DIR, basefilename, RESULT_FILE_EXT, compare_ext))
+ {
+ String result = getFileContents(RESULTS_DIR, basefilename, RESULT_FILE_EXT);
+ String compare = getFileContents(CMP_DIR, basefilename, compare_ext);
+
+ assertEquals(compare, result);
+ }
+ }
+}
diff --git a/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity904TestCase.java b/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity904TestCase.java
index b22e198b..86caa7cf 100755
--- a/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity904TestCase.java
+++ b/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity904TestCase.java
@@ -35,6 +35,7 @@ public class Velocity904TestCase extends BaseTestCase
@Override
protected void setUpEngine(VelocityEngine engine)
{
+ // that will also test the deprecation of velocimacro.arguments.preserve_literals towards velocimacro.enable_bc_mode
engine.setProperty("velocimacro.arguments.preserve_literals", getName().contains("NoPreserve") ? "false" : "true");
}
diff --git a/velocity-engine-core/src/test/resources/bc_mode/compare/test_bc_mode.bc_mode_disabled b/velocity-engine-core/src/test/resources/bc_mode/compare/test_bc_mode.bc_mode_disabled
new file mode 100644
index 00000000..59d87824
--- /dev/null
+++ b/velocity-engine-core/src/test/resources/bc_mode/compare/test_bc_mode.bc_mode_disabled
@@ -0,0 +1,40 @@
+
+
+
+
+A) Null Values
+1. missing argument
+foo=$foo => disp miss [foo=$foo] => foo=$foo
+foo=$foo => setn miss [foo=$foo] => foo=$foo
+foo=$foo => setv miss [foo=inn] => foo=inn
+2. null argument
+foo=$foo => disp null [foo=$foo] => foo=$foo
+foo=$foo => setn null [foo=$foo] => foo=$foo
+foo=$foo => setv null [foo=inn] => foo=inn
+3. non-colliding argument
+foo=$foo => disp ncol [foo=$foo] => foo=$foo
+foo=$foo => setn ncol [foo=$foo] => foo=$foo
+foo=$foo => setv ncol [foo=inn] => foo=inn
+4. colliding argument
+foo=$foo => disp coll [foo=$foo] => foo=$foo
+foo=$foo => setn coll [foo=$foo] => foo=$foo
+foo=$foo => setv coll [foo=inn] => foo=inn
+
+B) Non-null Values
+1. missing argument
+foo=foo => disp miss [foo=$foo] => foo=foo
+foo=foo => setn miss [foo=$foo] => foo=foo
+foo=foo => setv miss [foo=inn] => foo=inn
+2. null argument
+foo=foo => disp null [foo=$foo] => foo=foo
+foo=foo => setn null [foo=$foo] => foo=foo
+foo=foo => setv null [foo=inn] => foo=inn
+3. non-colliding argument
+foo=foo => disp ncol [foo=bar] => foo=foo
+foo=foo => setn ncol [foo=$foo] => foo=$foo
+foo=foo => setv ncol [foo=bar] => foo=foo
+4. colliding argument
+foo=foo => disp coll [foo=foo] => foo=foo
+foo=foo => setn coll [foo=$foo] => foo=$foo
+foo=foo => setv coll [foo=foo] => foo=foo
+
diff --git a/velocity-engine-core/src/test/resources/bc_mode/compare/test_bc_mode.bc_mode_enabled b/velocity-engine-core/src/test/resources/bc_mode/compare/test_bc_mode.bc_mode_enabled
new file mode 100644
index 00000000..934d659e
--- /dev/null
+++ b/velocity-engine-core/src/test/resources/bc_mode/compare/test_bc_mode.bc_mode_enabled
@@ -0,0 +1,40 @@
+
+
+
+
+A) Null Values
+1. missing argument
+foo=$foo => disp miss [foo=$foo] => foo=$foo
+foo=$foo => setn miss [foo=$foo] => foo=$foo
+foo=$foo => setv miss [foo=inn] => foo=inn
+2. null argument
+foo=$foo => disp null [foo=$null] => foo=$foo
+foo=$foo => setn null [foo=$foo] => foo=$foo
+foo=$foo => setv null [foo=inn] => foo=inn
+3. non-colliding argument
+foo=$foo => disp ncol [foo=$bar] => foo=$foo
+foo=$foo => setn ncol [foo=$foo] => foo=$foo
+foo=$foo => setv ncol [foo=inn] => foo=inn
+4. colliding argument
+foo=$foo => disp coll [foo=$foo] => foo=$foo
+foo=$foo => setn coll [foo=$foo] => foo=$foo
+foo=$foo => setv coll [foo=inn] => foo=inn
+
+B) Non-null Values
+1. missing argument
+foo=foo => disp miss [foo=foo] => foo=foo
+foo=foo => setn miss [foo=$foo] => foo=$foo
+foo=foo => setv miss [foo=foo] => foo=foo
+2. null argument
+foo=foo => disp null [foo=$null] => foo=foo
+foo=foo => setn null [foo=$foo] => foo=foo
+foo=foo => setv null [foo=inn] => foo=inn
+3. non-colliding argument
+foo=foo => disp ncol [foo=bar] => foo=foo
+foo=foo => setn ncol [foo=$foo] => foo=$foo
+foo=foo => setv ncol [foo=bar] => foo=foo
+4. colliding argument
+foo=foo => disp coll [foo=foo] => foo=foo
+foo=foo => setn coll [foo=$foo] => foo=$foo
+foo=foo => setv coll [foo=foo] => foo=foo
+
diff --git a/velocity-engine-core/src/test/resources/bc_mode/test_bc_mode.vtl b/velocity-engine-core/src/test/resources/bc_mode/test_bc_mode.vtl
new file mode 100644
index 00000000..8ab1050b
--- /dev/null
+++ b/velocity-engine-core/src/test/resources/bc_mode/test_bc_mode.vtl
@@ -0,0 +1,39 @@
+#macro(store)#set($foo_ = $foo)#set($bar_ = $bar)#end##
+#macro(reset)#set($foo = $foo_)#set($bar = $bar_)#end##
+#macro(state)foo=$foo#end##
+
+#macro(disp $foo)[foo=$foo]#end
+#macro(setn $foo)#set($foo=$null)#disp($foo)#end
+#macro(setv $foo)#if(!$foo)#set($foo='inn')#end#disp($foo)#end
+#macro(sub)#set($foo='sub')#end
+
+#macro(test)
+1. missing argument
+#store#state => disp miss #disp() => #state#reset
+#store#state => setn miss #setn() => #state#reset
+#store#state => setv miss #setv() => #state#reset
+2. null argument
+#store#state => disp null #disp($null) => #state#reset
+#store#state => setn null #setn($null) => #state#reset
+#store#state => setv null #setv($null) => #state#reset
+3. non-colliding argument
+#store#state => disp ncol #disp($bar) => #state#reset
+#store#state => setn ncol #setn($bar) => #state#reset
+#store#state => setv ncol #setv($bar) => #state#reset
+4. colliding argument
+#store#state => disp coll #disp($foo) => #state#reset
+#store#state => setn coll #setn($foo) => #state#reset
+#store#state => setv coll #setv($foo) => #state#reset
+#end
+
+
+A) Null Values
+#set($foo = $null)
+#set($bar = $null)
+#test()
+
+B) Non-null Values
+#set($foo = 'foo')
+#set($bar = 'bar')
+#test()
+