W3C doctype validation as jUnit test
When developing standards compliant web sites, it is important to regularly check your mark-up for validity to ensure you are adhering to the choosen doctype. While the W3C provides an excellent online validator for checking documents, it can be cumbersome to use regularly with internal, dynamically generated web sites.
Having this sort of task automated adds to overall quality, overall happiness, overall time-to-market, flexibility and everything!
Thinking that ‘someone must have done this before’ I put my googles on and found that indeed someone had, in dot.c-hash. Adding HTML validity checking to your ASP.NET web site via unit tests
Behold my conversion to java:
public class W3CValidityCheckerIntTest {
private final String OUR_URL = "http://localhost:8080/";
private final String VALIDATOR_URL = "http://validator.w3.org/check";
@Test
public void testValidity() throws Exception {
URLConnection our_url = (new URL(OUR_URL)).openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(our_url.getInputStream()));
String l;
StringBuffer buff = new StringBuffer();
while((l = br.readLine()) != null) {
buff.append(l).append("\n");
}
String data = URLEncoder.encode("fragment", "UTF-8") + "=" + URLEncoder.encode(buff.toString(), "UTF-8");
data += "&" + URLEncoder.encode("prefill", "UTF-8") + "=" + URLEncoder.encode("0", "UTF-8");
data += "&" + URLEncoder.encode("group", "UTF-8") + "=" + URLEncoder.encode("0", "UTF-8");
data += "&" + URLEncoder.encode("doctype", "UTF-8") + "=" + URLEncoder.encode("Inline", "UTF-8");
URL url = new URL(VALIDATOR_URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
String valid = conn.getHeaderField("X-W3C-Validator-Status");
System.out.println("Warnings: " + conn.getHeaderField("X-W3C-Validator-Warnings"));
System.out.println("Errors: " + conn.getHeaderField("X-W3C-Validator-Errors"));
System.out.println("Status: " + valid);
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
wr.close();
rd.close();
boolean is_valid = (!valid.equalsIgnoreCase("invalid"));
assertThat("The page is valid", is_valid, is(true));
}
}
Text tagged as: