org.apache.velocity.runtime.RuntimeInstance Java Examples

The following examples show how to use org.apache.velocity.runtime.RuntimeInstance. 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: StopCommand.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public boolean isFor(Object that)
{
    if (nearest) // if we're stopping at the first chance
    {
        // save that for message
        stopMe = that;
        return true;
    }
    else if (stopMe != null) // if we have a specified stopping point
    {
        return (that == stopMe);
    }
    else // only stop for the top :)
    {
        return (that instanceof Template ||
                that instanceof RuntimeInstance);
    }
}
 
Example #2
Source File: VelTools66TestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void testVelTools66()
        throws Exception
{
    /* the testcase is obsolete in JDK 8, as SystemManager.checkMemberAccess is not anymore called
     * by Class.getMethods() */

    int javaVersion = Integer.parseInt(System.getProperty("java.version").split("\\.")[1]);
    if (javaVersion >= 8)
    {
        return;
    }

    Method verifyMethod = TestInterface.class.getMethod("getTestValue");

    RuntimeInstance ri = new RuntimeInstance();
    log = new TestLogger(false, false);
    Introspector introspector = new Introspector(log);

    Method testMethod = introspector.getMethod(TestObject.class, "getTestValue", new Object[0]);
    assertNotNull(testMethod);
    assertEquals("Method object does not match!", verifyMethod, testMethod);
}
 
Example #3
Source File: TextblockTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
@Override
protected void setUp() throws Exception
{
    super.setUp();

    // get a valid parser instance to initialize string constants
    Field riField = engine.getClass().getDeclaredField("ri");
    riField.setAccessible(true);
    RuntimeInstance ri = (RuntimeInstance)riField.get(engine);
    Parser parser = ri.createNewParser();
    ASTTextblock astTextblock = new ASTTextblock(parser, 0);
    START = astTextblock.START;
    END = astTextblock.END;
    PARTIAL_START = START.substring(0, START.length() - 1);
    PARTIAL_END = END.substring(1);
    END_OF_START = START.substring(START.length() - 1);
    START_OF_END = END.substring(0, 1);
}
 
Example #4
Source File: DataSourceResourceLoaderTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void testUnicode(RuntimeInstance engine)
    throws Exception
{
    Template template = engine.getTemplate(UNICODE_TEMPLATE_NAME);

    Writer writer = new StringWriter();
    VelocityContext context = new VelocityContext();
    template.merge(context, writer);
    writer.flush();
    writer.close();

    String outputText = writer.toString();

    if (!normalizeNewlines(UNICODE_TEMPLATE).equals(
            normalizeNewlines( outputText ) ))
    {
        fail("Output incorrect for Template: " + UNICODE_TEMPLATE_NAME);
    }
}
 
Example #5
Source File: DataSourceResourceLoaderTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
protected Template executeTest(final String templateName, RuntimeInstance engine)
	throws Exception
{
    Template template = engine.getTemplate(templateName);

    FileOutputStream fos =
            new FileOutputStream (
                    getFileName(RESULTS_DIR, templateName, RESULT_FILE_EXT));

    Writer writer = new BufferedWriter(new OutputStreamWriter(fos));

    VelocityContext context = new VelocityContext();
    context.put("tool", new DSRLTCTool());

    template.merge(context, writer);
    writer.flush();
    writer.close();

    if (!isMatch(RESULTS_DIR, COMPARE_DIR, templateName,
                    RESULT_FILE_EXT, CMP_FILE_EXT))
    {
        fail("Output incorrect for Template: " + templateName);
    }

    return template;
}
 
Example #6
Source File: ConversionHandlerTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void testCustomConversionHandlerInstance()
{
    RuntimeInstance ve = new RuntimeInstance();
    ve.setProperty( Velocity.VM_PERM_INLINE_LOCAL, Boolean.TRUE);
    ve.setProperty(Velocity.RUNTIME_LOG_INSTANCE, log);
    ve.setProperty(RuntimeConstants.RESOURCE_LOADERS, "file");
    ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, TEST_COMPARE_DIR + "/conversion");
    ve.setProperty(RuntimeConstants.CONVERSION_HANDLER_INSTANCE, new MyCustomConverter());
    ve.init();
    Uberspect uberspect = ve.getUberspect();
    assertTrue(uberspect instanceof UberspectImpl);
    UberspectImpl ui = (UberspectImpl)uberspect;
    TypeConversionHandler ch = ui.getConversionHandler();
    assertTrue(ch != null);
    assertTrue(ch instanceof MyCustomConverter);
    VelocityContext context = new VelocityContext();
    context.put("obj", new Obj());
    Writer writer = new StringWriter();
    ve.evaluate(context, writer, "test", "$obj.objectString(1.0)");
    assertEquals("String ok: foo", writer.toString());
}
 
Example #7
Source File: TestVelocityView.java    From gocd with Apache License 2.0 5 votes vote down vote up
TestVelocityView(String templatePath, Map<String, Object> modelData) {
    this.templatePath = templatePath;
    this.modelData = modelData;

    this.additionalTemplates = new ArrayList<>();
    loader = mock(ResourceLoader.class);
    runtimeServices = spy(new RuntimeInstance());

    setupToolAttributes();
}
 
