diff --git a/Spring.build b/Spring.build index 060b71d6..0431f8ef 100644 --- a/Spring.build +++ b/Spring.build @@ -565,7 +565,7 @@ Commandline Examples: - + diff --git a/test/Spring/Spring.Template.Velocity.Castle.Tests/Spring.Template.Velocity.Castle.Tests.2010.csproj b/test/Spring/Spring.Template.Velocity.Castle.Tests/Spring.Template.Velocity.Castle.Tests.2010.csproj index cdad98e0..e8ea5307 100644 --- a/test/Spring/Spring.Template.Velocity.Castle.Tests/Spring.Template.Velocity.Castle.Tests.2010.csproj +++ b/test/Spring/Spring.Template.Velocity.Castle.Tests/Spring.Template.Velocity.Castle.Tests.2010.csproj @@ -60,9 +60,6 @@ - - Template\Velocity\Config\TemplateNamespaceParserTests.cs - Template\Velocity\VelocityEngineFactoryObjectTests.cs @@ -72,6 +69,7 @@ VelocityCompilerOptionsTests.cs + diff --git a/test/Spring/Spring.Template.Velocity.Castle.Tests/Spring.Template.Velocity.Castle.Tests.build b/test/Spring/Spring.Template.Velocity.Castle.Tests/Spring.Template.Velocity.Castle.Tests.build index f1815bee..61377b44 100644 --- a/test/Spring/Spring.Template.Velocity.Castle.Tests/Spring.Template.Velocity.Castle.Tests.build +++ b/test/Spring/Spring.Template.Velocity.Castle.Tests/Spring.Template.Velocity.Castle.Tests.build @@ -38,6 +38,7 @@ + diff --git a/test/Spring/Spring.Template.Velocity.Castle.Tests/Template/Velocity/Config/TemplateNamespaceParserTests.cs b/test/Spring/Spring.Template.Velocity.Castle.Tests/Template/Velocity/Config/TemplateNamespaceParserTests.cs new file mode 100644 index 00000000..b8abc116 --- /dev/null +++ b/test/Spring/Spring.Template.Velocity.Castle.Tests/Template/Velocity/Config/TemplateNamespaceParserTests.cs @@ -0,0 +1,205 @@ +#region License + +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed 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. + */ + +#endregion + +#region Imports + +using System.Collections; +using System.IO; +using System.Text; +using Commons.Collections; +using NUnit.Framework; +using NVelocity.App; +using NVelocity.Runtime; +using NVelocity.Runtime.Resource; +using NVelocity.Runtime.Resource.Loader; +using Spring.Core.IO; +using Spring.Template.Velocity.Config; + +#endregion + +namespace Spring.Template.Velocity.Config { + /// + /// This class contains tests for the template namespace configuration parser + /// + /// Erez Mazor + [TestFixture] + public class TemplateNamespaceParserTests : VelocityEngineTestBase { + #region convinience properties aliases + private const string PropertyModificationCheck = + TemplateDefinitionConstants.PropertyResourceLoaderModificationCheckInterval; + private const string PropertyResourceLoaderCachce = + TemplateDefinitionConstants.PropertyResourceLoaderCaching; + #endregion + + #region test constants + private const int DEFAULT_CACHE_SIZE = 200; + private const int DEFAULT_MOD_CHECK = 30; + private const bool DEFAULT_CACHE_FLAG = true; + #endregion + + /// + /// Test a full file-base configuration + /// + [Test] + public void TestFileBasedConfig() { + VelocityEngine velocityEngine = appContext.GetObject("cnFileVelocityEngine") as VelocityEngine; + Assert.IsNotNull(velocityEngine, "velocity engine is null"); + Assert.AreEqual(VelocityConstants.File, getSingleProperty(velocityEngine, RuntimeConstants.RESOURCE_LOADER), "incorrect resource loader"); + Assert.AreEqual(TemplateDefinitionConstants.FileResourceLoaderClass, getSingleProperty(velocityEngine, + TemplateNamespaceParser.getResourceLoaderProperty(VelocityConstants.File, VelocityConstants.Class)), "incorrect resource loader type"); + Assert.AreEqual(new string[]{"Template/Velocity/", "Template/"}, velocityEngine.GetProperty( + TemplateNamespaceParser.getResourceLoaderProperty(VelocityConstants.File, VelocityConstants.Path)), "incorrect resource loader path"); + Assert.AreEqual(DEFAULT_CACHE_SIZE, velocityEngine.GetProperty(RuntimeConstants.RESOURCE_MANAGER_DEFAULTCACHE_SIZE), "incorrect default cache size"); + Assert.AreEqual(DEFAULT_MOD_CHECK, velocityEngine.GetProperty( + VelocityConstants.File + VelocityConstants.Separator + PropertyModificationCheck), "incorrect mod check interval"); + Assert.AreEqual(DEFAULT_CACHE_FLAG, velocityEngine.GetProperty(VelocityConstants.File + VelocityConstants.Separator + PropertyResourceLoaderCachce), + "incorrect caching flag"); + + AssertMergedValue(velocityEngine, "SimpleTemplate.vm"); + } + + /// + /// Test a full aaembly-based configuration + /// + [Test] + public void TestAssemblyBasedConfig() { + VelocityEngine velocityEngine = appContext.GetObject("cnAssemblyVelocityEngine") as VelocityEngine; + Assert.IsNotNull(velocityEngine, "velocity engine is null"); + Assert.AreEqual(VelocityConstants.Assembly, getSingleProperty(velocityEngine, RuntimeConstants.RESOURCE_LOADER), "incorrect resource loader"); + Assert.AreEqual(TemplateDefinitionConstants.AssemblyResourceLoaderClass, getSingleProperty(velocityEngine, + TemplateNamespaceParser.getResourceLoaderProperty(VelocityConstants.Assembly, VelocityConstants.Class)), "incorrect resource loader type"); + Assert.AreEqual("Spring.Template.Velocity.Castle.Tests", getSingleProperty(velocityEngine, + TemplateNamespaceParser.getResourceLoaderProperty(VelocityConstants.Assembly,VelocityConstants.Assembly)), "incorrect resource loader path"); + Assert.AreEqual(DEFAULT_CACHE_SIZE, velocityEngine.GetProperty(RuntimeConstants.RESOURCE_MANAGER_DEFAULTCACHE_SIZE), "incorrect default cache size"); + Assert.AreEqual(DEFAULT_CACHE_FLAG, velocityEngine.GetProperty(VelocityConstants.Assembly + VelocityConstants.Separator + PropertyResourceLoaderCachce), + "incorrect caching flag"); + AssertMergedValue(velocityEngine, "Spring.Template.Velocity.SimpleTemplate.vm"); + } + + /// + /// Test a full aaembly-based configuration + /// + [Test] + public void TestSpringBasedConfig() { + VelocityEngine velocityEngine = appContext.GetObject("cnSpringVelocityEngine") as VelocityEngine; + Assert.IsNotNull(velocityEngine, "velocity engine is null"); + const string PropertySpring = TemplateDefinitionConstants.Spring; + Assert.AreEqual(PropertySpring, getSingleProperty(velocityEngine, RuntimeConstants.RESOURCE_LOADER), "incorrect resource loader"); + Assert.AreEqual(TemplateDefinitionConstants.SpringResourceLoaderClass, getSingleProperty(velocityEngine, + TemplateNamespaceParser.getResourceLoaderProperty(PropertySpring, VelocityConstants.Class)), "incorrect resource loader type"); + // no way to test the path property other than trying to actually perform a merge (it is set in velocity engine as an application attribute which is not exposed) + Assert.AreEqual(DEFAULT_CACHE_SIZE, velocityEngine.GetProperty(RuntimeConstants.RESOURCE_MANAGER_DEFAULTCACHE_SIZE), "incorrect default cache size"); + Assert.AreEqual(DEFAULT_CACHE_FLAG, velocityEngine.GetProperty(PropertySpring + VelocityConstants.Separator + PropertyResourceLoaderCachce), + "incorrect caching flag"); + + AssertMergedValue(velocityEngine, "SimpleTemplate.vm"); + AssertMergedValue(velocityEngine, "EmbeddedTemplate.vm"); + } + + /// + /// Test using an external configuration properties definition + /// + [Test] + public void TestGlobalConfig() { + VelocityEngine velocityEngine = appContext.GetObject("cnVelocityEngineConfig") as VelocityEngine; + Assert.IsNotNull(velocityEngine, "velocity engine is null"); + + string classProp = TemplateNamespaceParser.getResourceLoaderProperty(VelocityConstants.Assembly, VelocityConstants.Class); + string descProp = TemplateNamespaceParser.getResourceLoaderProperty(VelocityConstants.Assembly, VelocityConstants.Description); + + Assert.AreEqual(VelocityConstants.Assembly, getSingleProperty(velocityEngine, RuntimeConstants.RESOURCE_LOADER), "incorrect resource loader"); + Assert.AreEqual("NVelocity.Runtime.Resource.Loader.AssemblyResourceLoader", + getSingleProperty(velocityEngine,classProp), "incorrect resource loader type"); + Assert.AreEqual("TestDescription", getSingleProperty(velocityEngine, descProp), "incorrect description"); + } + + /// + /// Test using local config. + /// + [Test] + public void TestLocalConfig() { + VelocityEngine velocityEngine = appContext.GetObject("cnVelocityEngineLocalConfig") as VelocityEngine; + Assert.IsNotNull(velocityEngine, "velocity engine is null"); + + Assert.AreEqual(Encoding.UTF8.WebName.ToUpper(), getSingleProperty(velocityEngine, RuntimeConstants.INPUT_ENCODING), "incorrect input encoding value"); + Assert.AreEqual(TEST_VALUE, getSingleProperty(velocityEngine, "myproperty.mysubproperty"), "incorrect custom property value"); + } + + /// + /// Test a custom resource loader definition + /// + [Test] + public void TestCustomResourceLoader() { + VelocityEngine velocityEngine = appContext.GetObject("cnVelocityEngingCustomResourceLoader") as VelocityEngine; + Assert.IsNotNull(velocityEngine, "velocity engine is null"); + const string PropertyMyResourceLoader = "myResourceLoader"; + string classProp = TemplateNamespaceParser.getResourceLoaderProperty(PropertyMyResourceLoader, VelocityConstants.Class); + string descProp = TemplateNamespaceParser.getResourceLoaderProperty(PropertyMyResourceLoader, VelocityConstants.Description); + + Assert.AreEqual(PropertyMyResourceLoader, getSingleProperty(velocityEngine, RuntimeConstants.RESOURCE_LOADER), "incorrect resource loader"); + + Assert.AreEqual("Spring.Template.Velocity.Config.TestCustomResourceLoader; Spring.Template.Velocity.Castle.Tests", + getSingleProperty(velocityEngine, classProp), "incorrect resource loader type"); + Assert.AreEqual(PropertyMyResourceLoader, getSingleProperty(velocityEngine, RuntimeConstants.RESOURCE_LOADER), "incorrect resource loader"); + Assert.AreEqual("A custom resource loader", + getSingleProperty(velocityEngine, descProp), "incorrect resource loader description"); + Assert.AreEqual("Template/Velocity/", getSingleProperty(velocityEngine, + TemplateNamespaceParser.getResourceLoaderProperty(PropertyMyResourceLoader, VelocityConstants.Path)), "incorrect resource loader path"); + + AssertMergedValue(velocityEngine, "Template/Velocity/SimpleTemplate.vm"); + } + + #region internal test methods + /// + /// Grab a single property from (if it's an array return the first value) from the velocityEngine + /// + private string getSingleProperty(VelocityEngine velocityEngine, string prop) { + object property = velocityEngine.GetProperty(prop); + if (property is ArrayList) { + ArrayList list = (ArrayList)property; + return (string)list[0]; + } + return (string)property; + } + #endregion + } + + #region test classes + /// + /// Test class for a custom resource loader class + /// + internal sealed class TestCustomResourceLoader : ResourceLoader { + public override void Init(ExtendedProperties configuration) { + } + + public override Stream GetResourceStream(string source) { + return new ConfigurableResourceLoader().GetResource(source).InputStream; + } + + public override bool IsSourceModified(Resource resource) { + return false; + } + + public override long GetLastModified(Resource resource) { + return resource.LastModified; + } + } + #endregion +} \ No newline at end of file diff --git a/test/Spring/Spring.Template.Velocity.Tests/Spring.Template.Velocity.Tests.build b/test/Spring/Spring.Template.Velocity.Tests/Spring.Template.Velocity.Tests.build index f16ad631..54967b4b 100644 --- a/test/Spring/Spring.Template.Velocity.Tests/Spring.Template.Velocity.Tests.build +++ b/test/Spring/Spring.Template.Velocity.Tests/Spring.Template.Velocity.Tests.build @@ -58,7 +58,7 @@ - + diff --git a/test/Spring/Spring.Template.Velocity.Tests/Template/Velocity/Config/TemplateNamespaceParserTests.cs b/test/Spring/Spring.Template.Velocity.Tests/Template/Velocity/Config/TemplateNamespaceParserTests.cs index 6a00cffd..91172f8e 100644 --- a/test/Spring/Spring.Template.Velocity.Tests/Template/Velocity/Config/TemplateNamespaceParserTests.cs +++ b/test/Spring/Spring.Template.Velocity.Tests/Template/Velocity/Config/TemplateNamespaceParserTests.cs @@ -85,7 +85,7 @@ namespace Spring.Template.Velocity.Config { Assert.AreEqual(VelocityConstants.Assembly, getSingleProperty(velocityEngine, RuntimeConstants.RESOURCE_LOADER), "incorrect resource loader"); Assert.AreEqual(TemplateDefinitionConstants.AssemblyResourceLoaderClass, getSingleProperty(velocityEngine, TemplateNamespaceParser.getResourceLoaderProperty(VelocityConstants.Assembly, VelocityConstants.Class)), "incorrect resource loader type"); - Assert.AreEqual("Spring.Template.Velocity.Castle.Tests", getSingleProperty(velocityEngine, + Assert.AreEqual("Spring.Template.Velocity.Tests", getSingleProperty(velocityEngine, TemplateNamespaceParser.getResourceLoaderProperty(VelocityConstants.Assembly,VelocityConstants.Assembly)), "incorrect resource loader path"); Assert.AreEqual(DEFAULT_CACHE_SIZE, velocityEngine.GetProperty(RuntimeConstants.RESOURCE_MANAGER_DEFAULTCACHE_SIZE), "incorrect default cache size"); Assert.AreEqual(DEFAULT_CACHE_FLAG, velocityEngine.GetProperty(VelocityConstants.Assembly + VelocityConstants.Separator + PropertyResourceLoaderCachce), @@ -155,7 +155,7 @@ namespace Spring.Template.Velocity.Config { Assert.AreEqual(PropertyMyResourceLoader, getSingleProperty(velocityEngine, RuntimeConstants.RESOURCE_LOADER), "incorrect resource loader"); - Assert.AreEqual("Spring.Template.Velocity.Config.TestCustomResourceLoader; Spring.Template.Velocity.Castle.Tests", + Assert.AreEqual("Spring.Template.Velocity.Config.TestCustomResourceLoader; Spring.Template.Velocity.Tests", getSingleProperty(velocityEngine, classProp), "incorrect resource loader type"); Assert.AreEqual(PropertyMyResourceLoader, getSingleProperty(velocityEngine, RuntimeConstants.RESOURCE_LOADER), "incorrect resource loader"); Assert.AreEqual("A custom resource loader",