org.springframework.ui.velocity.VelocityEngineFactoryBean Java Examples

The following examples show how to use org.springframework.ui.velocity.VelocityEngineFactoryBean. 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
Source File: VelocityConfigurerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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
Source File: VelocityAutoConfiguration.java    From velocity-spring-boot-project with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public VelocityEngineFactoryBean velocityConfiguration() {
    VelocityEngineFactoryBean velocityEngineFactoryBean = new VelocityEngineFactoryBean();
    applyProperties(velocityEngineFactoryBean);
    return velocityEngineFactoryBean;
}
 
Example #3
Source File: VelocityConfigurerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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
Source File: VelocityConfigurerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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
Source File: VelocityConfigurerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 File: Saml20AutoConfiguration.java    From MaxKey with 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();
}