You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by cb...@apache.org on 2011/07/11 15:37:58 UTC
svn commit: r1145171 - in /velocity/tools/branches/2.0.x/src:
main/java/org/apache/velocity/tools/config/
main/java/org/apache/velocity/tools/view/
test/java/org/apache/velocity/tools/generic/
Author: cbrisson
Date: Mon Jul 11 13:37:57 2011
New Revision: 1145171
URL: http://svn.apache.org/viewvc?rev=1145171&view=rev
Log:
finish backporting change into tools 2.0.x branch
Added:
velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/BaseTestCase.java (with props)
velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/TestLogChute.java (with props)
velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/Veltools128Tests.java (with props)
Modified:
velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/config/ConfigurationException.java
velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/config/ToolConfiguration.java
velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java
Modified: velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/config/ConfigurationException.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/config/ConfigurationException.java?rev=1145171&r1=1145170&r2=1145171&view=diff
==============================================================================
--- velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/config/ConfigurationException.java (original)
+++ velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/config/ConfigurationException.java Mon Jul 11 13:37:57 2011
@@ -41,6 +41,12 @@ public class ConfigurationException exte
this.source = data;
}
+ public ConfigurationException(Data data, String message, Throwable cause)
+ {
+ super(message, cause);
+ this.source = data;
+ }
+
public ConfigurationException(Configuration config, Throwable cause)
{
super(cause);
@@ -53,6 +59,12 @@ public class ConfigurationException exte
this.source = config;
}
+ public ConfigurationException(Configuration config, String message, Throwable cause)
+ {
+ super(message, cause);
+ this.source = config;
+ }
+
public Object getSource()
{
return source;
Modified: velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/config/ToolConfiguration.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/config/ToolConfiguration.java?rev=1145171&r1=1145170&r2=1145171&view=diff
==============================================================================
--- velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/config/ToolConfiguration.java (original)
+++ velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/config/ToolConfiguration.java Mon Jul 11 13:37:57 2011
@@ -280,7 +280,9 @@ public class ToolConfiguration extends C
info = new OldToolInfo(getKey(), getToolClass());
break;
default:
- throw new ConfigurationException(this, getError(status));
+ throw problem == null ?
+ new ConfigurationException(this, getError(status)) :
+ new ConfigurationException(this, getError(status), problem);
}
info.restrictTo(getRestrictTo());
Modified: velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java?rev=1145171&r1=1145170&r2=1145171&view=diff
==============================================================================
--- velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java (original)
+++ velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java Mon Jul 11 13:37:57 2011
@@ -195,6 +195,15 @@ public class ViewToolManager extends Too
}
/**
+ * Publish {@link Scope#APPLICATION} Toolbox.
+ */
+ protected void publishApplicationTools()
+ {
+ servletContext.setAttribute(this.toolboxKey, getApplicationToolbox());
+ appToolsPublished = true;
+ }
+
+ /**
* Removes any published {@link Scope#APPLICATION} Toolbox.
*/
protected void unpublishApplicationTools()
@@ -334,7 +343,7 @@ public class ViewToolManager extends Too
}
if (!appToolsPublished && hasApplicationTools())
{
- servletContext.setAttribute(this.toolboxKey, getApplicationToolbox());
+ publishApplicationTools();
}
}
Added: velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/BaseTestCase.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/BaseTestCase.java?rev=1145171&view=auto
==============================================================================
--- velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/BaseTestCase.java (added)
+++ velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/BaseTestCase.java Mon Jul 11 13:37:57 2011
@@ -0,0 +1,325 @@
+package org.apache.velocity.tools.generic;
+
+/*
+ * 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 java.io.StringWriter;
+import junit.framework.TestCase;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.Velocity;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.runtime.RuntimeConstants;
+import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
+import org.apache.velocity.runtime.resource.util.StringResourceRepository;
+
+/**
+ * Base test case that provides utility methods for other tests.
+ * (Adapted from Velocity Engine's BaseTestCase)
+ *
+ * @author Nathan Bubna
+ * @version $Id: BaseTestCase.java 898032 2010-01-11 19:51:03Z nbubna $
+ */
+public abstract class BaseTestCase extends TestCase
+{
+ protected VelocityEngine engine;
+ protected VelocityContext context;
+ protected boolean DEBUG = false;
+ protected TestLogChute log;
+ protected String stringRepoName = "string.repo";
+
+ public BaseTestCase(String name)
+ {
+ super(name);
+
+ // if we're just running one case, then have DEBUG
+ // automatically set to true
+ String testcase = System.getProperty("testcase");
+ if (testcase != null)
+ {
+ DEBUG = testcase.equals(getClass().getName());
+ }
+ }
+
+ protected void setUp() throws Exception
+ {
+ engine = new VelocityEngine();
+
+ //by default, make the engine's log output go to the test-report
+ log = new TestLogChute(false, false);
+ log.setEnabledLevel(TestLogChute.INFO_ID);
+ log.setSystemErrLevel(TestLogChute.WARN_ID);
+ engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, log);
+
+ // use string resource loader by default, instead of file
+ engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file,string");
+ engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
+ engine.addProperty("string.resource.loader.repository.name", stringRepoName);
+ engine.addProperty("string.resource.loader.repository.static", "false");
+
+ setUpEngine(engine);
+
+ context = new VelocityContext();
+ setUpContext(context);
+ }
+
+ protected void setUpEngine(VelocityEngine engine)
+ {
+ // extension hook
+ }
+
+ protected void setUpContext(VelocityContext context)
+ {
+ // extension hook
+ }
+
+ protected StringResourceRepository getStringRepository()
+ {
+ StringResourceRepository repo =
+ (StringResourceRepository)engine.getApplicationAttribute(stringRepoName);
+ if (repo == null)
+ {
+ try
+ {
+ engine.init();
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ repo =
+ (StringResourceRepository)engine.getApplicationAttribute(stringRepoName);
+ }
+ return repo;
+ }
+
+ protected void addTemplate(String name, String template)
+ {
+ info("Template '"+name+"': "+template);
+ getStringRepository().putStringResource(name, template);
+ }
+
+ protected void removeTemplate(String name)
+ {
+ info("Removed: '"+name+"'");
+ getStringRepository().removeStringResource(name);
+ }
+
+ public void tearDown()
+ {
+ engine = null;
+ context = null;
+ }
+
+ protected void info(String msg)
+ {
+ if (DEBUG)
+ {
+ if (engine == null)
+ {
+ Velocity.getLog().info(msg);
+ }
+ else
+ {
+ engine.getLog().info(msg);
+ }
+ }
+ }
+
+ protected void info(String msg, Throwable t)
+ {
+ if (DEBUG)
+ {
+ if (engine == null)
+ {
+ Velocity.getLog().info(msg);
+ }
+ else
+ {
+ engine.getLog().info(msg, t);
+ }
+ }
+ }
+
+ public void testBase()
+ {
+ if (DEBUG && engine != null)
+ {
+ assertSchmoo("");
+ assertSchmoo("abc\n123");
+ }
+ }
+
+ /**
+ * Compare an expected string with the given loaded template
+ */
+ protected void assertTmplEquals(String expected, String template)
+ {
+ info("Expected: " + expected + " from '" + template + "'");
+
+ StringWriter writer = new StringWriter();
+ try
+ {
+ engine.mergeTemplate(template, "utf-8", context, writer);
+ }
+ catch (RuntimeException re)
+ {
+ info("RuntimeException!", re);
+ throw re;
+ }
+ catch (Exception e)
+ {
+ info("Exception!", e);
+ throw new RuntimeException(e);
+ }
+
+ info("Result: " + writer.toString());
+ assertEquals(expected, writer.toString());
+ }
+
+ /**
+ * Ensure that a context value is as expected.
+ */
+ protected void assertContextValue(String key, Object expected)
+ {
+ info("Expected value of '"+key+"': "+expected);
+ Object value = context.get(key);
+ info("Result: "+value);
+ assertEquals(expected, value);
+ }
+
+ /**
+ * Ensure that a template renders as expected.
+ */
+ protected void assertEvalEquals(String expected, String template)
+ {
+ info("Expectation: "+expected);
+ assertEquals(expected, evaluate(template));
+ }
+
+ /**
+ * Ensure that the given string renders as itself when evaluated.
+ */
+ protected void assertSchmoo(String templateIsExpected)
+ {
+ assertEvalEquals(templateIsExpected, templateIsExpected);
+ }
+
+ /**
+ * Ensure that an exception occurs when the string is evaluated.
+ */
+ protected Exception assertEvalException(String evil)
+ {
+ return assertEvalException(evil, null);
+ }
+
+ /**
+ * Ensure that a specified type of exception occurs when evaluating the string.
+ */
+ protected Exception assertEvalException(String evil, Class exceptionType)
+ {
+ try
+ {
+ if (!DEBUG)
+ {
+ log.off();
+ }
+ if (exceptionType != null)
+ {
+ info("Expectation: "+exceptionType.getName());
+ }
+ evaluate(evil);
+ fail("Template '"+evil+"' should have thrown an exception.");
+ }
+ catch (Exception e)
+ {
+ if (exceptionType != null && !exceptionType.isAssignableFrom(e.getClass()))
+ {
+ fail("Was expecting template '"+evil+"' to throw "+exceptionType+" not "+e);
+ }
+ return e;
+ }
+ finally
+ {
+ if (!DEBUG)
+ {
+ log.on();
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Ensure that the error message of the expected exception has the proper location info.
+ */
+ protected Exception assertEvalExceptionAt(String evil, String template,
+ int line, int col)
+ {
+ String loc = template+"[line "+line+", column "+col+"]";
+ info("Expectation: Exception at "+loc);
+ Exception e = assertEvalException(evil);
+
+ info("Result: "+e.getClass().getName()+" - "+e.getMessage());
+ if (e.getMessage().indexOf(loc) < 1)
+ {
+ fail("Was expecting exception at "+loc+" instead of "+e.getMessage());
+ }
+ return e;
+ }
+
+ /**
+ * Only ensure that the error message of the expected exception
+ * has the proper line and column info.
+ */
+ protected Exception assertEvalExceptionAt(String evil, int line, int col)
+ {
+ return assertEvalExceptionAt(evil, "", line, col);
+ }
+
+ /**
+ * Evaluate the specified String as a template and return the result as a String.
+ */
+ protected String evaluate(String template)
+ {
+ StringWriter writer = new StringWriter();
+ try
+ {
+ info("Template: "+template);
+
+ // use template as its own name, since our templates are short
+ // unless it's not that short, then shorten it...
+ String name = (template.length() <= 15) ? template : template.substring(0,15);
+ engine.evaluate(context, writer, name, template);
+
+ String result = writer.toString();
+ info("Result: "+result);
+ return result;
+ }
+ catch (RuntimeException re)
+ {
+ info("RuntimeException!", re);
+ throw re;
+ }
+ catch (Exception e)
+ {
+ info("Exception!", e);
+ throw new RuntimeException(e);
+ }
+ }
+
+}
Propchange: velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/BaseTestCase.java
------------------------------------------------------------------------------
svn:executable = *
Added: velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/TestLogChute.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/TestLogChute.java?rev=1145171&view=auto
==============================================================================
--- velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/TestLogChute.java (added)
+++ velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/TestLogChute.java Mon Jul 11 13:37:57 2011
@@ -0,0 +1,118 @@
+package org.apache.velocity.tools.generic;
+
+/*
+ * 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 java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import org.apache.velocity.runtime.RuntimeServices;
+import org.apache.velocity.runtime.log.SystemLogChute;
+
+/**
+ * LogChute implementation that can easily capture output
+ * or suppress it entirely. By default, both capture and suppress
+ * are on. To have this behave like a normal SystemLogChute,
+ * you must turn it on() and stopCapture().
+ *
+ * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
+ * @author Nathan Bubna
+ * @version $Id: TestLogChute.java 749684 2009-03-03 18:38:16Z nbubna $
+ */
+public class TestLogChute extends SystemLogChute
+{
+ public static final String TEST_LOGGER_LEVEL = "runtime.log.logsystem.test.level";
+
+ private ByteArrayOutputStream log;
+ private PrintStream systemDotIn;
+ private boolean suppress = true;
+ private boolean capture = true;
+
+ public TestLogChute()
+ {
+ log = new ByteArrayOutputStream();
+ systemDotIn = new PrintStream(log, true);
+ }
+
+ public TestLogChute(boolean suppress, boolean capture)
+ {
+ this();
+ this.suppress = suppress;
+ this.capture = capture;
+ }
+
+ public void init(RuntimeServices rs)
+ throws Exception
+ {
+ super.init(rs);
+
+ String level = rs.getString(TEST_LOGGER_LEVEL);
+ if (level != null)
+ {
+ setEnabledLevel(toLevel(level));
+ }
+ }
+
+ public void on()
+ {
+ suppress = false;
+ }
+
+ public void off()
+ {
+ suppress = true;
+ }
+
+ public void startCapture()
+ {
+ capture = true;
+ }
+
+ public void stopCapture()
+ {
+ capture = false;
+ }
+
+ public boolean isLevelEnabled(int level)
+ {
+ return (!suppress || capture) && super.isLevelEnabled(level);
+ }
+
+
+ protected void write(PrintStream ps, String prefix, String message, Throwable t)
+ {
+ if (capture)
+ {
+ super.write(systemDotIn, prefix, message, t);
+ }
+ else
+ {
+ super.write(ps, prefix, message, t);
+ }
+ }
+
+ /**
+ * Return the captured log messages to date.
+ * @return log messages
+ */
+ public String getLog()
+ {
+ return log.toString();
+ }
+
+}
Propchange: velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/TestLogChute.java
------------------------------------------------------------------------------
svn:executable = *
Added: velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/Veltools128Tests.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/Veltools128Tests.java?rev=1145171&view=auto
==============================================================================
--- velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/Veltools128Tests.java (added)
+++ velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/Veltools128Tests.java Mon Jul 11 13:37:57 2011
@@ -0,0 +1,48 @@
+package org.apache.velocity.tools.generic;
+
+/*
+ * 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;
+
+/**
+ * This class tests VELOCITY-762.
+ */
+public class Veltools128Tests extends BaseTestCase
+{
+ public Veltools128Tests(String name)
+ {
+ super(name);
+ }
+
+ protected void setUpContext(VelocityContext context)
+ {
+ context.put("loop", new LoopTool());
+ }
+
+ public void testLoopToolSync()
+ {
+ String template =
+ "#foreach( $item in $loop.watch([1..3]).sync([3..5], 'other') )"+
+ "$item:$loop.other "+
+ "#end";
+ assertEvalEquals("1:3 2:4 3:5 ", template);
+ }
+
+}
Propchange: velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/Veltools128Tests.java
------------------------------------------------------------------------------
svn:executable = *