[Jsptest-svn-commits] SF.net SVN: jsptest: [201] trunk/jsptest-generic/jsptest-framework/src/main
Status: Alpha
Brought to you by:
lkoskela
From: <lko...@us...> - 2008-04-09 17:39:04
|
Revision: 201 http://jsptest.svn.sourceforge.net/jsptest/?rev=201&view=rev Author: lkoskela Date: 2008-04-09 10:38:59 -0700 (Wed, 09 Apr 2008) Log Message: ----------- Added javadocs Modified Paths: -------------- trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/AbstractAssertion.java trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/AlwaysAcceptChooser.java trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/DOMAssertion.java trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/ElementAssertion.java trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/ElementChooser.java trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/ExpectedAssertionFailure.java trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/FormAssertion.java trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/FormFieldAssertion.java trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/LinkAssertion.java Modified: trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/AbstractAssertion.java =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/AbstractAssertion.java 2008-04-04 12:53:08 UTC (rev 200) +++ trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/AbstractAssertion.java 2008-04-09 17:38:59 UTC (rev 201) @@ -2,24 +2,69 @@ import junit.framework.Assert; +/** + * Base class providing common assertion methods for concrete subclasses. + * + * @author Lasse Koskela + */ public abstract class AbstractAssertion { + /** + * Assert that the given substring (needle) is present in the given string + * (haystack). + * + * @param message + * The optional failure message. + * @param haystack + * The string to find the substring from. + * @param needle + * The substring to find from the haystack. + */ protected void assertContains(String message, String haystack, String needle) { Assert.assertTrue(message, contains(haystack, needle)); } + /** + * Assert that the given substring (needle) is present in the given string + * (haystack). + * + * @param haystack + * The string to find the substring from. + * @param needle + * The substring to find from the haystack. + */ protected void assertContains(String haystack, String needle) { String message = "Expected text <" + needle + "> was not found from <" + haystack + ">"; assertContains(message, haystack, needle); } + /** + * Assert that the given substring (needle) is <i>not</i> present in the + * given string (haystack). + * + * @param message + * The optional failure message. + * @param haystack + * The string to find the substring from. + * @param needle + * The substring to find from the haystack. + */ protected void assertDoesNotContain(String message, String haystack, String needle) { Assert.assertFalse(message, contains(haystack, needle)); } + /** + * Assert that the given substring (needle) is <i>not</i> present in the + * given string (haystack). + * + * @param haystack + * The string to find the substring from. + * @param needle + * The substring to find from the haystack. + */ protected void assertDoesNotContain(String haystack, String needle) { assertDoesNotContain("Expected text <" + needle + "> not to be found from <" + haystack + ">", @@ -29,5 +74,4 @@ private boolean contains(String haystack, String needle) { return haystack.indexOf(needle) > -1; } - } Modified: trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/AlwaysAcceptChooser.java =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/AlwaysAcceptChooser.java 2008-04-04 12:53:08 UTC (rev 200) +++ trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/AlwaysAcceptChooser.java 2008-04-09 17:38:59 UTC (rev 201) @@ -3,6 +3,8 @@ import org.w3c.dom.Element; /** + * An <tt>ElementChooser</tt> that always accepts any offered <tt>Element</tt>. + * * @author Lasse Koskela */ public class AlwaysAcceptChooser implements ElementChooser { Modified: trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/DOMAssertion.java =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/DOMAssertion.java 2008-04-04 12:53:08 UTC (rev 200) +++ trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/DOMAssertion.java 2008-04-09 17:38:59 UTC (rev 201) @@ -10,30 +10,61 @@ import org.w3c.dom.Element; /** + * Base class providing assertion methods related to the HTML DOM tree. + * * @author Lasse Koskela */ public abstract class DOMAssertion extends AbstractAssertion { protected Element context; + /** + * Returns the <tt>Element</tt> as the context of assertions. + */ public Element getElement() { return context; } + /** + * Assert that the selected DOM element contains the given text. + * + * @param text + * The (partial) content that should be found. + */ public void shouldContain(String text) { assertContains(context.getTextContent(), text); } + /** + * Assert that the selected DOM element does not contain the given text. + * + * @param text + * The (partial) content that should not be found. + */ public void shouldNotContain(String text) { assertDoesNotContain(context.getTextContent(), text); } + /** + * Assert that the selected DOM element contains the given element. + * + * @param xpathExpression + * An XPath expression describing the expected child element. + */ public void shouldContainElement(String xpathExpression) { shouldContainElement("No matching nodes found for XPath: " + xpathExpression + " from\n" + getContextAsString(), xpathExpression); } + /** + * Assert that the selected DOM element contains the given element. + * + * @param message + * The optional failure message. + * @param xpathExpression + * An XPath expression describing the expected child element. + */ public void shouldContainElement(String message, String xpathExpression) { try { @@ -45,6 +76,9 @@ } } + /** + * Renders the content of the selected element as a <tt>String</tt>. + */ protected String getContextAsString() { return XML.toString(context); } Modified: trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/ElementAssertion.java =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/ElementAssertion.java 2008-04-04 12:53:08 UTC (rev 200) +++ trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/ElementAssertion.java 2008-04-09 17:38:59 UTC (rev 201) @@ -8,10 +8,21 @@ import org.w3c.dom.Element; /** + * Collection of HTML-related assertion methods specifically applicable to + * "element" nodes in a DOM tree. + * * @author Lasse Koskela */ public class ElementAssertion extends DOMAssertion { + /** + * @param content + * The <tt>Document</tt> serving as the context for the + * assertion. + * @param xpathExpression + * The XPath expression that identifies the element, which + * the subsequent assertion methods should be applied to. + */ public ElementAssertion(Document content, String xpathExpression) { this.context = content.getDocumentElement(); try { Modified: trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/ElementChooser.java =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/ElementChooser.java 2008-04-04 12:53:08 UTC (rev 200) +++ trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/ElementChooser.java 2008-04-09 17:38:59 UTC (rev 201) @@ -3,9 +3,20 @@ import org.w3c.dom.Element; /** + * An interface for building a variety of element selection rules. + * * @author Lasse Koskela */ public interface ElementChooser { + /** + * Indicates whether this particular <tt>ElementChooser</tt> accepts the + * given <tt>Element</tt>. + * + * @param element + * The candidate <tt>Element</tt>. + * @return <tt>true</tt> if this <tt>ElementChooser</tt> accepts the + * given <tt>Element</tt>, <tt>false</tt> otherwise. + */ boolean accept(Element element); } Modified: trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/ExpectedAssertionFailure.java =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/ExpectedAssertionFailure.java 2008-04-04 12:53:08 UTC (rev 200) +++ trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/ExpectedAssertionFailure.java 2008-04-09 17:38:59 UTC (rev 201) @@ -40,14 +40,29 @@ verify(message); } + /** + * Gives access to a <tt>PageAssertion</tt> object, enabling page-oriented + * (HTML) assertions. + */ protected PageAssertion page() { return testcase.page(); } + /** + * Gives access to an <tt>OutputAssertion</tt> object, enabling raw + * output-oriented assertions. + */ protected OutputAssertion output() { return testcase.output(); } + /** + * Gives access to an <tt>ElementAssertion</tt> object for the HTML + * element identified by the given XPath expression. + * + * @param xpath + * @return + */ protected ElementAssertion element(String xpath) { return testcase.element(xpath); } @@ -67,6 +82,12 @@ } } + /** + * Thrown from an assertion method indicating that the wrong kind of + * exception was thrown by the code under test. + * + * @author Lasse Koskela + */ public static class IncorrectExceptionError extends RuntimeException { public IncorrectExceptionError(String message, Throwable e) { @@ -74,6 +95,14 @@ } } + /** + * Thrown from an assertion method indicating that no exception was thrown + * by the code under test against the expectations. This class is only used + * internally and is never passed to client code (test written by a JspTest + * user). + * + * @author Lasse Koskela + */ private static class NoExceptionWasThrown extends Exception { } } Modified: trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/FormAssertion.java =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/FormAssertion.java 2008-04-04 12:53:08 UTC (rev 200) +++ trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/FormAssertion.java 2008-04-09 17:38:59 UTC (rev 201) @@ -13,12 +13,22 @@ import org.w3c.dom.NodeList; /** + * Provides form-related assertion methods. + * * @author Lasse Koskela */ public class FormAssertion { private final List forms; + /** + * @param document + * The context from where to select the form as the context + * for subsequent assertion methods. + * @param chooser + * The <tt>ElementChooser</tt> to use for selecting the + * form. + */ public FormAssertion(Document document, ElementChooser chooser) { this.forms = new ArrayList(); NodeList elements = document.getElementsByTagName("FORM"); @@ -33,6 +43,12 @@ } } + /** + * Assert that the form has a field by the given name. + * + * @param name + * The name of the expected form field. + */ public void shouldHaveField(String name) { for (Iterator i = forms.iterator(); i.hasNext();) { Form form = (Form) i.next(); @@ -43,6 +59,12 @@ Assert.fail("No form field '" + name + "' on page"); } + /** + * Assert that the form has a submit button by the given name or label. + * + * @param nameOrLabel + * The name or label of the expected submit button. + */ public void shouldHaveSubmitButton(String nameOrLabel) { for (Iterator i = forms.iterator(); i.hasNext();) { Form form = (Form) i.next(); @@ -54,6 +76,9 @@ + "' on page"); } + /** + * Assert that the form has a submit button. + */ public void shouldHaveSubmitButton() { for (Iterator i = forms.iterator(); i.hasNext();) { Form form = (Form) i.next(); @@ -64,6 +89,13 @@ Assert.fail("No form submit button on page"); } + /** + * Gives access to form field-specific assertions such as: + * <code>form("name").field("name").shouldHaveValue("foo");</code> + * + * @param fieldName + * The name of the field. + */ public FormFieldAssertion field(String fieldName) { shouldHaveField(fieldName); return new FormFieldAssertion(forms, fieldName); Modified: trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/FormFieldAssertion.java =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/FormFieldAssertion.java 2008-04-04 12:53:08 UTC (rev 200) +++ trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/FormFieldAssertion.java 2008-04-09 17:38:59 UTC (rev 201) @@ -9,6 +9,8 @@ import net.sf.jsptest.html.FormField; /** + * Provides form field-oriented assertion methods. + * * @author Lasse Koskela */ public class FormFieldAssertion { @@ -17,6 +19,14 @@ private final String fieldName; + /** + * @param forms + * The list of forms that should be considered the context + * for the subsequent assertion methods. + * @param fieldName + * The name of the form field that should be considered the + * context for the subsequent assertion methods. + */ public FormFieldAssertion(List forms, String fieldName) { this.fieldName = fieldName; this.fields = new ArrayList(); @@ -28,6 +38,12 @@ } } + /** + * Assert that the selected form field has the given value. + * + * @param expectedValue + * The expected value. + */ public void shouldHaveValue(String expectedValue) { List actuals = new ArrayList(); for (Iterator i = fields.iterator(); i.hasNext();) { Modified: trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/LinkAssertion.java =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/LinkAssertion.java 2008-04-04 12:53:08 UTC (rev 200) +++ trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/assertion/LinkAssertion.java 2008-04-09 17:38:59 UTC (rev 201) @@ -2,18 +2,44 @@ import org.w3c.dom.Element; +/** + * Provides assertion methods related to HTML anchors/links. + * + * @author Lasse Koskela + */ public class LinkAssertion extends DOMAssertion { + /** + * @param context + * The link element to serve as the context for subsequent + * assertion methods. + */ public LinkAssertion(Element context) { this.context = context; } + /** + * Assert that the selected link has the exact given label. Used in the + * following way: + * <code>page().shouldHaveLink().withText("click here");</code> + * + * @param labelText + * The expected label (has to be an exact match). + */ public void withText(String labelText) { shouldContainElement("Link with text '" + labelText + "' was not found from " + getContextAsString(), "//A[text()='" + labelText + "']"); } + /** + * Assert that the selected link's label contains the given text. Used in + * the following way: + * <code>page().shouldHaveLink().withPartialText("click");</code> + * + * @param substringOfLabelText + * The substring that the label is expected to contain. + */ public void withPartialText(String substringOfLabelText) { shouldContainElement("Link with partial text '" + substringOfLabelText + "' was not found from " @@ -21,54 +47,120 @@ + substringOfLabelText + "')]"); } + /** + * Assert that the selected link has the exact given name (that is, the + * "name" attribute). Used in the following way: + * <code>page().shouldHaveLink().withName("link_confirm_purchase");</code> + * + * @param name + * The expected value of the "name" attribute of the link + * element. + */ public void withName(String name) { shouldContainElement("Link with name '" + name + "' was not found from " + getContextAsString(), "//A[@NAME='" + name + "']"); } + /** + * Assert that the selected link has the exact given ID. Used in the + * following way: + * <code>page().shouldHaveLink().withId("link_confirm_purchase");</code> + * + * @param id + * The expected value of the "id" attribute of the link + * element. + */ public void withId(String id) { shouldContainElement("Link with ID '" + id + "' was not found from " + getContextAsString(), "//A[@ID='" + id + "']"); } + /** + * Assert that the selected link points to the given URL. + * + * @param url + * The expected URL to compare to the link element's "href" + * attribute. + */ public void withHref(String url) { shouldContainElement("Link pointing to " + url + " was not found from " + getContextAsString(), "//A[@HREF='" + url + "']"); } + /** + * Assert that the selected link has been styled with the given CSS class. + * + * @param cssClass + * The expected value of the link element's "class" + * attribute. + */ public void withClass(String cssClass) { shouldContainElement("Link with CSS class '" + cssClass + "' was not found from " + getContextAsString(), "//A[@CLASS='" + cssClass + "']"); } + /** + * Assert that the selected link wraps an image by the given ID. + * + * @param id + * The expected ID of the wrapped image. + */ public void withImageId(String id) { shouldContainElement("Image link with image ID '" + id + "' was not found from " + getContextAsString(), "//A/IMG[@ID='" + id + "']"); } + /** + * Assert that the selected link wraps an image by the given title (that is, + * the "title" attribute of the <tt>img</tt> element). + * + * @param title + * The expected title of the wrapped image. + */ public void withImageTitle(String title) { shouldContainElement("Image link with image titled '" + title + "' was not found from " + getContextAsString(), "//A/IMG[@TITLE='" + title + "']"); } + /** + * Assert that the selected link wraps an image by the given name (that is, + * the "name" attribute of the <tt>img</tt> element). + * + * @param name + * The expected name of the wrapped image. + */ public void withImageName(String name) { shouldContainElement("Image link with image named '" + name + "' was not found from " + getContextAsString(), "//A/IMG[@NAME='" + name + "']"); } + /** + * Assert that the selected link wraps an image at the given URL. + * + * @param url + * The expected URL ("src" attribute) of the wrapped image. + */ public void withImageSrc(String url) { shouldContainElement("Image link with image URL '" + url + "' was not found from " + getContextAsString(), "//A/IMG[@SRC='" + url + "']"); } + /** + * Assert that the selected link wraps an image by the given file name. + * + * @param filename + * The expected file name of the wrapped image. Can be just + * the base name (e.g. "apple.gif") or a partial path to the + * image (e.g. "images/press/chairman.jpg"). + */ public void withImageFileName(String filename) { shouldContainElement( "Image link with the image URL ending with '" This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |