Java Code Examples for org.springframework.ui.velocity.VelocityEngineFactoryBean

The following examples show how to use org.springframework.ui.velocity.VelocityEngineFactoryBean. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void velocityEngineFactoryBeanWithVelocityProperties() throws VelocityException, IOException {
	VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
	Properties props = new Properties();
	props.setProperty("myprop", "/mydir");
	vefb.setVelocityProperties(props);
	Object value = new Object();
	Map<String, Object> map = new HashMap<>();
	map.put("myentry", value);
	vefb.setVelocityPropertiesMap(map);
	vefb.afterPropertiesSet();
	assertThat(vefb.getObject(), instanceOf(VelocityEngine.class));
	VelocityEngine ve = vefb.getObject();
	assertEquals("/mydir", ve.getProperty("myprop"));
	assertEquals(value, ve.getProperty("myentry"));
}
 
Example 2
@Bean
@ConditionalOnMissingBean
public VelocityEngineFactoryBean velocityConfiguration() {
    VelocityEngineFactoryBean velocityEngineFactoryBean = new VelocityEngineFactoryBean();
    applyProperties(velocityEngineFactoryBean);
    return velocityEngineFactoryBean;
}
 
Example 3
@Test
public void velocityEngineFactoryBeanWithConfigLocation() throws VelocityException {
	VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
	vefb.setConfigLocation(new FileSystemResource("myprops.properties"));
	Properties props = new Properties();
	props.setProperty("myprop", "/mydir");
	vefb.setVelocityProperties(props);
	try {
		vefb.afterPropertiesSet();
		fail("Should have thrown IOException");
	}
	catch (IOException ex) {
		// expected
	}
}
 
Example 4
@Test
public void velocityEngineFactoryBeanWithResourceLoaderPath() throws IOException, VelocityException {
	VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
	vefb.setResourceLoaderPath("file:/mydir");
	vefb.afterPropertiesSet();
	assertThat(vefb.getObject(), instanceOf(VelocityEngine.class));
	VelocityEngine ve = vefb.getObject();
	assertEquals(new File("/mydir").getAbsolutePath(), ve.getProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH));
}
 
Example 5
@Test
@SuppressWarnings("deprecation")
public void velocityEngineFactoryBeanWithNonFileResourceLoaderPath() throws Exception {
	VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
	vefb.setResourceLoaderPath("file:/mydir");
	vefb.setResourceLoader(new ResourceLoader() {
		@Override
		public Resource getResource(String location) {
			if (location.equals("file:/mydir") || location.equals("file:/mydir/test")) {
				return new ByteArrayResource("test".getBytes(), "test");
			}
			try {
				return new UrlResource(location);
			}
			catch (MalformedURLException ex) {
				throw new IllegalArgumentException(ex.toString());
			}
		}
		@Override
		public ClassLoader getClassLoader() {
			return getClass().getClassLoader();
		}
	});
	vefb.afterPropertiesSet();
	assertThat(vefb.getObject(), instanceOf(VelocityEngine.class));
	VelocityEngine ve = vefb.getObject();
	assertEquals("test", VelocityEngineUtils.mergeTemplateIntoString(ve, "test", Collections.emptyMap()));
}
 
Example 6
Source Project: MaxKey   Source File: Saml20AutoConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * VelocityEngineFactoryBean.
 * @return velocityEngine
 * @throws IOException 
 * @throws VelocityException 
 */
@Bean(name = "velocityEngine")
public VelocityEngine velocityEngine() throws VelocityException, IOException {
    VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
    factory.setPreferFileSystemAccess(false);
    Properties velocityProperties = new Properties();
    velocityProperties.put("resource.loader", "classpath");
    velocityProperties.put("classpath.resource.loader.class", 
            "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    factory.setVelocityProperties(velocityProperties);
    return factory.createVelocityEngine();
}