|
If someone would take me up on the pairing, then I
could help fix these myself :-)
the basic's of the test (for those who don't want
to read the 5 lines themselves) are
$!method
$!method.getInstance()
will return null if the toString return's null.
probably because it is outside of the validation
process.
Llewellyn
|
package com.spun.util.velocity.tests; import junit.framework.TestCase; import org.apache.velocity.context.Context; import com.spun.util.velocity.ContextAware; import com.spun.util.velocity.VelocityParser;
public class VelocitySilentTest
extends TestCase implements ContextAware
{
/***********************************************************************/
public void testMethod() throws Exception
{
assertEquals("", VelocityParser.parseString("$!main.getInstance()", this));
}
/***********************************************************************/
public void testField() throws Exception
{
assertEquals("", VelocityParser.parseString("$!main", this));
}
/***********************************************************************/
public Object getInstance()
{
return this;
}
/***********************************************************************/
public String toString()
{
return null;
}
/***********************************************************************/
public void setupContext(Context context)
{
context.put("main", this);
}
}
/***********************************************************************/
/***********************************************************************/
package com.spun.util.velocity;
import org.apache.velocity.context.Context;
public interface ContextAware
{
public void setupContext(Context context);
/***********************************************************************/
/***********************************************************************/
}
package com.spun.util.velocity;
import java.io.File;
import java.io.StringWriter;
import java.util.Properties;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import com.spun.facilitate.util.Commons;
import com.spun.facilitate.util.Config;
import com.spun.util.ObjectUtils;
public class VelocityParser
{
/***********************************************************************/
public static String parseJarFile(String template, ContextAware process)
{
Properties props = new Properties();
if (Config.FORCE_ACTUAL_FILE_INSTEAD_OF_JAR)
{
props.put("file.resource.loader.path", Config.ALTERNATIVE_TEMPLATE_DIRECTORY + ", " + Config.TEMPLATE_DIRECTORY);
props.put("file.resource.cache", "" + true);
}
else
{
props.put("resource.loader", "class");
props.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
props.put("class.resource.cache", "" + true);
}
props.put("runtime.introspector.uberspect", "com.spun.util.velocity.TestableUberspect");
props.put("velocimacro.context.localscope", "" + true);
props.put("velocimacro.permissions.allow.inline.local.scope", "" + true);
return parseFile(template, props, process);
}
/***********************************************************************/
public static String parseFile(String template, ContextAware process)
{
Properties props = new Properties();
int pathBreak = template.lastIndexOf(File.separatorChar);
pathBreak = pathBreak == -1 ? template.length() : pathBreak;
String path = template.substring(0, pathBreak);
String file = template.substring(pathBreak + 1);
props.put("file.resource.loader.path", path);
props.put("velocimacro.context.localscope", "" + true);
props.put("velocimacro.permissions.allow.inline.local.scope", "" + true);
return parseFile(file, props, process);
}
/***********************************************************************/
public static String parseString(String template, ContextAware process) throws Exception
{
Properties props = new Properties();
props.put("resource.loader", "class");
props.put("class.resource.loader.class", "com.spun.util.velocity.StringResourceLoader");
props.put("velocimacro.context.localscope", "" + true);
props.put("runtime.introspector.uberspect", "com.spun.util.velocity.TestableUberspect");
props.put("velocimacro.permissions.allow.inline.local.scope", "" + true);
return parseFile(template, props, process);
}
/***********************************************************************/
public static String parseFile(String template, Properties props , ContextAware process)
{
try
{
props.put("directive.foreach.counter.initial.value", "0");
Velocity.init(props);
VelocityContext context = new VelocityContext();
Template velocityTemplate = Velocity.getTemplate(template);
process.setupContext(context);
context.put("config", Config.INSTANCE);
context.put("commons", Commons.INSTANCE);
StringWriter out = new StringWriter();
velocityTemplate.merge(context, out);
return out.toString();
}
catch (Exception e)
{
throw ObjectUtils.throwAsError(e);
}
}
/***********************************************************************/
/***********************************************************************/
}
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
