1

I am using Jtidy I pass an InputSteam and then I tried to validate whether or not the XHTML code in the InputSteam is valid.

InputStream s = new ByteArrayInputStream(my_string).getBytes(StandardCharsets.UTF_8));

Tidy tidy = new Tidy();
tidy.setXHTML(true);

tidy.parse(s, System.out);

I would like to validate my test, true or false instead of printing this to the console: 5 warnings, no errors were found!

If I get 1 error how do I make my JUnit reflect a fail?

I noticed that no matter what my string looks like it gets parsed as valid always I tried this:

String my_string = "TABLE width = 400 &&&& table height = ";

That gets parsed as valid without any errors.

1

Try replacing the last line with something like this:

tidy.setErrout(null);
tidy.parse(s, null);
Assert.assertEquals(0,tidy.getParseErrors());
2
  • tidy.parse(s, null); Gives me this compiler error: The method parse(InputStream, OutputStream) is ambiguous for the type Tidy. How can I fix it? Also Assert is depricated.
    – Mike John
    Jun 17 '14 at 3:48
  • Try using a cast. Or use a StringWriter instead of null. I assume you can figure out the assertion part on your own.
    – user246
    Jun 17 '14 at 11:23
2

I had the same question/problem. This is how I solved it:

String validHtml = "some valid html";
InputStream inputStream = new ByteArrayInputStream(validHtml.getBytes(StandardCharsets.UTF_8));

Tidy tidy = new Tidy();
tidy.setXmlTags(true);

tidy.parse(inputStream, null);

System.out.println("errors "+tidy.getParseErrors());
System.out.println("warnings "+tidy.getParseWarnings());
assertTrue("HTML has errors and it should not.",tidy.getParseErrors()==0);

The trick is in setXMLTags(true).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.