diff --git a/doc/reference/src/templating.xml b/doc/reference/src/templating.xml index b2ab0d8a..25697f64 100644 --- a/doc/reference/src/templating.xml +++ b/doc/reference/src/templating.xml @@ -134,8 +134,9 @@ string mergedContent = stringWriter.ToString(); Instructs the NVelocity engine factory to attempt use NVelocity's file loader. When set to false the provided - SpringResourceLoader will be used (and the ResourceLoaderPath - property must be set) + SpringResourceLoader will be used (and the + ResourceLoaderPath property must be + set) no @@ -148,7 +149,7 @@ string mergedContent = stringWriter.ToString(); 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 @@ -208,12 +209,12 @@ string mergedContent = stringWriter.ToString(); - 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 - prefer-file-system-access property of the factory object - to false which will cause the factory to utilize the supplied - spring resource loader. + 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 + prefer-file-system-access property of the factory + object to false which will cause the factory to + utilize the supplied spring resource loader. 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 preferFileSystemAccess property of the factory object - to false which will cause the factory to utilize the supplied - spring resource loader. + to false which will cause the factory to utilize the + supplied spring resource loader. To refer to a property file based configuration of the diff --git a/src/Spring/Spring.Template.Velocity/Template/Velocity/VelocityEngineFactory.cs b/src/Spring/Spring.Template.Velocity/Template/Velocity/VelocityEngineFactory.cs index 34dc126b..1ee39127 100644 --- a/src/Spring/Spring.Template.Velocity/Template/Velocity/VelocityEngineFactory.cs +++ b/src/Spring/Spring.Template.Velocity/Template/Velocity/VelocityEngineFactory.cs @@ -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)); diff --git a/test/Spring/Spring.Template.Velocity.Tests/Template/Velocity/VelocityEngineFactoryObjectTests.cs b/test/Spring/Spring.Template.Velocity.Tests/Template/Velocity/VelocityEngineFactoryObjectTests.cs index 9dc9f54c..447de176 100644 --- a/test/Spring/Spring.Template.Velocity.Tests/Template/Velocity/VelocityEngineFactoryObjectTests.cs +++ b/test/Spring/Spring.Template.Velocity.Tests/Template/Velocity/VelocityEngineFactoryObjectTests.cs @@ -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"); + } } /// @@ -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"); + } + + /// + /// 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) + /// + [Test] + public void TestMergeUsingResourceLoaderPathFallback() { + VelocityEngine velocityEngine = + appContext.GetObject("springFallbackVelocityEngine") as VelocityEngine; + Assert.IsNotNull(velocityEngine, "velocityEngine is null"); + AssertMergedValue(velocityEngine, "SimpleTemplate.vm"); } /// @@ -99,6 +122,7 @@ namespace Spring.Template.Velocity.Tests.Template.Velocity { AssertMergedValue(velocityEngine, "SimpleTemplate.vm"); } + /// /// Test using invalid configuration /// @@ -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(); + 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"); + } + + } } \ No newline at end of file diff --git a/test/Spring/Spring.Template.Velocity.Tests/Template/Velocity/VelocityEngineFactoryObjectTests.xml b/test/Spring/Spring.Template.Velocity.Tests/Template/Velocity/VelocityEngineFactoryObjectTests.xml index ad7bb506..dd108bdf 100644 --- a/test/Spring/Spring.Template.Velocity.Tests/Template/Velocity/VelocityEngineFactoryObjectTests.xml +++ b/test/Spring/Spring.Template.Velocity.Tests/Template/Velocity/VelocityEngineFactoryObjectTests.xml @@ -66,6 +66,12 @@ --> + + + + + +