@rolflが言ったように、これにはJTidyを使用できます。ただし、JTidy のドキュメントはひどいものです (そして、私はこれまで使用したことがありません)。そのため、ダウンロードして使用してみました。このテストが実行され、3 つの警告が表示されます。
package com.sandbox;
import org.junit.Test;
import org.w3c.tidy.Tidy;
import java.io.StringReader;
import java.io.StringWriter;
import static org.junit.Assert.assertEquals;
public class SandboxTest {
@Test
public void myTest() {
Tidy tidy = new Tidy();
StringWriter writer = new StringWriter();
tidy.parse(new StringReader("invalid html"), writer);
assertEquals(0, tidy.getParseErrors());
assertEquals(0, tidy.getParseWarnings());
}
}
このアサーションは、0 ではなく 3 を返すため、最後の行で失敗します。それはあなたが探しているものですか?
あなたの入力を使用してみましたが、警告が表示されます。
package com.sandbox;
import org.junit.Test;
import org.w3c.tidy.Tidy;
import java.io.StringReader;
import java.io.StringWriter;
import static org.junit.Assert.assertEquals;
public class SandboxTest {
@Test
public void myTest() {
Tidy tidy = new Tidy();
StringWriter writer = new StringWriter();
tidy.parse(new StringReader("<html>\n" +
"<head>\n" +
" <title>This is a sample doc</title>\n" +
"</head>\n" +
"<body>\n" +
" <p> <b>this is a sample paragraph</b></p>"), writer);
assertEquals(0, tidy.getParseErrors());
assertEquals(0, tidy.getParseWarnings());
}
}
出力:
line 1 column 1 - Warning: missing <!DOCTYPE> declaration
InputStream: Document content looks like HTML 2.0
1 warning, no errors were found!
java.lang.AssertionError:
Expected :0
Actual :1
<Click to see difference>
at org.junit.Assert.fail(Assert.java:93)
at org.junit.Assert.failNotEquals(Assert.java:647)
at org.junit.Assert.assertEquals(Assert.java:128)
at org.junit.Assert.assertEquals(Assert.java:472)
at org.junit.Assert.assertEquals(Assert.java:456)
at com.sandbox.SandboxTest.myTest(SandboxTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Process finished with exit code -1