FileDocCategorySizeDatePackage
SearchTest.javaAPI DocExample2289Wed Jul 16 16:20:38 BST 2003com.develop.ss.httpunit

SearchTest.java

package com.develop.ss.httpunit;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.develop.ss.test.ConfigBean;
import junit.framework.TestCase;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.beans.Introspector;
import java.beans.IntrospectionException;
import java.beans.BeanInfo;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

public class SearchTest extends TestCase {
  private WebConversation conversation= new WebConversation();
  private ConfigBean config;

  public SearchTest(String name) {
    super(name);
  }

  protected void setUp() throws Exception {
    config = new ConfigBean();
  }

  public void doTestFindOccurences(String searchString, int expected) throws SAXException, IOException {
    WebResponse searchPage = conversation.getResponse(config.getSearchJspUrl());
    WebForm webForm = searchPage.getForms()[0];
    webForm.setParameter("query", searchString);
    WebResponse responsePage = webForm.submit();
    WebLink[] links = responsePage.getLinks();
    assertEquals(expected, links.length);
  }
  public void testFindOne() throws Exception {
    doTestFindOccurences("one", 1);
  }
  public void testFindNada() throws Exception {
    doTestFindOccurences("nada", 0);
  }
  public void testFindMany() throws Exception {
    doTestFindOccurences("SPIDER", 3);
  }

//  public void logStringProperties(Object o) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
//    BeanInfo beanInfo = Introspector.getBeanInfo(o.getClass());
//    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
//    for (int i = 0; i < propertyDescriptors.length; i++) {
//      PropertyDescriptor pd = propertyDescriptors[i];
//      Method readMethod = pd.getReadMethod();
//      if (readMethod.getReturnType() == String.class) {
//        if (readMethod.getParameterTypes().length == 0) {
//          System.out.println(readMethod.getName() + ": " + readMethod.invoke(o, new Object[0]));
//        }
//      }
//    }
//  }
}