Clover coverage report - Ledge Web - SNAPSHOT
Coverage timestamp: Fri Nov 17 2006 05:20:55 CET
file stats: LOC: 329   Methods: 15
NCLOC: 177   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
LedgeWebTestCase.java 0% 0% 0% 0%
coverage
 1    //
 2    // Copyright (c) 2003,2004 , Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 3    // All rights reserved.
 4    //
 5    // Redistribution and use in source and binary forms, with or without modification,
 6    // are permitted provided that the following conditions are met:
 7    //
 8    // * Redistributions of source code must retain the above copyright notice,
 9    // this list of conditions and the following disclaimer.
 10    // * Redistributions in binary form must reproduce the above copyright notice,
 11    // this list of conditions and the following disclaimer in the documentation
 12    // and/or other materials provided with the distribution.
 13    // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 14    // nor the names of its contributors may be used to endorse or promote products
 15    // derived from this software without specific prior written permission.
 16    //
 17    // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 18    // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19    // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 20    // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 21    // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 22    // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 23    // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 24    // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 25    // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 26    // POSSIBILITY OF SUCH DAMAGE.
 27    //
 28    package org.objectledge.web.test;
 29   
 30    import junit.framework.Assert;
 31   
 32    import org.w3c.dom.Document;
 33    import org.w3c.dom.Element;
 34   
 35    import com.meterware.httpunit.HTMLElementPredicate;
 36    import com.meterware.httpunit.WebImage;
 37    import com.meterware.httpunit.WebLink;
 38    import com.meterware.httpunit.WebTable;
 39   
 40    import net.sourceforge.jwebunit.WebTestCase;
 41   
 42    /**
 43    * Base class for ObjectLedge Web functional testcases
 44    *
 45    * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
 46    * @version $Id: LedgeWebTestCase.java,v 1.12 2005/05/20 00:47:22 rafal Exp $
 47    */
 48    public class LedgeWebTestCase
 49    extends WebTestCase
 50    {
 51    /**
 52    * Set up test context.
 53    */
 54  0 public void setUp()
 55    {
 56  0 String url = System.getProperty("base.url", "http://localhost:8080");
 57  0 String baseUrl = url.endsWith("/") ? url.substring(0, url.length()-1) : url;
 58  0 getTestContext().setBaseUrl(baseUrl);
 59  0 beginAt("/action/i18n.SetLocale/locale/pl_PL");
 60  0 beginAt("/action/webcore,SetLocale/locale/pl_PL");
 61    }
 62   
 63    // -- ObjectLedge specific asserts ----------------------------------------------------------
 64   
 65    /**
 66    * Assert that the currently displayed view matches expected value.
 67    *
 68    * @param expectedView the expected view.
 69    */
 70  0 public void assertActualView(String expectedView)
 71    {
 72  0 String actualView = Utils.getActualView(getTester().getDialog().getResponseText());
 73  0 if(actualView == null)
 74    {
 75  0 Assert.fail("unable to determine current view, expected: "+expectedView);
 76    }
 77  0 if(!actualView.equals(expectedView))
 78    {
 79  0 Assert.fail("expected view " + expectedView+" but current view is " + actualView);
 80    }
 81    }
 82   
 83    /**
 84    * Assert that the executed action's result matches expected value.
 85    *
 86    * @param expectedResult the expected result.
 87    */
 88  0 public void assertActionResult(String expectedResult)
 89    {
 90  0 String actionResult = Utils.getActionResult(getTester().getDialog().getResponseText());
 91  0 if(actionResult == null)
 92    {
 93  0 Assert.fail("unable to determine action result");
 94    }
 95  0 if(!actionResult.equals(expectedResult))
 96    {
 97  0 Assert.fail("expected result " + expectedResult + " but action reported " +
 98    actionResult);
 99    }
 100    }
 101   
 102    /**
 103    * Assert that the executed action does not report any result.
 104    */
 105  0 public void assertNoActionResult()
 106    {
 107  0 String actionResult = Utils.getActionResult(getTester().getDialog().getResponseText());
 108  0 if(actionResult != null && actionResult.length() > 0)
 109    {
 110  0 Assert.fail("exected no result but action reported "+actionResult);
 111    }
 112    }
 113   
 114   
 115    // -- utiltity methods ----------------------------------------------------------------------
 116   
 117    /**
 118    * Retruns the first link that contains the specified text in it's href attribute.
 119    *
 120    * @param text the text to find.
 121    * @return first matching link, or null if not found.
 122    * @throws Exception if document traversal fails.
 123    */
 124  0 protected WebLink getLinkWithURL(String text)
 125    throws Exception
 126    {
 127  0 WebLink[] links = getTester().getDialog().getResponse().getLinks();
 128  0 WebLink link = null;
 129  0 for(WebLink l: links)
 130    {
 131  0 if(l.getURLString().contains(text))
 132    {
 133  0 return l;
 134    }
 135    }
 136  0 return null;
 137    }
 138   
 139    /**
 140    * Returns the count of the links that contain the specified text in their body.
 141    *
 142    * @param text the text to find.
 143    * @return the number of matching links.
 144    * @throws Exception if document traversal fails.
 145    */
 146  0 protected int countLinksWithString(String text)
 147    throws Exception
 148    {
 149  0 int i = 0;
 150  0 WebLink[] links = getTester().getDialog().getResponse().getLinks();
 151  0 for(WebLink l: links)
 152    {
 153  0 if(l.getText().contains(text))
 154    {
 155  0 i++;
 156    }
 157    }
 158  0 return i;
 159    }
 160   
 161    /**
 162    * Returns the count of the links with specified text in their body.
 163    *
 164    * @param text the text to find.
 165    * @return the number of matching links.
 166    * @throws Exception if document traversal fails.
 167    */
 168  0 protected int countLinksWithExactString(String text)
 169    throws Exception
 170    {
 171  0 int i = 0;
 172  0 WebLink[] links = getTester().getDialog().getResponse().getLinks();
 173  0 for(WebLink l: links)
 174    {
 175  0 if(l.getText().equals(text))
 176    {
 177  0 i++;
 178    }
 179    }
 180  0 return i;
 181    }
 182   
 183    /**
 184    * Returns the count of images with source URL containing the specified text.
 185    *
 186    * @param text text to find.
 187    * @return nubmer of matching images.
 188    * @throws Exception if document traversal fails.
 189    */
 190  0 protected int countImagesWithSource(String text)
 191    throws Exception
 192    {
 193  0 int i = 0;
 194  0 WebImage[] images = getTester().getDialog().getResponse().getImages();
 195  0 for(WebImage l: images)
 196    {
 197  0 if(l.getSource().contains(text))
 198    {
 199  0 i++;
 200    }
 201    }
 202  0 return i;
 203    }
 204   
 205    /**
 206    * Returns the count of occurances of a specified string in the page body.
 207    *
 208    * @param text the text to find.
 209    * @return the number of occurances.
 210    */
 211  0 protected int countString(String text)
 212    {
 213  0 String response = getTester().getDialog().getResponseText();
 214  0 int counter = 0;
 215  0 int index = response.indexOf(text);
 216  0 while(index >= 0)
 217    {
 218  0 counter++;
 219  0 index = response.indexOf(text, index + text.length());
 220    }
 221  0 return counter;
 222    }
 223   
 224    /**
 225    * Returns the nubmer of occurances of a specified tag in the page body.
 226    *
 227    * @param tagName name of the tag.
 228    * @return the number of occurances.
 229    * @throws Exception if document traversal fails.
 230    */
 231  0 protected int countElements(String tagName)
 232    throws Exception
 233    {
 234  0 Document doc = getTester().getDialog().getResponse().getDOM();
 235  0 DOMTreeWalker walker = new DOMTreeWalker(doc.getDocumentElement());
 236  0 return walker.countTags(tagName);
 237    }
 238   
 239    /**
 240    * Returns the table element that contains the specified text.
 241    *
 242    * @param text the text to find.
 243    * @return matching table element.
 244    * @throws Exception if tree traversal fails.
 245    */
 246  0 public Element getTableWithText(String text)
 247    throws Exception
 248    {
 249  0 Document doc = getTester().getDialog().getResponse().getDOM();
 250  0 DOMTreeWalker walker = new DOMTreeWalker(doc.getDocumentElement());
 251  0 return getTableWithText(walker, text);
 252    }
 253   
 254    /**
 255    * Returns the table element that contains the specified text.
 256    *
 257    * @param walker the DOMTreeWalker component instance.
 258    * @param text the text to find.
 259    * @return matching table element.
 260    */
 261  0 public Element getTableWithText(DOMTreeWalker walker, String text)
 262    {
 263  0 return walker.findElementWithText(text,"table");
 264    }
 265   
 266    /**
 267    * Returns the contents of a table cell.
 268    *
 269    * @param table table element.
 270    * @param row row number.
 271    * @param cell cell number.
 272    * @return the cell content.
 273    * @throws Exception if document travelsal fails.
 274    */
 275  0 public String getTableCellText(Element table, int row, int cell)
 276    throws Exception
 277    {
 278  0 Document doc = getTester().getDialog().getResponse().getDOM();
 279  0 DOMTreeWalker walker = new DOMTreeWalker(doc.getDocumentElement());
 280  0 return getTableRowText(walker, table, row, cell);
 281    }
 282   
 283    /**
 284    * Returns the contents of a table row.
 285    *
 286    * @param walker DOMTreeWalker component.
 287    * @param table table element.
 288    * @param row row number.
 289    * @param cell starting cell number.
 290    * @return contents of the table row.
 291    */
 292  0 public String getTableRowText(DOMTreeWalker walker, Element table, int row, int cell)
 293    {
 294  0 walker.gotoElement(table);
 295  0 for(int i = 0; i < row+1; i++)
 296    {
 297  0 walker.getNextElement(0,"tr");
 298    }
 299  0 for(int i = 0; i < cell+1; i++)
 300    {
 301  0 walker.getNextElement(0,"td");
 302    }
 303  0 return walker.getNextText();
 304    }
 305   
 306    /**
 307    * A predicate for picking out table element that contains the specified string.
 308    */
 309    public class TableContentPredicate implements HTMLElementPredicate
 310    {
 311    /**
 312    * {@inheritDoc}
 313    */
 314  0 public boolean matchesCriteria(Object htmlElement, Object criteria)
 315    {
 316  0 if(!(htmlElement instanceof WebTable))
 317    {
 318  0 return false;
 319    }
 320  0 WebTable table = (WebTable)htmlElement;
 321  0 String value = table.getText();
 322  0 if(value != null && value.contains((String)criteria))
 323    {
 324  0 return true;
 325    }
 326  0 return false;
 327    }
 328    }
 329    }