Author: nbelaevski
Date: 2009-06-03 12:15:49 -0400 (Wed, 03 Jun 2009)
New Revision: 14460
Added:
branches/community/3.3.X/framework/impl/src/test/java/org/ajax4jsf/renderkit/
branches/community/3.3.X/framework/impl/src/test/java/org/ajax4jsf/renderkit/RendererUtilsTest.java
Modified:
branches/community/3.3.X/framework/impl/src/main/java/org/ajax4jsf/renderkit/RendererUtils.java
Log:
https://jira.jboss.org/jira/browse/RF-7247
Modified:
branches/community/3.3.X/framework/impl/src/main/java/org/ajax4jsf/renderkit/RendererUtils.java
===================================================================
---
branches/community/3.3.X/framework/impl/src/main/java/org/ajax4jsf/renderkit/RendererUtils.java 2009-06-03
16:06:29 UTC (rev 14459)
+++
branches/community/3.3.X/framework/impl/src/main/java/org/ajax4jsf/renderkit/RendererUtils.java 2009-06-03
16:15:49 UTC (rev 14460)
@@ -23,7 +23,9 @@
import java.awt.Dimension;
import java.io.IOException;
+import java.lang.reflect.Array;
import java.util.Arrays;
+import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@@ -539,6 +541,93 @@
}
/**
+ * Checks if the argument passed in is empty or not.
+ * Object is empty if it is: <br />
+ * - <code>null<code><br />
+ * - zero-length string<br />
+ * - empty collection<br />
+ * - empty map<br />
+ * - zero-length array<br />
+ *
+ * @param o object to check for emptiness
+ * @since 3.3.2
+ * @return <code>true</code> if the argument is empty,
<code>false</code> otherwise
+ */
+ public boolean isEmpty(Object o) {
+ if (null == o) {
+ return true;
+ }
+ if (o instanceof String ) {
+ return (0 == ((String)o).length());
+ }
+ if (o instanceof Collection) {
+ return ((Collection<?>)o).isEmpty();
+ }
+ if (o instanceof Map) {
+ return ((Map<?, ?>)o).isEmpty();
+ }
+ if (o.getClass().isArray()) {
+ return Array.getLength(o) == 0;
+ }
+ return false;
+ }
+
+ public static enum ScriptHashVariableWrapper {
+ DEFAULT {
+
+ @Override
+ Object wrap(Object o) {
+ return o;
+ }
+
+ },
+
+ EVENT_HANDLER {
+
+ @Override
+ Object wrap(Object o) {
+ return new JSFunctionDefinition("event").addToBody(o);
+ }
+
+ };
+
+ abstract Object wrap(Object o);
+ }
+
+ /**
+ * Puts value into map under specified key if the value is not empty and not default.
+ * Performs optional value wrapping.
+ *
+ * @param hash
+ * @param name
+ * @param value
+ * @param defaultValue
+ * @param wrapper
+ *
+ * @since 3.3.2
+ */
+ public void addToScriptHash(Map<String, Object> hash,
+ String name,
+ Object value,
+ String defaultValue,
+ ScriptHashVariableWrapper wrapper) {
+
+ ScriptHashVariableWrapper wrapperOrDefault = wrapper != null ? wrapper :
ScriptHashVariableWrapper.DEFAULT;
+
+ if (isValidProperty(value) && !isEmpty(value)) {
+ if (!isEmpty(defaultValue)) {
+ if (!defaultValue.equals(value.toString())) {
+ hash.put(name, wrapperOrDefault.wrap(value));
+ }
+ } else {
+ if (!(value instanceof Boolean) || ((Boolean) value).booleanValue()) {
+ hash.put(name, wrapperOrDefault.wrap(value));
+ }
+ }
+ }
+ }
+
+ /**
* Convert HTML attribute name to component property name.
*
* @param key
Added:
branches/community/3.3.X/framework/impl/src/test/java/org/ajax4jsf/renderkit/RendererUtilsTest.java
===================================================================
---
branches/community/3.3.X/framework/impl/src/test/java/org/ajax4jsf/renderkit/RendererUtilsTest.java
(rev 0)
+++
branches/community/3.3.X/framework/impl/src/test/java/org/ajax4jsf/renderkit/RendererUtilsTest.java 2009-06-03
16:15:49 UTC (rev 14460)
@@ -0,0 +1,166 @@
+/**
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.ajax4jsf.renderkit;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.ajax4jsf.javascript.JSFunctionDefinition;
+import org.ajax4jsf.renderkit.RendererUtils.ScriptHashVariableWrapper;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Nick Belaevski
+ * @since 3.3.2
+ */
+public class RendererUtilsTest extends TestCase {
+
+ public void testIsEmpty() throws Exception {
+ RendererUtils utils = RendererUtils.getInstance();
+
+ assertTrue(utils.isEmpty(null));
+ assertFalse(utils.isEmpty(new Object()));
+ assertFalse(utils.isEmpty(Boolean.FALSE));
+ assertFalse(utils.isEmpty(Long.valueOf(0)));
+ assertFalse(utils.isEmpty(Integer.valueOf(0)));
+ assertFalse(utils.isEmpty(Short.valueOf((short) 0)));
+ assertFalse(utils.isEmpty(Byte.valueOf((byte) 0)));
+
+ assertTrue(utils.isEmpty(""));
+ assertFalse(utils.isEmpty("s"));
+
+ assertTrue(utils.isEmpty(new ArrayList<Object>()));
+ assertTrue(utils.isEmpty(Collections.EMPTY_LIST));
+
+ List<Object> testList = new ArrayList<Object>();
+ testList.add("x");
+ assertFalse(utils.isEmpty(testList));
+
+ assertTrue(utils.isEmpty(new HashMap<String, Object>()));
+ assertTrue(utils.isEmpty(Collections.EMPTY_MAP));
+
+ Map<String, Object> testMap = new HashMap<String, Object>();
+ testMap.put("x", "y");
+ assertFalse(utils.isEmpty(testMap));
+
+ assertTrue(utils.isEmpty(new Object[0]));
+ assertTrue(utils.isEmpty(new int[0]));
+
+ assertFalse(utils.isEmpty(new Object[1]));
+ assertFalse(utils.isEmpty(new int[1]));
+ }
+
+ public void testScriptHashVariableWrapper() throws Exception {
+ assertEquals("abc",
RendererUtils.ScriptHashVariableWrapper.DEFAULT.wrap("abc"));
+
+ Object eventHandler =
RendererUtils.ScriptHashVariableWrapper.EVENT_HANDLER.wrap("abc");
+ assertTrue(eventHandler instanceof JSFunctionDefinition);
+
+ JSFunctionDefinition handlerFunction = (JSFunctionDefinition) eventHandler;
+ assertEquals("function(event){abc}",
handlerFunction.toScript().replaceAll("\\s", ""));
+ }
+
+ public void testAddToScriptHash() throws Exception {
+ Map<String,Object> hash = new HashMap<String, Object>();
+ RendererUtils utils = RendererUtils.getInstance();
+
+ utils.addToScriptHash(hash, "x", "y", null, null);
+ assertEquals("y", hash.get("x"));
+
+ utils.addToScriptHash(hash, "y", "", null, null);
+ assertNull(hash.get("y"));
+ assertFalse(hash.containsKey("y"));
+
+ utils.addToScriptHash(hash, "y1", null, null, null);
+ assertNull(hash.get("y1"));
+ assertFalse(hash.containsKey("y1"));
+
+ utils.addToScriptHash(hash, "st", "server", "", null);
+ assertEquals("server", hash.get("st"));
+
+ utils.addToScriptHash(hash, "st1", "ajax", "ajax",
null);
+ assertNull(hash.get("st1"));
+ assertFalse(hash.containsKey("st1"));
+
+ utils.addToScriptHash(hash, "st2", "", "ajax", null);
+ assertNull(hash.get("st2"));
+ assertFalse(hash.containsKey("st2"));
+
+ utils.addToScriptHash(hash, "null", null, "server", null);
+ assertNull(hash.get("null"));
+ assertFalse(hash.containsKey("null"));
+
+ utils.addToScriptHash(hash, "b", false, null, null);
+ assertNull(hash.get("b"));
+ assertFalse(hash.containsKey("b"));
+
+ utils.addToScriptHash(hash, "b1", true, null, null);
+ assertEquals(Boolean.TRUE, hash.get("b1"));
+
+ utils.addToScriptHash(hash, "b2", true, "true", null);
+ assertNull(hash.get("b2"));
+ assertFalse(hash.containsKey("b2"));
+
+ utils.addToScriptHash(hash, "b3", false, "true", null);
+ assertEquals(Boolean.FALSE, hash.get("b3"));
+
+ utils.addToScriptHash(hash, "b4", true, "false", null);
+ assertEquals(Boolean.TRUE, hash.get("b4"));
+
+ utils.addToScriptHash(hash, "b5", false, "false", null);
+ assertNull(hash.get("b5"));
+ assertFalse(hash.containsKey("b5"));
+
+ utils.addToScriptHash(hash, "i", Integer.valueOf(0), null, null);
+ assertEquals(Integer.valueOf(0), hash.get("i"));
+
+ utils.addToScriptHash(hash, "i1", Integer.valueOf(0), "0", null);
+ assertNull(hash.get("i1"));
+ assertFalse(hash.containsKey("i1"));
+
+ utils.addToScriptHash(hash, "i2", Integer.valueOf(0), "1", null);
+ assertEquals(Integer.valueOf(0), hash.get("i2"));
+
+ utils.addToScriptHash(hash, "i3", Integer.MIN_VALUE, null, null);
+ assertNull(hash.get("i3"));
+ assertFalse(hash.containsKey("i3"));
+
+ utils.addToScriptHash(hash, "i4", Integer.MIN_VALUE, "0", null);
+ assertNull(hash.get("i4"));
+ assertFalse(hash.containsKey("i4"));
+
+ utils.addToScriptHash(hash, "plain", "test", null,
ScriptHashVariableWrapper.DEFAULT);
+ assertEquals("test", hash.get("plain"));
+
+ utils.addToScriptHash(hash, "plain1", "newtest", "blank",
ScriptHashVariableWrapper.DEFAULT);
+ assertEquals("newtest", hash.get("plain1"));
+
+ utils.addToScriptHash(hash, "onclick", "alert(1)", null,
ScriptHashVariableWrapper.EVENT_HANDLER);
+ assertTrue(hash.get("onclick") instanceof JSFunctionDefinition);
+
+ utils.addToScriptHash(hash, "onclick1", "alert(1)",
"no-val", ScriptHashVariableWrapper.EVENT_HANDLER);
+ assertTrue(hash.get("onclick1") instanceof JSFunctionDefinition);
+ }
+}