That sounds like an excellent plan.  Thanks for the patch.

Someone (maybe me) will apply the patch soon.  I need it as well.

I can't tell you when it will be released though.  Hopefully, within the
next 2 weeks to 2 months.

Much thanks,

Nick Neuberger

> -----Original Message-----
> From: Bjorn Beskow [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, December 28, 2004 6:40 AM
> To: [email protected]
> Subject: [Jwebunit-development] RegExp matching
> 
> 
> Hi!
> 
> I discover over and over again that matching fixed text in 
> responses is too restrictive. There has been some discussion 
> in this list before about introducing the possibility to 
> write checks using regular expressions. Would it be possible 
> to add such functionality?
> 
> Attached is a patch which allows checks on matching regular 
> expressions anywhere in the response:
> 
> assertMatch(String regex)
> assertNoMatch(String regex)
> 
> as well in a named element:
> 
> assertMatchInElement(String elementID, String regex)
> assertNoMatchInElement(String elementID, String regex)
> 
> and in a named table:
> 
> assertMatchInTable(String tableSummaryOrId, String regex)
> assertNoMatchInTable(String tableSummaryOrId, String regex)
> 
> assertMatchInTable(String tableSummaryOrId, String[] regex)
> assertNoMatchInTable(String tableSummaryOrId, String[] regex).
> 
> Jakarta Regexp is used for the regexp matching (I don't think 
> it would be a good idea to make jWebUnit dependent on JDK 1.4).
> 
> The patch of course also includes corresponding test cases.
> 
> I would be very pleased if this functionality could be added 
> to jWebUnit!
> 
> Regards
> /Bj�rn
> 
> Index: src/net/sourceforge/jwebunit/HttpUnitDialog.java
> ===================================================================
> RCS file: 
> /cvsroot/jwebunit/jWebUnit/src/net/sourceforge/jwebunit/HttpUn
> itDialog.java,v
> retrieving revision 1.48
> diff -u -r1.48 HttpUnitDialog.java
> --- src/net/sourceforge/jwebunit/HttpUnitDialog.java  27 Sep 
> 2004 17:10:22 -0000   1.48
> +++ src/net/sourceforge/jwebunit/HttpUnitDialog.java  28 Dec 
> 2004 11:08:34 -0000
> @@ -18,6 +18,8 @@
>  import java.util.List;
>  import java.util.Map;
>  
> +import org.apache.regexp.RE;
> +
>  /**
>   * Acts as the wrapper for HttpUnit access. A dialog is 
> initialized with a given
>   * URL, and maintains conversational state as the dialog 
> progresses through link
> @@ -540,6 +542,21 @@
>      }
>  
>      /**
> +     * Return true if given regexp has a match anywhere in 
> the current response.
> +     * 
> +     * @param regexp
> +     *            regexp to match.
> +     */
> +    public boolean matchInResponse(String regexp) {
> +        try {
> +            RE re = new RE(regexp, RE.MATCH_SINGLELINE);
> +            return re.match(context.toEncodedString(resp.getText()));
> +        } catch (IOException e) {
> +            throw new 
> RuntimeException(ExceptionUtility.stackTraceToString(e));
> +        }
> +    }
> +
> +    /**
>       * Return true if given text is present in a specified 
> table of the
>       * response.
>       * 
> @@ -565,6 +582,34 @@
>          return false;
>      }
>  
> +    /**
> +     * Return true if given regexp has a match in a 
> specified table of the response.
> +     * 
> +     * @param tableSummaryOrId
> +     *            table summary or id to inspect for expected text.
> +     * @param regexp
> +     *            regexp to match.
> +     */
> +    public boolean matchInTable(String tableSummaryOrId, 
> String regexp) {
> +        WebTable table = getWebTableBySummaryOrId(tableSummaryOrId);
> +        if (table == null) {
> +            throw new RuntimeException("No table with 
> summary or id [" + tableSummaryOrId + "] found in response.");
> +        }
> +        RE re = new RE(regexp, RE.MATCH_SINGLELINE);
> +        for (int row = 0; row < table.getRowCount(); row++) {
> +            for (int col = 0; col < table.getColumnCount(); col++) {
> +                TableCell cell = table.getTableCell(row, col);
> +                if (cell != null) {
> +                    String cellHtml = getNodeHtml(cell.getDOM());
> +                    if (re.match(cellHtml)) {
> +                        return true;
> +                    }
> +                }
> +            }
> +        }
> +        return false;
> +    }
> +
>      private String getNodeHtml(Node node) {
>          String nodeHtml = "";
>          NodeList children = node.getChildNodes();
> @@ -1086,6 +1131,34 @@
>      }
>  
>      /**
> +     * Return true if a given regexp is contained within the 
> specified element.
> +     * 
> +     * @param element
> +     *            org.w3c.com.Element to inspect.
> +     * @param regexp
> +     *            regexp to match.
> +     */
> +    public boolean matchInElement(Element element, String regexp) {
> +        NodeList children = element.getChildNodes();
> +        RE re = new RE(regexp, RE.MATCH_SINGLELINE);
> +        for (int i = 0; i < children.getLength(); i++) {
> +            Node child = children.item(i);
> +            if (child.getNodeType() == Node.TEXT_NODE) {
> +                if (re.match(((Text) child).getData())) {
> +                    return true;
> +                }
> +            }
> +
> +            if (child.getNodeType() == Node.ELEMENT_NODE) {
> +                if (matchInElement((Element) child, regexp)) {
> +                    return true;
> +                }
> +            }
> +        }
> +        return false;
> +    }
> +
> +    /**
>       * Make the window with the given name in the current 
> conversation active.
>       * 
>       * @param windowName
> Index: src/net/sourceforge/jwebunit/WebTestCase.java
> ===================================================================
> RCS file: 
> /cvsroot/jwebunit/jWebUnit/src/net/sourceforge/jwebunit/WebTes
> tCase.java,v
> retrieving revision 1.50
> diff -u -r1.50 WebTestCase.java
> --- src/net/sourceforge/jwebunit/WebTestCase.java     30 Sep 
> 2004 16:11:57 -0000   1.50
> +++ src/net/sourceforge/jwebunit/WebTestCase.java     28 Dec 
> 2004 11:05:21 -0000
> @@ -66,6 +66,10 @@
>          tester.assertTextPresent(text);
>      }
>  
> +    public void assertMatch(String regexp) {
> +        tester.assertMatch(regexp);
> +    }
> +
>      public void assertKeyNotPresent(String key) {
>          tester.assertKeyNotPresent(key);
>      }
> @@ -74,6 +78,10 @@
>          tester.assertTextNotPresent(text);
>      }
>  
> +    public void assertNoMatch(String regexp) {
> +        tester.assertNoMatch(regexp);
> +    }
> +
>      public void assertTablePresent(String tableSummaryOrId) {
>          tester.assertTablePresent(tableSummaryOrId);
>      }
> @@ -90,6 +98,10 @@
>          tester.assertTextInTable(tableSummaryOrId, text);
>      }
>  
> +    public void assertMatchInTable(String tableSummaryOrId, 
> String regexp) {
> +        tester.assertMatchInTable(tableSummaryOrId, regexp);
> +    }
> +
>      public void assertKeysInTable(String tableSummaryOrId, 
> String[] keys) {
>          tester.assertKeysInTable(tableSummaryOrId, keys);
>      }
> @@ -98,6 +110,10 @@
>          tester.assertTextInTable(tableSummaryOrId, text);
>      }
>  
> +    public void assertMatchInTable(String tableSummaryOrId, 
> String[] regexp) {
> +        tester.assertMatchInTable(tableSummaryOrId, regexp);
> +    }
> +
>      public void assertKeyNotInTable(String tableSummaryOrId, 
> String key) {
>          tester.assertKeyNotInTable(tableSummaryOrId, key);
>      }
> @@ -110,6 +126,14 @@
>          tester.assertTextNotInTable(tableSummaryOrId, text);
>      }
>  
> +    public void assertNoMatchInTable(String 
> tableSummaryOrId, String regexp) {
> +        tester.assertNoMatchInTable(tableSummaryOrId, regexp);
> +    }
> +
> +    public void assertNoMatchInTable(String 
> tableSummaryOrId, String[] regexp) {
> +        tester.assertNoMatchInTable(tableSummaryOrId, regexp);
> +    }
> +
>      public void assertTableEquals(String tableSummaryOrId,
>              ExpectedTable expectedTable) {
>          tester.assertTableEquals(tableSummaryOrId, expectedTable
> @@ -303,6 +327,14 @@
>          tester.assertTextNotInElement(elID, text);
>      }
>  
> +    public void assertMatchInElement(String elID, String regexp) {
> +        tester.assertMatchInElement(elID, regexp);
> +    }
> +
> +    public void assertNoMatchInElement(String elID, String regexp) {
> +        tester.assertNoMatchInElement(elID, regexp);
> +    }
> +
>      public void assertWindowPresent(String windowName) {
>          tester.assertWindowPresent(windowName);
>      }
> Index: src/net/sourceforge/jwebunit/WebTester.java
> ===================================================================
> RCS file: 
> /cvsroot/jwebunit/jWebUnit/src/net/sourceforge/jwebunit/WebTes
> ter.java,v
> retrieving revision 1.53
> diff -u -r1.53 WebTester.java
> --- src/net/sourceforge/jwebunit/WebTester.java       30 Sep 
> 2004 16:11:57 -0000   1.53
> +++ src/net/sourceforge/jwebunit/WebTester.java       28 Dec 
> 2004 11:05:21 -0000
> @@ -147,6 +147,16 @@
>      }
>  
>      /**
> +     * Assert that supplied regexp is matched.
> +     *
> +     * @param regexp
> +     */
> +    public void assertMatch(String regexp) {
> +        if (!dialog.matchInResponse(regexp))
> +            Assert.fail("Expected rexexp not matched in 
> response: [" + regexp + "]");
> +    }
> +
> +    /**
>       * Assert that a web resource's value is not present.
>       * 
>       * @param key
> @@ -168,6 +178,16 @@
>      }
>  
>      /**
> +     * Assert that supplied regexp is not present.
> +     *
> +     * @param regexp
> +     */
> +    public void assertNoMatch(String regexp) {
> +        if (dialog.matchInResponse(regexp))
> +            Assert.fail("Regexp matched in response when not 
> expected: [" + regexp + "]");
> +    }
> +
> +    /**
>       * Assert that a table with a given summary or id value 
> is present.
>       * 
>       * @param tableSummaryOrId
> @@ -218,6 +238,20 @@
>      }
>  
>      /**
> +     * Assert that supplied regexp is matched in a specific table.
> +     *
> +     * @param tableSummaryOrId summary or id attribute value of table
> +     * @param regexp
> +     */
> +    public void assertMatchInTable(String tableSummaryOrId, 
> String regexp) {
> +        assertTablePresent(tableSummaryOrId);
> +        Assert.assertTrue("Could not match: [" + regexp + "]" +
> +                "in table [" + tableSummaryOrId + "]",
> +                dialog.matchInTable(tableSummaryOrId, regexp));
> +    }
> +
> +
> +    /**
>       * Assert that the values of a set of web resources are 
> all present in a
>       * specific table.
>       * 
> @@ -247,6 +281,18 @@
>      }
>  
>      /**
> +     * Assert that a set of regexp values are all matched in 
> a specific table.
> +     *
> +     * @param tableSummaryOrId summary or id attribute value of table
> +     * @param text Array of expected regexps to match.
> +     */
> +    public void assertMatchInTable(String tableSummaryOrId, 
> String[] regexp) {
> +        for (int i = 0; i < regexp.length; i++) {
> +            assertMatchInTable(tableSummaryOrId, regexp[i]);
> +        }
> +    }
> +
> +    /**
>       * Assert that the value of a given web resource is not 
> present in a
>       * specific table.
>       * 
> @@ -288,6 +334,31 @@
>      }
>  
>      /**
> +     * Assert that supplied regexp is not present in a 
> specific table.
> +     *
> +     * @param tableSummaryOrId summary or id attribute value of table
> +     * @param text
> +     */
> +    public void assertNoMatchInTable(String 
> tableSummaryOrId, String regexp) {
> +        assertTablePresent(tableSummaryOrId);
> +        Assert.assertTrue("Found regexp: [" + regexp + "] in 
> table [" +
> +                tableSummaryOrId + "]",
> +                !dialog.matchInTable(tableSummaryOrId, regexp));
> +    }
> +
> +    /**
> +     * Assert that none of a set of regexp values are 
> present in a specific table.
> +     *
> +     * @param tableSummaryOrId summary or id attribute value of table
> +     * @param text Array of text values
> +     */
> +    public void assertNoMatchInTable(String 
> tableSummaryOrId, String[] regexp) {
> +        for (int i = 0; i < regexp.length; i++) {
> +            assertNoMatchInTable(tableSummaryOrId, regexp[i]);
> +        }
> +    }
> +
> +    /**
>       * Assert that a specific table matches an ExpectedTable.
>       * 
>       * @param tableSummaryOrId
> @@ -904,6 +975,33 @@
>      }
>  
>      /**
> +     * Assert that a given element matches a specific regexp.
> +     *
> +     * @param elementID id of element to be inspected.
> +     * @param regexp to match.
> +     */
> +    public void assertMatchInElement(String elementID, 
> String regexp) {
> +        Element element = dialog.getElement(elementID);
> +        Assert.assertNotNull("Unable to locate element with 
> id \"" + elementID + "\"", element);
> +        Assert.assertTrue("Unable to match [" + regexp + "] 
> in element \"" + elementID + "\"", 
> dialog.matchInElement(element, regexp));
> +    }
> +
> +    /**
> +     * Assert that a given element does not match a specific regexp.
> +     *
> +     * @param elementID id of element to be inspected.
> +     * @param regexp to match.
> +     */
> +    public void assertNoMatchInElement(String elementID, 
> String regexp) {
> +        assertElementPresent(elementID);
> +        Element element = dialog.getElement(elementID);
> +        Assert.assertNotNull("Unable to locate element with 
> id \"" + elementID
> +                + "\"", element);
> +        Assert.assertFalse("Regexp [" + regexp + "] matched 
> in element [" + elementID
> +                + "] when not expected", 
> dialog.matchInElement(element, regexp));
> +    }
> +
> +    /**
>       * Assert that a window with the given name is open.
>       * 
>       * @param windowName
> Index: test/net/sourceforge/jwebunit/TableAssertionsTest.java
> ===================================================================
> RCS file: 
> /cvsroot/jwebunit/jWebUnit/test/net/sourceforge/jwebunit/Table
> AssertionsTest.java,v
> retrieving revision 1.6
> diff -u -r1.6 TableAssertionsTest.java
> --- test/net/sourceforge/jwebunit/TableAssertionsTest.java    
> 16 Sep 2004 16:41:38 -0000    1.6
> +++ test/net/sourceforge/jwebunit/TableAssertionsTest.java    
> 28 Dec 2004 11:06:36 -0000
> @@ -45,6 +45,30 @@
>                         new Object[]{"testTable", new 
> String[]{"no such row 1", "table text row 2"}});
>      }
>  
> +    public void testAssertMatchInTable() throws Throwable {
> +        assertPassFail("assertMatchInTable",
> +                       new Object[]{"testTable", "table [Tt]ext"},
> +                       new Object[]{"testTable", "no.*text"});
> +    }
> +
> +    public void testAssertNoMatchInTable() throws Throwable {
> +        assertPassFail("assertNoMatchInTable",
> +                       new Object[]{"testTable", "no.*text"},
> +                       new Object[]{"testTable", "table [Tt]ext"});
> +    }
> +
> +    public void testAssertMatchArrayInTable() throws Throwable {
> +        assertPassFail("assertMatchInTable",
> +                       new Object[]{"testTable", new 
> String[]{"table [Tt]ext", "table [Tt]ext row 2"}},
> +                       new Object[]{"testTable", new 
> String[]{"table [Tt]ext", "no.*row 2"}});
> +    }
> +
> +    public void testAssertNoMatchArrayInTable() throws Throwable {
> +        assertPassFail("assertNoMatchInTable",
> +                       new Object[]{"testTable", new 
> String[]{"no.*row 1", "no.*row 2"}},
> +                       new Object[]{"testTable", new 
> String[]{"no.*row 1", "table [Tt]ext row 2"}});
> +    }
> +
>      public void testAssertTableEquals() throws Throwable {
>          assertPass("assertTableEquals",
>                     new Object[]{"testTable", new 
> String[][]{{"table text", ""},
> Index: test/net/sourceforge/jwebunit/WebAssertionsTest.java
> ===================================================================
> RCS file: 
> /cvsroot/jwebunit/jWebUnit/test/net/sourceforge/jwebunit/WebAs
> sertionsTest.java,v
> retrieving revision 1.17
> diff -u -r1.17 WebAssertionsTest.java
> --- test/net/sourceforge/jwebunit/WebAssertionsTest.java      
> 11 Jun 2004 02:38:01 -0000    1.17
> +++ test/net/sourceforge/jwebunit/WebAssertionsTest.java      
> 28 Dec 2004 11:06:36 -0000
> @@ -26,10 +26,18 @@
>          assertPassFail("assertTextPresent", "This is a test 
> page.", "no such text");
>      }
>  
> +    public void testAssertMatch() throws Throwable {
> +        assertPassFail("assertMatch", "This (is)* a .* 
> page.", "no.*text");
> +    }
> +
>      public void testAssertTextNotPresent() throws Throwable {
>          assertPassFail("assertTextNotPresent", "no such 
> text", "This is a test page.");
>      }
>  
> +    public void testAssertNoMatch() throws Throwable {
> +        assertPassFail("assertNoMatch", "no.*text", "This 
> (is)* a .* page.");
> +    }
> +
>      public void testAssertLinkPresentWithText() throws Throwable {
>          assertPassFail("assertLinkPresentWithText", "test 
> link", "no such link");
>      }
> @@ -100,6 +108,25 @@
>          assertTextInElement("outer2", "$100,000/$300,000");
>      }
>  
> +    public void testAssertNoMatchInElement() throws Throwable {
> +        assertNoMatchInElement("outer_id", "no[Ss]uchtext");
> +        assertNoMatchInElement("inner_id", "Out+er");
> +        assertFail("assertNoMatchInElement", new Object[] 
> {"outer_id", "Out+er"});
> +    }
> +
> +    public void testAssertMatchInElement() throws Throwable {
> +        assertMatchInElement("span_id", "Sp[Aa]n");
> +        assertMatchInElement("span_id", "Te+xt");
> +        assertMatchInElement("span_id", "Span\\sText");
> +        assertFail("assertMatchInElement", new Object[] 
> {"span_id", "Not.*Text"});
> +    }
> +    
> +    public void testAssertMatchInElementChild() throws Throwable {
> +        assertMatchInElement("outer_id", "Out+er");
> +        assertMatchInElement("outer_id", "Texx*t");
> +        assertMatchInElement("outer_id", "Inner.*Text");
> +    }
> +
>  
>      private void addTestPage() {
>          defineWebPage("testPage", "This is a test page." +
> 
> 
> -------------------------------------------------
> WebMail fr�n Tele2 http://www.tele2.se
> -------------------------------------------------
> 


- - - - - - 
This e-mail message is intended only for the use of the individual or entity
identified in the alias address of this message and may contain confidential
and privileged information. Any unauthorized review, use, disclosure or
distribution of this e-mail message is strictly prohibited. If you have
received this e-mail message in error, please notify the sender immediately
by reply e-mail and delete this message from your system. Thank you.


-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/
_______________________________________________
Jwebunit-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-development

Reply via email to