Created
September 8, 2011 23:22
-
-
Save peteroyle/1205078 to your computer and use it in GitHub Desktop.
Approach for bundeling string helpers into templates in Seam Render
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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