Skip to content

Commit

Permalink
Show messages in HTML report when class has no debug information (#818)
Browse files Browse the repository at this point in the history
  • Loading branch information
Godin committed Jan 6, 2019
1 parent 3718bd9 commit c7d70c7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
2 changes: 2 additions & 0 deletions org.jacoco.doc/docroot/doc/changes.html
Expand Up @@ -37,6 +37,8 @@ <h3>New Features</h3>
<a href="https://github.com/jacoco/jacoco/issues/809">#809</a>).</li> <a href="https://github.com/jacoco/jacoco/issues/809">#809</a>).</li>
<li>HTML report shows message when source file can't be found <li>HTML report shows message when source file can't be found
(GitHub <a href="https://github.com/jacoco/jacoco/issues/801">#801</a>).</li> (GitHub <a href="https://github.com/jacoco/jacoco/issues/801">#801</a>).</li>
<li>HTML report shows message when class has no debug information
(GitHub <a href="https://github.com/jacoco/jacoco/issues/818">#818</a>).</li>
</ul> </ul>


<h3>Fixed Bugs</h3> <h3>Fixed Bugs</h3>
Expand Down
Expand Up @@ -17,6 +17,7 @@


import org.jacoco.core.analysis.IClassCoverage; import org.jacoco.core.analysis.IClassCoverage;
import org.jacoco.core.internal.analysis.ClassCoverageImpl; import org.jacoco.core.internal.analysis.ClassCoverageImpl;
import org.jacoco.core.internal.analysis.CounterImpl;
import org.jacoco.core.internal.analysis.MethodCoverageImpl; import org.jacoco.core.internal.analysis.MethodCoverageImpl;
import org.jacoco.report.internal.ReportOutputFolder; import org.jacoco.report.internal.ReportOutputFolder;
import org.jacoco.report.internal.html.ILinkable; import org.jacoco.report.internal.html.ILinkable;
Expand All @@ -37,8 +38,10 @@ public class ClassPageTest extends PageTestBase {
@Override @Override
public void setup() throws Exception { public void setup() throws Exception {
super.setup(); super.setup();
final MethodCoverageImpl m = new MethodCoverageImpl("a", "()V", null);
m.increment(CounterImpl.COUNTER_1_0, CounterImpl.COUNTER_0_0, 42);
node = new ClassCoverageImpl("org/jacoco/example/Foo", 123, false); node = new ClassCoverageImpl("org/jacoco/example/Foo", 123, false);
node.addMethod(new MethodCoverageImpl("a", "()V", null)); node.addMethod(m);
node.addMethod(new MethodCoverageImpl("b", "()V", null)); node.addMethod(new MethodCoverageImpl("b", "()V", null));
node.addMethod(new MethodCoverageImpl("c", "()V", null)); node.addMethod(new MethodCoverageImpl("c", "()V", null));
} }
Expand All @@ -61,13 +64,15 @@ public void testContents() throws Exception {
} }


@Test @Test
public void should_not_generate_message_when_SourceFileName_not_present() public void should_generate_message_when_SourceFileName_not_present()
throws Exception { throws Exception {
page = new ClassPage(node, null, null, rootFolder, context); page = new ClassPage(node, null, null, rootFolder, context);
page.render(); page.render();


final Document doc = support.parse(output.getFile("Foo.html")); final Document doc = support.parse(output.getFile("Foo.html"));
assertEquals("", support.findStr(doc, "/html/body/p[1]")); assertEquals(
"Class files must be compiled with debug information to link with source files.",
support.findStr(doc, "/html/body/p[1]"));
} }


@Test @Test
Expand All @@ -87,8 +92,10 @@ public void should_generate_message_when_SourceFileName_present_but_no_SourceFil
@Test @Test
public void should_generate_message_with_default_package_when_SourceFileName_present_but_no_SourceFilePage() public void should_generate_message_with_default_package_when_SourceFileName_present_but_no_SourceFilePage()
throws Exception { throws Exception {
final MethodCoverageImpl m = new MethodCoverageImpl("a", "()V", null);
m.increment(CounterImpl.COUNTER_1_0, CounterImpl.COUNTER_0_0, 42);
node = new ClassCoverageImpl("Foo", 123, false); node = new ClassCoverageImpl("Foo", 123, false);
node.addMethod(new MethodCoverageImpl("a", "()V", null)); node.addMethod(m);
node.setSourceFileName("Foo.java"); node.setSourceFileName("Foo.java");


page = new ClassPage(node, null, null, rootFolder, context); page = new ClassPage(node, null, null, rootFolder, context);
Expand All @@ -112,6 +119,20 @@ public void should_not_generate_message_when_SourceFileName_and_SourceFilePage_p
assertEquals("", support.findStr(doc, "/html/body/p[1]")); assertEquals("", support.findStr(doc, "/html/body/p[1]"));
} }


@Test
public void should_generate_message_when_no_lines() throws Exception {
node = new ClassCoverageImpl("Foo", 123, false);
node.addMethod(new MethodCoverageImpl("m", "()V", null));

page = new ClassPage(node, null, new SourceLink(), rootFolder, context);
page.render();

final Document doc = support.parse(output.getFile("Foo.html"));
assertEquals(
"Class files must be compiled with debug information to show line coverage.",
support.findStr(doc, "/html/body/p[1]"));
}

private class SourceLink implements ILinkable { private class SourceLink implements ILinkable {


public String getLink(final ReportOutputFolder base) { public String getLink(final ReportOutputFolder base) {
Expand Down
Expand Up @@ -83,12 +83,22 @@ public String getLinkLabel() {


@Override @Override
protected void content(HTMLElement body) throws IOException { protected void content(HTMLElement body) throws IOException {
if (getNode().getSourceFileName() != null && sourcePage == null) { if (getNode().getLineCounter().getTotalCount() == 0) {
body.p().text(
"Class files must be compiled with debug information to show line coverage.");
}

final String sourceFileName = getNode().getSourceFileName();
if (sourceFileName == null) {
body.p().text(
"Class files must be compiled with debug information to link with source files.");

} else if (sourcePage == null) {
final String sourcePath; final String sourcePath;
if (getNode().getPackageName().length() != 0) { if (getNode().getPackageName().length() != 0) {
sourcePath = getNode().getPackageName() + "/" + getNode().getSourceFileName(); sourcePath = getNode().getPackageName() + "/" + sourceFileName;
} else { } else {
sourcePath = getNode().getSourceFileName(); sourcePath = sourceFileName;
} }
body.p().text("Source file \"" + sourcePath body.p().text("Source file \"" + sourcePath
+ "\" was not found during generation of report."); + "\" was not found during generation of report.");
Expand Down

0 comments on commit c7d70c7

Please sign in to comment.