001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krad.uif.util; 017 018import org.junit.Test; 019 020import java.util.Arrays; 021import java.util.Collections; 022import java.util.List; 023 024import static org.junit.Assert.assertEquals; 025import static org.junit.Assert.assertNull; 026 027/** 028 * ScriptUtilsTest tests {@link ScriptUtils} 029 * 030 * @author Kuali Rice Team (rice.collab@kuali.org) 031 */ 032public class ScriptUtilsTest { 033 034 @Test 035 /** 036 * tests {@link ScriptUtils#escapeHtml(String)} 037 */ 038 public void testEscapeHtml() throws Exception { 039 assertEquals("wasn't", ScriptUtils.escapeHtml("wasn't")); 040 assertEquals("\\u0022wasn't\\u0022", ScriptUtils.escapeHtml("\"wasn't\"")); 041 } 042 043 @Test 044 /** 045 * tests {@link ScriptUtils#escapeHtml(java.util.List)} 046 */ 047 public void testEscapeHtmlStringList() { 048 String[] escaped = {"wasn't", "\\u0022wasn't\\u0022"}; 049 String[] unEscaped = {"wasn't", "\"wasn't\""}; 050 assertEquals(Arrays.asList(escaped), ScriptUtils.escapeHtml(Arrays.asList(unEscaped))); 051 052 List<String> nullList = null; 053 assertNull(ScriptUtils.escapeHtml(nullList)); 054 055 List<String> emptyList = Collections.emptyList(); 056 assertEquals(emptyList, ScriptUtils.escapeHtml(emptyList)); 057 } 058 059 @Test 060 /** 061 * tests {@link ScriptUtils#convertToJsValue(String)} for a function value 062 */ 063 public void testConvertToJSValue_function() { 064 // test for white space 065 String jsFunction = "\n function () {alert('1 + 1 ' is 1 + 1);} \n\n"; 066 assertEquals("function was not converted to js value as expected", jsFunction, ScriptUtils.convertToJsValue(jsFunction)); 067 } 068 069 @Test 070 /** 071 * tests {@link ScriptUtils#convertToJsValue(String)} for numeric values 072 */ 073 public void testConvertToJSValue_numeric() { 074 assertEquals("number was not converted to js value as expected", " -1 ", ScriptUtils.convertToJsValue(" -1 ")); 075 assertEquals("number was not converted to js value as expected", "1.01 ", ScriptUtils.convertToJsValue("1.01 ")); 076 assertEquals("string was not converted to js value as expected", "'1.o1 '", ScriptUtils.convertToJsValue("1.o1 ")); 077 } 078 079 @Test 080 /** 081 * tests {@link ScriptUtils#convertToJsValue(String)} for maps and arrays 082 */ 083 public void testConvertToJSValue_mapAndArray() { 084 assertEquals("array was not converted to js value as expected", " [-1, 4, 5] ", ScriptUtils.convertToJsValue(" [-1, 4, 5] ")); 085 String jsMap = " {'a':1, 'b':2} \n"; 086 assertEquals("map was not converted to js value as expected", jsMap, ScriptUtils.convertToJsValue(jsMap)); 087 } 088 089 /** 090 * Test building of event script matches the expected JavaScript 091 */ 092 @Test 093 public void testBuildEventHandlerScript() { 094 String onClickScript = "alert('A click happened');"; 095 String onClickHandler = ScriptUtils.buildEventHandlerScript("u09", "click", onClickScript); 096 097 String expectedHandler = "jQuery('#u09').on('click', function(e) {" + onClickScript + "}); "; 098 099 assertEquals("generate event script is not correct", expectedHandler, onClickHandler); 100 } 101 102}