Update of /cvsroot/jwebunit/jWebUnit/src/net/sourceforge/jwebunit
In directory 
sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22368/src/net/sourceforge/jwebunit

Modified Files:
        CompositeJWebUnitDialog.java HttpUnitDialog.java 
        IJWebUnitDialog.java WebTestCase.java WebTester.java 
Log Message:
Added support for assertions using regular expressions.

Index: WebTester.java
===================================================================
RCS file: 
/cvsroot/jwebunit/jWebUnit/src/net/sourceforge/jwebunit/WebTester.java,v
retrieving revision 1.57
retrieving revision 1.58
diff -C2 -d -r1.57 -r1.58
*** WebTester.java      11 May 2005 12:11:15 -0000      1.57
--- WebTester.java      15 May 2005 19:55:12 -0000      1.58
***************
*** 15,18 ****
--- 15,19 ----
  import net.sourceforge.jwebunit.util.ExceptionUtility;
  
+ import org.apache.regexp.RE;
  import org.w3c.dom.Element;
  
***************
*** 178,181 ****
--- 179,195 ----
        }
  
+     /**
+      * Assert title of current html page in conversation matches an expected
+      * regexp.
+      * 
+      * @param regexp
+      *            expected title regexp
+      */
+     public void assertTitleMatch(String regexp) {
+         RE re = new RE(regexp, RE.MATCH_SINGLELINE);
+         Assert.assertTrue("Unable to match [" + regexp + "] in title",
+                           re.match(getDialog().getResponsePageTitle()));
+     }
+ 
        /**
         * Assert title of current html page matches the value of a specified 
web
***************
*** 210,213 ****
--- 224,237 ----
        }
  
+     /**
+      * Assert that supplied regexp is matched.
+      *
+      * @param regexp
+      */
+     public void assertMatch(String regexp) {
+         if (!getDialog().isMatchInResponse(regexp))
+             Assert.fail("Expected rexexp not matched in response: [" + regexp 
+ "]");
+     }
+ 
        /**
         * Assert that a web resource's value is not present.
***************
*** 231,234 ****
--- 255,268 ----
        }
  
+     /**
+      * Assert that supplied regexp is not present.
+      *
+      * @param regexp
+      */
+     public void assertNoMatch(String regexp) {
+         if (getDialog().isMatchInResponse(regexp))
+             Assert.fail("Regexp matched in response when not expected: [" + 
regexp + "]");
+     }
+ 
        /**
         * Assert that a table with a given summary or id value is present.
***************
*** 280,283 ****
--- 314,330 ----
        }
  
+     /**
+      * 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 + "]",
+                 getDialog().isMatchInTable(tableSummaryOrId, regexp));
+     }
+ 
        /**
         * Assert that the values of a set of web resources are all present in a
***************
*** 309,312 ****
--- 356,371 ----
        }
  
+     /**
+      * 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
***************
*** 350,353 ****
--- 409,437 ----
        }
  
+     /**
+      * 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 + "]",
+                 !getDialog().isMatchInTable(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.
***************
*** 437,440 ****
--- 521,609 ----
        }
  
+     /**
+      * Assert that a specific table matches an ExpectedTable.
+      * 
+      * @param tableSummaryOrId
+      *            summary or id attribute value of table
+      * @param expectedTable
+      *            represents expected regexps (colspan supported).
+      */
+     public void assertTableMatch(String tableSummaryOrId,
+             ExpectedTable expectedTable) {
+         assertTableMatch(tableSummaryOrId, 
expectedTable.getExpectedStrings());
+     }
+ 
+     /**
+      * Assert that a specific table matches a matrix of supplied regexps.
+      * 
+      * @param tableSummaryOrId
+      *            summary or id attribute value of table
+      * @param expectedCellValues
+      *            double dimensional array of expected regexps
+      */
+     public void assertTableMatch(String tableSummaryOrId,
+             String[][] expectedCellValues) {
+         assertTableRowsMatch(tableSummaryOrId, 0, expectedCellValues);
+     }
+ 
+     /**
+      * Assert that a range of rows for a specific table matches a matrix of
+      * supplied regexps.
+      * 
+      * @param tableSummaryOrId
+      *            summary or id attribute value of table
+      * @param startRow
+      *            index of start row for comparison
+      * @param expectedTable
+      *            represents expected regexps (colspan supported).
+      */
+     public void assertTableRowsMatch(String tableSummaryOrId, int startRow,
+             ExpectedTable expectedTable) {
+         assertTableRowsMatch(tableSummaryOrId, startRow, expectedTable
+                 .getExpectedStrings());
+     }
+ 
+     /**
+      * Assert that a range of rows for a specific table matches a matrix of
+      * supplied regexps.
+      * 
+      * @param tableSummaryOrId
+      *            summary or id attribute value of table
+      * @param startRow
+      *            index of start row for comparison
+      * @param expectedCellValues
+      *            double dimensional array of expected regexps
+      */
+     public void assertTableRowsMatch(String tableSummaryOrId, int startRow,
+             String[][] expectedCellValues) {
+         assertTablePresent(tableSummaryOrId);
+         String[][] actualTableCellValues;
+         if (tableEmptyCellCompression) {
+             actualTableCellValues = 
getDialog().getSparseTableBySummaryOrId(tableSummaryOrId);
+         } else {
+             actualTableCellValues = 
getDialog().getWebTableBySummaryOrId(tableSummaryOrId).asText();
+         }
+         if (expectedCellValues.length > (actualTableCellValues.length - 
startRow))
+                 Assert.fail("Expected rows [" + expectedCellValues.length
+                         + "] larger than actual rows in range being compared"
+                         + " [" + (actualTableCellValues.length - startRow)
+                         + "].");
+         for (int i = 0; i < expectedCellValues.length; i++) {
+             String[] row = expectedCellValues[i];
+             for (int j = 0; j < row.length; j++) {
+                 if (row.length != actualTableCellValues[i].length)
+                         Assert.fail("Unequal number of columns for row " + i
+                                 + " of table " + tableSummaryOrId
+                                 + ". Expected [" + row.length + "] found ["
+                                 + actualTableCellValues[i].length + "].");
+                 String regexp = row[j];
+                 RE re = new RE(regexp, RE.MATCH_SINGLELINE);
+                 Assert.assertTrue("Unable to match " + regexp + " in " + 
tableSummaryOrId
+                     + " at [" + i + "," + j + "].",
+                     
re.match(getTestContext().toEncodedString(actualTableCellValues[i + 
startRow][j].trim())));
+             }
+         }
+     }
+ 
        /**
         * Assert that a form input element with a given name is present.
***************
*** 539,542 ****
--- 708,725 ----
        }
  
+     /**
+      * Assert that a specific form element matches an expected regexp.
+      * 
+      * @param formElementName
+      * @param regexp
+      */
+     public void assertFormElementMatch(String formElementName,
+             String regexp) {
+         assertFormElementPresent(formElementName);
+         RE re = new RE(regexp, RE.MATCH_SINGLELINE);
+         Assert.assertTrue("Unable to match [" + regexp + "] in form element 
\"" + formElementName + "\"",
+                           
re.match(getDialog().getFormParameterValue(formElementName)));
+     }
+ 
        /**
         * Assert that a form element had no value / is empty.
***************
*** 759,762 ****
--- 942,961 ----
        }
  
+     /**
+      * Assert that the currently selected display value of a select box 
matches
+      * a given value.
+      * 
+      * @param selectName
+      *            name of the select element.
+      * @param option
+      *            expected display value of the selected option.
+      */
+     public void assertOptionMatch(String selectName, String regexp) {
+         assertFormElementPresent(selectName);
+         RE re = new RE(regexp, RE.MATCH_SINGLELINE);
+         Assert.assertTrue("Unable to match [" + regexp + "] in option \"" + 
selectName + "\"",
+                           
re.match(getDialog().getSelectedOption(selectName)));
+     }
+ 
        /**
         * Assert that a submit button with a given name is present.
***************
*** 1036,1039 ****
--- 1235,1265 ----
        }
  
+     /**
+      * 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 = getDialog().getElement(elementID);
+         Assert.assertNotNull("Unable to locate element with id \"" + 
elementID + "\"", element);
+         Assert.assertTrue("Unable to match [" + regexp + "] in element \"" + 
elementID + "\"", getDialog().isMatchInElement(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 = getDialog().getElement(elementID);
+         Assert.assertNotNull("Unable to locate element with id \"" + elementID
+                 + "\"", element);
+         Assert.assertFalse("Regexp [" + regexp + "] matched in element [" + 
elementID
+                 + "] when not expected", 
getDialog().isMatchInElement(element, regexp));
+     }
+ 
        /**
         * Assert that a window with the given name is open.
***************
*** 1084,1087 ****
--- 1310,1320 ----
        }
  
+     public void assertCookieValueMatch(String cookieName, String regexp) {
+         assertCookiePresent(cookieName);
+         RE re = new RE(regexp, RE.MATCH_SINGLELINE);
+         Assert.assertTrue("Unable to match [" + regexp + "] in cookie \"" + 
cookieName + "\"",
+                           re.match(getDialog().getCookieValue(cookieName)));
+     }
+ 
        public void dumpCookies() {
                dumpCookies(System.out);

Index: CompositeJWebUnitDialog.java
===================================================================
RCS file: 
/cvsroot/jwebunit/jWebUnit/src/net/sourceforge/jwebunit/CompositeJWebUnitDialog.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** CompositeJWebUnitDialog.java        15 Apr 2005 04:46:25 -0000      1.5
--- CompositeJWebUnitDialog.java        15 May 2005 19:55:12 -0000      1.6
***************
*** 282,285 ****
--- 282,294 ----
         * (non-Javadoc)
         * 
+      * @see 
net.sourceforge.jwebunit.IJWebUnitDialog#isMatchInResponse(java.lang.String)
+      */
+     public boolean isMatchInResponse(String regexp) {
+         throw new UnsupportedOperationException("matchInResponse");
+     }
+ 
+     /*
+      * (non-Javadoc)
+      * 
         * @see 
net.sourceforge.jwebunit.IJWebUnitDialog#isTextInTable(java.lang.String,
         *      java.lang.String)
***************
*** 292,295 ****
--- 301,314 ----
         * (non-Javadoc)
         * 
+      * @see 
net.sourceforge.jwebunit.IJWebUnitDialog#isMatchInTable(java.lang.String,
+      *      java.lang.String)
+      */
+     public boolean isMatchInTable(String tableSummaryOrId, String regexp) {
+         throw new UnsupportedOperationException("isTextInTable");
+     }
+ 
+     /*
+      * (non-Javadoc)
+      * 
         * @see 
net.sourceforge.jwebunit.IJWebUnitDialog#getWebTableBySummaryOrId(java.lang.String)
         */
