Author: nbelaevski
Date: 2009-03-18 16:11:48 -0400 (Wed, 18 Mar 2009)
New Revision: 13014
Added:
trunk/ui/tree/src/main/java/org/richfaces/converter/
trunk/ui/tree/src/main/java/org/richfaces/converter/BaseTreeConverter.java
trunk/ui/tree/src/main/java/org/richfaces/converter/TreeAdaptorIntegerRowKeyConverter.java
trunk/ui/tree/src/main/java/org/richfaces/converter/TreeAdaptorRowKeyConverter.java
trunk/ui/tree/src/main/java/org/richfaces/converter/TreeRowKeyConverter.java
trunk/ui/tree/src/test/java/org/richfaces/converter/
trunk/ui/tree/src/test/java/org/richfaces/converter/BaseTreeConverterTest.java
trunk/ui/tree/src/test/java/org/richfaces/converter/TreeAdaptorIntegerRowKeyConverterTest.java
trunk/ui/tree/src/test/java/org/richfaces/converter/TreeAdaptorRowKeyConverterTest.java
trunk/ui/tree/src/test/java/org/richfaces/converter/TreeRowKeyConverterTest.java
Modified:
trunk/ui/tree/src/main/config/component/tree.xml
trunk/ui/tree/src/main/java/org/richfaces/component/UITree.java
trunk/ui/tree/src/test/java/org/richfaces/component/ListRowKeyTest.java
trunk/ui/tree/src/test/java/org/richfaces/component/TreeComponentTest.java
Log:
https://jira.jboss.org/jira/browse/RF-4351
Modified: trunk/ui/tree/src/main/config/component/tree.xml
===================================================================
--- trunk/ui/tree/src/main/config/component/tree.xml 2009-03-18 20:10:19 UTC (rev 13013)
+++ trunk/ui/tree/src/main/config/component/tree.xml 2009-03-18 20:11:48 UTC (rev 13014)
@@ -336,7 +336,7 @@
<description>Keys of the nodes (without subtree) to be updated for ajax request
risen by the node itself</description>
</property>
- <property hidden="true">
+ <property>
<name>rowKeyConverter</name>
</property>
@@ -360,5 +360,20 @@
</component>
&listeners;
+
+ <converter generate="false">
+ <id>org.richfaces.TreeRowKeyConverter</id>
+ <classname>org.richfaces.converter.TreeRowKeyConverter</classname>
+ </converter>
+
+ <converter generate="false">
+ <id>org.richfaces.TreeAdaptorRowKeyConverter</id>
+ <classname>org.richfaces.converter.TreeAdaptorRowKeyConverter</classname>
+ </converter>
+
+ <converter generate="false">
+ <id>org.richfaces.TreeAdaptorIntegerRowKeyConverter</id>
+ <classname>org.richfaces.converter.TreeAdaptorIntegerRowKeyConverter</classname>
+ </converter>
</components>
Modified: trunk/ui/tree/src/main/java/org/richfaces/component/UITree.java
===================================================================
--- trunk/ui/tree/src/main/java/org/richfaces/component/UITree.java 2009-03-18 20:10:19
UTC (rev 13013)
+++ trunk/ui/tree/src/main/java/org/richfaces/component/UITree.java 2009-03-18 20:11:48
UTC (rev 13014)
@@ -1051,12 +1051,16 @@
@Override
public Converter getRowKeyConverter() {
- return KEY_CONVERTER;
+ Converter converter = super.getRowKeyConverter();
+ if (converter == null) {
+ converter = KEY_CONVERTER;
+ }
+ return converter;
}
@Override
public void setRowKeyConverter(Converter rowKeyConverter) {
- throw new UnsupportedOperationException();
+ super.setRowKeyConverter(rowKeyConverter);
}
/**
Added: trunk/ui/tree/src/main/java/org/richfaces/converter/BaseTreeConverter.java
===================================================================
--- trunk/ui/tree/src/main/java/org/richfaces/converter/BaseTreeConverter.java
(rev 0)
+++ trunk/ui/tree/src/main/java/org/richfaces/converter/BaseTreeConverter.java 2009-03-18
20:11:48 UTC (rev 13014)
@@ -0,0 +1,121 @@
+package org.richfaces.converter;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import javax.faces.convert.Converter;
+
+import org.richfaces.model.AbstractTreeDataModel;
+
+/**
+ * @author Nick Belaevski
+ * @since 3.3.1
+ */
+
+public abstract class BaseTreeConverter implements Converter {
+
+ private static final Pattern unescapePattern =
Pattern.compile("_(:|_)|_(x[0-9A-Fa-f]{2}|[0-9A-Fa-f]{4})?|(:)");
+
+ //private static final Pattern htmlEscapePattern =
Pattern.compile(":|_|[^0-9a-zA-Z]");
+
+ private static final Pattern xhtmlEscapePattern =
Pattern.compile(":|_|[^A-Za-z\\-\\.0-9\\xC0-\\xD6\\xD8-\\xF6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD\\xB7\\u0300-\\u036F\\u203F-\\u2040]");
+
+ private String escape(String s) {
+ Matcher matcher = xhtmlEscapePattern.matcher(s);
+ StringBuilder sb = new StringBuilder(s.length());
+
+ int start = 0;
+
+ while (matcher.find()) {
+ int idx = matcher.start();
+ sb.append(s, start, idx);
+ sb.append('_');
+
+ char c = s.charAt(idx);
+ if (c == ':' || c == '_') {
+ sb.append(c); // _: or __
+ } else {
+ String asHex = Integer.toHexString(c);
+ switch (asHex.length()) {
+
+ case 1:
+ sb.append("x0"); // _x05
+ break;
+ case 2:
+ sb.append("x"); // _xef
+ break;
+ case 3:
+ sb.append("0"); // _0fed
+ break;
+ case 4:
+ //no padding required
+ // _fcda
+ break;
+
+ default:
+ throw new IllegalArgumentException();
+ }
+
+ sb.append(asHex);
+ }
+
+ start = idx + 1;
+ }
+
+ if (start < s.length()) {
+ sb.append(s, start, s.length());
+ }
+
+ return sb.toString();
+ }
+
+ protected void appendToKeyString(StringBuilder builder, String segment) {
+ builder.append(escape(segment));
+ builder.append(AbstractTreeDataModel.SEPARATOR);
+ }
+
+ protected List<String> splitKeyString(String string) {
+ Matcher matcher = unescapePattern.matcher(string);
+
+ List<String> result = new ArrayList<String>();
+ StringBuffer sb = new StringBuffer();
+
+ while (matcher.find()) {
+ if (matcher.group(3) == null) {
+ String oneChar = matcher.group(1);
+ if (oneChar != null) {
+ matcher.appendReplacement(sb, "$1");
+ } else {
+ matcher.appendReplacement(sb, "");
+ String hex = matcher.group(2);
+ if (hex != null) {
+ if (hex.charAt(0) == 'x') {
+ hex = hex.substring(1);
+ }
+
+ int h = Integer.parseInt(hex, 16);
+ sb.append((char) h);
+ } else {
+ throw new IllegalArgumentException();
+ }
+ }
+ } else {
+ matcher.appendReplacement(sb, "");
+ result.add(sb.toString());
+ sb.setLength(0);
+ }
+ }
+
+ matcher.appendTail(sb);
+
+ if (sb.length() != 0) {
+ result.add(sb.toString());
+ sb.setLength(0);
+ }
+
+ return result;
+ }
+
+}
Added:
trunk/ui/tree/src/main/java/org/richfaces/converter/TreeAdaptorIntegerRowKeyConverter.java
===================================================================
---
trunk/ui/tree/src/main/java/org/richfaces/converter/TreeAdaptorIntegerRowKeyConverter.java
(rev 0)
+++
trunk/ui/tree/src/main/java/org/richfaces/converter/TreeAdaptorIntegerRowKeyConverter.java 2009-03-18
20:11:48 UTC (rev 13014)
@@ -0,0 +1,16 @@
+package org.richfaces.converter;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+
+public class TreeAdaptorIntegerRowKeyConverter extends TreeAdaptorRowKeyConverter {
+
+ public static final String CONVERTER_ID =
"org.richfaces.TreeAdaptorIntegerRowKeyConverter";
+
+ @Override
+ protected Object convertStringToModelKey(FacesContext context,
+ UIComponent component, String modelId, String value) {
+
+ return Integer.parseInt(value);
+ }
+}
Added:
trunk/ui/tree/src/main/java/org/richfaces/converter/TreeAdaptorRowKeyConverter.java
===================================================================
--- trunk/ui/tree/src/main/java/org/richfaces/converter/TreeAdaptorRowKeyConverter.java
(rev 0)
+++
trunk/ui/tree/src/main/java/org/richfaces/converter/TreeAdaptorRowKeyConverter.java 2009-03-18
20:11:48 UTC (rev 13014)
@@ -0,0 +1,107 @@
+package org.richfaces.converter;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.ConverterException;
+
+import org.ajax4jsf.Messages;
+import org.richfaces.component.util.MessageUtil;
+import org.richfaces.model.ListRowKey;
+import org.richfaces.model.StackingTreeModelKey;
+import org.richfaces.model.TreeRowKey;
+
+public class TreeAdaptorRowKeyConverter extends BaseTreeConverter {
+
+ public static final String CONVERTER_ID =
"org.richfaces.TreeAdaptorRowKeyConverter";
+
+ protected Object convertStringToModelKey(FacesContext context, UIComponent component,
+ String modelId, String value) {
+
+ return value;
+ }
+
+ protected String convertModelKeyToString(FacesContext context, UIComponent component,
+ String modelId, Object value) {
+
+ return value.toString();
+ }
+
+ public Object getAsObject(FacesContext context, UIComponent component,
+ String value) {
+
+ if (context == null) {
+ throw new NullPointerException("context");
+ }
+
+ if (component == null) {
+ throw new NullPointerException("component");
+ }
+
+ if (value == null || value.length() == 0) {
+ return null;
+ }
+
+ try {
+ List<StackingTreeModelKey<?>> keys = new
ArrayList<StackingTreeModelKey<?>>();
+ Iterator<String> iterator = splitKeyString(value).iterator();
+ while (iterator.hasNext()) {
+ String modelId = iterator.next();
+ String modelKey = iterator.next();
+
+ Object convertedModelKey = convertStringToModelKey(context, component, modelId,
modelKey);
+ keys.add(new StackingTreeModelKey<Object>(modelId, convertedModelKey));
+ }
+
+
+ return new ListRowKey<StackingTreeModelKey<?>>(keys);
+ } catch (Exception e) {
+ Object label = MessageUtil.getLabel(context, component);
+ String summary = Messages.getMessage(Messages.COMPONENT_CONVERSION_ERROR, label,
value);
+
+ throw new ConverterException(new FacesMessage(summary), e);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public String getAsString(FacesContext context, UIComponent component,
+ Object value) {
+
+ if (context == null) {
+ throw new NullPointerException("context");
+ }
+
+ if (component == null) {
+ throw new NullPointerException("component");
+ }
+
+ if (value == null) {
+ return null;
+ }
+
+ try {
+ StringBuilder sb = new StringBuilder();
+ TreeRowKey<StackingTreeModelKey<?>> treeRowKey =
(TreeRowKey<StackingTreeModelKey<?>>) value;
+ Iterator<StackingTreeModelKey<?>> iterator = treeRowKey.iterator();
+ while (iterator.hasNext()) {
+ StackingTreeModelKey<?> key = iterator.next();
+ String modelId = key.getModelId();
+ appendToKeyString(sb, modelId);
+ Object modelKey = key.getModelKey();
+ String convertedModelKey = convertModelKeyToString(context, component, modelId,
modelKey);
+ appendToKeyString(sb, convertedModelKey);
+ }
+
+ return sb.toString();
+ } catch (Exception e) {
+ Object label = MessageUtil.getLabel(context, component);
+ String summary = Messages.getMessage(Messages.COMPONENT_CONVERSION_ERROR, label,
value);
+
+ throw new ConverterException(new FacesMessage(summary), e);
+ }
+ }
+}
Added: trunk/ui/tree/src/main/java/org/richfaces/converter/TreeRowKeyConverter.java
===================================================================
--- trunk/ui/tree/src/main/java/org/richfaces/converter/TreeRowKeyConverter.java
(rev 0)
+++
trunk/ui/tree/src/main/java/org/richfaces/converter/TreeRowKeyConverter.java 2009-03-18
20:11:48 UTC (rev 13014)
@@ -0,0 +1,102 @@
+package org.richfaces.converter;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.ConverterException;
+
+import org.ajax4jsf.Messages;
+import org.richfaces.component.util.MessageUtil;
+import org.richfaces.model.ListRowKey;
+import org.richfaces.model.TreeRowKey;
+
+public class TreeRowKeyConverter extends BaseTreeConverter {
+
+ public static final String CONVERTER_ID =
"org.richfaces.TreeRowKeyConverter";
+
+ protected Object convertStringToKey(FacesContext context, UIComponent component, String
value) {
+ return value;
+ }
+
+ protected String convertKeyToString(FacesContext context, UIComponent component, Object
value) {
+ return value.toString();
+ }
+
+ public Object getAsObject(FacesContext context, UIComponent component,
+ String value) {
+
+ if (context == null) {
+ throw new NullPointerException("context");
+ }
+
+ if (component == null) {
+ throw new NullPointerException("component");
+ }
+
+ if (value == null || value.length() == 0) {
+ return null;
+ }
+
+ try {
+ List<String> split = splitKeyString(value);
+ List<Object> result = new ArrayList<Object>(split.size());
+
+ Iterator<String> iterator = split.iterator();
+ while (iterator.hasNext()) {
+ String stringKey = iterator.next();
+ Object convertedKey = convertStringToKey(context, component, stringKey);
+ result.add(convertedKey);
+ }
+
+ return new ListRowKey<Object>(result);
+ } catch (Exception e) {
+ Object label = MessageUtil.getLabel(context, component);
+ String summary = Messages.getMessage(Messages.COMPONENT_CONVERSION_ERROR, label,
value);
+
+ throw new ConverterException(new FacesMessage(summary), e);
+ }
+
+ }
+
+ public String getAsString(FacesContext context, UIComponent component,
+ Object value) {
+
+ if (context == null) {
+ throw new NullPointerException("context");
+ }
+
+ if (component == null) {
+ throw new NullPointerException("component");
+ }
+
+ if (value == null) {
+ return null;
+ }
+
+ try {
+ TreeRowKey<?> key = (TreeRowKey<?>) value;
+ Iterator<?> iterator = key.iterator();
+ StringBuilder sb = new StringBuilder();
+
+ while (iterator.hasNext()) {
+ Object next = iterator.next();
+ String convertedKey = convertKeyToString(context, component, next);
+
+ appendToKeyString(sb, convertedKey);
+ }
+
+ return sb.toString();
+ } catch (Exception e) {
+ Object label = MessageUtil.getLabel(context, component);
+ String summary = Messages.getMessage(Messages.COMPONENT_CONVERSION_ERROR, label,
value);
+
+ throw new ConverterException(new FacesMessage(summary), e);
+ }
+
+ }
+
+}
Modified: trunk/ui/tree/src/test/java/org/richfaces/component/ListRowKeyTest.java
===================================================================
--- trunk/ui/tree/src/test/java/org/richfaces/component/ListRowKeyTest.java 2009-03-18
20:10:19 UTC (rev 13013)
+++ trunk/ui/tree/src/test/java/org/richfaces/component/ListRowKeyTest.java 2009-03-18
20:11:48 UTC (rev 13014)
@@ -62,7 +62,7 @@
assertFalse(iterator.hasNext());
- key = new ListRowKey<Long>(new Long(5));
+ key = new ListRowKey<Long>(null, new Long(5));
assertEquals(1, key.depth());
iterator = key.iterator();
assertTrue(iterator.hasNext());
@@ -70,7 +70,7 @@
assertFalse(iterator.hasNext());
- key = new ListRowKey<Long>(new ListRowKey<Long>(new Long(6)), new
Long(7));
+ key = new ListRowKey<Long>(new ListRowKey<Long>(null, new Long(6)), new
Long(7));
assertEquals(2, key.depth());
iterator = key.iterator();
assertTrue(iterator.hasNext());
Modified: trunk/ui/tree/src/test/java/org/richfaces/component/TreeComponentTest.java
===================================================================
--- trunk/ui/tree/src/test/java/org/richfaces/component/TreeComponentTest.java 2009-03-18
20:10:19 UTC (rev 13013)
+++ trunk/ui/tree/src/test/java/org/richfaces/component/TreeComponentTest.java 2009-03-18
20:11:48 UTC (rev 13014)
@@ -564,7 +564,7 @@
defaultFacet.getChildren().add(input1);
tree.setNodeFace("node");
- tree.setRowKey(new ListRowKey("testId"));
+ tree.setRowKey(new ListRowKey(null, "testId"));
Iterator dataChildren = tree.dataChildren();
assertTrue(dataChildren.hasNext());
assertSame(treeNode, dataChildren.next());
Added: trunk/ui/tree/src/test/java/org/richfaces/converter/BaseTreeConverterTest.java
===================================================================
--- trunk/ui/tree/src/test/java/org/richfaces/converter/BaseTreeConverterTest.java
(rev 0)
+++
trunk/ui/tree/src/test/java/org/richfaces/converter/BaseTreeConverterTest.java 2009-03-18
20:11:48 UTC (rev 13014)
@@ -0,0 +1,196 @@
+/**
+ * License Agreement.
+ *
+ * JBoss RichFaces - Ajax4jsf Component Library
+ *
+ * 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.richfaces.converter;
+
+import java.util.List;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+
+import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
+
+/**
+ * @author Nick Belaevski
+ * @since 3.3.1
+ */
+
+public class BaseTreeConverterTest extends AbstractAjax4JsfTestCase {
+
+ private static class BaseTreeConverterImpl extends BaseTreeConverter {
+
+ public Object getAsObject(FacesContext context, UIComponent component,
+ String value) {
+ return null;
+ }
+
+ public String getAsString(FacesContext context, UIComponent component,
+ Object value) {
+ return null;
+ }
+
+ };
+
+ public BaseTreeConverterTest(String name) {
+ super(name);
+ }
+
+ public void testAppendToKeyString() throws Exception {
+ BaseTreeConverterImpl converter = new BaseTreeConverterImpl();
+ StringBuilder sb = new StringBuilder();
+
+ converter.appendToKeyString(sb, "_a_b_");
+
+ assertEquals("__a__b__:", sb.toString());
+ sb.setLength(0);
+
+ converter.appendToKeyString(sb, ":a:b:");
+ assertEquals("_:a_:b_::", sb.toString());
+ sb.setLength(0);
+
+ converter.appendToKeyString(sb, "test");
+ assertEquals("test:", sb.toString());
+ sb.setLength(0);
+
+ converter.appendToKeyString(sb, "a\u0009b");
+ assertEquals("a_x09b:", sb.toString());
+ sb.setLength(0);
+
+ converter.appendToKeyString(sb, "a\u00a0c");
+ assertEquals("a_xa0c:", sb.toString());
+ sb.setLength(0);
+
+ converter.appendToKeyString(sb, "a\u037ec");
+ assertEquals("a_037ec:", sb.toString());
+ sb.setLength(0);
+
+ converter.appendToKeyString(sb, "a\ue1acd");
+ assertEquals("a_e1acd:", sb.toString());
+ sb.setLength(0);
+ }
+
+ public void testSplitKeyString() throws Exception {
+ BaseTreeConverterImpl converter = new BaseTreeConverterImpl();
+ List<String> split;
+
+ split = converter.splitKeyString("__a__b__:c");
+ assertEquals(2, split.size());
+ assertEquals("_a_b_", split.get(0));
+ assertEquals("c", split.get(1));
+
+ split = converter.splitKeyString("c:__a__");
+ assertEquals(2, split.size());
+ assertEquals("c", split.get(0));
+ assertEquals("_a_", split.get(1));
+
+ split = converter.splitKeyString("_:a_::b");
+ assertEquals(2, split.size());
+ assertEquals(":a:", split.get(0));
+ assertEquals("b", split.get(1));
+
+ split = converter.splitKeyString("b:_:a_:");
+ assertEquals(2, split.size());
+ assertEquals("b", split.get(0));
+ assertEquals(":a:", split.get(1));
+
+ split = converter.splitKeyString("_x09:c_x08ab");
+ assertEquals(2, split.size());
+ assertEquals("\u0009", split.get(0));
+ assertEquals("c\u0008ab", split.get(1));
+
+ split = converter.splitKeyString("c_x08ab:_x09");
+ assertEquals(2, split.size());
+ assertEquals("c\u0008ab", split.get(0));
+ assertEquals("\u0009", split.get(1));
+
+ split = converter.splitKeyString("_xa9:c_x98ab");
+ assertEquals(2, split.size());
+ assertEquals("\u00a9", split.get(0));
+ assertEquals("c\u0098ab", split.get(1));
+
+ split = converter.splitKeyString("_0008:a_0009bcd");
+ assertEquals(2, split.size());
+ assertEquals("\u0008", split.get(0));
+ assertEquals("a\u0009bcd", split.get(1));
+
+ split = converter.splitKeyString("_0028:a_00a9bcd");
+ assertEquals(2, split.size());
+ assertEquals("\u0028", split.get(0));
+ assertEquals("a\u00a9bcd", split.get(1));
+
+ split = converter.splitKeyString("_0a28:a_0ea9bcd");
+ assertEquals(2, split.size());
+ assertEquals("\u0a28", split.get(0));
+ assertEquals("a\u0ea9bcd", split.get(1));
+
+ split = converter.splitKeyString("_9e28:a_f3a9bcd");
+ assertEquals(2, split.size());
+ assertEquals("\u9e28", split.get(0));
+ assertEquals("a\uf3a9bcd", split.get(1));
+
+ split = converter.splitKeyString("");
+ assertEquals(0, split.size());
+
+ try {
+ converter.splitKeyString("_3");
+
+ fail();
+ } catch (IllegalArgumentException e) {
+ }
+
+ try {
+ converter.splitKeyString("_30");
+
+ fail();
+ } catch (IllegalArgumentException e) {
+ }
+
+ try {
+ converter.splitKeyString("_x3");
+
+ fail();
+ } catch (IllegalArgumentException e) {
+ }
+
+ try {
+ converter.splitKeyString("_x3q");
+
+ fail();
+ } catch (IllegalArgumentException e) {
+ }
+
+ try {
+ converter.splitKeyString("_234");
+
+ fail();
+ } catch (IllegalArgumentException e) {
+ }
+
+ try {
+ converter.splitKeyString("_f3eq");
+
+ fail();
+ } catch (IllegalArgumentException e) {
+ }
+ }
+}
+
+
Added:
trunk/ui/tree/src/test/java/org/richfaces/converter/TreeAdaptorIntegerRowKeyConverterTest.java
===================================================================
---
trunk/ui/tree/src/test/java/org/richfaces/converter/TreeAdaptorIntegerRowKeyConverterTest.java
(rev 0)
+++
trunk/ui/tree/src/test/java/org/richfaces/converter/TreeAdaptorIntegerRowKeyConverterTest.java 2009-03-18
20:11:48 UTC (rev 13014)
@@ -0,0 +1,104 @@
+/**
+ * License Agreement.
+ *
+ * JBoss RichFaces - Ajax4jsf Component Library
+ *
+ * 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.richfaces.converter;
+
+import java.util.Arrays;
+
+import javax.faces.component.UIComponent;
+import javax.faces.convert.Converter;
+
+import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
+import org.richfaces.component.UITree;
+import org.richfaces.model.ListRowKey;
+import org.richfaces.model.StackingTreeModelKey;
+
+/**
+ * @author Nick Belaevski
+ * @since 3.3.1
+ */
+
+public class TreeAdaptorIntegerRowKeyConverterTest extends AbstractAjax4JsfTestCase {
+
+ private UIComponent tree;
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+
+ this.tree = facesContext.getApplication().createComponent(UITree.COMPONENT_TYPE);
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ super.tearDown();
+
+ this.tree = null;
+ }
+
+
+ public TreeAdaptorIntegerRowKeyConverterTest(String name) {
+ super(name);
+ }
+
+ public void testCreateConverter() throws Exception {
+ Converter converter =
facesContext.getApplication().createConverter(TreeAdaptorIntegerRowKeyConverter.CONVERTER_ID);
+ assertNotNull(converter);
+ }
+
+ public void testGetAsString() throws Exception {
+ Converter converter = new TreeAdaptorIntegerRowKeyConverter();
+
+ Object key = new ListRowKey<StackingTreeModelKey<Integer>>(Arrays.asList(
+ new StackingTreeModelKey<Integer>("list", Integer.valueOf(12)),
+ new StackingTreeModelKey<Integer>("map", Integer.valueOf(456)),
+ new StackingTreeModelKey<Integer>("custom", Integer.valueOf(789))
+ ));
+
+ String string = converter.getAsString(facesContext, tree, key);
+ assertEquals("list:12:map:456:custom:789:", string);
+
+ string = converter.getAsString(facesContext, tree, null);
+ assertNull(string);
+ }
+
+ public void testGetAsObject() throws Exception {
+ Converter converter = new TreeAdaptorIntegerRowKeyConverter();
+ ListRowKey<StackingTreeModelKey<Integer>> key =
(ListRowKey<StackingTreeModelKey<Integer>>)
+ converter.getAsObject(facesContext, tree, "map:123:anotherMap:456:m:789");
+ StackingTreeModelKey stackingModelKey;
+
+ assertEquals(3, key.depth());
+
+ stackingModelKey = key.get(0);
+ assertEquals("map", stackingModelKey.getModelId());
+ assertEquals(Integer.valueOf(123), stackingModelKey.getModelKey());
+
+ stackingModelKey = key.get(1);
+ assertEquals("anotherMap", stackingModelKey.getModelId());
+ assertEquals(Integer.valueOf(456), stackingModelKey.getModelKey());
+
+ stackingModelKey = key.get(2);
+ assertEquals("m", stackingModelKey.getModelId());
+ assertEquals(Integer.valueOf(789), stackingModelKey.getModelKey());
+ }
+
+}
Added:
trunk/ui/tree/src/test/java/org/richfaces/converter/TreeAdaptorRowKeyConverterTest.java
===================================================================
---
trunk/ui/tree/src/test/java/org/richfaces/converter/TreeAdaptorRowKeyConverterTest.java
(rev 0)
+++
trunk/ui/tree/src/test/java/org/richfaces/converter/TreeAdaptorRowKeyConverterTest.java 2009-03-18
20:11:48 UTC (rev 13014)
@@ -0,0 +1,160 @@
+/**
+ * License Agreement.
+ *
+ * JBoss RichFaces - Ajax4jsf Component Library
+ *
+ * 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.richfaces.converter;
+
+import java.util.Arrays;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.convert.ConverterException;
+
+import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
+import org.richfaces.component.UITree;
+import org.richfaces.model.ListRowKey;
+import org.richfaces.model.StackingTreeModelKey;
+
+/**
+ * @author Nick Belaevski
+ * @since 3.3.1
+ */
+
+public class TreeAdaptorRowKeyConverterTest extends AbstractAjax4JsfTestCase {
+
+ private UIComponent tree;
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+
+ this.tree = facesContext.getApplication().createComponent(UITree.COMPONENT_TYPE);
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ super.tearDown();
+
+ this.tree = null;
+ }
+
+
+ public TreeAdaptorRowKeyConverterTest(String name) {
+ super(name);
+ }
+
+ public void testCreateConverter() throws Exception {
+ Converter converter =
facesContext.getApplication().createConverter(TreeAdaptorRowKeyConverter.CONVERTER_ID);
+ assertNotNull(converter);
+ }
+
+ public void testGetAsString() throws Exception {
+ Converter converter = new TreeAdaptorRowKeyConverter();
+
+ Object key = new ListRowKey<StackingTreeModelKey<Object>>(Arrays.asList(
+ new StackingTreeModelKey<Object>("list", Integer.valueOf(12)),
+ new StackingTreeModelKey<Object>("map", "key"),
+ new StackingTreeModelKey<Object>("custom", Long.valueOf(456))
+ ));
+
+ String string = converter.getAsString(facesContext, tree, key);
+ assertEquals("list:12:map:key:custom:456:", string);
+
+ string = converter.getAsString(facesContext, tree, null);
+ assertNull(string);
+ }
+
+ public void testGetAsObject() throws Exception {
+ Converter converter = new TreeAdaptorRowKeyConverter();
+ ListRowKey<StackingTreeModelKey<Object>> key =
(ListRowKey<StackingTreeModelKey<Object>>)
+ converter.getAsObject(facesContext, tree,
"map:key1:anotherMap:itsKey:m:key_x32");
+ StackingTreeModelKey stackingModelKey;
+
+ assertEquals(3, key.depth());
+
+ stackingModelKey = key.get(0);
+ assertEquals("map", stackingModelKey.getModelId());
+ assertEquals("key1", stackingModelKey.getModelKey());
+
+ stackingModelKey = key.get(1);
+ assertEquals("anotherMap", stackingModelKey.getModelId());
+ assertEquals("itsKey", stackingModelKey.getModelKey());
+
+ stackingModelKey = key.get(2);
+ assertEquals("m", stackingModelKey.getModelId());
+ assertEquals("key2", stackingModelKey.getModelKey());
+
+ Object object = converter.getAsObject(facesContext, tree, null);
+ assertNull(object);
+
+ object = converter.getAsObject(facesContext, tree, "");
+ assertNull(object);
+ }
+
+ public void testThrows() throws Exception {
+ Converter converter = new TreeAdaptorRowKeyConverter();
+ try {
+ converter.getAsObject(null, tree, "model:value");
+ fail();
+ } catch (NullPointerException e) {
+ }
+ try {
+ converter.getAsObject(facesContext, null, "model:value");
+ fail();
+ } catch (NullPointerException e) {
+ }
+ try {
+ converter.getAsString(null, tree, new
ListRowKey<StackingTreeModelKey<String>>(null, new
StackingTreeModelKey<String>("a", "b")));
+ fail();
+ } catch (NullPointerException e) {
+ }
+ try {
+ converter.getAsString(facesContext, null, new
ListRowKey<StackingTreeModelKey<String>>(null, new
StackingTreeModelKey<String>("a", "b")));
+ fail();
+ } catch (NullPointerException e) {
+ }
+
+ try {
+ converter.getAsObject(facesContext, tree, "a:b:c_d");
+ fail();
+ } catch (ConverterException e) {
+ FacesMessage message = e.getFacesMessage();
+ System.out.println(message.getSummary());
+ }
+
+ converter = new TreeAdaptorRowKeyConverter() {
+ @Override
+ protected String convertModelKeyToString(FacesContext context,
+ UIComponent component, String modelId, Object value) {
+ return Double.toString((Double) value);
+ }
+ };
+
+ try {
+ converter.getAsString(facesContext, tree, new
ListRowKey<StackingTreeModelKey<Object>>(null, new
StackingTreeModelKey<Object>("a", "b")));
+ fail();
+ } catch (ConverterException e) {
+ FacesMessage message = e.getFacesMessage();
+ System.out.println(message.getSummary());
+ }
+ }
+}
Added: trunk/ui/tree/src/test/java/org/richfaces/converter/TreeRowKeyConverterTest.java
===================================================================
--- trunk/ui/tree/src/test/java/org/richfaces/converter/TreeRowKeyConverterTest.java
(rev 0)
+++
trunk/ui/tree/src/test/java/org/richfaces/converter/TreeRowKeyConverterTest.java 2009-03-18
20:11:48 UTC (rev 13014)
@@ -0,0 +1,181 @@
+/**
+ * License Agreement.
+ *
+ * JBoss RichFaces - Ajax4jsf Component Library
+ *
+ * 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.richfaces.converter;
+
+import java.util.Arrays;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.convert.ConverterException;
+
+import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
+import org.richfaces.component.UITree;
+import org.richfaces.model.ListRowKey;
+
+/**
+ * @author Nick Belaevski
+ * @since 3.3.1
+ */
+
+public class TreeRowKeyConverterTest extends AbstractAjax4JsfTestCase {
+
+ private UIComponent tree;
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+
+ this.tree = facesContext.getApplication().createComponent(UITree.COMPONENT_TYPE);
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ super.tearDown();
+
+ this.tree = null;
+ }
+
+ public TreeRowKeyConverterTest(String name) {
+ super(name);
+ }
+
+ public void testCreateConverter() throws Exception {
+ Converter converter =
facesContext.getApplication().createConverter(TreeRowKeyConverter.CONVERTER_ID);
+ assertNotNull(converter);
+ }
+
+ public void testGetAsString() throws Exception {
+ Converter converter = new TreeRowKeyConverter();
+
+ ListRowKey key = new ListRowKey(Arrays.asList("test", Long.valueOf(456),
Integer.valueOf(123)));
+
+ String string = converter.getAsString(facesContext, tree, key);
+ assertEquals("test:456:123:", string);
+
+ string = converter.getAsString(facesContext, tree, null);
+ assertNull(string);
+ }
+
+ public void testGetAsObject() throws Exception {
+ Converter converter = new TreeRowKeyConverter();
+ ListRowKey<String> listRowKey = (ListRowKey<String>)
converter.getAsObject(facesContext, tree, "test:test__2:test_:3");
+
+ assertEquals(3, listRowKey.depth());
+ assertEquals("test", listRowKey.get(0));
+ assertEquals("test_2", listRowKey.get(1));
+ assertEquals("test:3", listRowKey.get(2));
+
+ Object convertedKey = converter.getAsObject(facesContext, tree, null);
+ assertNull(convertedKey);
+
+ convertedKey = converter.getAsObject(facesContext, tree, "");
+ assertNull(convertedKey);
+ }
+
+ private static final class TreeRowKeyIntegerHexConverter extends TreeRowKeyConverter {
+ @Override
+ protected String convertKeyToString(FacesContext context,
+ UIComponent component, Object value) {
+
+ return Integer.toHexString((Integer) value);
+ }
+
+ @Override
+ protected Object convertStringToKey(FacesContext context,
+ UIComponent component, String value) {
+
+ return Integer.parseInt(value, 16);
+ }
+ };
+
+ public void testGetAsStringHexConversion() throws Exception {
+ Converter converter = new TreeRowKeyIntegerHexConverter();
+
+ Object key = new ListRowKey<Integer>(Arrays.asList(Integer.valueOf(0x33),
Integer.valueOf(0x4512),
+ Integer.valueOf(0xfed0)));
+
+ String string = converter.getAsString(facesContext, tree, key);
+ assertEquals("33:4512:fed0:", string);
+ }
+
+ public void testGetAsObjectHexConversion() throws Exception {
+ Converter converter = new TreeRowKeyIntegerHexConverter();
+
+ ListRowKey<Integer> key = (ListRowKey<Integer>)
converter.getAsObject(facesContext, tree, "45:678:fabc");
+
+ assertEquals(3, key.depth());
+ assertEquals(Integer.valueOf(0x45), key.get(0));
+ assertEquals(Integer.valueOf(0x678), key.get(1));
+ assertEquals(Integer.valueOf(0xfabc), key.get(2));
+ }
+
+ public void testThrows() throws Exception {
+ Converter converter = new TreeRowKeyConverter();
+ try {
+ converter.getAsObject(null, tree, "model:value");
+ fail();
+ } catch (NullPointerException e) {
+ }
+ try {
+ converter.getAsObject(facesContext, null, "model:value");
+ fail();
+ } catch (NullPointerException e) {
+ }
+ try {
+ converter.getAsString(null, tree, new ListRowKey<String>(null, "a"));
+ fail();
+ } catch (NullPointerException e) {
+ }
+ try {
+ converter.getAsString(facesContext, null, new ListRowKey<String>(null,
"a"));
+ fail();
+ } catch (NullPointerException e) {
+ }
+
+ try {
+ converter.getAsObject(facesContext, tree, "_x");
+ fail();
+ } catch (ConverterException e) {
+ FacesMessage message = e.getFacesMessage();
+ System.out.println(message.getSummary());
+ }
+
+ converter = new TreeRowKeyConverter() {
+ @Override
+ protected String convertKeyToString(FacesContext context,
+ UIComponent component, Object value) {
+
+ return Double.toString((Double) value);
+ }
+ };
+
+ try {
+ converter.getAsString(facesContext, tree, new ListRowKey<Object>(null,
"abc"));
+ fail();
+ } catch (ConverterException e) {
+ FacesMessage message = e.getFacesMessage();
+ System.out.println(message.getSummary());
+ }
+ }
+}