1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.util;
17
18 import org.junit.Test;
19
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.List;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNull;
26
27
28
29
30
31
32 public class ScriptUtilsTest {
33
34 @Test
35
36
37
38 public void testEscapeHtml() throws Exception {
39 assertEquals("wasn't", ScriptUtils.escapeHtml("wasn't"));
40 assertEquals("\\u0022wasn't\\u0022", ScriptUtils.escapeHtml("\"wasn't\""));
41 }
42
43 @Test
44
45
46
47 public void testEscapeHtmlStringList() {
48 String[] escaped = {"wasn't", "\\u0022wasn't\\u0022"};
49 String[] unEscaped = {"wasn't", "\"wasn't\""};
50 assertEquals(Arrays.asList(escaped), ScriptUtils.escapeHtml(Arrays.asList(unEscaped)));
51
52 List<String> nullList = null;
53 assertNull(ScriptUtils.escapeHtml(nullList));
54
55 List<String> emptyList = Collections.emptyList();
56 assertEquals(emptyList, ScriptUtils.escapeHtml(emptyList));
57 }
58
59 @Test
60
61
62
63 public void testConvertToJSValue_function() {
64
65 String jsFunction = "\n function () {alert('1 + 1 ' is 1 + 1);} \n\n";
66 assertEquals("function was not converted to js value as expected", jsFunction, ScriptUtils.convertToJsValue(jsFunction));
67 }
68
69 @Test
70
71
72
73 public void testConvertToJSValue_numeric() {
74 assertEquals("number was not converted to js value as expected", " -1 ", ScriptUtils.convertToJsValue(" -1 "));
75 assertEquals("number was not converted to js value as expected", "1.01 ", ScriptUtils.convertToJsValue("1.01 "));
76 assertEquals("string was not converted to js value as expected", "'1.o1 '", ScriptUtils.convertToJsValue("1.o1 "));
77 }
78
79 @Test
80
81
82
83 public void testConvertToJSValue_mapAndArray() {
84 assertEquals("array was not converted to js value as expected", " [-1, 4, 5] ", ScriptUtils.convertToJsValue(" [-1, 4, 5] "));
85 String jsMap = " {'a':1, 'b':2} \n";
86 assertEquals("map was not converted to js value as expected", jsMap, ScriptUtils.convertToJsValue(jsMap));
87 }
88
89
90
91
92 @Test
93 public void testBuildEventHandlerScript() {
94 String onClickScript = "alert('A click happened');";
95 String onClickHandler = ScriptUtils.buildEventHandlerScript("u09", "click", onClickScript);
96
97 String expectedHandler = "jQuery('#u09').on('click', function(e) {" + onClickScript + "}); ";
98
99 assertEquals("generate event script is not correct", expectedHandler, onClickHandler);
100 }
101
102 }