Author: nbelaevski
Date: 2008-08-26 20:34:27 -0400 (Tue, 26 Aug 2008)
New Revision: 10205
Added:
trunk/framework/api/src/test/java/org/richfaces/
trunk/framework/api/src/test/java/org/richfaces/model/
trunk/framework/api/src/test/java/org/richfaces/model/ClassicTreeModelKeyConversionTest.java
trunk/framework/api/src/test/java/org/richfaces/model/SwingTreeModelKeyConversionTest.java
Modified:
trunk/framework/api/src/main/java/org/richfaces/model/ClassicTreeDataModel.java
trunk/framework/api/src/main/java/org/richfaces/model/SwingTreeDataModel.java
trunk/framework/api/src/main/java/org/richfaces/model/SwingTreeNodeImpl.java
Log:
https://jira.jboss.org/jira/browse/RF-2813
Modified: trunk/framework/api/src/main/java/org/richfaces/model/ClassicTreeDataModel.java
===================================================================
---
trunk/framework/api/src/main/java/org/richfaces/model/ClassicTreeDataModel.java 2008-08-26
23:38:52 UTC (rev 10204)
+++
trunk/framework/api/src/main/java/org/richfaces/model/ClassicTreeDataModel.java 2008-08-27
00:34:27 UTC (rev 10205)
@@ -57,7 +57,8 @@
@Override
public Object convertToKey(FacesContext context, String keyString, UIComponent
component, Converter converter) {
- String[] strings = ListRowKey.fromString(keyString);
+ //TODO optimize search for empty string
+ String[] strings = ListRowKey.fromString(keyString);
List<Object> list = new ArrayList<Object>(strings.length);
TreeNode node = (TreeNode) getWrappedData();
Modified: trunk/framework/api/src/main/java/org/richfaces/model/SwingTreeDataModel.java
===================================================================
---
trunk/framework/api/src/main/java/org/richfaces/model/SwingTreeDataModel.java 2008-08-26
23:38:52 UTC (rev 10204)
+++
trunk/framework/api/src/main/java/org/richfaces/model/SwingTreeDataModel.java 2008-08-27
00:34:27 UTC (rev 10205)
@@ -57,22 +57,21 @@
public void setWrappedData(Object data) {
if (data != null) {
SwingTreeNodeImpl treeNodeImpl = new SwingTreeNodeImpl();
- int counter = 0;
if (data instanceof Collection<?>) {
Collection<?> collection = (Collection<?>) data;
for (Iterator<?> iterator = collection.iterator(); iterator
.hasNext();) {
- treeNodeImpl.addChild(Integer.valueOf(counter++), (TreeNode) iterator.next());
+ treeNodeImpl.addChild((TreeNode) iterator.next());
}
} else if (data.getClass().isArray()) {
Object[] nodes = (Object[]) data;
for (int i = 0; i < nodes.length; i++) {
- treeNodeImpl.addChild(Integer.valueOf(counter++), (TreeNode) nodes[i]);
+ treeNodeImpl.addChild((TreeNode) nodes[i]);
}
} else {
- treeNodeImpl.addChild(Integer.valueOf(counter++), (TreeNode) data);
+ treeNodeImpl.addChild((TreeNode) data);
}
this.treeNode = treeNodeImpl;
Modified: trunk/framework/api/src/main/java/org/richfaces/model/SwingTreeNodeImpl.java
===================================================================
---
trunk/framework/api/src/main/java/org/richfaces/model/SwingTreeNodeImpl.java 2008-08-26
23:38:52 UTC (rev 10204)
+++
trunk/framework/api/src/main/java/org/richfaces/model/SwingTreeNodeImpl.java 2008-08-27
00:34:27 UTC (rev 10205)
@@ -125,6 +125,10 @@
return children.isEmpty();
}
+ public void addChild(TreeNode node) {
+ children.add(node);
+ }
+
public void addChild(Object key, TreeNode node) {
if (Integer.valueOf(children.size()).equals(key)) {
children.add(node);
Added:
trunk/framework/api/src/test/java/org/richfaces/model/ClassicTreeModelKeyConversionTest.java
===================================================================
---
trunk/framework/api/src/test/java/org/richfaces/model/ClassicTreeModelKeyConversionTest.java
(rev 0)
+++
trunk/framework/api/src/test/java/org/richfaces/model/ClassicTreeModelKeyConversionTest.java 2008-08-27
00:34:27 UTC (rev 10205)
@@ -0,0 +1,142 @@
+/**
+ * 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.model;
+
+import java.util.Iterator;
+
+import junit.framework.TestCase;
+
+/**
+ * Created 26.08.2008
+ * @author Nick Belaevski
+ * @since 3.2.2
+ */
+
+public class ClassicTreeModelKeyConversionTest extends TestCase {
+
+ private ClassicTreeDataModel dataModel;
+
+ /* (non-Javadoc)
+ * @see junit.framework.TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ dataModel = new ClassicTreeDataModel();
+
+ TreeNodeImpl<String> rootNode = new TreeNodeImpl<String>();
+
+ TreeNodeImpl<String> node;
+
+ node = new TreeNodeImpl<String>();
+ node.addChild("0", new TreeNodeImpl<String>());
+ node.addChild("1", new TreeNodeImpl<String>());
+ for (int i = 2; i < 5; i++) {
+ node.addChild(Long.valueOf(i), new TreeNodeImpl<String>());
+ }
+ rootNode.addChild("6", node);
+
+ node = new TreeNodeImpl<String>();
+ for (int i = 0; i < 3; i++) {
+ node.addChild(Integer.valueOf(i), new TreeNodeImpl<String>());
+ }
+ rootNode.addChild("abc_cde:fgh", node);
+
+ dataModel.setWrappedData(rootNode);
+ }
+
+ /* (non-Javadoc)
+ * @see junit.framework.TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+
+ dataModel = null;
+ }
+
+ public void testEmptyString() throws Exception {
+ TreeRowKey<Object> key = (TreeRowKey<Object>) dataModel.convertToKey(null,
"", null, null);
+ assertNull(key);
+ }
+
+ public void testStraightWayShort() throws Exception {
+ TreeRowKey<Object> key = (TreeRowKey<Object>) dataModel.convertToKey(null,
"6", null, null);
+ assertNotNull(key);
+
+ Iterator<Object> iterator = key.iterator();
+ assertEquals("6", iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+ public void testStraightWay() throws Exception {
+ TreeRowKey<Object> key = (TreeRowKey<Object>) dataModel.convertToKey(null,
"6:1", null, null);
+ assertNotNull(key);
+
+ Iterator<Object> iterator = key.iterator();
+ assertEquals("6", iterator.next());
+ assertEquals("1", iterator.next());
+ assertFalse(iterator.hasNext());
+
+ key = (TreeRowKey<Object>) dataModel.convertToKey(null, "6:1", null,
null);
+ assertNotNull(key);
+
+ iterator = key.iterator();
+ assertEquals("6", iterator.next());
+ assertEquals("1", iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+ public void testSearch() throws Exception {
+ TreeRowKey<Object> key = (TreeRowKey<Object>) dataModel.convertToKey(null,
"6:4", null, null);
+ assertNotNull(key);
+
+ Iterator<Object> iterator = key.iterator();
+ assertEquals("6", iterator.next());
+ assertEquals(Long.valueOf(4), iterator.next());
+ assertFalse(iterator.hasNext());
+
+ }
+
+ public void testSearchMissing() throws Exception {
+ assertNull(dataModel.convertToKey(null, "6:10", null, null));
+ assertNull(dataModel.convertToKey(null, "7", null, null));
+ }
+
+ public void testUnescape() throws Exception {
+ TreeRowKey<Object> key = (TreeRowKey<Object>) dataModel.convertToKey(null,
"abc__cde_:fgh:1", null, null);
+ assertNotNull(key);
+
+ Iterator<Object> iterator = key.iterator();
+ assertEquals("abc_cde:fgh", iterator.next());
+ assertEquals(Integer.valueOf(1), iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+ public void testUnescapeShort() throws Exception {
+ TreeRowKey<Object> key = (TreeRowKey<Object>) dataModel.convertToKey(null,
"abc__cde_:fgh", null, null);
+ assertNotNull(key);
+
+ Iterator<Object> iterator = key.iterator();
+ assertEquals("abc_cde:fgh", iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+}
Added:
trunk/framework/api/src/test/java/org/richfaces/model/SwingTreeModelKeyConversionTest.java
===================================================================
---
trunk/framework/api/src/test/java/org/richfaces/model/SwingTreeModelKeyConversionTest.java
(rev 0)
+++
trunk/framework/api/src/test/java/org/richfaces/model/SwingTreeModelKeyConversionTest.java 2008-08-27
00:34:27 UTC (rev 10205)
@@ -0,0 +1,109 @@
+/**
+ * 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.model;
+
+import java.util.Iterator;
+
+import javax.swing.tree.TreeNode;
+
+import junit.framework.TestCase;
+
+/**
+ * Created 26.08.2008
+ * @author Nick Belaevski
+ * @since 3.2.2
+ */
+
+public class SwingTreeModelKeyConversionTest extends TestCase {
+
+ private SwingTreeDataModel dataModel;
+
+ /* (non-Javadoc)
+ * @see junit.framework.TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ dataModel = new SwingTreeDataModel();
+
+ //1
+ SwingTreeNodeImpl node = new SwingTreeNodeImpl();
+ //1:0
+ node.addChild(new SwingTreeNodeImpl());
+ //1:1
+ node.addChild(new SwingTreeNodeImpl());
+ //1:2
+ node.addChild(new SwingTreeNodeImpl());
+
+ dataModel.setWrappedData(new TreeNode[] {
+ new SwingTreeNodeImpl(), node
+ });
+ }
+
+ /* (non-Javadoc)
+ * @see junit.framework.TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+
+ dataModel = null;
+ }
+
+ public void testEmptyString() throws Exception {
+ //TODO make it work
+// Object key = dataModel.convertToKey(null, "", null, null);
+// assertNull(key);
+ }
+
+ public void testStraightWay() throws Exception {
+ TreeRowKey<Integer> key;
+ Iterator<Integer> iterator;
+
+ key = (TreeRowKey<Integer>) dataModel.convertToKey(null, "0", null,
null);
+ iterator = key.iterator();
+ assertEquals(Integer.valueOf(0), iterator.next());
+ assertFalse(iterator.hasNext());
+
+ key = (TreeRowKey<Integer>) dataModel.convertToKey(null, "1", null,
null);
+ iterator = key.iterator();
+ assertEquals(Integer.valueOf(1), iterator.next());
+ assertFalse(iterator.hasNext());
+
+ key = (TreeRowKey<Integer>) dataModel.convertToKey(null, "1:0", null,
null);
+ iterator = key.iterator();
+ assertEquals(Integer.valueOf(1), iterator.next());
+ assertEquals(Integer.valueOf(0), iterator.next());
+ assertFalse(iterator.hasNext());
+
+ key = (TreeRowKey<Integer>) dataModel.convertToKey(null, "1:2", null,
null);
+ iterator = key.iterator();
+ assertEquals(Integer.valueOf(1), iterator.next());
+ assertEquals(Integer.valueOf(2), iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+ public void testMissingNodes() throws Exception {
+ assertNull(dataModel.convertToKey(null, "2", null, null));
+ assertNull(dataModel.convertToKey(null, "0:3", null, null));
+ }
+}
+