示例#1
2
 public void Init(IDictionary<string, string> properties)
 {
     //初始化NVelocity引擎
     var nv_properties = new Commons.Collections.ExtendedProperties();
     var prefix = this.GetType().FullName + ".";
     foreach (String key in properties.Keys.Where(t => t.StartsWith(prefix)))
         nv_properties.SetProperty(key.Substring(prefix.Length), properties[key]);
     engine = new VelocityEngine(nv_properties);
     engine.Init();
 }
        protected void SetUp()
        {
            ve = new VelocityEngine();

            ExtendedProperties ep = new ExtendedProperties();

            ep.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
                           TemplateTest.FILE_RESOURCE_LOADER_PATH);

            ep.SetProperty(RuntimeConstants.RUNTIME_LOG_ERROR_STACKTRACE, "true");
            ep.SetProperty(RuntimeConstants.RUNTIME_LOG_WARN_STACKTRACE, "true");
            ep.SetProperty(RuntimeConstants.RUNTIME_LOG_INFO_STACKTRACE, "true");
            ep.SetProperty("userdirective", "X3Platform.Velocity.Runtime.Directive.Component;X3Platform.Velocity,X3Platform.Velocity.Runtime.Directive.BlockComponent;X3Platform.Velocity");

            ve.Init(ep);

            testProperties = new ExtendedProperties();
            testProperties.Load(new FileStream(TemplateTest.TEST_CASE_PROPERTIES, FileMode.Open, FileAccess.Read));
        }
		protected void SetUp()
		{
			ve = new VelocityEngine();

			ExtendedProperties ep = new ExtendedProperties();
			
			ep.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, 
				TemplateTest.FILE_RESOURCE_LOADER_PATH);

			ep.SetProperty(RuntimeConstants.RUNTIME_LOG_ERROR_STACKTRACE, "true");
			ep.SetProperty(RuntimeConstants.RUNTIME_LOG_WARN_STACKTRACE, "true");
			ep.SetProperty(RuntimeConstants.RUNTIME_LOG_INFO_STACKTRACE, "true");
			ep.SetProperty("userdirective", "NVelocity.Runtime.Directive.Component;NVelocity,NVelocity.Runtime.Directive.BlockComponent;NVelocity");

			ve.Init(ep);

			testProperties = new ExtendedProperties();
			testProperties.Load(new FileStream(TemplateTest.TEST_CASE_PROPERTIES, FileMode.Open, FileAccess.Read));
		}
        /// <summary>
        /// Creates a new instance.
        /// </summary>
        public TemplateTestCase()
        {
            try
            {
                velocityEngine = new VelocityEngine();

                ExtendedProperties extendedProperties = new ExtendedProperties();
                extendedProperties.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, TemplateTest.FILE_RESOURCE_LOADER_PATH);

                extendedProperties.SetProperty(RuntimeConstants.RUNTIME_LOG_ERROR_STACKTRACE, "true");
                extendedProperties.SetProperty(RuntimeConstants.RUNTIME_LOG_WARN_STACKTRACE, "true");
                extendedProperties.SetProperty(RuntimeConstants.RUNTIME_LOG_INFO_STACKTRACE, "true");

                velocityEngine.Init(extendedProperties);

                testProperties = new ExtendedProperties();
                testProperties.Load(new FileStream(TemplateTest.TEST_CASE_PROPERTIES, FileMode.Open, FileAccess.Read));
            }
            catch(Exception)
            {
                throw new Exception("Cannot setup TemplateTestSuite!");
            }
        }
        /// <summary>
        /// Creates a new instance.
        /// </summary>
        public TemplateTestCase()
        {
            try
            {
                ve = new VelocityEngine();

                ExtendedProperties ep = new ExtendedProperties();
                ep.SetProperty(RuntimeConstants_Fields.FILE_RESOURCE_LOADER_PATH, TemplateTestBase_Fields.FILE_RESOURCE_LOADER_PATH);

                ep.SetProperty(RuntimeConstants_Fields.RUNTIME_LOG_ERROR_STACKTRACE, "true");
                ep.SetProperty(RuntimeConstants_Fields.RUNTIME_LOG_WARN_STACKTRACE, "true");
                ep.SetProperty(RuntimeConstants_Fields.RUNTIME_LOG_INFO_STACKTRACE, "true");

                ve.Init(ep);

                testProperties = new ExtendedProperties();
                testProperties.Load(new FileStream(TemplateTestBase_Fields.TEST_CASE_PROPERTIES, FileMode.Open, FileAccess.Read));
            }
            catch (Exception e)
            {
                throw new Exception("Cannot setup TemplateTestSuite!");
            }
        }
