DefaultVelocityFactory implements VelocityFactory { @Override public VelocityEngine createVelocityEngine(String key, Properties properties) throws XWikiVelocityException { VelocityEngine engine; try { engine = this.componentManager.getInstance(VelocityEngine.class); } catch (ComponentLookupException e) { throw new XWikiVelocityException("Failed to create Velocity Engine", e); } engine.initialize(properties); this.velocityEngines.put(key, engine); return engine; }  @Override boolean hasVelocityEngine(String key); @Override VelocityEngine getVelocityEngine(String key); @Override VelocityEngine createVelocityEngine(String key, Properties properties); @Override VelocityEngine removeVelocityEngine(String key);  }
@Test void createVelocityEngine() throws Exception { assertNull(this.velocityFactory.getVelocityEngine("somekey")); assertFalse(this.velocityFactory.hasVelocityEngine("somekey")); VelocityEngine mockEngine = this.componentManager.registerMockComponent(VelocityEngine.class); Properties props = new Properties(); props.setProperty("prop1", "value1"); this.velocityFactory.createVelocityEngine("somekey", props); assertSame(mockEngine, this.velocityFactory.getVelocityEngine("somekey")); assertTrue(this.velocityFactory.hasVelocityEngine("somekey")); verify(mockEngine).initialize(props); }
@Test void createVelocityEngineWhenFailing() { Throwable exception = assertThrows(XWikiVelocityException.class, () -> { this.velocityFactory.createVelocityEngine("somekey", new Properties()); }); assertEquals("Failed to create Velocity Engine", exception.getMessage()); assertEquals("ComponentLookupException: Can't find descriptor for the component with type " + "[interface org.xwiki.velocity.VelocityEngine] and hint [null]", ExceptionUtils.getRootCauseMessage(exception)); }