1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.service.template;
35
36 import java.util.Date;
37 import java.util.HashMap;
38 import java.util.Locale;
39 import java.util.Map;
40
41 import fr.paris.lutece.test.LuteceTestCase;
42 import fr.paris.lutece.util.html.HtmlTemplate;
43
44 public class AbstractMessageFormatTemplateMethodTest extends LuteceTestCase
45 {
46
47 private static final class TestAbstractMessageFormatTemplateMethod extends AbstractMessageFormatTemplateMethod
48 {
49 private final String strKey;
50
51 public TestAbstractMessageFormatTemplateMethod( String key )
52 {
53 strKey = key;
54 }
55
56 @Override
57 protected String getPattern( String key, Locale locale )
58 {
59 return strKey;
60 }
61 }
62
63 public void testExec( )
64 {
65 AbstractMessageFormatTemplateMethod method = new TestAbstractMessageFormatTemplateMethod( "test with 'quote'" );
66 Map<Object, Object> model = new HashMap<>( );
67 model.put( "method", method );
68 String template = "${method(\"key\")}";
69 HtmlTemplate res = FreeMarkerTemplateService.getInstance( ).loadTemplateFromStringFtl( template, Locale.FRANCE, model );
70 assertNotNull( res );
71 assertEquals( "test with 'quote'", res.getHtml( ).replaceAll( "\n", "" ) );
72
73 template = "${method(\"key\",\"arg\")}";
74 res = FreeMarkerTemplateService.getInstance( ).loadTemplateFromStringFtl( template, Locale.FRANCE, model );
75 assertNotNull( res );
76 assertEquals( "test with quote", res.getHtml( ).replaceAll( "\n", "" ) );
77
78 method = new TestAbstractMessageFormatTemplateMethod( "test with 'quote' and arg {0}" );
79 model.put( "method", method );
80 template = "${method(\"key\",\"arg\")}";
81 res = FreeMarkerTemplateService.getInstance( ).loadTemplateFromStringFtl( template, Locale.FRANCE, model );
82 assertNotNull( res );
83 assertEquals( "test with quote and arg arg", res.getHtml( ).replaceAll( "\n", "" ) );
84
85 template = "${method(\"key\",arg)}";
86 model.put( "arg", "arg" );
87 res = FreeMarkerTemplateService.getInstance( ).loadTemplateFromStringFtl( template, Locale.FRANCE, model );
88 assertNotNull( res );
89 assertEquals( "test with quote and arg arg", res.getHtml( ).replaceAll( "\n", "" ) );
90
91 model.put( "arg", 10 );
92 res = FreeMarkerTemplateService.getInstance( ).loadTemplateFromStringFtl( template, Locale.FRANCE, model );
93 assertNotNull( res );
94 assertEquals( "test with quote and arg 10", res.getHtml( ).replaceAll( "\n", "" ) );
95
96 model.put( "arg", new Date( 123456789 ) );
97 res = FreeMarkerTemplateService.getInstance( ).loadTemplateFromStringFtl( template, Locale.FRANCE, model );
98 assertNotNull( res );
99 String expResultJava8 = "test with quote and arg 02/01/70 11:17";
100 String expResultJava10 = "test with quote and arg 02/01/1970 11:17";
101 assertTrue( expResultJava8.equals( res.getHtml( ).replaceAll( "\n", "" ) ) || expResultJava10.equals( res.getHtml( ).replaceAll( "\n", "" ) ) );
102
103 model.put( "arg", new Date( 123456789 ) );
104 res = FreeMarkerTemplateService.getInstance( ).loadTemplateFromStringFtl( template, Locale.US, model );
105 assertNotNull( res );
106 expResultJava8 = "test with quote and arg 1/2/70 11:17 AM";
107 expResultJava10 = "test with quote and arg 1/2/70, 11:17 AM";
108 assertTrue( expResultJava8.equals( res.getHtml( ).replaceAll( "\n", "" ) ) || expResultJava10.equals( res.getHtml( ).replaceAll( "\n", "" ) ) );
109 }
110
111 }