1 package org.apache.velocity.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24 import java.io.StringWriter;
25 import org.apache.velocity.app.VelocityEngine;
26 import org.apache.velocity.VelocityContext;
27 import org.apache.velocity.test.BaseTestCase;
28 import org.apache.velocity.runtime.parser.node.ASTStringLiteral;
29
30
31
32
33 public class UnicodeEscapeTestCase extends BaseTestCase
34 {
35 private VelocityEngine engine;
36 private VelocityContext context;
37
38 public UnicodeEscapeTestCase(final String name) throws Exception
39 {
40 super(name);
41 }
42
43 public static Test suite()
44 {
45 return new TestSuite(UnicodeEscapeTestCase.class);
46 }
47
48 public void setUp() throws Exception
49 {
50 engine = new VelocityEngine();
51 context = new VelocityContext();
52 }
53
54 public void tearDown()
55 {
56 engine = null;
57 context = null;
58 }
59
60 private String evaluate(String template) throws Exception
61 {
62 StringWriter writer = new StringWriter();
63 engine.evaluate(context, writer, "test", template);
64 return writer.toString();
65 }
66
67 public void testUnicodeEscape() throws Exception
68 {
69 assertEquals("a", evaluate("#set($v = \"\\u0061\")$v"));
70 }
71
72 private void assertUnescape(String expected, String escaped)
73 {
74 String unescaped = ASTStringLiteral.unescape(escaped);
75 assertEquals(expected, unescaped);
76 if (escaped.equals(expected))
77 {
78
79 assertSame(unescaped, escaped);
80 }
81 }
82
83 public void testASTStringLiteralUnescape()
84 {
85 assertUnescape("", "");
86 assertUnescape("bebe", "bebe");
87 assertUnescape("as\\nsd", "as\\nsd");
88 assertUnescape("a", "\\u0061");
89 assertUnescape("abc", "\\u0061bc");
90 assertUnescape("\u0061bc\u0064", "\\u0061bc\\u0064");
91 assertUnescape("z\u0061bc\u0064f", "z\\u0061bc\\u0064f");
92 }
93
94 }