View Javadoc

1   /**
2    * Copyright 2005-2014 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.assertArrayEquals;
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertNull;
27  
28  /**
29   *  ScriptUtilsTest tests {@link ScriptUtils}
30   *
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class ScriptUtilsTest {
34  
35      @Test
36      /**
37       * tests {@link ScriptUtils#escapeHtml(String)}
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       * tests {@link ScriptUtils#escapeHtml(java.util.List)}
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       * tests {@link ScriptUtils#convertToJsValue(String)} for a function value
63       */
64      public void testConvertToJSValue_function() {
65          // test for white space
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       * tests {@link ScriptUtils#convertToJsValue(String)} for numeric values
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       * tests {@link ScriptUtils#convertToJsValue(String)} for  maps and arrays
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  }