***************
*** 625,628 ****
--- 644,658 ----
         * (non-Javadoc)
         * 
+      * @see 
net.sourceforge.jwebunit.IJWebUnitDialog#isMatchInElement(org.w3c.dom.Element,
+      *      java.lang.String)
+      */
+     public boolean isMatchInElement(Element element, String regexp) {
+         throw new UnsupportedOperationException("isTextInElement");
+ 
+     }
+ 
+     /*
+      * (non-Javadoc)
+      * 
         * @see 
net.sourceforge.jwebunit.IJWebUnitDialog#gotoWindow(java.lang.String)
         */

Index: HttpUnitDialog.java
===================================================================
RCS file: 
/cvsroot/jwebunit/jWebUnit/src/net/sourceforge/jwebunit/HttpUnitDialog.java,v
retrieving revision 1.53
retrieving revision 1.54
diff -C2 -d -r1.53 -r1.54
*** HttpUnitDialog.java 11 May 2005 12:11:15 -0000      1.53
--- HttpUnitDialog.java 15 May 2005 19:55:12 -0000      1.54
***************
*** 12,15 ****
--- 12,17 ----
  import java.util.Map;
  
+ import org.apache.regexp.RE;
+ 
  import net.sourceforge.jwebunit.exception.TestingEngineResponseException;
  import net.sourceforge.jwebunit.exception.UnableToSetFormException;
