[Httpunit-commit] SF.net SVN: httpunit:[1051] trunk/httpunit
Brought to you by:
russgold
|
From: <wol...@us...> - 2009-08-21 11:08:20
|
Revision: 1051
http://httpunit.svn.sourceforge.net/httpunit/?rev=1051&view=rev
Author: wolfgang_fahl
Date: 2009-08-21 11:08:14 +0000 (Fri, 21 Aug 2009)
Log Message:
-----------
fix for BR 2534057 by Igor Kanshin
Modified Paths:
--------------
trunk/httpunit/src/com/meterware/httpunit/dom/HTMLContainerDelegate.java
trunk/httpunit/src/com/meterware/httpunit/dom/NodeImpl.java
trunk/httpunit/test/com/meterware/httpunit/WebLinkTest.java
trunk/httpunit/test/com/meterware/httpunit/dom/NodeTest.java
Modified: trunk/httpunit/src/com/meterware/httpunit/dom/HTMLContainerDelegate.java
===================================================================
--- trunk/httpunit/src/com/meterware/httpunit/dom/HTMLContainerDelegate.java 2009-08-21 09:43:42 UTC (rev 1050)
+++ trunk/httpunit/src/com/meterware/httpunit/dom/HTMLContainerDelegate.java 2009-08-21 11:08:14 UTC (rev 1051)
@@ -44,12 +44,12 @@
/**
* get Links for a given Node
- * @param rootNode
+ * @param rootNode - an array of forms
* @return
*/
HTMLCollection getLinks( NodeImpl rootNode ) {
ArrayList elements = new ArrayList();
- for (Iterator each = rootNode.preOrderIteratorAfterNode( _iteratorMask ); each.hasNext();) {
+ for (Iterator each = rootNode.preOrderIteratorWithinNode( _iteratorMask ); each.hasNext();) {
Node node = (Node) each.next();
if (node.getNodeType() != Node.ELEMENT_NODE) continue;
@@ -61,9 +61,14 @@
}
+ /**
+ * get forms for a given Node
+ * @param rootNode - the node to start from
+ * @return - an array of forms
+ */
HTMLCollection getForms( NodeImpl rootNode ) {
ArrayList elements = new ArrayList();
- for (Iterator each = rootNode.preOrderIteratorAfterNode( _iteratorMask ); each.hasNext();) {
+ for (Iterator each = rootNode.preOrderIteratorWithinNode( _iteratorMask ); each.hasNext();) {
Node node = (Node) each.next();
if (node.getNodeType() != Node.ELEMENT_NODE) continue;
Modified: trunk/httpunit/src/com/meterware/httpunit/dom/NodeImpl.java
===================================================================
--- trunk/httpunit/src/com/meterware/httpunit/dom/NodeImpl.java 2009-08-21 09:43:42 UTC (rev 1050)
+++ trunk/httpunit/src/com/meterware/httpunit/dom/NodeImpl.java 2009-08-21 11:08:14 UTC (rev 1051)
@@ -352,7 +352,23 @@
return new PreOrderIterator( PreOrderIterator.nextNode( this ) );
}
+ /**
+ *
+ * @return
+ */
+ public Iterator preOrderIteratorWithinNode() {
+ PreOrderIterator result = new PreOrderIterator( PreOrderIterator.nextNode( this ) );
+ result.setDoNotLeaveNode(this);
+ return result;
+ }
+
+ public Iterator preOrderIteratorWithinNode(IteratorMask mask) {
+ PreOrderIterator result = new PreOrderIterator( PreOrderIterator.nextNode( this ),mask );
+ result.setDoNotLeaveNode(this);
+ return result;
+ }
+
public Iterator preOrderIteratorAfterNode( IteratorMask mask ) {
return new PreOrderIterator( PreOrderIterator.nextNode( this ), mask );
}
@@ -367,36 +383,105 @@
}
+ /**
+ * allow masking of the iteration
+ */
interface IteratorMask {
+ // skip a given subtree
boolean skipSubtree( Node subtreeRoot );
}
-
+ /**
+ * iterator for Nodetrees that can be influenced with an Iterator mask to skip
+ * specific parts
+ */
static class PreOrderIterator implements Iterator {
private NodeImpl _nextNode;
+ private NodeImpl _startNode;
private IteratorMask _mask;
+ private NodeImpl _doNotLeaveNode=null;
+ /**
+ * get the limit node
+ * @return
+ */
+ public NodeImpl getDoNotLeaveNode() {
+ return _doNotLeaveNode;
+ }
+ /**
+ * limit the PreOrderIterator not to leave the given node
+ * @param doNotLeaveNode
+ */
+ public void setDoNotLeaveNode(NodeImpl doNotLeaveNode) {
+ _doNotLeaveNode = doNotLeaveNode;
+ }
+
+ /**
+ * check whether the node is a child of the doNotLeaveNode (if one is set)
+ * @param node
+ * @return
+ */
+ private boolean isChild(Node node) {
+ if (node==null) {
+ return false;
+ } if (_doNotLeaveNode==null) {
+ return true;
+ } else {
+ Node parent = node.getParentNode();
+ if (parent==null) {
+ return false;
+ } else {
+ if (parent.isSameNode(_doNotLeaveNode)) {
+ return true;
+ } else {
+ return isChild(parent);
+ }
+ }
+ }
+ }
+
+ /**
+ * create a PreOrderIterator starting at a given currentNode
+ * @param currentNode
+ */
PreOrderIterator( NodeImpl currentNode ) {
_nextNode = currentNode;
+ _startNode= currentNode;
}
+ /**
+ * create a PreOrderIterator starting at a given currentNode and setting
+ * the iterator mask to the given mask
+ * @param currentNode
+ * @param mask
+ */
PreOrderIterator( NodeImpl currentNode, IteratorMask mask ) {
this( currentNode );
_mask = mask;
}
+ /**
+ * is there still a next node?
+ */
public boolean hasNext() {
return null != _nextNode;
}
+ /**
+ * move one step in the tree
+ */
public Object next() {
NodeImpl currentNode = _nextNode;
_nextNode = nextNode( _nextNode );
- while (_mask != null && _nextNode != null && _mask.skipSubtree( _nextNode )) _nextNode = nextSubtree( _nextNode );
+ while (_mask != null && _nextNode != null && _mask.skipSubtree( _nextNode ))
+ _nextNode = nextSubtree( _nextNode );
+ // check that we fit the doNotLeaveNode condition in case there is one
+ if (!isChild(_nextNode))
+ _nextNode=null;
return currentNode;
}
@@ -421,4 +506,5 @@
return null;
}
}
+
}
Modified: trunk/httpunit/test/com/meterware/httpunit/WebLinkTest.java
===================================================================
--- trunk/httpunit/test/com/meterware/httpunit/WebLinkTest.java 2009-08-21 09:43:42 UTC (rev 1050)
+++ trunk/httpunit/test/com/meterware/httpunit/WebLinkTest.java 2009-08-21 11:08:14 UTC (rev 1051)
@@ -19,6 +19,8 @@
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/
+import org.xml.sax.SAXException;
+
import junit.framework.TestSuite;
@@ -52,6 +54,7 @@
"<body>This has no forms but it does\n" +
"have <a href='/other.html#middle' id='activeID'>an <b>active</b> link</A>\n" +
" and <a name=here>an anchor</a>\n" +
+ "<table><tr><td name='acell'><a href='basic.html' name='acelllink'>a link in a cell</a></td></tr></table>"+
"<a href='basic.html' name=\"nextLink\"><IMG SRC=\"/images/arrow.gif\" ALT=\"Next -->\" WIDTH=1 HEIGHT=4></a>\n" +
"<a href='another.html' name='myLink'>some text</a>\n" +
"</body></html>\n" );
@@ -107,11 +110,14 @@
assertTrue("the blank %20 in the link2 should not be converted but we got '"+link2.getURLString()+"'",link2.getURLString().equals(blankLink2));
}
-
+ /**
+ * check the number of links in the sample page
+ * @throws Exception
+ */
public void testLinks() throws Exception {
WebLink[] links = _simplePage.getLinks();
assertNotNull( "Found no links", links );
- assertEquals( "number of links in page", 3, links.length );
+ assertEquals( "number of links in page", 4, links.length );
}
@@ -141,6 +147,25 @@
assertEquals( "URLString", "/other.html", link.getURLString() );
}
+
+ /**
+ * test for BR 2534057
+ * getLinks() for a Cell return all page links
+ * @throws SAXException
+ */
+ public void testGetLinksForCell() throws SAXException {
+ HTMLElement[] elements = _simplePage.getElementsWithName("acell");
+ assertTrue(elements.length==1);
+ assertTrue(elements[0] instanceof TableCell);
+ TableCell aCell=(TableCell)elements[0];
+ WebLink[] cellLinks = aCell.getLinks();
+ for (int i=0;i<cellLinks.length;i++) {
+ WebLink link=cellLinks[i];
+ System.out.println("link "+i+"="+link.getName());
+ }
+ assertEquals(1,cellLinks.length);
+ assertEquals("acelllink",cellLinks[0].getName());
+ }
public void testGetLinkByText() throws Exception {
WebLink link = _simplePage.getLinkWith( "no link" );
Modified: trunk/httpunit/test/com/meterware/httpunit/dom/NodeTest.java
===================================================================
--- trunk/httpunit/test/com/meterware/httpunit/dom/NodeTest.java 2009-08-21 09:43:42 UTC (rev 1050)
+++ trunk/httpunit/test/com/meterware/httpunit/dom/NodeTest.java 2009-08-21 11:08:14 UTC (rev 1051)
@@ -372,6 +372,19 @@
assertFalse( "Iterator should have terminated after " + expectedNodes.length + " nodes", each.hasNext() );
}
+ /**
+ * Verifies that we can iterate through nodes in order, starting after a specific node.
+ */
+ public void testPreOrderIteratorWithinNode() throws Exception {
+ Iterator each = ((NodeImpl)_foo1).preOrderIteratorWithinNode();
+ Node[] expectedNodes = { _bar1, _text, _foo2};
+ for (int i = 0; i < expectedNodes.length; i++) {
+ assertTrue( "Iterator prematurely terminated after " + i + " nodes", each.hasNext() );
+ Object node = each.next();
+ assertSame( "Node " + (1 + i) + ":", expectedNodes[i], node );
+ }
+ assertFalse( "Iterator should have terminated after " + expectedNodes.length + " nodes", each.hasNext() );
+ }
/**
* Verifies that we can iterate through nodes in order skipping a specified subtree.
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|