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.assertArrayEquals;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNull;
27
28
29
30
31
32
33 public class ScriptUtilsTest {
34
35 @Test
36
37
38
39 public void testEscapeHtml() throws Exception {
40 assertEquals("wasn't", ScriptUtils.escapeHtml("wasn't"));
41 assertEquals(""wasn't"", ScriptUtils.escapeHtml("\"wasn't\""));
42 }
43
44 @Test
45
46
47
48 public void testEscapeHtmlStringList() {
49 String[] escaped = {"wasn't", ""wasn't""};
50 String[] unEscaped = {"wasn't", "\"wasn't\""};
51 assertEquals(Arrays.asList(escaped), ScriptUtils.escapeHtml(Arrays.asList(unEscaped)));
52
53 List<String> nullList = null;
54 assertNull(ScriptUtils.escapeHtml(nullList));
55
56 List<String> emptyList = Collections.emptyList();
57 assertEquals(emptyList, ScriptUtils.escapeHtml(emptyList));
58 }
59
60 @Test
61
62
63
64 public void testConvertToJSValue_function() {
65
66 String jsFunction = "\n function () {alert('1 + 1 ' is 1 + 1);} \n\n";
67 assertEquals("function was not converted to js value as expected", jsFunction, ScriptUtils.convertToJsValue(jsFunction));
68 }
69
70 @Test
71
72
73
74 public void testConvertToJSValue_numeric() {
75 assertEquals("number was not converted to js value as expected", " -1 ", ScriptUtils.convertToJsValue(" -1 "));
76 assertEquals("number was not converted to js value as expected", "1.01 ", ScriptUtils.convertToJsValue("1.01 "));
77 assertEquals("string was not converted to js value as expected", "'1.o1 '", ScriptUtils.convertToJsValue("1.o1 "));
78 }
79
80 @Test
81
82
83
84 public void testConvertToJSValue_mapAndArray() {
85 assertEquals("array was not converted to js value as expected", " [-1, 4, 5] ", ScriptUtils.convertToJsValue(" [-1, 4, 5] "));
86 String jsMap = " {'a':1, 'b':2} \n";
87 assertEquals("map was not converted to js value as expected", jsMap, ScriptUtils.convertToJsValue(jsMap));
88 }
89 }