***************
*** 553,556 ****
--- 555,572 ----
      }
  
+     /**
+      * Return true if given regexp has a match anywhere in the current 
response.
+      * 
+      * @param regexp
+      *            regexp to match.
+      */
+     public boolean isMatchInResponse(String regexp) {
+         try {
+             RE re = new RE(regexp, RE.MATCH_SINGLELINE);
+             return re.match(getTestContext().toEncodedString(resp.getText()));
+         } catch (IOException e) {
+             throw new 
RuntimeException(ExceptionUtility.stackTraceToString(e));
+         }
+     }
        
      public boolean isCheckboxSelected(String checkBoxName) {
***************
*** 597,600 ****
--- 613,644 ----
      }
  
+     /**
+      * 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 isMatchInTable(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 = "";
***************
*** 1151,1154 ****
--- 1195,1226 ----
  
      /**
+      * 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 isMatchInElement(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 (isMatchInElement((Element) child, regexp)) {
+                     return true;
+                 }
+             }
+         }
+         return false;
+     }
+ 
+     /**
       * Make the window with the given name in the current conversation active.
       * 

Index: WebTestCase.java
===================================================================
RCS file: 
/cvsroot/jwebunit/jWebUnit/src/net/sourceforge/jwebunit/WebTestCase.java,v
retrieving revision 1.55
retrieving revision 1.56
diff -C2 -d -r1.55 -r1.56
*** WebTestCase.java    15 Apr 2005 04:46:25 -0000      1.55
--- WebTestCase.java    15 May 2005 19:55:12 -0000      1.56
***************
*** 108,111 ****
--- 108,115 ----
      }
  
+     public void assertTitleMatch(String regexp) {
+         tester.assertTitleMatch(regexp);
+     }
+ 
      public void assertTitleEqualsKey(String titleKey) {
          getTester().assertTitleEqualsKey(titleKey);
***************
*** 120,123 ****
--- 124,131 ----
      }
  
+     public void assertMatch(String regexp) {
+         tester.assertMatch(regexp);
+     }
+ 
      public void assertKeyNotPresent(String key) {
          getTester().assertKeyNotPresent(key);
***************
*** 128,131 ****
--- 136,143 ----
      }
  
+     public void assertNoMatch(String regexp) {
+         tester.assertNoMatch(regexp);
+     }
+ 
      public void assertTablePresent(String tableSummaryOrId) {
          getTester().assertTablePresent(tableSummaryOrId);
***************
*** 144,147 ****
--- 156,163 ----
      }
  
+     public void assertMatchInTable(String tableSummaryOrId, String regexp) {
+         tester.assertMatchInTable(tableSummaryOrId, regexp);
+     }
+ 
      public void assertKeysInTable(String tableSummaryOrId, String[] keys) {
          getTester().assertKeysInTable(tableSummaryOrId, keys);
***************
*** 152,155 ****
--- 168,175 ----
      }
  
+     public void assertMatchInTable(String tableSummaryOrId, String[] regexp) {
+         tester.assertMatchInTable(tableSummaryOrId, regexp);
+     }
+ 
      public void assertKeyNotInTable(String tableSummaryOrId, String key) {
          getTester().assertKeyNotInTable(tableSummaryOrId, key);
***************
*** 164,167 ****
--- 184,195 ----
      }
  
+     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) {
          getTester().assertTableEquals(tableSummaryOrId, 
expectedTable.getExpectedStrings());
***************
*** 180,183 ****
--- 208,233 ----
      }
  
+     public void assertTableMatch(String tableSummaryOrId,
+             ExpectedTable expectedTable) {
+         tester.assertTableMatch(tableSummaryOrId, expectedTable
+                 .getExpectedStrings());
+     }
+ 
+     public void assertTableMatch(String tableSummaryOrId,
+             String[][] expectedCellValues) {
+         tester.assertTableMatch(tableSummaryOrId, expectedCellValues);
+     }
+ 
+     public void assertTableRowsMatch(String tableSummaryOrId, int startRow,
+             ExpectedTable expectedTable) {
+         tester.assertTableRowsMatch(tableSummaryOrId, startRow, 
expectedTable);
+     }
+ 
+     public void assertTableRowsMatch(String tableSummaryOrId, int startRow,
+             String[][] expectedCellValues) {
+         tester.assertTableRowsMatch(tableSummaryOrId, startRow,
+                 expectedCellValues);
+     }
+ 
      public void assertFormElementPresent(String formElementName) {
          getTester().assertFormElementPresent(formElementName);
***************
*** 217,220 ****
--- 267,275 ----
      }
  
+     public void assertFormElementMatch(String formElementName,
+             String regexp) {
+         tester.assertFormElementMatch(formElementName, regexp);
+     }
+ 
      public void assertFormElementEmpty(String formElementName) {
          getTester().assertFormElementEmpty(formElementName);
***************
*** 273,276 ****
--- 328,335 ----
      }
  
+     public void assertOptionMatch(String selectName, String regexp) {
+         tester.assertOptionMatch(selectName, regexp);
+     }
+ 
      public void assertSubmitButtonPresent(String buttonName) {
          getTester().assertSubmitButtonPresent(buttonName);
***************
*** 372,375 ****
--- 431,442 ----
      }
  
+     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) {
          getTester().assertWindowPresent(windowName);
***************
*** 395,398 ****
--- 462,469 ----
      }
      
+     public void assertCookieValueMatch(String cookieName, String regexp) {
+         tester.assertCookieValueMatch(cookieName, regexp);
+     }
+ 
  //  is Pattern methods
       

Index: IJWebUnitDialog.java
===================================================================
RCS file: 
/cvsroot/jwebunit/jWebUnit/src/net/sourceforge/jwebunit/IJWebUnitDialog.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** IJWebUnitDialog.java        11 May 2005 12:11:15 -0000      1.6
--- IJWebUnitDialog.java        15 May 2005 19:55:12 -0000      1.7
***************
*** 183,186 ****
--- 183,194 ----
  
      /**
+      * Return true if given regexp has a match anywhere in the current 
response.
+      * 
+      * @param regexp
+      *            regexp to match.
+      */
+     public abstract boolean isMatchInResponse(String regexp);
+ 
+     /**
       * Return true if given text is present in a specified table of the 
response.
       * 
***************
*** 193,196 ****
--- 201,214 ----
  
      /**
+      * 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 abstract boolean isMatchInTable(String tableSummaryOrId, String 
regexp);
+ 
+     /**
       * Return the HttpUnit WebTable object representing a specified table in 
the current response. Null is returned if
       * a parsing exception occurs looking for the table or no table with the 
id or summary could be found.
***************
*** 492,495 ****
--- 510,523 ----
  
      /**
+      * 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 abstract boolean isMatchInElement(Element element, String regexp);
+ 
+     /**
       * Make the window with the given name in the current conversation active.
       * 



-------------------------------------------------------
This SF.Net email is sponsored by Oracle Space Sweepstakes
Want to be the first software developer in space?
Enter now for the Oracle Space Sweepstakes!
http://ads.osdn.com/?ad_id=7393&alloc_id=16281&op=click
_______________________________________________
Jwebunit-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-development

Reply via email to