[Xpcgi-cvs-reports] CVS: XP/test/com/visi/gyles19/xpcgi ValidatingTest.java,NONE,1.1
Status: Beta
Brought to you by:
joiellis
|
From: Joi E. <joi...@us...> - 2001-11-12 04:48:02
|
Update of /cvsroot/xpcgi/XP/test/com/visi/gyles19/xpcgi
In directory usw-pr-cvs1:/tmp/cvs-serv3434
Added Files:
ValidatingTest.java
Log Message:
Taint work, use strict work, and some unit tests written and applied to validate some of the html
produced by the cgi scripts.
--- NEW FILE ---
package com.visi.gyles19.xpcgi;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebResponse;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import java.lang.StringBuffer;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Text;
public class ValidatingTest extends TestCase {
public static String VALIDATOR =
"http://validator.gyles19.visi.com/cgi-bin/check?uri=";
public static String VALIDATOR2 = "";
// public static String VALIDATOR2 = ";ss=1"; // display source in result
// public static String VALIDATOR2 = ";sp=1"; // display parse tree in result
public static String VALIDMSG = "No errors found!";
public static String BADLOGIC = "Unexpected Logic";
public static String WARNINGS = "Warnings";
public static boolean DEBUG = false;
public static boolean VERBOSE = true;
public static boolean FAILWARNINGS = false;
public static String DOCTYPE =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"" +
" \"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd\">";
public static boolean PROVIDEDOCTYPE = true; // if doctype warning happens
public static String OVERRIDEDOCTYPE = "HTML+4.01+Transitional";
public static String MISSINGDOCTYPE = "no document type declaration";
public static String VALIDATOR3 = "&doctype=" + OVERRIDEDOCTYPE;
WebConversation wc = new WebConversation();
String URL;
boolean expectBadLogic = false;
String match = null;
public ValidatingTest( String url, boolean expectBadLogic, String match ) {
super( "testURL" );
this.URL = url;
this.expectBadLogic = expectBadLogic;
this.match = match;
}
// public TestSuite suite() {
// TestSuite suite = new TestSuite();
// suite.addTest( "testURL" );
// return suite;
// }
public void testURL() throws Exception {
multiTestPage(URL, expectBadLogic, match );
}
void multiTestPage( String URL ) throws Exception {
multiTestPage( URL, false, null );
}
void multiTestPage( String URL, boolean expectBadLogic ) throws Exception {
multiTestPage( URL, expectBadLogic, null );
}
void multiTestPage( String URL, String match ) throws Exception {
multiTestPage( URL, false, match );
}
void multiTestPage( String URL, boolean expectBadLogic,
String match ) throws Exception {
// System.out.println("Running " + URL);
// System.err.println("Running Stderr " + URL);
/*
ensure we get a 200 response for the URL
*/
WebRequest request =
new GetMethodWebRequest( URL );
WebResponse response = wc.getResponse( request );
assertEquals( "Page " + URL + " unexpected status code", 200, response.getResponseCode());
/*
If match is not null, its a string which we should find embedded somewhere
in the result.
*/
if ( match != null ) {
assertTrue("Expected to find:" + match, response.getText().indexOf( match ) > -1 );
}
/*
if this url should trigger an Unexpected Logic because its inputs are
incomplete/invalid, check for it.
*/
if ( expectBadLogic ) {
assertTrue( "Expected 'Unexpected Logic' result", ! isGoodLogic( response ) );
} else {
assertTrue( "Unexpected 'Unexpected Logic' result", isGoodLogic( response ) );
}
/*
now run the url through the validator and ensure the output is valid
HTML (even the error pages.)
*/
response = wc.getResponse( VALIDATOR + URL + VALIDATOR2 );
assertEquals( "Page " + URL + VALIDATOR2 +
": unexpected status code ", 200, response.getResponseCode());
if ( PROVIDEDOCTYPE && isMissingDoctype( response ) ) {
response = wc.getResponse( VALIDATOR + URL + VALIDATOR2 + VALIDATOR3 );
assertEquals( "Page " + URL + VALIDATOR2 + VALIDATOR3 +
": unexpected status code ", 200, response.getResponseCode());
assertTrue(URL + " does not validate with doctype."+ response.getText(),
isGoodHTML( response ));
} else {
assertTrue(URL + " does not validate.\n"+ response.getText(),
isGoodHTML( response ));
assertTrue(URL + " has warnings,\n" + response.getText(),
! hasWarnings(response));
}
}
boolean isMissingDoctype( WebResponse res ) throws Exception {
boolean missing = res.getText().indexOf( MISSINGDOCTYPE ) > -1;
return missing;
}
boolean isGoodHTML( WebResponse res ) throws Exception {
boolean good = res.getText().indexOf( VALIDMSG ) > -1;
// if (! good && VERBOSE ) {
// //NodeList liList = getListItems( res.getDOM() );
// System.out.println( res.getText() );
// } else if ( DEBUG ) {
// System.out.println( res.getText() );
// }
return good;
}
boolean isGoodLogic( WebResponse res ) throws Exception {
boolean good = res.getText().indexOf( BADLOGIC ) == -1;
//System.out.println("Logic test: " + good );
return good;
}
boolean hasWarnings( WebResponse res ) throws Exception {
if ( FAILWARNINGS ) {
boolean warnings = res.getText().indexOf(WARNINGS) > -1;
return warnings;
} else {
return false;
}
}
public NodeList getListItems( Document doc ) {
NodeList nl;
Node node;
nl = doc.getElementsByTagName( "ul" );
System.out.println("Printing elements from doc: " + nl.getLength() );
for (int j = 0; j < nl.getLength(); j++) {
node = nl.item(j);
System.out.println( "Item " + j + ": " + node.getNodeName() +
" " + dumpKids( node ) );
}
return nl;
}
String dumpKids( Node node ) {
if ( node.hasChildNodes() ) {
StringBuffer buf = new StringBuffer();
NodeList kids = node.getChildNodes();
for (int i = 0; i < kids.getLength(); i++) {
Node kid = kids.item(i);
buf.append( interestingStuff( kid ));
dumpKids( kid );
}
return buf.toString();
} else {
return node.getNodeValue();
}
}
String interestingStuff( Node kid ) {
switch (kid.getNodeType()) {
case Node.CDATA_SECTION_NODE:
return ( "CData:" + ((CDATASection) kid).getData());
case Node.TEXT_NODE:
return( "Text:" + ((Text) kid).getData());
default:
return( "Other: " + kid.getNodeName() + " " + kid.getNodeValue() );
}
}
}
|