blob: 95cf33184ef0fe3367cfa5f76d6180fb77d59c91 [file] [log] [blame]
/*
* $Id: WicketWebTestCase.java 5395 2006-04-16 13:42:28 +0000 (Sun, 16 Apr 2006)
* jdonnerstag $ $Revision$ $Date: 2006-04-16 13:42:28 +0000 (Sun, 16 Apr
* 2006) $
*
* ==================================================================== Licensed
* under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package wicket.examples;
import junit.framework.Test;
import junit.framework.TestSuite;
import net.sourceforge.jwebunit.WebTestCase;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import wicket.examples.test.jetty.JettyDecorator;
import com.meterware.httpunit.HttpUnitOptions;
/**
* Add XPATH based validation
*
* @author Juergen Donnerstag
*/
public abstract class WicketWebTestCase extends WebTestCase
{
private static final Logger logger = LoggerFactory.getLogger(WicketWebTestCase.class);
/**
* Suite method.
*
* @param clazz
* @return Test suite
*/
public static Test suite(final Class clazz)
{
// The javascript 'history' variable is not supported by
// httpunit and we don't want httpunit to throw an
// exception just because they can not handle it.
HttpUnitOptions.setExceptionsThrownOnScriptError(false);
TestSuite suite = new TestSuite();
suite.addTestSuite(clazz);
JettyDecorator deco = new JettyDecorator(suite);
deco.setPort(8098);
String basedir = System.getProperty("basedir");
logger.debug("basedir="+basedir);
String path = "";
if (basedir != null)
path = basedir + "/";
path += "src/webapp";
deco.setWebappContextRoot(path);
deco.setContextPath("/wicket-examples");
return deco;
}
/**
* Constructor
*
* @param name
*/
public WicketWebTestCase(final String name)
{
super(name);
}
/**
* Constructor
*/
public WicketWebTestCase()
{
super();
}
/**
* @see junit.framework.TestCase#setUp()
*/
@Override
public void setUp() throws Exception
{
getTestContext().setBaseUrl("http://localhost:8098/wicket-examples");
}
/**
* Select a single node based on the xpath expression
*
* @param xpath
* @return Node
* @throws Exception
*/
public Node selectSingleNode(final String xpath) throws Exception
{
final String resp = this.getDialog().getResponse().getText();
final Document doc = DocumentHelper.parseText(resp);
// String xml = doc.asXML();
// System.out.print(xml);
final Node node = doc.selectSingleNode(xpath);
return node;
}
/**
* Assert the value returned by the xpath matches 'assertValue'
*
* @param xpath
* @param assertValue
* @throws Exception
*/
public void assertXPath(final String xpath, final String assertValue) throws Exception
{
final Node node = selectSingleNode(xpath);
assertNotNull("Node not found: " + xpath, node);
final String value;
if (node instanceof Attribute)
{
value = ((Attribute)node).getValue();
}
else
{
value = node.getText();
}
assertTrue("xpath found, but values don't match: '" + value + "' != '" + assertValue + "'",
value.matches(".*" + assertValue + ".*"));
}
/**
* Assert the tag body of the tag identified by wicket:id="wicketId" matches
* 'assertValue'
*
* @param wicketId
* @param assertValue
* @throws Exception
*/
public void assertWicketIdTagText(final String wicketId, final String assertValue)
throws Exception
{
assertXPath("//*[@wicket:id='" + wicketId + "']", assertValue);
}
/**
* Assert no node matching the xpath exists
*
* @param xpath
* @throws Exception
*/
public void assertXpathNodeNotPresent(final String xpath) throws Exception
{
final Node node = selectSingleNode(xpath);
assertNull(node);
}
/**
* Assert the node matching the xpath exists
*
* @param xpath
* @return Node
* @throws Exception
*/
public Node assertXpathNodePresent(final String xpath) throws Exception
{
final Node node = selectSingleNode(xpath);
assertNotNull(node);
return node;
}
}