Added test coverage, fixed doc

This commit is contained in:
erezmazor
2009-08-01 17:20:44 +00:00
parent 4e006d8635
commit 04832335fd
4 changed files with 82 additions and 16 deletions

View File

@@ -134,8 +134,9 @@ string mergedContent = stringWriter.ToString();</programlisting>
<entry>
<para>Instructs the NVelocity engine factory to attempt use
NVelocity's file loader. When set to false the provided
<literal>SpringResourceLoader</literal> will be used (and the <literal>ResourceLoaderPath</literal>
property must be set)</para>
<literal>SpringResourceLoader</literal> will be used (and the
<literal>ResourceLoaderPath</literal> property must be
set)</para>
</entry>
<entry>no</entry>
@@ -148,7 +149,7 @@ string mergedContent = stringWriter.ToString();</programlisting>
<entry>
<para>Instructs the NVelocity engine factory to use the
provided spring commons logging based logging system. See See
provided spring commons logging based logging system. See
<xref
linkend="templating-nvelocity-resource-logging" /></para>
</entry>
@@ -208,12 +209,12 @@ string mergedContent = stringWriter.ToString();</programlisting>
</programlisting>
<note>
<para>By default spring will attempt to load resources using NVelocity's
file based template loading (useful for detection of template changes at
runtime). If this is not desirable you set the
<literal>prefer-file-system-access</literal> property of the factory object
to <literal>false</literal> which will cause the factory to utilize the supplied
spring resource loader.</para>
<para>By default spring will attempt to load resources using
NVelocity's file based template loading (useful for detection of
template changes at runtime). If this is not desirable you set the
<literal>prefer-file-system-access</literal> property of the factory
object to <literal>false</literal> which will cause the factory to
utilize the supplied spring resource loader.</para>
</note>
<para>Using the example above when resource loader paths are defined
@@ -435,8 +436,8 @@ string mergedTemplate = VelocityEngineUtils.MergeTemplateIntostring(velocityEngi
file based template loading (useful for detection of template changes at
runtime). If this is not desirable you set the
<literal>preferFileSystemAccess</literal> property of the factory object
to <literal>false</literal> which will cause the factory to utilize the supplied
spring resource loader.</para>
to <literal>false</literal> which will cause the factory to utilize the
supplied spring resource loader.</para>
</note>
<para>To refer to a property file based configuration of the

View File

@@ -22,7 +22,6 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Common.Logging;
using Commons.Collections;
using NVelocity.App;
@@ -290,11 +289,12 @@ namespace Spring.Template.Velocity {
// Try to load via the file system, fall back to SpringResourceLoader
// (for hot detection of template changes, if possible).
IList resolvedPaths = new ArrayList();
foreach (string path in paths) {
IResource resource = ResourceLoader.GetResource(path);
resolvedPaths.Add(resource.File.FullName);
}
try {
foreach (string path in paths) {
IResource resource = ResourceLoader.GetResource(path);
resolvedPaths.Add(resource.File.FullName);
}
extendedProperties.SetProperty(RuntimeConstants.RESOURCE_LOADER, VelocityConstants.File);
extendedProperties.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
StringUtils.CollectionToCommaDelimitedString(resolvedPaths));

View File

@@ -21,8 +21,11 @@
#region Imports
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using NVelocity.App;
using NVelocity.Exception;
#endregion
@@ -53,6 +56,14 @@ namespace Spring.Template.Velocity.Tests.Template.Velocity {
VelocityEngine velocityEngine = appContext.GetObject("fileBasedVelocityEngine") as VelocityEngine;
Assert.IsNotNull(velocityEngine, "velocityEngine is null");
AssertMergedValue(velocityEngine, "Template/Velocity/SimpleTemplate.vm");
try {
VelocityEngineUtils.MergeTemplateIntoString(velocityEngine, "NoneExistingFile", Encoding.UTF8.WebName, model);
throw new TestException(
"Merge using non existing file should throw exception");
} catch (Exception ex) {
Assert.IsTrue(ex is VelocityException, "Illegal merge should throw VelocityException");
}
}
/// <summary>
@@ -75,6 +86,18 @@ namespace Spring.Template.Velocity.Tests.Template.Velocity {
appContext.GetObject("pathBasedVelocityEngine") as VelocityEngine;
Assert.IsNotNull(velocityEngine, "velocityEngine is null");
AssertMergedValue(velocityEngine, "SimpleTemplate.vm");
}
/// <summary>
/// Test using definition of ResourceLoaderPath (file-based configuration) falling back from velocity
/// file-base to spring-based (prefer-file-system-access one but resource path is string compliant)
/// </summary>
[Test]
public void TestMergeUsingResourceLoaderPathFallback() {
VelocityEngine velocityEngine =
appContext.GetObject("springFallbackVelocityEngine") as VelocityEngine;
Assert.IsNotNull(velocityEngine, "velocityEngine is null");
AssertMergedValue(velocityEngine, "SimpleTemplate.vm");
}
/// <summary>
@@ -99,6 +122,7 @@ namespace Spring.Template.Velocity.Tests.Template.Velocity {
AssertMergedValue(velocityEngine, "SimpleTemplate.vm");
}
/// <summary>
/// Test using invalid configuration
/// </summary>
@@ -114,6 +138,41 @@ namespace Spring.Template.Velocity.Tests.Template.Velocity {
} catch (ArgumentException) {
Assert.IsNull(velocityEngine, "velocityEngine should be null");
}
// no resource loader with spring
velocityEngineFactory = new VelocityEngineFactory();
velocityEngineFactory.ResourceLoader = null;
velocityEngineFactory.PreferFileSystemAccess = false;
try {
velocityEngineFactory.CreateVelocityEngine();
throw new TestException(
"Should not be able to construct VelocityEngineFactory with null ResourceLoader");
} catch (ArgumentException) {
Assert.IsNull(velocityEngine, "velocityEngine should be null");
}
// no resource loader path with spring
velocityEngineFactory = new VelocityEngineFactory();
velocityEngineFactory.ResourceLoaderPaths = new List<string>();
velocityEngineFactory.PreferFileSystemAccess = false;
try {
velocityEngineFactory.CreateVelocityEngine();
throw new TestException(
"Should not be able to construct VelocityEngineFactory with empty resource loader path list");
} catch (ArgumentException) {
Assert.IsNull(velocityEngine, "velocityEngine should be null");
}
}
[Test]
public void TestLogging(){
VelocityEngine velocityEngine = new VelocityEngineFactoryObject().CreateVelocityEngine();
velocityEngine.Info("test");
velocityEngine.Debug("test");
velocityEngine.Warn("test");
velocityEngine.Error("test");
}
}
}

View File

@@ -66,6 +66,12 @@
-->
</object>
<!-- Spring based defualt file fallback to spring definition -->
<object id="springFallbackVelocityEngine" type="Spring.Template.Velocity.VelocityEngineFactoryObject, Spring.Template.Velocity" >
<property name="ResourceLoaderPath " value="assembly://Spring.Template.Velocity.Tests/Spring.Template.Velocity.Tests.Template.Velocity/" />
</object>
<!-- Configuration comes from a properties file -->
<object id="propertiesFileBasedVelocityEngine" type="Spring.Template.Velocity.VelocityEngineFactoryObject, Spring.Template.Velocity" >
<property name="ConfigLocation " value="file://Template/Velocity/config.properties" />