Author: cbrisson
Date: Mon Jul 18 21:20:38 2016
New Revision: 1753318
URL: http://svn.apache.org/viewvc?rev=1753318&view=rev
Log:
[engine] add optionl context.autoreference.key property, with testcase
Added:
velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/ContextAutoreferenceKeyTestCase.java
Modified:
velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java
velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java
Modified:
velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java?rev=1753318&r1=1753317&r2=1753318&view=diff
==============================================================================
---
velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java
(original)
+++
velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java
Mon Jul 18 21:20:38 2016
@@ -246,6 +246,9 @@ public interface RuntimeConstants
/** Switch for ignoring nulls in math equations vs throwing exceptions. */
String STRICT_MATH = "runtime.strict.math";
+ /** Key upon which a context should be accessible within itself */
+ String CONTEXT_AUTOREFERENCE_KEY = "context.autoreference.key";
+
/**
* The <code>parser.pool.class</code> property specifies the name of the
{@link org.apache.velocity.util.SimplePool}
* implementation to use.
@@ -256,7 +259,7 @@ public interface RuntimeConstants
* @see #NUMBER_OF_PARSERS
*/
String PARSER_POOL_SIZE = "parser.pool.size";
-
+
/*
* ----------------------------------------------------------------------
* These constants are used internally by the Velocity runtime i.e.
Modified:
velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java?rev=1753318&r1=1753317&r2=1753318&view=diff
==============================================================================
---
velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java
(original)
+++
velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java
Mon Jul 18 21:20:38 2016
@@ -1410,6 +1410,11 @@ public class RuntimeInstance implements
Object previous = ica.get(evaluateScopeName);
context.put(evaluateScopeName, new Scope(this, previous));
}
+ /**
+ * optionally put the context in itself if asked so
+ */
+ String self = getString(CONTEXT_AUTOREFERENCE_KEY);
+ if (self != null) context.put(self, context);
nodeTree.render(ica, writer);
}
catch (StopCommand stop)
Added:
velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/ContextAutoreferenceKeyTestCase.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/ContextAutoreferenceKeyTestCase.java?rev=1753318&view=auto
==============================================================================
---
velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/ContextAutoreferenceKeyTestCase.java
(added)
+++
velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/ContextAutoreferenceKeyTestCase.java
Mon Jul 18 21:20:38 2016
@@ -0,0 +1,56 @@
+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.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+
+/**
+ * This class tests passing expressions as method arguments
+ */
+
+public class ContextAutoreferenceKeyTestCase extends BaseTestCase
+{
+ public ContextAutoreferenceKeyTestCase(final String name)
+ {
+ super(name);
+ }
+
+ protected void setUpEngine(VelocityEngine engine)
+ {
+ engine.setProperty(VelocityEngine.CONTEXT_AUTOREFERENCE_KEY, "self");
+ }
+
+ protected void setUpContext(VelocityContext context)
+ {
+ context.put("foo", "bar");
+ }
+
+ public void testAutoreference()
+ {
+ assertEvalEquals("bar", "$foo");
+ assertEvalEquals("bar", "$self.foo");
+ assertEvalEquals("bar", "$self.self.foo");
+ assertEvalEquals("true", "$self.containsKey('foo')");
+ assertEvalEquals("false", "$self.containsKey('bar')");
+ assertEvalEquals("bar", "$self.put('foo', 'baz')");
+ assertEvalEquals("baz", "$foo");
+ }
+}