Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Approach for bundeling string helpers into templates in Seam Render
public class BundledHelpersTest {
@Test
public void testStringUtils() throws Exception {
testBundledHelper("TheInpu...", "@{render.abbreviate('TheInputString', 10)}");
testBundledHelper("...putSt...", "@{render.abbreviate('TheInputString', 5, 11)}");
testBundledHelper("TheInputString", "@{render.capitalize('TheInputString')}");
testBundledHelper(" TheInputString ", "@{render.center('TheInputString', 20)}");
testBundledHelper("TheInputString", "@{render.defaultString('TheInputString', 'BlahBlah')}");
testBundledHelper("BlahBlah", "@{render.defaultString('', 'BlahBlah')}");
testBundledHelper("BlahBlah", "@{render.defaultString(null, 'BlahBlah')}");
testBundledHelper("The Input String", "@{render.join(new String[]{'The', 'Input', 'String'}, ' ')}");
testBundledHelper(" TheInputString", "@{render.leftPad('TheInputString', 20)}");
testBundledHelper("XXXXXXTheInputString", "@{render.leftPad('TheInputString', 20, 'X')}");
testBundledHelper("theinputstring", "@{render.lowerCase('TheInputString')}");
testBundledHelper("TheOutputString", "@{render.replace('TheInputString', 'In', 'Out')}");
testBundledHelper("TheInputString ", "@{render.rightPad('TheInputString', 20)}");
testBundledHelper("TheInputStringXXXXXX", "@{render.rightPad('TheInputString', 20, 'X')}");
testBundledHelper(new String[]{"TheI", "putStri", "g"}, "@{render.split('TheInputString', 'n')}");
testBundledHelper(new String[]{"The", "Input", "String"}, "@{render.split('The Input String')}");
testBundledHelper("tHEiNPUTsTRING", "@{render.swapCase('TheInputString')}");
testBundledHelper("TheInputString", "@{render.trim(' TheInputString ')}");
testBundledHelper("theInputString", "@{render.uncapitalize('TheInputString')}");
testBundledHelper("THEINPUTSTRING", "@{render.upperCase('TheInputString')}");
}
private void testBundledHelper(Object expectedResult, String mvelExpression) throws SecurityException, NoSuchMethodException {
ParserContext ctx = prepareParserContext();
CompiledTemplate template = CustomTemplateCompiler.compileTemplate(mvelExpression, ctx);
Object result = TemplateRuntime.execute(template);
if (result instanceof Object[]) {
assertArrayEquals((Object[]) expectedResult, (Object[]) result);
} else {
assertEquals(expectedResult, result);
}
}
private ParserContext prepareParserContext() throws NoSuchMethodException, SecurityException {
ParserContext ctx = new ParserContext();
ctx.addImport("render", StringUtils.class);
return ctx;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.