1   package org.apache.velocity.test;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Test Case for <a href="https://issues.apache.org/jira/browse/VELTOOLS-520">Velocity Tools Issue 520</a>.
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              // checking that no new string allocated, for perfomance
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  }