VelocityTemplate extends BaseTemplate { @Override protected void processTemplate( TemplateResource templateResource, Writer writer) throws Exception { TemplateResourceThreadLocal.setTemplateResource( TemplateConstants.LANG_TYPE_VM, templateResource); if (_restricted) { RestrictedTemplateThreadLocal.setRestricted(true); } try { Template template = _velocityEngine.getTemplate( getTemplateResourceUUID(templateResource), TemplateConstants.DEFAUT_ENCODING); template.merge(_velocityContext, writer); } finally { TemplateResourceThreadLocal.setTemplateResource( TemplateConstants.LANG_TYPE_VM, null); if (_restricted) { RestrictedTemplateThreadLocal.setRestricted(false); } } } VelocityTemplate(
		TemplateResource templateResource, Map<String, Object> context,
		VelocityEngine velocityEngine,
		TemplateContextHelper templateContextHelper,
		TemplateResourceCache templateResourceCache, boolean restricted); @Override void prepareTaglib(
		HttpServletRequest httpServletRequest,
		HttpServletResponse httpServletResponse);  }
@Test public void testProcessTemplate1() throws Exception { Template template = new VelocityTemplate( new MockTemplateResource(_TEMPLATE_FILE_NAME), null, _velocityEngine, _templateContextHelper, _templateResourceCache, false); template.put(_TEST_KEY, _TEST_VALUE); UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(); template.processTemplate(unsyncStringWriter); String result = unsyncStringWriter.toString(); Assert.assertEquals(_TEST_VALUE, result); }
@Test public void testProcessTemplate2() throws Exception { Template template = new VelocityTemplate( new MockTemplateResource(_WRONG_TEMPLATE_ID), null, _velocityEngine, _templateContextHelper, _templateResourceCache, false); template.put(_TEST_KEY, _TEST_VALUE); UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(); try { template.processTemplate(unsyncStringWriter); Assert.fail(); } catch (TemplateException templateException) { String message = templateException.getMessage(); Assert.assertTrue(message, message.contains(_WRONG_TEMPLATE_ID)); } }
@Test public void testProcessTemplate3() throws Exception { Template template = new VelocityTemplate( new StringTemplateResource( _WRONG_TEMPLATE_ID, _TEST_TEMPLATE_CONTENT), null, _velocityEngine, _templateContextHelper, _templateResourceCache, false); template.put(_TEST_KEY, _TEST_VALUE); UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(); template.processTemplate(unsyncStringWriter); String result = unsyncStringWriter.toString(); Assert.assertEquals(_TEST_VALUE, result); }
@Test public void testProcessTemplate4() throws Exception { Template template = new VelocityTemplate( new MockTemplateResource(_TEMPLATE_FILE_NAME), null, _velocityEngine, _templateContextHelper, _templateResourceCache, false); template.put(_TEST_KEY, _TEST_VALUE); UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(); template.processTemplate( unsyncStringWriter, () -> new MockTemplateResource(_WRONG_ERROR_TEMPLATE_ID)); String result = unsyncStringWriter.toString(); Assert.assertEquals(_TEST_VALUE, result); }
@Test public void testProcessTemplate5() throws Exception { Template template = new VelocityTemplate( new MockTemplateResource(_WRONG_TEMPLATE_ID), null, _velocityEngine, _templateContextHelper, _templateResourceCache, false); template.put(_TEST_KEY, _TEST_VALUE); UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(); template.processTemplate( unsyncStringWriter, () -> new MockTemplateResource(_TEMPLATE_FILE_NAME)); String result = unsyncStringWriter.toString(); Assert.assertEquals(_TEST_VALUE, result); }
@Test public void testProcessTemplate6() throws Exception { Template template = new VelocityTemplate( new MockTemplateResource(_WRONG_TEMPLATE_ID), null, _velocityEngine, _templateContextHelper, _templateResourceCache, false); template.put(_TEST_KEY, _TEST_VALUE); UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(); try { template.processTemplate( unsyncStringWriter, () -> new MockTemplateResource(_WRONG_ERROR_TEMPLATE_ID)); Assert.fail(); } catch (TemplateException templateException) { String message = templateException.getMessage(); Assert.assertTrue( message, message.contains(_WRONG_ERROR_TEMPLATE_ID)); } }
@Test public void testProcessTemplate7() throws Exception { Template template = new VelocityTemplate( new MockTemplateResource(_WRONG_TEMPLATE_ID), null, _velocityEngine, _templateContextHelper, _templateResourceCache, false); template.put(_TEST_KEY, _TEST_VALUE); UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(); template.processTemplate( unsyncStringWriter, () -> new StringTemplateResource( _WRONG_ERROR_TEMPLATE_ID, _TEST_TEMPLATE_CONTENT)); String result = unsyncStringWriter.toString(); Assert.assertEquals(_TEST_VALUE, result); }
@Test public void testProcessTemplate8() throws Exception { Template template = new VelocityTemplate( new MockTemplateResource(_TEMPLATE_FILE_NAME), HashMapBuilder.<String, Object>put( _TEST_KEY, _TEST_VALUE ).build(), _velocityEngine, _templateContextHelper, _templateResourceCache, false); UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(); template.processTemplate(unsyncStringWriter); String result = unsyncStringWriter.toString(); Assert.assertEquals(_TEST_VALUE, result); }