Example #8
Source File: TestVelocityView.java    From gocd with Apache License 2.0 5 votes vote down vote up
private Template setupTemplate(ResourceLoader loader, RuntimeInstance runtimeServices, String templateName, InputStream templateContents) throws IOException {
    Template template = new Template();
    template.setRuntimeServices(runtimeServices);
    template.setResourceLoader(loader);
    template.setName(templateName);

    byte[] bytes = IOUtils.toByteArray(templateContents);
    templateContents.close();

    when(loader.getResourceStream(templateName)).thenReturn(new ByteArrayInputStream(bytes));
    doReturn(template).when(runtimeServices).getTemplate(templateName);
    doReturn(template).when(runtimeServices).getTemplate(eq(templateName), any(String.class));

    return template;
}
 
Example #9
Source File: TestVelocityView.java    From gocd with Apache License 2.0 5 votes vote down vote up
private void setupContentResource(ResourceLoader loader, RuntimeInstance runtimeServices, String templateName, String fakeContent) {
    try {
        ContentResource resource = new ContentResource();
        resource.setRuntimeServices(runtimeServices);
        resource.setResourceLoader(loader);
        resource.setName(templateName);
        resource.setData(fakeContent);

        doReturn(resource).when(runtimeServices).getContent(templateName);
        doReturn(resource).when(runtimeServices).getContent(eq(templateName), any(String.class));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #10
Source File: VelocityScriptEngine.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
private void initVelocityEngine(ScriptContext ctx)
{
    if (ctx == null)
    {
        ctx = getContext();
    }
    if (velocityEngine == null)
    {
        synchronized (this)
        {
            if (velocityEngine != null) return;

            Properties props = getVelocityProperties(ctx);
            RuntimeInstance tmpEngine = new RuntimeInstance();
            try
            {
                if (props != null)
                {
                    tmpEngine.init(props);
                }
                else
                {
                    tmpEngine.init();
                }
            }
            catch (RuntimeException rexp)
            {
                throw rexp;
            }
            catch (Exception exp)
            {
                throw new RuntimeException(exp);
            }
            velocityEngine = tmpEngine;
        }
    }
}
 
Example #11
Source File: MiscTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testRuntimeInstanceProperties()
{
    // check that runtime instance properties can be set and retrieved
    RuntimeInstance ri = new RuntimeInstance();
    ri.setProperty("baabaa.test","the answer");
    assertEquals("the answer",ri.getProperty("baabaa.test"));
}
 
Example #12
Source File: DataSourceResourceLoaderTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void setUp()
        throws Exception
{

    assureResultsDirectoryExists(RESULTS_DIR);

    DataSource ds1 = new TestDataSource(TEST_JDBC_DRIVER_CLASS, TEST_JDBC_URI, TEST_JDBC_LOGIN, TEST_JDBC_PASSWORD);
    DataSourceResourceLoader rl1 = new DataSourceResourceLoader();
    rl1.setDataSource(ds1);

    DataSource ds2 = new TestDataSource(TEST_JDBC_DRIVER_CLASS, TEST_JDBC_URI, TEST_JDBC_LOGIN, TEST_JDBC_PASSWORD);
    DataSourceResourceLoader rl2 = new DataSourceResourceLoader();
    rl2.setDataSource(ds2);

    ExtProperties props = new ExtProperties();
    props.addProperty( "resource.loader", "ds" );
    props.setProperty( "ds.resource.loader.instance", rl1);
    props.setProperty( "ds.resource.loader.resource.table",           "velocity_template_varchar");
    props.setProperty( "ds.resource.loader.resource.keycolumn",       "vt_id");
    props.setProperty( "ds.resource.loader.resource.templatecolumn",  "vt_def");
    props.setProperty( "ds.resource.loader.resource.timestampcolumn", "vt_timestamp");
    props.setProperty(Velocity.RUNTIME_LOG_INSTANCE, new TestLogger(false, false));

    varcharTemplatesEngine = new RuntimeInstance();
    varcharTemplatesEngine.setConfiguration(props);
    varcharTemplatesEngine.init();

    ExtProperties props2 = (ExtProperties)props.clone();
    props2.setProperty( "ds.resource.loader.instance", rl2);
    props2.setProperty( "ds.resource.loader.resource.table",           "velocity_template_clob");
    clobTemplatesEngine = new RuntimeInstance();
    clobTemplatesEngine.setConfiguration(props2);
    clobTemplatesEngine.init();
}
 
Example #13
Source File: VelocityScriptEngine.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
/**
 * get the internal Velocity RuntimeInstance
 * @return the internal Velocity RuntimeInstance
 */
protected RuntimeInstance getVelocityEngine()
{
    return velocityEngine;
}
 
Example #14
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
public void setUp()
        throws Exception
{
    ri = new RuntimeInstance();
    ri.init();
}