1
2
3
4
5 package gboat2.base.core.service.test;
6
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
9
10 import junit.framework.TestCase;
11
12 import org.apache.commons.lang3.StringEscapeUtils;
13 import org.apache.velocity.tools.generic.EscapeTool;
14
15
16 public class JavaScriptEscapeTest extends TestCase{
17
18 protected void setUp() throws Exception {
19 super.setUp();
20 }
21
22 protected void tearDown() throws Exception {
23 super.tearDown();
24 }
25
26
27
28 public void test1() {
29 String str = "中\"国";
30 System.out.println("用escapeJava方法转义之后的字符串为:"+StringEscapeUtils.escapeJava(str));
31 System.out.println("用unescapeJava方法反转义之后的字符串为:"+StringEscapeUtils.unescapeJava(StringEscapeUtils.escapeJava(str)));
32
33 System.out.println("用escapeHtml方法转义之后的字符串为:"+StringEscapeUtils.escapeHtml4(str));
34 System.out.println("用unescapeHtml方法反转义之后的字符串为:"+StringEscapeUtils.unescapeHtml4(StringEscapeUtils.escapeHtml4(str)));
35
36 System.out.println("用escapeXml方法转义之后的字符串为:"+StringEscapeUtils.escapeXml(str));
37 System.out.println("用unescapeXml方法反转义之后的字符串为:"+StringEscapeUtils.unescapeXml(StringEscapeUtils.escapeXml(str)));
38
39 System.out.println("用escapeJavaScript方法转义之后的字符串为:"+StringEscapeUtils.escapeEcmaScript(str));
40 System.out.println("用unescapeJavaScript方法反转义之后的字符串为:"+StringEscapeUtils.unescapeEcmaScript(StringEscapeUtils.escapeEcmaScript(str)));
41 }
42
43 public void test2(){
44 EscapeTool escape = new EscapeTool();
45
46 String es= "{\"desc\":\"\u672A\u53D1\u5E03\",\"name\":\"NotPublished\",\"ordinal\":1}";
47
48
49 System.out.println(escape.javascript(es));
50 System.out.println(escape.sql(es));
51
52 }
53
54
55
56 public void test3(){
57 String str="$!a.attachsJson";
58
59 String regEx="[^$!attachsJson]";
60 Pattern p=Pattern.compile(regEx);
61 Matcher m=p.matcher(str);
62 boolean result=m.find();
63 System.out.println(result);
64 }
65
66 public void test4(){
67
68
69
70
71
72
73
74
75
76
77 }
78
79
80
81
82
83
84
85
86
87 }