Author: nbelaevski
Date: 2009-06-03 20:49:57 -0400 (Wed, 03 Jun 2009)
New Revision: 14471
Added:
branches/community/3.3.X/framework/api/src/main/java/org/ajax4jsf/javascript/ResponseWriterWrapper.java
branches/community/3.3.X/framework/api/src/main/java/org/ajax4jsf/javascript/StringBuilderWriter.java
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/javascript/
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/javascript/ResponseWriterWrapperTest.java
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/javascript/StringBuilderWriterTest.java
Removed:
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/framework/util/javascript/ScriptUtilsTest.java
Modified:
branches/community/3.3.X/framework/api/pom.xml
branches/community/3.3.X/framework/api/src/main/java/org/ajax4jsf/javascript/ScriptUtils.java
Log:
https://jira.jboss.org/jira/browse/RF-7247
Modified: branches/community/3.3.X/framework/api/pom.xml
===================================================================
--- branches/community/3.3.X/framework/api/pom.xml 2009-06-04 00:38:38 UTC (rev 14470)
+++ branches/community/3.3.X/framework/api/pom.xml 2009-06-04 00:49:57 UTC (rev 14471)
@@ -20,5 +20,17 @@
<artifactId>commons-beanutils</artifactId>
<version>1.7.0</version>
</dependency>
+ <dependency>
+ <groupId>org.easymock</groupId>
+ <artifactId>easymock</artifactId>
+ <version>2.5</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.easymock</groupId>
+ <artifactId>easymockclassextension</artifactId>
+ <version>2.4</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
Added:
branches/community/3.3.X/framework/api/src/main/java/org/ajax4jsf/javascript/ResponseWriterWrapper.java
===================================================================
---
branches/community/3.3.X/framework/api/src/main/java/org/ajax4jsf/javascript/ResponseWriterWrapper.java
(rev 0)
+++
branches/community/3.3.X/framework/api/src/main/java/org/ajax4jsf/javascript/ResponseWriterWrapper.java 2009-06-04
00:49:57 UTC (rev 14471)
@@ -0,0 +1,88 @@
+/**
+ * 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.javascript;
+
+import java.io.IOException;
+import java.io.Writer;
+
+import javax.faces.context.ResponseWriter;
+
+final class ResponseWriterWrapper extends Writer {
+
+ private static final int BUFFER_SIZE = 1024;
+
+ private final ResponseWriter responseWriter;
+
+ private char[] writeBuffer;
+
+ ResponseWriterWrapper(ResponseWriter responseWriter) {
+ this.responseWriter = responseWriter;
+ }
+
+ @Override
+ public void close() throws IOException {
+ responseWriter.close();
+ }
+
+ @Override
+ public void flush() throws IOException {
+ responseWriter.flush();
+ }
+
+ @Override
+ public void write(char[] cbuf, int off, int len) throws IOException {
+ responseWriter.writeText(cbuf, off, len);
+ }
+
+ @Override
+ public void write(String str) throws IOException {
+ responseWriter.writeText(str, null);
+ }
+
+ @Override
+ public void write(String str, int off, int len) throws IOException {
+ char cbuf[];
+ if (len <= BUFFER_SIZE) {
+ if (writeBuffer == null) {
+ writeBuffer = new char[BUFFER_SIZE];
+ }
+
+ cbuf = writeBuffer;
+ } else {
+ cbuf = new char[len];
+ }
+
+ str.getChars(off, off + len, cbuf, 0);
+
+ responseWriter.writeText(cbuf, 0, len);
+ }
+
+ @Override
+ public void write(int c) throws IOException {
+ if (writeBuffer == null){
+ writeBuffer = new char[BUFFER_SIZE];
+ }
+ writeBuffer[0] = (char) c;
+
+ responseWriter.writeText(writeBuffer, 0, 1);
+ }
+}
\ No newline at end of file
Modified:
branches/community/3.3.X/framework/api/src/main/java/org/ajax4jsf/javascript/ScriptUtils.java
===================================================================
---
branches/community/3.3.X/framework/api/src/main/java/org/ajax4jsf/javascript/ScriptUtils.java 2009-06-04
00:38:38 UTC (rev 14470)
+++
branches/community/3.3.X/framework/api/src/main/java/org/ajax4jsf/javascript/ScriptUtils.java 2009-06-04
00:49:57 UTC (rev 14471)
@@ -22,13 +22,16 @@
package org.ajax4jsf.javascript;
import java.beans.PropertyDescriptor;
+import java.io.IOException;
import java.io.UnsupportedEncodingException;
+import java.io.Writer;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import javax.faces.FacesException;
+import javax.faces.context.ResponseWriter;
import org.apache.commons.beanutils.PropertyUtils;
@@ -46,79 +49,71 @@
}
- /**
- * Convert any Java Object to JavaScript representation ( as possible ).
- * @param obj
- * @return
- */
- public static String toScript(Object obj) {
+ private static void writeScriptToStream(Writer writer, Object obj) throws IOException {
if (null == obj) {
- return "null";
+ writer.write("null");
} else if (obj instanceof ScriptString) {
- return ((ScriptString) obj).toScript();
+ writer.write(((ScriptString) obj).toScript());
} else if (obj.getClass().isArray()) {
- StringBuilder ret = new StringBuilder("[");
+ writer.write("[");
boolean first = true;
for (int i = 0; i < Array.getLength(obj); i++) {
Object element = Array.get(obj, i);
if (!first) {
- ret.append(',');
+ writer.write(',');
}
- ret.append(toScript(element));
+ writeScriptToStream(writer, element);
first = false;
}
- return ret.append("] ").toString();
+
+ writer.write("] ");
} else if (obj instanceof Collection) {
// Collections put as JavaScript array.
@SuppressWarnings("unchecked")
Collection<Object> collection = (Collection<Object>) obj;
- StringBuilder ret = new StringBuilder("[");
+ writer.write("[");
boolean first = true;
for (Iterator<Object> iter = collection.iterator(); iter.hasNext();) {
Object element = iter.next();
if (!first) {
- ret.append(',');
+ writer.write(',');
}
- ret.append(toScript(element));
+ writeScriptToStream(writer, element);
first = false;
}
- return ret.append("] ").toString();
+ writer.write("] ");
} else if (obj instanceof Map) {
// Maps put as JavaScript hash.
@SuppressWarnings("unchecked")
Map<Object, Object> map = (Map<Object, Object>) obj;
- StringBuilder ret = new StringBuilder("{");
+ writer.write("{");
boolean first = true;
for (Map.Entry<Object, Object> entry : map.entrySet()) {
if (!first) {
- ret.append(',');
+ writer.write(',');
}
- addEncodedString(ret, entry.getKey());
- ret.append(":");
- ret.append(toScript(entry.getValue()));
+ writeEncodedString(writer, entry.getKey());
+ writer.write(":");
+ writeScriptToStream(writer, entry.getValue());
first = false;
}
- return ret.append("} ").toString();
+ writer.write("} ");
} else if (obj instanceof Number || obj instanceof Boolean) {
// numbers and boolean put as-is, without conversion
- return obj.toString();
+ writer.write(obj.toString());
} else if (obj instanceof String) {
// all other put as encoded strings.
- StringBuilder ret = new StringBuilder();
- addEncodedString(ret, obj);
- return ret.toString();
+ writeEncodedString(writer, obj);
} else if (obj instanceof Enum) {
// all other put as encoded strings.
- StringBuilder ret = new StringBuilder();
- addEncodedString(ret, obj);
- return ret.toString();
+ writeEncodedString(writer, obj);
} else if (obj.getClass().getName().startsWith("java.sql.")) {
- StringBuilder ret = new StringBuilder("{");
+ writer.write("{");
boolean first = true;
for (PropertyDescriptor propertyDescriptor :
PropertyUtils.getPropertyDescriptors(obj)) {
@@ -134,23 +129,28 @@
}
if (!first) {
- ret.append(',');
+ writer.write(',');
}
- addEncodedString(ret, key);
- ret.append(":");
- ret.append(toScript(value));
+ writeEncodedString(writer, key);
+ writer.write(":");
+ writeScriptToStream(writer, value);
first = false;
}
- return ret.append("} ").toString();
- }
+ writer.write("} ");
+ } else {
+ // All other objects threaded as Java Beans.
+ writer.write("{");
- // All other objects threaded as Java Beans.
- try {
- StringBuilder ret = new StringBuilder("{");
- PropertyDescriptor[] propertyDescriptors = PropertyUtils
- .getPropertyDescriptors(obj);
+ PropertyDescriptor[] propertyDescriptors;
+ try {
+ propertyDescriptors = PropertyUtils.getPropertyDescriptors(obj);
+ } catch (Exception e) {
+ throw new FacesException(
+ "Error in conversion Java Object to JavaScript", e);
+ }
+
boolean first = true;
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
String key = propertyDescriptor.getName();
@@ -158,39 +158,80 @@
continue;
}
if (!first) {
- ret.append(',');
+ writer.write(',');
}
- addEncodedString(ret, key);
- ret.append(":");
- ret.append(toScript(PropertyUtils.getProperty(obj, key)));
+ writeEncodedString(writer, key);
+ writer.write(":");
+
+ Object propertyValue;
+ try{
+ propertyValue = PropertyUtils.getProperty(obj, key);
+ } catch (Exception e) {
+ throw new FacesException(
+ "Error in conversion Java Object to JavaScript", e);
+ }
+
+ writeScriptToStream(writer, propertyValue);
first = false;
}
- return ret.append("} ").toString();
- } catch (Exception e) {
- throw new FacesException(
- "Error in conversion Java Object to JavaScript", e);
+
+ writer.write("} ");
}
}
+
+ public static void writeToStream(final ResponseWriter responseWriter, Object obj) throws
IOException {
+ writeScriptToStream(new ResponseWriterWrapper(responseWriter), obj);
+ }
+
+ /**
+ * Convert any Java Object to JavaScript representation ( as possible ).
+ * @param obj
+ * @return
+ */
+ public static String toScript(Object obj) {
+ StringBuilder sb = new StringBuilder();
+ try {
+ writeScriptToStream(new StringBuilderWriter(sb), obj);
+ } catch (IOException e) {
+ //ignore
+ }
+ return sb.toString();
+ }
+ public static void writeEncodedString(Writer w, Object obj) throws IOException {
+ w.write("'");
+ writeEncoded(w, obj);
+ w.write("'");
+ }
+
public static void addEncodedString(StringBuilder buff, Object obj) {
- buff.append("'");
- addEncoded(buff, obj);
- buff.append("'");
-
+ try {
+ writeEncodedString(new StringBuilderWriter(buff), obj);
+ } catch (IOException e) {
+ //ignore
+ }
}
- public static void addEncoded(StringBuilder buff, Object obj) {
+ public static void writeEncoded(Writer w, Object obj) throws IOException {
JSEncoder encoder = new JSEncoder();
char chars[] = obj.toString().toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (!encoder.compile(c)) {
- buff.append(encoder.encode(c));
+ w.write(encoder.encode(c));
} else {
- buff.append(c);
+ w.write(c);
}
}
}
+
+ public static void addEncoded(StringBuilder buff, Object obj) {
+ try {
+ writeEncoded(new StringBuilderWriter(buff), obj);
+ } catch (IOException e) {
+ //ignore
+ }
+ }
public static String getValidJavascriptName(String s) {
Added:
branches/community/3.3.X/framework/api/src/main/java/org/ajax4jsf/javascript/StringBuilderWriter.java
===================================================================
---
branches/community/3.3.X/framework/api/src/main/java/org/ajax4jsf/javascript/StringBuilderWriter.java
(rev 0)
+++
branches/community/3.3.X/framework/api/src/main/java/org/ajax4jsf/javascript/StringBuilderWriter.java 2009-06-04
00:49:57 UTC (rev 14471)
@@ -0,0 +1,84 @@
+/**
+ * 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.javascript;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * @author Nick Belaevski
+ * @since 3.3.2
+ */
+final class StringBuilderWriter extends Writer {
+
+ private StringBuilder builder;
+
+ public StringBuilderWriter(StringBuilder builder) {
+ super();
+ this.builder = builder;
+ }
+
+ /**
+ * Closing this writer doesn't have any effect
+ */
+ @Override
+ public void close() throws IOException {
+ //do nothing
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.Writer#flush()
+ */
+ @Override
+ public void flush() throws IOException {
+ //do nothing
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.Writer#write(char[], int, int)
+ */
+ @Override
+ public void write(char[] cbuf, int off, int len) throws IOException {
+ builder.append(cbuf, off, len);
+ }
+
+ @Override
+ public void write(char[] cbuf) throws IOException {
+ builder.append(cbuf);
+ }
+
+ @Override
+ public void write(String str) throws IOException {
+ builder.append(str);
+ }
+
+ @Override
+ public void write(String str, int off, int len) throws IOException {
+ builder.append(str, off, off + len);
+ }
+
+ @Override
+ public void write(int c) throws IOException {
+ builder.append((char) c);
+ }
+
+}
Deleted:
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/framework/util/javascript/ScriptUtilsTest.java
===================================================================
---
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/framework/util/javascript/ScriptUtilsTest.java 2009-06-04
00:38:38 UTC (rev 14470)
+++
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/framework/util/javascript/ScriptUtilsTest.java 2009-06-04
00:49:57 UTC (rev 14471)
@@ -1,223 +0,0 @@
-/**
- * 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.framework.util.javascript;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.TreeMap;
-import java.util.TreeSet;
-
-import org.ajax4jsf.javascript.ScriptUtils;
-
-import junit.framework.TestCase;
-
-/**
- * @author shura
- *
- */
-public class ScriptUtilsTest extends TestCase {
-
- /**
- * @author shura
- *
- */
- public static class Bean {
-
- int _integer;
- boolean _bool;
- Object _foo;
-
- public Bean() {
- }
- /**
- * @param ineger
- * @param bool
- * @param foo
- */
- public Bean(int ineger, boolean bool, Object foo) {
- this._integer = ineger;
- this._bool = bool;
- this._foo = foo;
- }
- /**
- * @return the bool
- */
- public boolean isBool() {
- return this._bool;
- }
- /**
- * @param bool the bool to set
- */
- public void setBool(boolean bool) {
- this._bool = bool;
- }
- /**
- * @return the ineger
- */
- public int getInteger() {
- return this._integer;
- }
- /**
- * @param ineger the ineger to set
- */
- public void setInteger(int ineger) {
- this._integer = ineger;
- }
- /**
- * @return the foo
- */
- public Object getFoo() {
- return this._foo;
- }
- /**
- * @param foo the foo to set
- */
- public void setFoo(Object foo) {
- this._foo = foo;
- }
- }
-
- /**
- * @param name
- */
- public ScriptUtilsTest(String name) {
- super(name);
- }
-
- /**
- * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
- */
- public void testStringToScript() {
- Object obj = "foo";
- assertEquals("'foo'", ScriptUtils.toScript(obj));
- }
-
- /**
- * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
- */
- public void testArrayToScript() {
- int[] obj = {1,2,3,4,5};
- assertEquals("[1,2,3,4,5] ", ScriptUtils.toScript(obj));
- }
-
- public void testSqlDate() {
- java.sql.Time obj = new java.sql.Time(1);
- assertNotNull(ScriptUtils.toScript(obj));
-
- java.sql.Date obj1 = new java.sql.Date(1);
- assertNotNull(ScriptUtils.toScript(obj1));
- }
-
- /**
- * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
- */
- public void testTwoDimentionalArrayToScript() {
- int[][] obj = {{1,2},{3,4}};
- assertEquals("[[1,2] ,[3,4] ] ", ScriptUtils.toScript(obj));
- }
-
- /**
- * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
- */
- public void testTwoDimentionalStringArrayToScript() {
- String[][] obj =
{{"one","two"},{"three","four"}};
- assertEquals("[['one','two'] ,['three','four'] ]
", ScriptUtils.toScript(obj));
- Map<String, Object> map = new TreeMap<String, Object>();
- map.put("a", obj);
- map.put("b", "c");
- assertEquals("{'a':[['one','two']
,['three','four'] ] ,'b':'c'} ",
ScriptUtils.toScript(map));
- }
- /**
- * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
- */
- public void testListToScript() {
- List<Integer> obj = new ArrayList<Integer>();
- obj.add(new Integer(1));
- obj.add(new Integer(2));
- obj.add(new Integer(3));
- obj.add(new Integer(4));
- obj.add(new Integer(5));
- assertEquals("[1,2,3,4,5] ", ScriptUtils.toScript(obj));
- }
-
- /**
- * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
- */
- public void testSetToScript() {
- Set<Integer> obj = new TreeSet<Integer>();
- obj.add(new Integer(1));
- obj.add(new Integer(2));
- obj.add(new Integer(3));
- obj.add(new Integer(4));
- obj.add(new Integer(5));
- assertEquals("[1,2,3,4,5] ", ScriptUtils.toScript(obj));
- }
- /**
- * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
- */
- public void testObjectArrayToScript() {
- Bean[] obj = {new Bean(1,true,"foo"),new Bean(2,false,"bar")};
- assertEquals("[{'bool':true,'foo':'foo',\'integer\':1}
,{'bool':false,'foo':'bar','integer':2} ] ",
ScriptUtils.toScript(obj));
- }
-
- /**
- * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
- */
- public void testObjectListToScript() {
- Bean[] array = {new Bean(1,true,"foo"),new Bean(2,false,"bar")};
- List<Bean> obj = Arrays.asList(array);
- assertEquals("[{'bool':true,'foo':'foo',\'integer\':1}
,{'bool':false,'foo':'bar','integer':2} ] ",
ScriptUtils.toScript(obj));
- }
-
- /**
- * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
- */
- public void testMapToScript() {
- TreeMap<String, String> obj = new TreeMap<String, String>();
- obj.put("a", "foo");
- obj.put("b", "bar");
- obj.put("c", "baz");
- assertEquals("{'a':'foo','b':'bar','c':'baz'}
", ScriptUtils.toScript(obj));
- }
- /**
- * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#addEncodedString(java.lang.StringBuffer,
java.lang.Object)}.
- */
- public void testAddEncodedString() {
- StringBuilder buff = new StringBuilder();
- ScriptUtils.addEncodedString(buff, "foo");
- assertEquals("'foo'", buff.toString());
- }
-
- /**
- * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#addEncoded(java.lang.StringBuffer,
java.lang.Object)}.
- */
- public void testAddEncoded() {
- StringBuilder buff = new StringBuilder();
- ScriptUtils.addEncoded(buff, "foo\"\'");
- assertEquals("foo\\\"\\\'", buff.toString());
- }
-
-
-}
Added:
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/javascript/ResponseWriterWrapperTest.java
===================================================================
---
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/javascript/ResponseWriterWrapperTest.java
(rev 0)
+++
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/javascript/ResponseWriterWrapperTest.java 2009-06-04
00:49:57 UTC (rev 14471)
@@ -0,0 +1,209 @@
+/**
+ * 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.javascript;
+
+import static org.easymock.EasyMock.aryEq;
+import static org.easymock.EasyMock.eq;
+import static org.easymock.EasyMock.isNull;
+import static org.easymock.EasyMock.reportMatcher;
+import static org.easymock.classextension.EasyMock.createStrictMock;
+import static org.easymock.classextension.EasyMock.replay;
+import static org.easymock.classextension.EasyMock.verify;
+
+import java.io.Writer;
+import java.lang.reflect.Array;
+import java.util.Random;
+
+import javax.faces.context.ResponseWriter;
+
+import junit.framework.TestCase;
+
+import org.easymock.IArgumentMatcher;
+
+/**
+ * @author Nick Belaevski
+ * @since 3.3.2
+ */
+public class ResponseWriterWrapperTest extends TestCase {
+
+ private ResponseWriter mockWriter;
+
+ private Writer writer;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ mockWriter = createStrictMock(ResponseWriter.class);
+ writer = new ResponseWriterWrapper(mockWriter);
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+
+ this.writer = null;
+ this.mockWriter = null;
+ }
+
+ public void testWrite1() throws Exception {
+ char[] cs = new char[] {'a', 'b'};
+ mockWriter.writeText(cs, 0, 2);
+ replay(mockWriter);
+
+ writer.write(cs);
+ verify(mockWriter);
+ }
+
+ private static char[] expectSingleChar(final char c) {
+ reportMatcher(new IArgumentMatcher() {
+
+ private String failureMessage;
+
+ public void appendTo(StringBuffer sb) {
+ sb.append(failureMessage);
+ }
+
+ public boolean matches(Object o) {
+ if (!(o instanceof char[])) {
+ failureMessage = "Array of chars expected as argument";
+ } else {
+ if (Array.getLength(o) == 0) {
+ failureMessage = "Array should be of non-zero length";
+ } else {
+ if (Array.getChar(o, 0) != c) {
+ failureMessage = "["+c+"] expected as [0] char";
+ }
+ }
+ }
+
+ return failureMessage == null;
+ }
+
+ });
+
+ return null;
+ }
+
+ private static char[] expectFirstChars(final char[] cs) {
+ return expectFirstChars(cs, cs.length);
+ }
+
+ private static char[] expectFirstChars(final char[] cs, final int length) {
+ reportMatcher(new IArgumentMatcher() {
+
+ private String failureMessage;
+
+ public void appendTo(StringBuffer sb) {
+ sb.append(failureMessage);
+ }
+
+ public boolean matches(Object o) {
+ if (!(o instanceof char[])) {
+ failureMessage = "Array of chars expected as argument";
+ } else {
+ char[] argChars = (char[]) o;
+ if (argChars.length < length) {
+ failureMessage = "Array should have minimum " + length + " length,
but has only: " + argChars.length;
+ } else {
+ for (int i = 0; i < length; i++) {
+ if (argChars[i] != cs[i]) {
+ failureMessage = "Char at offset [" + i + "] mismath: expected
" + cs[i] + " but was " + argChars[i];
+ break;
+ }
+ }
+ }
+ }
+
+ return failureMessage == null;
+ }
+
+ });
+
+ return null;
+ }
+
+ public void testWrite2() throws Exception {
+ mockWriter.writeText(expectSingleChar((char) 0x5678), eq(0), eq(1));
+ mockWriter.writeText(expectSingleChar((char) 0xBA98), eq(0), eq(1));
+ replay(mockWriter);
+ writer.write(0x12345678);
+ writer.write(0xFECDBA98);
+ verify(mockWriter);
+ }
+
+ public void testWrite3() throws Exception {
+ mockWriter.writeText(eq("test"), (String) isNull());
+ replay(mockWriter);
+ writer.write("test");
+ verify(mockWriter);
+ }
+
+ public void testWrite4() throws Exception {
+ mockWriter.writeText(aryEq("abcd".toCharArray()), eq(1), eq(2));
+ mockWriter.writeText(aryEq("efgh".toCharArray()), eq(0), eq(3));
+ mockWriter.writeText(aryEq("ijklm".toCharArray()), eq(2), eq(3));
+
+ replay(mockWriter);
+ writer.write("abcd".toCharArray(), 1, 2);
+ writer.write("efgh".toCharArray(), 0, 3);
+ writer.write("ijklm".toCharArray(), 2, 3);
+ verify(mockWriter);
+ }
+
+ public void testWrite5() throws Exception {
+ mockWriter.writeText(expectFirstChars("string to".toCharArray()), eq(0),
eq(9));
+ mockWriter.writeText(expectFirstChars("one".toCharArray()), eq(0), eq(3));
+
+ replay(mockWriter);
+ writer.write("string to test", 0, 9);
+ writer.write("short one", 6, 3);
+ verify(mockWriter);
+ }
+
+ public void testWrite6() throws Exception {
+ char[] cs = new char[4098];
+ int length = cs.length - 2;
+
+ for (int i = 0; i < cs.length; i++) {
+ cs[i] = (char) new Random().nextInt(Character.MAX_VALUE + 1);
+ }
+
+ mockWriter.writeText(expectFirstChars(cs, length), eq(0), eq(length));
+ replay(mockWriter);
+ writer.write(String.valueOf(cs), 0, length);
+ verify(mockWriter);
+ }
+
+ public void testFlush() throws Exception {
+ mockWriter.flush();
+ replay(mockWriter);
+ writer.flush();
+ verify(mockWriter);
+ }
+
+ public void testClose() throws Exception {
+ mockWriter.close();
+ replay(mockWriter);
+ writer.close();
+ verify(mockWriter);
+ }
+}
Copied:
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java
(from rev 14467,
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/framework/util/javascript/ScriptUtilsTest.java)
===================================================================
---
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java
(rev 0)
+++
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java 2009-06-04
00:49:57 UTC (rev 14471)
@@ -0,0 +1,307 @@
+/**
+ * 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.javascript;
+
+import static org.easymock.EasyMock.capture;
+import static org.easymock.EasyMock.isNull;
+import static org.easymock.classextension.EasyMock.createNiceMock;
+import static org.easymock.classextension.EasyMock.replay;
+import static org.easymock.classextension.EasyMock.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import javax.faces.context.ResponseWriter;
+
+import junit.framework.TestCase;
+
+import org.easymock.Capture;
+import org.easymock.CaptureType;
+
+/**
+ * @author shura
+ *
+ */
+public class ScriptUtilsTest extends TestCase {
+
+ /**
+ * @author shura
+ *
+ */
+ public static class Bean {
+
+ int _integer;
+ boolean _bool;
+ Object _foo;
+
+ public Bean() {
+ }
+ /**
+ * @param ineger
+ * @param bool
+ * @param foo
+ */
+ public Bean(int ineger, boolean bool, Object foo) {
+ this._integer = ineger;
+ this._bool = bool;
+ this._foo = foo;
+ }
+ /**
+ * @return the bool
+ */
+ public boolean isBool() {
+ return this._bool;
+ }
+ /**
+ * @param bool the bool to set
+ */
+ public void setBool(boolean bool) {
+ this._bool = bool;
+ }
+ /**
+ * @return the ineger
+ */
+ public int getInteger() {
+ return this._integer;
+ }
+ /**
+ * @param ineger the ineger to set
+ */
+ public void setInteger(int ineger) {
+ this._integer = ineger;
+ }
+ /**
+ * @return the foo
+ */
+ public Object getFoo() {
+ return this._foo;
+ }
+ /**
+ * @param foo the foo to set
+ */
+ public void setFoo(Object foo) {
+ this._foo = foo;
+ }
+ }
+
+ /**
+ * @param name
+ */
+ public ScriptUtilsTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
+ */
+ public void testStringToScript() {
+ Object obj = "foo";
+ assertEquals("'foo'", ScriptUtils.toScript(obj));
+ }
+
+ /**
+ * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
+ */
+ public void testArrayToScript() {
+ int[] obj = {1,2,3,4,5};
+ assertEquals("[1,2,3,4,5] ", ScriptUtils.toScript(obj));
+ }
+
+ public void testSqlDate() {
+ java.sql.Time obj = new java.sql.Time(1);
+ assertNotNull(ScriptUtils.toScript(obj));
+
+ java.sql.Date obj1 = new java.sql.Date(1);
+ assertNotNull(ScriptUtils.toScript(obj1));
+ }
+
+ /**
+ * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
+ */
+ public void testTwoDimentionalArrayToScript() {
+ int[][] obj = {{1,2},{3,4}};
+ assertEquals("[[1,2] ,[3,4] ] ", ScriptUtils.toScript(obj));
+ }
+
+ /**
+ * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
+ */
+ public void testTwoDimentionalStringArrayToScript() {
+ String[][] obj =
{{"one","two"},{"three","four"}};
+ assertEquals("[['one','two'] ,['three','four'] ]
", ScriptUtils.toScript(obj));
+ Map<String, Object> map = new TreeMap<String, Object>();
+ map.put("a", obj);
+ map.put("b", "c");
+ assertEquals("{'a':[['one','two']
,['three','four'] ] ,'b':'c'} ",
ScriptUtils.toScript(map));
+ }
+ /**
+ * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
+ */
+ public void testListToScript() {
+ List<Integer> obj = new ArrayList<Integer>();
+ obj.add(new Integer(1));
+ obj.add(new Integer(2));
+ obj.add(new Integer(3));
+ obj.add(new Integer(4));
+ obj.add(new Integer(5));
+ assertEquals("[1,2,3,4,5] ", ScriptUtils.toScript(obj));
+ }
+
+ /**
+ * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
+ */
+ public void testSetToScript() {
+ Set<Integer> obj = new TreeSet<Integer>();
+ obj.add(new Integer(1));
+ obj.add(new Integer(2));
+ obj.add(new Integer(3));
+ obj.add(new Integer(4));
+ obj.add(new Integer(5));
+ assertEquals("[1,2,3,4,5] ", ScriptUtils.toScript(obj));
+ }
+ /**
+ * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
+ */
+ public void testObjectArrayToScript() {
+ Bean[] obj = {new Bean(1,true,"foo"),new Bean(2,false,"bar")};
+ assertEquals("[{'bool':true,'foo':'foo',\'integer\':1}
,{'bool':false,'foo':'bar','integer':2} ] ",
ScriptUtils.toScript(obj));
+ }
+
+ /**
+ * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
+ */
+ public void testObjectListToScript() {
+ Bean[] array = {new Bean(1,true,"foo"),new Bean(2,false,"bar")};
+ List<Bean> obj = Arrays.asList(array);
+ assertEquals("[{'bool':true,'foo':'foo',\'integer\':1}
,{'bool':false,'foo':'bar','integer':2} ] ",
ScriptUtils.toScript(obj));
+ }
+
+ /**
+ * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#toScript(java.lang.Object)}.
+ */
+ public void testMapToScript() {
+ TreeMap<String, String> obj = new TreeMap<String, String>();
+ obj.put("a", "foo");
+ obj.put("b", "bar");
+ obj.put("c", "baz");
+ assertEquals("{'a':'foo','b':'bar','c':'baz'}
", ScriptUtils.toScript(obj));
+ }
+ /**
+ * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#addEncodedString(java.lang.StringBuffer,
java.lang.Object)}.
+ */
+ public void testAddEncodedString() {
+ StringBuilder buff = new StringBuilder();
+ ScriptUtils.addEncodedString(buff, "foo");
+ assertEquals("'foo'", buff.toString());
+ }
+
+ /**
+ * Test method for {@link
org.ajax4jsf.javascript.ScriptUtils#addEncoded(java.lang.StringBuffer,
java.lang.Object)}.
+ */
+ public void testAddEncoded() {
+ StringBuilder buff = new StringBuilder();
+ ScriptUtils.addEncoded(buff, "foo\"\'");
+ assertEquals("foo\\\"\\\'", buff.toString());
+ }
+
+ /**
+ * Test method for {@link ScriptUtils#toScript(Object)}
+ */
+ public void testNull() throws Exception {
+ assertEquals("null", ScriptUtils.toScript(null));
+ }
+
+ /**
+ * Test method for {@link ScriptUtils#toScript(Object)}
+ */
+ public void testScriptString() throws Exception {
+ assertEquals("alert(x<y);", ScriptUtils.toScript(new
JSLiteral("alert(x<y);")));
+ }
+
+ private static enum TestEnum {
+ A, B, C;
+
+ @Override
+ public String toString() {
+ return "TestEnum: " + super.toString();
+ }
+ }
+
+ /**
+ * Test method for {@link ScriptUtils#toScript(Object)}
+ */
+ public void testEnum() throws Exception {
+ assertEquals("'TestEnum: B'", ScriptUtils.toScript(TestEnum.B));
+ }
+
+ private void assertCaptureEquals(Capture<? extends Object> capture, String
expected) {
+ StringBuilder sb = new StringBuilder();
+ List<? extends Object> list = capture.getValues();
+ for (Object o : list) {
+ assertNotNull(o);
+ sb.append(o);
+ }
+
+ assertEquals(expected, sb.toString().trim());
+ }
+
+ /**
+ * Test method for {@link ScriptUtils#writeToStream(javax.faces.context.ResponseWriter,
Object)}
+ */
+ public void testWriteToStream() throws Exception {
+ ResponseWriter mockWriter = createNiceMock(ResponseWriter.class);
+ Capture<? extends Object> capture = new Capture<Object>(CaptureType.ALL) {
+ /**
+ *
+ */
+ private static final long serialVersionUID = -4915440411892856583L;
+
+ @Override
+ public void setValue(Object value) {
+ if (value instanceof char[]) {
+ char[] cs = (char[]) value;
+ super.setValue(new String(cs, 0, 1));
+ } else {
+ super.setValue(value);
+ }
+ }
+ };
+
+
+ mockWriter.writeText(capture(capture), (String) isNull());
+ expectLastCall().anyTimes();
+ mockWriter.writeText((char[])capture(capture), eq(0), eq(1));
+ expectLastCall().anyTimes();
+
+ replay(mockWriter);
+ ScriptUtils.writeToStream(mockWriter, Collections.singletonMap("delay",
Integer.valueOf(1500)));
+ verify(mockWriter);
+
+ assertCaptureEquals(capture, "{'delay':1500}");
+ }
+}
Added:
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/javascript/StringBuilderWriterTest.java
===================================================================
---
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/javascript/StringBuilderWriterTest.java
(rev 0)
+++
branches/community/3.3.X/framework/api/src/test/java/org/ajax4jsf/javascript/StringBuilderWriterTest.java 2009-06-04
00:49:57 UTC (rev 14471)
@@ -0,0 +1,97 @@
+/**
+ * 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.javascript;
+
+import java.io.Writer;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Nick Belaevski
+ * @since 3.3.2
+ */
+public class StringBuilderWriterTest extends TestCase {
+
+ private StringBuilder builder;
+
+ private Writer writer;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ this.builder = new StringBuilder();
+ this.writer = new StringBuilderWriter(this.builder);
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+
+ this.builder = null;
+ this.writer = null;
+ }
+
+ public void testWrite() throws Exception {
+ writer.write(new char[] {'a', 'b'});
+ assertEquals("ab", builder.toString());
+ }
+
+ public void testWrite2() throws Exception {
+ writer.write(0x12345678);
+
+ String s = builder.toString();
+ assertEquals(1, s.length());
+ assertEquals(0x5678, s.charAt(0));
+ }
+
+ public void testWrite3() throws Exception {
+ writer.write("test");
+
+ assertEquals("test", builder.toString());
+ }
+
+ public void testWrite4() throws Exception {
+ writer.write("abcd".toCharArray(), 1, 2);
+ assertEquals("bc", builder.toString());
+ writer.write("efgh".toCharArray(), 0, 3);
+ assertEquals("bcefg", builder.toString());
+ writer.write("ijkl".toCharArray(), 2, 2);
+ assertEquals("bcefgkl", builder.toString());
+ }
+
+ public void testWrite5() throws Exception {
+ writer.write("abcd", 1, 2);
+ assertEquals("bc", builder.toString());
+ writer.write("efgh", 0, 3);
+ assertEquals("bcefg", builder.toString());
+ writer.write("ijklm", 2, 3);
+ assertEquals("bcefgklm", builder.toString());
+ }
+
+ public void testFlush() throws Exception {
+ writer.flush();
+ }
+
+ public void testClose() throws Exception {
+ writer.close();
+ }
+}