@Lazy @Bean(name = "shibboleth.VelocityEngine") public VelocityEngineFactoryBean velocityEngineFactoryBean() { final VelocityEngineFactoryBean bean = new VelocityEngineFactoryBean(); final Properties properties = new Properties(); properties.put(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, SLF4JLogChute.class.getName()); properties.put(RuntimeConstants.INPUT_ENCODING, StandardCharsets.UTF_8.name()); properties.put(RuntimeConstants.OUTPUT_ENCODING, StandardCharsets.UTF_8.name()); properties.put(RuntimeConstants.ENCODING_DEFAULT, StandardCharsets.UTF_8.name()); properties.put(RuntimeConstants.RESOURCE_LOADER, "file, classpath, string"); properties.put(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FileUtils.getTempDirectory().getAbsolutePath()); properties.put(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, Boolean.FALSE); properties.put("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); properties.put("string.resource.loader.class", StringResourceLoader.class.getName()); properties.put("file.resource.loader.class", FileResourceLoader.class.getName()); bean.setOverrideLogging(false); bean.setVelocityProperties(properties); return bean; }
@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")); }
public void testVelocityEngineFactoryBeanWithNonFileResourceLoaderPath() 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", new HashMap())); }
@Bean @ConditionalOnMissingBean public VelocityEngineFactoryBean velocityConfiguration() { VelocityEngineFactoryBean velocityEngineFactoryBean = new VelocityEngineFactoryBean(); applyProperties(velocityEngineFactoryBean); return velocityEngineFactoryBean; }
@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 } }
@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)); }
@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())); }
@Bean public VelocityEngine velocityEngine() throws IOException { VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean(); Properties props = new Properties(); props.put("resource.loader", "class"); props.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); factory.setVelocityProperties(props); return factory.createVelocityEngine(); }
@Bean @Scope("singleton") // the method name (mockMessageService) becomes the bean "name" VelocityEngineFactory velocityEngine() { Properties velocityProps = new Properties(); velocityProps.put("resource.loader","class"); velocityProps.put("class.resource.loader.class","org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); VelocityEngineFactory outVelocityEngineFactory = new VelocityEngineFactoryBean(); outVelocityEngineFactory.setVelocityProperties(velocityProps); return outVelocityEngineFactory; }
@Override protected void configure() { R01F.initSystemEnv(); bind(MailManager.class).toInstance(new MailManager()); bind(JavaMailSender.class).toInstance(GMailSMTPMailSender.create("futuretelematics", "qewsvvedftyinsgm")); bind(MailMessage.class).toInstance(new SimpleMailMessage()); bind(VelocityEngine.class).toProvider(new Provider<VelocityEngine>() { @Override public VelocityEngine get() { Properties velocityProps = new Properties(); velocityProps.put("resource.loader","class"); velocityProps.put("class.resource.loader.class","org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); VelocityEngineFactory velocityEngineFactory = new VelocityEngineFactoryBean(); velocityEngineFactory.setVelocityProperties(velocityProps); VelocityEngine outVelocityEngine = null; try { outVelocityEngine = velocityEngineFactory.createVelocityEngine(); } catch(IOException ioEx) { ioEx.printStackTrace(System.out); } return outVelocityEngine; } }) .in(Singleton.class); }
@Bean public VelocityEngine velocityEngine() throws VelocityException, IOException{ VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean(); Properties props = new Properties(); props.put("resource.loader", "class"); props.put("class.resource.loader.class","org.apache.velocity.runtime.resource.loader." +"ClasspathResourceLoader"); factory.setVelocityProperties(props); return factory.createVelocityEngine(); }
private int getGeneratedJSONObjectsCount(int amountOfConfigs) throws IOException { List<Config> simpleConfigs = new ArrayList<>(); for(int i = 0; i < amountOfConfigs; i++) { Config simpleConfig = new Config(); simpleConfig.setName("simpleConfiguration" + Integer.toString(i)); simpleConfigs.add(simpleConfig); } Configs configs = new Configs(simpleConfigs, "simpleConfiguration"); when(configService.getConfigs()).thenReturn(configs); openMRSTaskDataProviderBuilder.setOpenMRSConfigService(configService); VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean(); Map<String, Object> velocityPropertiesMap = new HashMap<String, Object>(); velocityPropertiesMap.put("resource.loader", "class"); velocityPropertiesMap.put("class.resource.loader.class", "org.motechproject.openmrs.tasks.VelocityResourceLoader"); vefb.setVelocityPropertiesMap(velocityPropertiesMap); VelocityEngine velocityEngine = vefb.createVelocityEngine(); assertNotNull(velocityEngine); openMRSTaskDataProviderBuilder.setVelocityEngine(velocityEngine); String generatedJSON = openMRSTaskDataProviderBuilder.generateDataProvider(); JSONObject jsonResult = new JSONObject(generatedJSON); JSONArray objects = jsonResult.getJSONArray("objects"); return objects.length(); }
@Bean public VelocityEngineFactoryBean velocityEngine() { VelocityEngineFactoryBean factoryBean = new VelocityEngineFactoryBean(); Properties properties = new Properties(); properties.put("resource.loader", "class"); properties.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName()); factoryBean.setVelocityProperties(properties); return factoryBean; }
public void testVelocityEngineFactoryBeanWithConfigLocation() 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 } }
public void testVelocityEngineFactoryBeanWithVelocityProperties() throws VelocityException, IOException { VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean(); Properties props = new Properties(); props.setProperty("myprop", "/mydir"); vefb.setVelocityProperties(props); Object value = new Object(); Map 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")); }
public void testVelocityEngineFactoryBeanWithResourceLoaderPath() 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)); }
/** * Velocity engine. * * @return the velocity engine factory bean */ @Bean public VelocityEngineFactoryBean velocityEngine() { VelocityEngineFactoryBean velocityFactoryBean = new VelocityEngineFactoryBean(); Properties velocityProperties = new Properties(); velocityProperties.put("resource.loader", "class"); velocityProperties .put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); velocityFactoryBean.setVelocityProperties(velocityProperties); return velocityFactoryBean; }
public VelocityEngineFactoryBean velocityConfiguration() { VelocityEngineFactoryBean velocityEngineFactoryBean = new VelocityEngineFactoryBean(); applyProperties(velocityEngineFactoryBean); return velocityEngineFactoryBean; }