示例#6
0
 public static void RenderTemplateToWriter(string templateFile, TextWriter writer, NVelocity.VelocityContext vc)
 {
     NVelocity.App.VelocityEngine engine = null;
     IPersonalSettings provider = PersonalSettings.Provider;
     if (provider == null) {
         Commons.Collections.ExtendedProperties props = new Commons.Collections.ExtendedProperties();
         props.SetProperty(NVelocity.Runtime.RuntimeConstants.RESOURCE_LOADER, "file");
         props.SetProperty(NVelocity.Runtime.RuntimeConstants.FILE_RESOURCE_LOADER_PATH, _defaultPath);
         props.SetProperty(NVelocity.Runtime.RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, "true");
         engine = new NVelocity.App.VelocityEngine(props);
     }
     else
         engine = (NVelocity.App.VelocityEngine)PersonalSettings.Provider.ViewEngine;
     NVelocity.Template template = engine.GetTemplate(templateFile);
     template.Merge(vc, writer);
 }
示例#7
0
        public void Test_Example1()
        {
            String templateFile = "example1.vm";

            try
            {
                /*
                 * setup
                 */

                VelocityEngine ve = new VelocityEngine();

                ExtendedProperties ep = new ExtendedProperties();
                ep.SetProperty(RuntimeConstants_Fields.FILE_RESOURCE_LOADER_PATH, "../../test/templates");

                ve.Init(ep);

                /*
                 *  Make a context object and populate with the data.  This
                 *  is where the Velocity engine gets the data to resolve the
                 *  references (ex. $list) in the template
                 */
                VelocityContext context = new VelocityContext();
                context.Put("list", GetNames());

                ExtendedProperties props = new ExtendedProperties();
                props.Add("runtime.log", "nvelocity.log");
                context.Put("props", props);

                /*
                 *    get the Template object.  This is the parsed version of your
                 *  template input file.  Note that getTemplate() can throw
                 *   ResourceNotFoundException : if it doesn't find the template
                 *   ParseErrorException : if there is something wrong with the VTL
                 *   Exception : if something else goes wrong (this is generally
                 *        indicative of as serious problem...)
                 */
                Template template = null;

                try
                {
                    template = ve.GetTemplate(templateFile);
                }
                catch (ResourceNotFoundException rnfe)
                {
                    Console.Out.WriteLine("Example : error : cannot find template " + templateFile + " : \n" + rnfe.Message);
                    Assertion.Fail();
                }
                catch (ParseErrorException pee)
                {
                    Console.Out.WriteLine("Example : Syntax error in template " + templateFile + " : \n" + pee);
                    Assertion.Fail();
                }

                /*
                 *  Now have the template engine process your template using the
                 *  data placed into the context.  Think of it as a  'merge'
                 *  of the template and the data to produce the output stream.
                 */

                // using Console.Out will send it to the screen
                TextWriter writer = new StringWriter();
                if (template != null)
                {
                    template.Merge(context, writer);
                }

                /*
                 *  flush and cleanup
                 */

                writer.Flush();
                writer.Close();
            }
            catch (Exception ex)
            {
                Assertion.Fail(ex.Message);
            }
        }
        public void Test_Example1()
        {
            String templateFile = "example1.vm";
            try
            {
                /*
                * setup
                */

                VelocityEngine velocityEngine = new VelocityEngine();

                ExtendedProperties extendedProperties = new ExtendedProperties();
                extendedProperties.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, TemplateTest.FILE_RESOURCE_LOADER_PATH);

                velocityEngine.Init(extendedProperties);

                /*
                *  Make a context object and populate with the data.  This
                *  is where the Velocity engine gets the data to resolve the
                *  references (ex. $list) in the template
                */
                VelocityContext context = new VelocityContext();
                context.Put("list", GetNames());

                ExtendedProperties props = new ExtendedProperties();
                props.Add("runtime.log", "nvelocity.log");
                context.Put("props", props);

                /*
                *    get the Template object.  This is the parsed version of your
                *  template input file.  Note that getTemplate() can throw
                *   ResourceNotFoundException : if it doesn't find the template
                *   ParseErrorException : if there is something wrong with the VTL
                *   Exception : if something else goes wrong (this is generally
                *        indicative of as serious problem...)
                */
                Template template = null;

                try
                {
                    template = velocityEngine.GetTemplate(templateFile);
                }
                catch(ResourceNotFoundException resourceNotFoundException)
                {
                    Console.Out.WriteLine("Example : error : cannot find template {0} : \n{1}", templateFile,
                                          resourceNotFoundException.Message);
                    Assert.Fail();
                }
                catch(ParseErrorException parseErrorException)
                {
                    Console.Out.WriteLine("Example : Syntax error in template {0} : \n{1}", templateFile, parseErrorException);
                    Assert.Fail();
                }

                /*
                *  Now have the template engine process your template using the
                *  data placed into the context.  Think of it as a  'merge'
                *  of the template and the data to produce the output stream.
                */

                // using Console.Out will send it to the screen
                TextWriter writer = new StringWriter();
                if (template != null)
                    template.Merge(context, writer);

                /*
                *  flush and cleanup
                */

                writer.Flush();
                writer.Close();
            }
            catch(Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }