View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   *  ScriptUtilsTest tests {@link ScriptUtils}
29   *
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public class ScriptUtilsTest extends ProcessLoggingUnitTest {
33  
34      @Test
35      /**
36       * tests {@link ScriptUtils#escapeHtml(String)}
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       * tests {@link ScriptUtils#escapeHtml(java.util.List)}
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       * tests {@link ScriptUtils#convertToJsValue(String)} for a function value
62       */
63      public void testConvertToJSValue_function() {
64          // test for white space
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       * tests {@link ScriptUtils#convertToJsValue(String)} for numeric values
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       * tests {@link ScriptUtils#convertToJsValue(String)} for  maps and arrays
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       * Test building of event script matches the expected JavaScript
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 }