Author: nbelaevski
Date: 2010-10-27 10:33:43 -0400 (Wed, 27 Oct 2010)
New Revision: 19685
Added:
branches/RF-7817/ui/common/ui/richfaces-suppressions.xml
Modified:
branches/RF-7817/ui/common/api/pom.xml
branches/RF-7817/ui/common/ui/pom.xml
branches/RF-7817/ui/common/ui/src/main/java/org/richfaces/renderkit/RenderKitUtils.java
branches/RF-7817/ui/common/ui/src/main/java/org/richfaces/renderkit/util/RendererUtils.java
branches/RF-7817/ui/common/ui/src/test/java/org/richfaces/renderkit/RenderKitUtilsMocksTest.java
branches/RF-7817/ui/common/ui/src/test/java/org/richfaces/renderkit/RenderKitUtilsTest.java
branches/RF-7817/ui/common/ui/src/test/java/org/richfaces/renderkit/util/RendererUtilsTest.java
Log:
Merged revisions
19156-19157,19159-19160,19176,19186-19187,19190,19194,19196,19199,19203-19204,19231-19234,19248-19249,19255-19256,19258-19262,19276,19279-19281,19283,19285,19291-19299,19307,19312,19315,19318-19319,19325,19341-19342,19345-19351,19353-19355,19358-19359,19364,19367-19369,19371,19374-19377,19379-19385,19387,19393-19394,19400,19402-19404,19418-19422,19426,19430-19431,19434-19441,19443-19444,19446,19468,19470,19477,19491,19498-19499,19501,19504,19506-19507,19517-19519,19525,19542-19543,19548,19550,19555,19557,19560-19561,19565-19566,19571-19573,19582-19585,19592-19593,19599,19601-19602,19613,19646,19657-19659,19665,19671-19674
via svnmerge from
https://svn.jboss.org/repos/richfaces/trunk
Modified: branches/RF-7817/ui/common/api/pom.xml
===================================================================
--- branches/RF-7817/ui/common/api/pom.xml 2010-10-27 14:23:10 UTC (rev 19684)
+++ branches/RF-7817/ui/common/api/pom.xml 2010-10-27 14:33:43 UTC (rev 19685)
@@ -33,4 +33,13 @@
<artifactId>richfaces-ui-common-api</artifactId>
<name>Richfaces UI Components: Common API</name>
<packaging>jar</packaging>
+
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-checkstyle-plugin</artifactId>
+ </plugin>
+ </plugins>
+ </build>
+
</project>
\ No newline at end of file
Modified: branches/RF-7817/ui/common/ui/pom.xml
===================================================================
--- branches/RF-7817/ui/common/ui/pom.xml 2010-10-27 14:23:10 UTC (rev 19684)
+++ branches/RF-7817/ui/common/ui/pom.xml 2010-10-27 14:33:43 UTC (rev 19685)
@@ -55,7 +55,7 @@
<!-- Checkstyle only required here because suppressions needed -->
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
-
<suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
+
<suppressionsLocation>${project.basedir}/richfaces-suppressions.xml</suppressionsLocation>
</configuration>
</plugin>
</plugins>
Copied: branches/RF-7817/ui/common/ui/richfaces-suppressions.xml (from rev 19674,
trunk/ui/common/ui/richfaces-suppressions.xml)
===================================================================
--- branches/RF-7817/ui/common/ui/richfaces-suppressions.xml (rev
0)
+++ branches/RF-7817/ui/common/ui/richfaces-suppressions.xml 2010-10-27 14:33:43 UTC (rev
19685)
@@ -0,0 +1,11 @@
+<?xml version="1.0"?>
+
+<!DOCTYPE suppressions PUBLIC
+ "-//Puppy Crawl//DTD Suppressions 1.0//EN"
+ "http://www.puppycrawl.com/dtds/suppressions_1_0.dtd">
+
+<suppressions>
+ <suppress checks="MissingSwitchDefault"
files="JSONTokener.java" />
+ <suppress checks="FallThrough" files="XMLTokener.java" />
+ <suppress checks="ModifiedControlVariable" files="Cookie.java"
/>
+</suppressions>
Modified:
branches/RF-7817/ui/common/ui/src/main/java/org/richfaces/renderkit/RenderKitUtils.java
===================================================================
---
branches/RF-7817/ui/common/ui/src/main/java/org/richfaces/renderkit/RenderKitUtils.java 2010-10-27
14:23:10 UTC (rev 19684)
+++
branches/RF-7817/ui/common/ui/src/main/java/org/richfaces/renderkit/RenderKitUtils.java 2010-10-27
14:33:43 UTC (rev 19685)
@@ -22,10 +22,12 @@
package org.richfaces.renderkit;
import java.io.IOException;
+import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -41,6 +43,8 @@
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
+import org.ajax4jsf.javascript.JSFunctionDefinition;
+import org.ajax4jsf.javascript.ScriptUtils;
import org.richfaces.renderkit.ComponentAttribute.Kind;
/**
@@ -82,10 +86,115 @@
private static final String DISABLED_ATTRIBUTE_NAME = "disabled";
+ /**
+ * Wrapper class around object value used to transform values into particular JS
objects
+ *
+ * @author Nick Belaevski
+ * @since 3.3.2
+ */
+ public enum ScriptHashVariableWrapper {
+
+ /**
+ * No-op default wrapper
+ */
+ noop {
+
+ @Override
+ Object wrap(Object o) {
+ return o;
+ }
+
+ },
+
+
+ /**
+ * Convert parameter to array of srings.
+ */
+ asArray {
+
+ @Override
+ Object wrap(Object o) {
+ return asArray(o);
+ }
+
+ },
+
+ /**
+ * Event handler functions wrapper. Wraps <pre>functionCode</pre>
object into:
+ * <pre>function(event) {
+ * functionCode
+ * }</pre>
+ */
+ eventHandler {
+
+ @Override
+ Object wrap(Object o) {
+ return new JSFunctionDefinition("event").addToBody(o);
+ }
+
+ };
+
+ /**
+ * Method that does the wrapping
+ *
+ * @param o object to wrap
+ * @return wrapped object
+ */
+ abstract Object wrap(Object o);
+ }
+
private RenderKitUtils() {
// utility constructor
}
+ static String[] asArray(Object object) {
+ if (object == null) {
+ return null;
+ }
+
+ Class<?> componentType = object.getClass().getComponentType();
+
+ if (String.class.equals(componentType)) {
+ return (String[]) object;
+ } else if (componentType != null) {
+ Object[] objects = (Object[]) object;
+ String[] result = new String[objects.length];
+
+ for (int i = 0; i < objects.length; i++) {
+ Object o = objects[i];
+
+ if (o == null) {
+ continue;
+ }
+
+ result[i] = o.toString();
+ }
+
+ return result;
+ } else if (object instanceof Collection) {
+ Collection<?> collection = (Collection<?>) object;
+ String[] result = new String[collection.size()];
+ Iterator<?> iterator = collection.iterator();
+
+ for (int i = 0; i < result.length; i++) {
+ Object next = iterator.next();
+
+ if (next == null) {
+ continue;
+ }
+
+ result[i] = next.toString();
+ }
+
+ return result;
+ } else {
+ String string = object.toString().trim();
+ String[] split = string.split("\\s*,\\s*");
+
+ return split;
+ }
+ }
+
private static Map<String, List<ClientBehavior>>
getClientBehaviorsMap(UIComponent component) {
Map<String, List<ClientBehavior>> result;
if (component instanceof ClientBehaviorHolder) {
@@ -188,20 +297,19 @@
}
if (isChained) {
- result.insert(0, "jsf.util.chain(");
+ result.insert(0, "jsf.util.chain(this, event, ");
result.append(")");
}
return result.toString();
}
- public static boolean shouldRenderAttribute(Object attributeValue) {
+ private static boolean isAttributeSet(Object attributeValue) {
+ //TODO - consider required attributes with "" value (like
'alt')
if (attributeValue == null) {
return false;
} else if (attributeValue instanceof String) {
return ((String) attributeValue).length() > 0;
- } else if (attributeValue instanceof Boolean &&
Boolean.FALSE.equals(attributeValue)) {
- return false;
} else if (attributeValue instanceof Integer && (Integer) attributeValue
== Integer.MIN_VALUE) {
return false;
} else if (attributeValue instanceof Double && (Double) attributeValue ==
Double.MIN_VALUE) {
@@ -220,7 +328,20 @@
return attributeValue.toString().length() > 0;
}
+
+ public static boolean shouldRenderAttribute(Object attributeValue) {
+ //TODO - consider required attributes with "" value (like
'alt')
+ if (!isAttributeSet(attributeValue)) {
+ return false;
+ }
+
+ if (attributeValue instanceof Boolean &&
Boolean.FALSE.equals(attributeValue)) {
+ return false;
+ }
+ return attributeValue.toString().length() > 0;
+ }
+
public static String prefixAttributeName(String attributeName, boolean isXhtmlMode)
{
if (isXhtmlMode) {
if (Arrays.binarySearch(XHTML_ATTRIBUTE_NAMES, attributeName) >= 0) {
@@ -422,6 +543,148 @@
return res;
}
+ /**
+ * 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
+ */
+ private static 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 void addToScriptHash(Map<String, Object> hash,
+ String name,
+ Object value) {
+
+ addToScriptHash(hash, name, value, null, null);
+ }
+
+ public static void addToScriptHash(Map<String, Object> hash,
+ String name,
+ Object value,
+ Object defaultValue) {
+
+ addToScriptHash(hash, name, value, defaultValue, null);
+ }
+
+ /**
+ * 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 static void addToScriptHash(Map<String, Object> hash,
+ String name,
+ Object value,
+ Object defaultValue,
+ ScriptHashVariableWrapper wrapper) {
+
+ ScriptHashVariableWrapper wrapperOrDefault = wrapper != null ? wrapper :
ScriptHashVariableWrapper.noop;
+
+ if (!isEmpty(value) && isAttributeSet(value)) {
+ if (defaultValue != null) {
+ if (!String.valueOf(defaultValue).equals(value.toString())) {
+ hash.put(name, wrapperOrDefault.wrap(value));
+ }
+ } else {
+ if (!(value instanceof Boolean) || ((Boolean) value).booleanValue()) {
+ hash.put(name, wrapperOrDefault.wrap(value));
+ }
+ }
+ }
+ }
+
+ public static void addToScriptHash(Map<String, Object> hash, FacesContext
facesContext, UIComponent component,
+ Attributes attributes, ScriptHashVariableWrapper wrapper) {
+
+ boolean disabled = isDisabled(component);
+ for (ComponentAttribute knownAttribute : attributes) {
+ if (!disabled || knownAttribute.getEventNames().length == 0) {
+ String attributeName = knownAttribute.getComponentAttributeName();
+ addToScriptHash(hash, attributeName,
+ getAttributeAndBehaviorsValue(facesContext, component,
knownAttribute),
+ knownAttribute.getDefaultValue(), wrapper);
+ }
+ }
+ }
+
+ public static String concat(String... strings) {
+ if (strings == null) {
+ return "";
+ }
+
+ StringBuilder sb = new StringBuilder();
+ for (String s : strings) {
+ if (s == null || s.length() == 0) {
+ continue;
+ }
+
+ sb.append(s);
+ }
+ return sb.toString();
+ }
+
+ public static String toScriptArgs(Object... objects) {
+ if (objects == null) {
+ return "";
+ }
+
+ int lastNonNullIdx = objects.length - 1;
+ for (; 0 <= lastNonNullIdx; lastNonNullIdx--) {
+ if (!isEmpty(objects[lastNonNullIdx])) {
+ break;
+ }
+ }
+
+ if (lastNonNullIdx < 0) {
+ return "";
+ }
+
+ if (lastNonNullIdx == 0) {
+ return ScriptUtils.toScript(objects[lastNonNullIdx]);
+ }
+
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i <= lastNonNullIdx; i++) {
+ if (sb.length() > 0) {
+ sb.append(",");
+ }
+
+ sb.append(ScriptUtils.toScript(objects[i]));
+ }
+
+ return sb.toString();
+ }
+
@SuppressWarnings("serial")
public static final class Attributes extends TreeSet<ComponentAttribute> {
Modified:
branches/RF-7817/ui/common/ui/src/main/java/org/richfaces/renderkit/util/RendererUtils.java
===================================================================
---
branches/RF-7817/ui/common/ui/src/main/java/org/richfaces/renderkit/util/RendererUtils.java 2010-10-27
14:23:10 UTC (rev 19684)
+++
branches/RF-7817/ui/common/ui/src/main/java/org/richfaces/renderkit/util/RendererUtils.java 2010-10-27
14:33:43 UTC (rev 19685)
@@ -28,11 +28,9 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
-import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
-import java.util.Set;
import javax.faces.FacesException;
import javax.faces.application.ViewHandler;
@@ -51,10 +49,9 @@
import org.ajax4jsf.Messages;
import org.ajax4jsf.component.JavaScriptParameter;
-import org.ajax4jsf.javascript.JSEncoder;
-import org.ajax4jsf.javascript.JSFunctionDefinition;
import org.ajax4jsf.javascript.JSReference;
import org.richfaces.renderkit.HtmlConstants;
+import org.richfaces.renderkit.RenderKitUtils;
/**
* Util class for common render operations - render passthru html attributes,
@@ -76,12 +73,8 @@
*/
private static final Map<String, String> SUBSTITUTIONS = new HashMap<String,
String>();
- private static final Set<String> REQUIRED_ATTRIBUTES = new
HashSet<String>();
-
static {
SUBSTITUTIONS.put(HtmlConstants.CLASS_ATTRIBUTE, "styleClass");
-
- REQUIRED_ATTRIBUTES.add(HtmlConstants.ALT_ATTRIBUTE);
Arrays.sort(HtmlConstants.PASS_THRU);
Arrays.sort(HtmlConstants.PASS_THRU_EVENTS);
@@ -96,46 +89,6 @@
}
/**
- * Wrapper class around object value used to transform values into particular JS
objects
- *
- * @author Nick Belaevski
- * @since 3.3.2
- */
- public static enum ScriptHashVariableWrapper {
-
- /**
- * No-op default wrapper
- */
- DEFAULT {
- @Override
- Object wrap(Object o) {
- return o;
- }
- },
-
- /**
- * Event handler functions wrapper. Wraps <pre>functionCode</pre>
object into:
- * <pre>function(event) {
- * functionCode
- * }</pre>
- */
- EVENT_HANDLER {
- @Override
- Object wrap(Object o) {
- return new JSFunctionDefinition("event").addToBody(o);
- }
- };
-
- /**
- * Method that does the wrapping
- *
- * @param o object to wrap
- * @return wrapped object
- */
- abstract Object wrap(Object o);
- }
-
- /**
* Use this method to get singleton instance of RendererUtils
* @return singleton instance
*/
@@ -360,7 +313,7 @@
Object value = attributeValue(attribute,
attributes.get(getComponentAttributeName(attribute)));
- if ((null != value) && shouldRenderAttribute(attribute, value)) {
+ if ((null != value) && RenderKitUtils.shouldRenderAttribute(value)) {
if (Arrays.binarySearch(HtmlConstants.PASS_THRU_URI, attribute) >= 0) {
String url =
context.getApplication().getViewHandler().getResourceURL(context, value.toString());
@@ -418,7 +371,7 @@
ResponseWriter writer = context.getResponseWriter();
Object value = component.getAttributes().get(property);
- if (shouldRenderAttribute(attributeName, value)) {
+ if (RenderKitUtils.shouldRenderAttribute(value)) {
writer.writeAttribute(attributeName, value, property.toString());
}
}
@@ -428,81 +381,6 @@
}
/**
- * Write html-attribute
- *
- * @param writer
- * @param attribute
- * @param value
- * @throws IOException
- */
- public void writeAttribute(ResponseWriter writer, String attribute, Object value)
throws IOException {
- if (shouldRenderAttribute(attribute, value)) {
- writer.writeAttribute(attribute, value.toString(), attribute);
- }
- }
-
- /**
- * @return true if and only if the argument <code>attributeVal</code> is
- * an instance of a wrapper for a primitive type and its value is
- * equal to the default value for that type as given in the spec.
- */
- public boolean shouldRenderAttribute(Object attributeVal) {
- if (null == attributeVal) {
- return false;
- } else if ((attributeVal instanceof Boolean)
- && ((Boolean) attributeVal).booleanValue() ==
Boolean.FALSE.booleanValue()) {
- return false;
- } else if (attributeVal.toString().length() == 0) {
- return false;
- } else {
- return isValidProperty(attributeVal);
- }
- }
-
- public boolean shouldRenderAttribute(String attributeName, Object attributeVal) {
- if (REQUIRED_ATTRIBUTES.contains(attributeName)) {
- if (attributeVal == null) {
- return false;
- }
- } else {
- return shouldRenderAttribute(attributeVal);
- }
-
- return true;
- }
-
- /**
- * Test for valid value of property. by default, for non-setted properties
- * with Java primitive types of JSF component return appropriate MIN_VALUE .
- *
- * @param property -
- * value of property returned from
- * {@link UIComponent#getAttributes()}
- * @return true for setted property, false otherthise.
- */
- public boolean isValidProperty(Object property) {
- if (null == property) {
- return false;
- } else if ((property instanceof Integer) && ((Integer)
property).intValue() == Integer.MIN_VALUE) {
- return false;
- } else if ((property instanceof Double) && ((Double)
property).doubleValue() == Double.MIN_VALUE) {
- return false;
- } else if ((property instanceof Character) && ((Character)
property).charValue() == Character.MIN_VALUE) {
- return false;
- } else if ((property instanceof Float) && ((Float) property).floatValue()
== Float.MIN_VALUE) {
- return false;
- } else if ((property instanceof Short) && ((Short) property).shortValue()
== Short.MIN_VALUE) {
- return false;
- } else if ((property instanceof Byte) && ((Byte) property).byteValue() ==
Byte.MIN_VALUE) {
- return false;
- } else if ((property instanceof Long) && ((Long) property).longValue() ==
Long.MIN_VALUE) {
- return false;
- }
-
- return true;
- }
-
- /**
* Checks if the argument passed in is empty or not.
* Object is empty if it is: <br />
* - <code>null</code><br />
@@ -540,64 +418,6 @@
}
/**
- * 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));
- }
- }
- }
- }
-
- /**
- * 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
- *
- * @since 3.3.2
- */
- public void addToScriptHash(Map<String, Object> hash, String name, Object
value, String defaultValue) {
- addToScriptHash(hash, name, value, defaultValue, null);
- }
-
- /**
- * 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
- *
- * @since 3.3.2
- */
- public void addToScriptHash(Map<String, Object> hash, String name, Object
value) {
- addToScriptHash(hash, name, value, null, null);
- }
-
- /**
* Convert HTML attribute name to component property name.
*
* @param key
@@ -916,83 +736,6 @@
return forAttr;
}
- public static void writeEventHandlerFunction(FacesContext context, UIComponent
component, String eventName)
- throws IOException {
-
- ResponseWriter writer = context.getResponseWriter();
- Object script = component.getAttributes().get(eventName);
-
- if ((script != null) && !script.equals("")) {
- JSFunctionDefinition onEventDefinition = new JSFunctionDefinition();
-
- onEventDefinition.addParameter("event");
- onEventDefinition.addToBody(script);
- writer.writeText(eventName + ": " + onEventDefinition.toScript(),
null);
- } else {
- writer.writeText(eventName + ": ''", null);
- }
- }
-
- public JSFunctionDefinition getAsEventHandler(FacesContext context, UIComponent
component, String attributeName,
- String append) {
- String event = (String) component.getAttributes().get(attributeName);
-
- if (event != null) {
- event = event.trim();
-
- if (event.length() != 0) {
- JSFunctionDefinition function = new JSFunctionDefinition();
-
- function.addParameter("event");
-
- if ((null != append) && (append.length() > 0)) {
- function.addToBody(event + append);
- } else {
- function.addToBody(event);
- }
-
- return function;
- }
- }
-
- return null;
- }
-
- public String escapeJavaScript(Object o) {
- if (o != null) {
- StringBuilder result = new StringBuilder();
- JSEncoder encoder = new JSEncoder();
- char[] chars = o.toString().toCharArray();
- int start = 0;
- int end = chars.length;
-
- for (int x = start; x < end; x++) {
- char c = chars[x];
-
- if (encoder.compile(c)) {
- continue;
- }
-
- if (start != x) {
- result.append(chars, start, x - start);
- }
-
- result.append(encoder.encode(c));
- start = x + 1;
-
- continue;
- }
-
- if (start != end) {
- result.append(chars, start, end - start);
- }
-
- return result.toString();
- } else {
- return null;
- }
- }
-
public void encodeChildren(FacesContext context, UIComponent component) throws
IOException {
if (component.getChildCount() > 0) {
for (UIComponent child : component.getChildren()) {
Modified:
branches/RF-7817/ui/common/ui/src/test/java/org/richfaces/renderkit/RenderKitUtilsMocksTest.java
===================================================================
---
branches/RF-7817/ui/common/ui/src/test/java/org/richfaces/renderkit/RenderKitUtilsMocksTest.java 2010-10-27
14:23:10 UTC (rev 19684)
+++
branches/RF-7817/ui/common/ui/src/test/java/org/richfaces/renderkit/RenderKitUtilsMocksTest.java 2010-10-27
14:33:43 UTC (rev 19685)
@@ -199,10 +199,10 @@
ClientBehaviorHolder behaviorHolder = createMockClientBehaviorHolder();
UIComponent component = (UIComponent) behaviorHolder;
- responseWriter.writeAttribute(eq("onkeypress"),
eq("jsf.util.chain('alert(keypress)','prompt(keypress)')"),
+ responseWriter.writeAttribute(eq("onkeypress"),
eq("jsf.util.chain(this, event,
'alert(keypress)','prompt(keypress)')"),
EasyMock.<String>isNull());
responseWriter.writeAttribute(eq("onclick"),
-
eq("jsf.util.chain('alert(click)','prompt(action1)','prompt(action2)')"),
EasyMock.<String>isNull());
+ eq("jsf.util.chain(this, event,
'alert(click)','prompt(action1)','prompt(action2)')"),
EasyMock.<String>isNull());
responseWriter.writeAttribute(eq("onmousemove"),
eq("alert(mousemove)"), EasyMock.<String>isNull());
responseWriter.writeAttribute(eq("oncontextmenu"),
eq("prompt(contextmenu)"), EasyMock.<String>isNull());
@@ -249,7 +249,7 @@
componentAttributes.put("disabled", Boolean.FALSE);
UIComponent component = setupBehaviorsTestForDisabledComponent();
- responseWriter.writeAttribute(eq("onclick"),
eq("jsf.util.chain('alert(click)','prompt(action1)')"),
+ responseWriter.writeAttribute(eq("onclick"),
eq("jsf.util.chain(this, event,
'alert(click)','prompt(action1)')"),
EasyMock.<String>isNull());
responseWriter.writeAttribute(eq("onmousemove"),
eq("alert(mousemove)"), EasyMock.<String>isNull());
Modified:
branches/RF-7817/ui/common/ui/src/test/java/org/richfaces/renderkit/RenderKitUtilsTest.java
===================================================================
---
branches/RF-7817/ui/common/ui/src/test/java/org/richfaces/renderkit/RenderKitUtilsTest.java 2010-10-27
14:23:10 UTC (rev 19684)
+++
branches/RF-7817/ui/common/ui/src/test/java/org/richfaces/renderkit/RenderKitUtilsTest.java 2010-10-27
14:33:43 UTC (rev 19685)
@@ -23,9 +23,25 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
+import static org.richfaces.renderkit.RenderKitUtils.addToScriptHash;
+import static org.richfaces.renderkit.RenderKitUtils.concat;
+import static org.richfaces.renderkit.RenderKitUtils.toScriptArgs;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.ajax4jsf.javascript.JSFunctionDefinition;
+import org.ajax4jsf.javascript.ScriptUtils;
import org.junit.Test;
+import org.richfaces.renderkit.RenderKitUtils.ScriptHashVariableWrapper;
/**
* @author Nick Belaevski
@@ -107,4 +123,180 @@
assertFalse(RenderKitUtils.shouldRenderAttribute(Boolean.FALSE));
}
-}
\ No newline at end of file
+ @Test
+ public void testConcat() throws Exception {
+ assertEquals("", concat());
+ assertEquals("", concat((String) null));
+ assertEquals("", concat((String[]) null));
+ assertEquals("", concat(""));
+ assertEquals("test", concat("test"));
+ assertEquals("build a string", concat("build", " a
", "string"));
+ assertEquals("omit nulls", concat(null, "omit", null, "
", null, "nulls", null));
+ }
+
+ private static String dehydrate(String s) {
+ return s.replaceAll("\\s+", "");
+ }
+
+ @Test
+ public void testToScriptArgs() throws Exception {
+ assertEquals("", toScriptArgs());
+ assertEquals("", toScriptArgs((Object) null));
+ assertEquals("", toScriptArgs((Object[]) null));
+
+ assertEquals("\"test\"", toScriptArgs("test"));
+ assertEquals("[5,8]", dehydrate(toScriptArgs(Arrays.asList(5, 8))));
+ assertEquals("{\"a\":true}",
dehydrate(toScriptArgs(Collections.singletonMap("a", true))));
+
+ assertEquals("\"test\"", toScriptArgs("test",
null));
+ assertEquals("null,\"test\"", toScriptArgs(null,
"test"));
+
+ assertEquals("\"test\"", toScriptArgs("test",
Collections.emptyList()));
+ assertEquals("[],\"test\"",
dehydrate(toScriptArgs(Collections.emptyList(), "test")));
+
+ assertEquals("\"test\"", toScriptArgs("test",
Collections.emptyMap()));
+ assertEquals("{},\"test\"",
dehydrate(toScriptArgs(Collections.emptyMap(), "test")));
+
+ assertEquals("\"test\"", toScriptArgs("test",
""));
+ assertEquals("\"\",\"test\"",
dehydrate(toScriptArgs("", "test")));
+
+ assertEquals("1,2,3", toScriptArgs(1, 2, 3, null));
+ assertEquals("1,2,null,3", toScriptArgs(1, 2, null, 3));
+ }
+
+ @Test
+ public void testScriptHashVariableWrapper() throws Exception {
+ assertEquals("abc",
ScriptHashVariableWrapper.noop.wrap("abc"));
+
+ Object eventHandler =
ScriptHashVariableWrapper.eventHandler.wrap("abc");
+
+ assertTrue(eventHandler instanceof JSFunctionDefinition);
+
+ JSFunctionDefinition handlerFunction = (JSFunctionDefinition) eventHandler;
+ assertEquals("function(event){abc}",
dehydrate(handlerFunction.toScript()));
+
+ Object arrayObject = ScriptHashVariableWrapper.asArray.wrap("header,
footer");
+ assertEquals("[\"header\",\"footer\"]",
dehydrate(ScriptUtils.toScript(arrayObject)));
+ }
+
+ @Test
+ public void testAddToScriptHash() throws Exception {
+ Map<String, Object> hash = new HashMap<String, Object>();
+
+ addToScriptHash(hash, "x", "y", null, null);
+ assertEquals("y", hash.get("x"));
+ addToScriptHash(hash, "y", "", null, null);
+ assertNull(hash.get("y"));
+ assertFalse(hash.containsKey("y"));
+ addToScriptHash(hash, "y1", null, null, null);
+ assertNull(hash.get("y1"));
+ assertFalse(hash.containsKey("y1"));
+ addToScriptHash(hash, "st", "server", "", null);
+ assertEquals("server", hash.get("st"));
+ addToScriptHash(hash, "st1", "ajax", "ajax",
null);
+ assertNull(hash.get("st1"));
+ assertFalse(hash.containsKey("st1"));
+ addToScriptHash(hash, "st2", "", "ajax", null);
+ assertNull(hash.get("st2"));
+ assertFalse(hash.containsKey("st2"));
+ addToScriptHash(hash, "null", null, "server", null);
+ assertNull(hash.get("null"));
+ assertFalse(hash.containsKey("null"));
+ addToScriptHash(hash, "b", false, null, null);
+ assertNull(hash.get("b"));
+ assertFalse(hash.containsKey("b"));
+ addToScriptHash(hash, "b1", true, null, null);
+ assertEquals(Boolean.TRUE, hash.get("b1"));
+ addToScriptHash(hash, "b2", true, "true", null);
+ assertNull(hash.get("b2"));
+ assertFalse(hash.containsKey("b2"));
+ addToScriptHash(hash, "b3", false, "true", null);
+ assertEquals(Boolean.FALSE, hash.get("b3"));
+ addToScriptHash(hash, "b4", true, "false", null);
+ assertEquals(Boolean.TRUE, hash.get("b4"));
+ addToScriptHash(hash, "b5", false, "false", null);
+ assertNull(hash.get("b5"));
+ assertFalse(hash.containsKey("b5"));
+ addToScriptHash(hash, "i", Integer.valueOf(0), null, null);
+ assertEquals(Integer.valueOf(0), hash.get("i"));
+ addToScriptHash(hash, "i1", Integer.valueOf(0), "0", null);
+ assertNull(hash.get("i1"));
+ assertFalse(hash.containsKey("i1"));
+ addToScriptHash(hash, "i2", Integer.valueOf(0), "1", null);
+ assertEquals(Integer.valueOf(0), hash.get("i2"));
+ addToScriptHash(hash, "i3", Integer.MIN_VALUE, null, null);
+ assertNull(hash.get("i3"));
+ assertFalse(hash.containsKey("i3"));
+ addToScriptHash(hash, "i4", Integer.MIN_VALUE, "0", null);
+ assertNull(hash.get("i4"));
+ assertFalse(hash.containsKey("i4"));
+ addToScriptHash(hash, "plain", "test", null,
ScriptHashVariableWrapper.noop);
+ assertEquals("test", hash.get("plain"));
+ addToScriptHash(hash, "plain1", "newtest", "blank",
ScriptHashVariableWrapper.noop);
+ assertEquals("newtest", hash.get("plain1"));
+ addToScriptHash(hash, "onclick", "alert(1)", null,
ScriptHashVariableWrapper.eventHandler);
+ assertTrue(hash.get("onclick") instanceof JSFunctionDefinition);
+ addToScriptHash(hash, "onclick1", "alert(1)",
"no-val", ScriptHashVariableWrapper.eventHandler);
+ assertTrue(hash.get("onclick1") instanceof JSFunctionDefinition);
+ }
+
+ @Test
+ public void testAsArray() {
+ assertNull(RenderKitUtils.asArray(null));
+ }
+
+ @Test
+ public void testAsArray1() {
+ String[] strings = new String[] {"a", "b"};
+ String[] array = RenderKitUtils.asArray(strings);
+
+ assertSame(strings, array);
+ }
+
+ @Test
+ public void testAsArray2() {
+ Object[] objects = new Object[] {Integer.valueOf(12), null, Integer.valueOf(22),
Integer.valueOf(42)};
+ String[] array = RenderKitUtils.asArray(objects);
+ String[] etalon = new String[] {"12", null, "22",
"42"};
+
+ assertTrue(Arrays.equals(etalon, array));
+ }
+
+ @Test
+ public void testAsArray3() {
+ ArrayList<Integer> list = new ArrayList<Integer>();
+
+ list.add(new Integer(12));
+ list.add(null);
+ list.add(new Integer(22));
+ list.add(new Integer(42));
+
+ String[] array = RenderKitUtils.asArray(list);
+ String[] etalon = new String[] {"12", null, "22",
"42"};
+
+ assertTrue(Arrays.equals(etalon, array));
+ }
+
+ @Test
+ public void testAsArray31() {
+ Set<Integer> set = new TreeSet<Integer>();
+
+ set.add(new Integer(12));
+ set.add(new Integer(22));
+ set.add(new Integer(42));
+
+ String[] array = RenderKitUtils.asArray(set);
+ String[] etalon = new String[] {"12", "22", "42"};
+
+ assertTrue(Arrays.equals(etalon, array));
+ }
+
+ @Test
+ public void testAsArray4() {
+ String string = " a , \t\n b \n , c ";
+ String[] strings = RenderKitUtils.asArray(string);
+ String[] etalon = new String[] {"a", "b", "c"};
+
+ assertTrue(Arrays.equals(etalon, strings));
+ }
+}
Modified:
branches/RF-7817/ui/common/ui/src/test/java/org/richfaces/renderkit/util/RendererUtilsTest.java
===================================================================
---
branches/RF-7817/ui/common/ui/src/test/java/org/richfaces/renderkit/util/RendererUtilsTest.java 2010-10-27
14:23:10 UTC (rev 19684)
+++
branches/RF-7817/ui/common/ui/src/test/java/org/richfaces/renderkit/util/RendererUtilsTest.java 2010-10-27
14:33:43 UTC (rev 19685)
@@ -31,10 +31,6 @@
import junit.framework.TestCase;
-import org.ajax4jsf.javascript.JSFunctionDefinition;
-import org.richfaces.renderkit.util.RendererUtils;
-import org.richfaces.renderkit.util.RendererUtils.ScriptHashVariableWrapper;
-
/**
* @author Nick Belaevski
* @since 3.3.2
@@ -71,77 +67,4 @@
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);
- }
}