diff options
Diffstat (limited to 'org.jacoco.report.test/src/org/jacoco/report/internal')
30 files changed, 660 insertions, 531 deletions
diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/NormalizedFileNamesTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/NormalizedFileNamesTest.java index 0dfb6801..c0396d8e 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/NormalizedFileNamesTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/NormalizedFileNamesTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/ReportOutputFolderTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/ReportOutputFolderTest.java index 7ff9641c..905fe646 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/ReportOutputFolderTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/ReportOutputFolderTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/HTMLDocumentTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/HTMLDocumentTest.java deleted file mode 100644 index 436fe9a9..00000000 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/HTMLDocumentTest.java +++ /dev/null @@ -1,85 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.report.internal.html; - -import static org.junit.Assert.assertEquals; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.StringWriter; - -import org.junit.Test; - -/** - * Unit tests for {@link HTMLDocument}. - */ -public class HTMLDocumentTest { - - @Test - public void testWriter() throws IOException { - StringWriter buffer = new StringWriter(); - new HTMLDocument(buffer, "UTF-8").close(); - assertEquals( - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" - + "<html xmlns=\"http://www.w3.org/1999/xhtml\"/>", - buffer.toString()); - } - - @Test - public void testStream() throws IOException { - ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - new HTMLDocument(buffer, "UTF-8").close(); - assertEquals( - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" - + "<html xmlns=\"http://www.w3.org/1999/xhtml\"/>", - buffer.toString("UTF-8")); - } - - @Test - public void testHead() throws IOException { - StringWriter buffer = new StringWriter(); - final HTMLDocument doc = new HTMLDocument(buffer, "UTF-8"); - doc.head(); - doc.close(); - assertEquals( - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" - + "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head/></html>", - buffer.toString()); - } - - @Test - public void testBody() throws IOException { - StringWriter buffer = new StringWriter(); - final HTMLDocument doc = new HTMLDocument(buffer, "UTF-8"); - doc.body(); - doc.close(); - assertEquals( - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" - + "<html xmlns=\"http://www.w3.org/1999/xhtml\"><body/></html>", - buffer.toString()); - } - - @Test - public void testMinimalHTMLDocument() throws Exception { - StringWriter buffer = new StringWriter(); - final HTMLDocument doc = new HTMLDocument(buffer, "UTF-8"); - doc.head().title(); - doc.body(); - doc.close(); - new HTMLSupport().parse(buffer.toString()); - } - -} diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/HTMLElementTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/HTMLElementTest.java index 2eff51a1..89a04ec9 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/HTMLElementTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/HTMLElementTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -13,8 +13,8 @@ package org.jacoco.report.internal.html; import static org.junit.Assert.assertEquals; +import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.StringWriter; import org.junit.Before; import org.junit.Test; @@ -24,218 +24,215 @@ import org.junit.Test; */ public class HTMLElementTest { - private StringWriter buffer; + private static final String PREFIX = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\">"; + + private static final String SUFFIX = "</html>"; + + private ByteArrayOutputStream buffer; private HTMLElement root; @Before - public void setUp() throws IOException { - buffer = new StringWriter(); - root = new HTMLElement(buffer, "root") { - { - beginOpenTag(); - } - }; + public void setup() throws IOException { + buffer = new ByteArrayOutputStream(); + root = new HTMLElement(buffer, "UTF-8"); } @Test - public void testMeta() throws IOException { - root.meta("key", "value"); + public void should_create_minimal_valid_html_document() throws Exception { + root.head().title(); + root.body(); root.close(); - assertEquals( - "<root><meta http-equiv=\"key\" content=\"value\"/></root>", - buffer.toString()); + new HTMLSupport().parse(buffer); + } + + @Test + public void head_should_create_head_tag() throws IOException { + root.head(); + assertContent("<head/>"); + } + + @Test + public void meta_should_create_meta_tag_with_attributes() + throws IOException { + root.meta("key", "value"); + assertContent("<meta http-equiv=\"key\" content=\"value\"/>"); } @Test - public void testLink() throws IOException { + public void body_should_create_body_tag() throws IOException { + root.body(); + assertContent("<body/>"); + } + + @Test + public void link_should_create_link_tag_with_attributes() + throws IOException { root.link("stylesheet", "style.css", "text/css"); - root.close(); - assertEquals( - "<root><link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\"/></root>", - buffer.toString()); + assertContent( + "<link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\"/>"); } @Test - public void testTitle() throws IOException { + public void title_should_create_title_tag() throws IOException { root.title(); - root.close(); - assertEquals("<root><title/></root>", buffer.toString()); + assertContent("<title/>"); } @Test - public void testH1() throws IOException { + public void h1_should_create_h1_tag() throws IOException { root.h1(); - root.close(); - assertEquals("<root><h1/></root>", buffer.toString()); + assertContent("<h1/>"); } @Test - public void testP() throws IOException { + public void p_should_create_p_tag() throws IOException { root.p(); - root.close(); - assertEquals("<root><p/></root>", buffer.toString()); + assertContent("<p/>"); } @Test - public void testSpan1() throws IOException { + public void span_should_create_span_tag() throws IOException { root.span(); - root.close(); - assertEquals("<root><span/></root>", buffer.toString()); + assertContent("<span/>"); } @Test - public void testSpan2() throws IOException { + public void span_should_create_span_tag_with_class_attribute() + throws IOException { root.span("abc"); - root.close(); - assertEquals("<root><span class=\"abc\"/></root>", buffer.toString()); + assertContent("<span class=\"abc\"/>"); } @Test - public void testSpan3() throws IOException { + public void span_should_create_span_tag_with_class_and_id_attribute() + throws IOException { root.span("abc", "xy"); - root.close(); - assertEquals("<root><span class=\"abc\" id=\"xy\"/></root>", - buffer.toString()); + assertContent("<span class=\"abc\" id=\"xy\"/>"); } @Test - public void testPre() throws IOException { + public void pre_should_create_pre_tag_with_class_attribute() + throws IOException { root.pre("mystyle"); - root.close(); - assertEquals("<root><pre class=\"mystyle\"/></root>", buffer.toString()); + assertContent("<pre class=\"mystyle\"/>"); } @Test - public void testDiv() throws IOException { + public void div_should_create_div_tag_with_class_attribute() + throws IOException { root.div("mystyle"); - root.close(); - assertEquals("<root><div class=\"mystyle\"/></root>", buffer.toString()); + assertContent("<div class=\"mystyle\"/>"); } @Test - public void testCode() throws IOException { + public void code_should_create_code_tag() throws IOException { root.code().text("0xCAFEBABE"); - root.close(); - assertEquals("<root><code>0xCAFEBABE</code></root>", buffer.toString()); + assertContent("<code>0xCAFEBABE</code>"); } @Test - public void testA1() throws IOException { + public void a_should_create_a_tag_with_href_attribute() throws IOException { root.a("http://www.jacoco.org/"); - root.close(); - assertEquals("<root><a href=\"http://www.jacoco.org/\"/></root>", - buffer.toString()); + assertContent("<a href=\"http://www.jacoco.org/\"/>"); } @Test - public void testA2() throws IOException { + public void a_should_create_a_tag_with_href_and_class_attribute() + throws IOException { root.a("http://www.jacoco.org/", "extern"); - root.close(); - assertEquals( - "<root><a href=\"http://www.jacoco.org/\" class=\"extern\"/></root>", - buffer.toString()); + assertContent("<a href=\"http://www.jacoco.org/\" class=\"extern\"/>"); } @Test - public void testALinkable1() throws IOException { + public void a_should_create_span_tag_when_no_link_is_given() + throws IOException { root.a(new LinkableStub(null, "here", null), null); - root.close(); - assertEquals("<root><span>here</span></root>", buffer.toString()); + assertContent("<span>here</span>"); } @Test - public void testALinkable2() throws IOException { + public void a_should_create_span_tag_with_class_attribute_when_no_link_is_given() + throws IOException { root.a(new LinkableStub(null, "here", "blue"), null); - root.close(); - assertEquals("<root><span class=\"blue\">here</span></root>", - buffer.toString()); + assertContent("<span class=\"blue\">here</span>"); } @Test - public void testALinkable3() throws IOException { + public void a_should_create_a_tag_when_link_is_given() throws IOException { root.a(new LinkableStub("index.html", "here", null), null); - root.close(); - assertEquals("<root><a href=\"index.html\">here</a></root>", - buffer.toString()); + assertContent("<a href=\"index.html\">here</a>"); } @Test - public void testALinkable4() throws IOException { + public void a_should_create_a_tag_with_class_attribute_when_link_is_given() + throws IOException { root.a(new LinkableStub("index.html", "here", "red"), null); - root.close(); - assertEquals( - "<root><a href=\"index.html\" class=\"red\">here</a></root>", - buffer.toString()); + assertContent("<a href=\"index.html\" class=\"red\">here</a>"); } @Test - public void testTable() throws IOException { + public void table_should_create_table_tag_with_attributes() + throws IOException { root.table("tablestyle"); - root.close(); - assertEquals( - "<root><table class=\"tablestyle\" cellspacing=\"0\"/></root>", - buffer.toString()); + assertContent("<table class=\"tablestyle\" cellspacing=\"0\"/>"); } @Test - public void testThead() throws IOException { + public void thead_should_create_thead_tag() throws IOException { root.thead(); - root.close(); - assertEquals("<root><thead/></root>", buffer.toString()); + assertContent("<thead/>"); } @Test - public void testTfoot() throws IOException { + public void tfoot_should_create_tfoot_tag() throws IOException { root.tfoot(); - root.close(); - assertEquals("<root><tfoot/></root>", buffer.toString()); + assertContent("<tfoot/>"); } @Test - public void testTbody() throws IOException { + public void tbody_should_create_tbody_tag() throws IOException { root.tbody(); - root.close(); - assertEquals("<root><tbody/></root>", buffer.toString()); + assertContent("<tbody/>"); } @Test - public void testTr() throws IOException { + public void tr_should_create_tr_tag() throws IOException { root.tr(); - root.close(); - assertEquals("<root><tr/></root>", buffer.toString()); + assertContent("<tr/>"); } @Test - public void testTd1() throws IOException { + public void td_should_create_td_tag() throws IOException { root.td(); - root.close(); - assertEquals("<root><td/></root>", buffer.toString()); + assertContent("<td/>"); } @Test - public void testTd2() throws IOException { + public void td_should_create_td_tag_with_class_attribute() + throws IOException { root.td("mystyle"); - root.close(); - assertEquals("<root><td class=\"mystyle\"/></root>", buffer.toString()); + assertContent("<td class=\"mystyle\"/>"); } @Test - public void testImg() throws IOException { + public void img_should_create_img_tag_with_attributes() throws IOException { root.img("sample.gif", 16, 32, "Hello"); - root.close(); - assertEquals( - "<root><img src=\"sample.gif\" width=\"16\" height=\"32\" title=\"Hello\" alt=\"Hello\"/></root>", - buffer.toString()); + assertContent( + "<img src=\"sample.gif\" width=\"16\" height=\"32\" title=\"Hello\" alt=\"Hello\"/>"); } @Test - public void testScript() throws IOException { - root.script("text/javascript", "file.js"); + public void script_should_create_script_tag_with_attributes() + throws IOException { + root.script("file.js"); + assertContent( + "<script type=\"text/javascript\" src=\"file.js\"></script>"); + } + + private void assertContent(String expected) throws IOException { root.close(); - assertEquals( - "<root><script type=\"text/javascript\" src=\"file.js\"></script></root>", - buffer.toString()); + assertEquals(PREFIX + expected + SUFFIX, buffer.toString("UTF-8")); } } diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/HTMLSupport.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/HTMLSupport.java index 1e83ebc7..ae3a2976 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/HTMLSupport.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/HTMLSupport.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/LinkableStub.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/LinkableStub.java index 799cd794..b80b5acb 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/LinkableStub.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/LinkableStub.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/BundlePageTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/BundlePageTest.java new file mode 100644 index 00000000..716249cf --- /dev/null +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/BundlePageTest.java @@ -0,0 +1,111 @@ +/******************************************************************************* + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Evgeny Mandrikov - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.report.internal.html.page; + +import org.jacoco.core.analysis.IBundleCoverage; +import org.jacoco.core.analysis.IClassCoverage; +import org.jacoco.core.analysis.IPackageCoverage; +import org.jacoco.core.analysis.ISourceFileCoverage; +import org.jacoco.core.internal.analysis.BundleCoverageImpl; +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.PackageCoverageImpl; +import org.junit.Before; +import org.junit.Test; +import org.w3c.dom.Document; + +import java.util.Arrays; +import java.util.Collections; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link BundlePage}. + */ +public class BundlePageTest extends PageTestBase { + + @Before + @Override + public void setup() throws Exception { + super.setup(); + } + + @Test + public void should_render_non_empty_packages() throws Exception { + final ClassCoverageImpl classCoverage = new ClassCoverageImpl( + "example/Class", 0, false); + final MethodCoverageImpl methodCoverage = new MethodCoverageImpl("m", + "()V", null); + methodCoverage.increment(CounterImpl.COUNTER_1_0, + CounterImpl.COUNTER_0_0, 42); + classCoverage.addMethod(methodCoverage); + final IPackageCoverage nonEmptyPackage = new PackageCoverageImpl( + "example", + Collections.<IClassCoverage> singleton(classCoverage), + Collections.<ISourceFileCoverage> emptySet()); + + final IPackageCoverage emptyPackage = new PackageCoverageImpl("empty", + Collections.<IClassCoverage> emptySet(), + Collections.<ISourceFileCoverage> emptySet()); + + final IBundleCoverage node = new BundleCoverageImpl("bundle", + Arrays.asList(nonEmptyPackage, emptyPackage)); + + final BundlePage page = new BundlePage(node, null, null, rootFolder, + context); + page.render(); + + final Document doc = support.parse(output.getFile("index.html")); + assertEquals("el_package", support.findStr(doc, + "/html/body/table[1]/tbody/tr[1]/td[1]/a/@class")); + assertEquals("example", support.findStr(doc, + "/html/body/table[1]/tbody/tr[1]/td[1]/a")); + assertEquals("1", + support.findStr(doc, "count(/html/body/table[1]/tbody/tr)")); + } + + @Test + public void should_render_message_when_no_class_files_specified() + throws Exception { + final IBundleCoverage node = new BundleCoverageImpl("bundle", + Collections.<IPackageCoverage> emptySet()); + + final BundlePage page = new BundlePage(node, null, null, rootFolder, + context); + page.render(); + + final Document doc = support.parse(output.getFile("index.html")); + assertEquals("No class files specified.", + support.findStr(doc, "/html/body/p")); + } + + @Test + public void should_render_message_when_all_classes_empty() + throws Exception { + final ClassCoverageImpl emptyClass = new ClassCoverageImpl( + "example/Class", 0, false); + final IBundleCoverage node = new BundleCoverageImpl("bundle", + Collections.<IClassCoverage> singleton(emptyClass), + Collections.<ISourceFileCoverage> emptySet()); + + final BundlePage page = new BundlePage(node, null, null, rootFolder, + context); + page.render(); + + final Document doc = support.parse(output.getFile("index.html")); + assertEquals( + "None of the analyzed classes contain code relevant for code coverage.", + support.findStr(doc, "/html/body/p")); + } + +} diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/ClassPageTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/ClassPageTest.java index 5a296f4b..732d1c0e 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/ClassPageTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/ClassPageTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -17,7 +17,10 @@ import java.io.IOException; import org.jacoco.core.analysis.IClassCoverage; import org.jacoco.core.internal.analysis.ClassCoverageImpl; +import org.jacoco.core.internal.analysis.CounterImpl; import org.jacoco.core.internal.analysis.MethodCoverageImpl; +import org.jacoco.report.internal.ReportOutputFolder; +import org.jacoco.report.internal.html.ILinkable; import org.junit.Before; import org.junit.Test; import org.w3c.dom.Document; @@ -35,8 +38,10 @@ public class ClassPageTest extends PageTestBase { @Override public void setup() throws Exception { 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.addMethod(new MethodCoverageImpl("a", "()V", null)); + node.addMethod(m); node.addMethod(new MethodCoverageImpl("b", "()V", null)); node.addMethod(new MethodCoverageImpl("c", "()V", null)); } @@ -47,6 +52,7 @@ public class ClassPageTest extends PageTestBase { page.render(); final Document doc = support.parse(output.getFile("Foo.html")); + assertEquals("", support.findStr(doc, "doc/body/p[1]")); assertEquals("el_method", support.findStr(doc, "/html/body/table[1]/tbody/tr[1]/td[1]/span/@class")); assertEquals("a()", support.findStr(doc, @@ -58,6 +64,106 @@ public class ClassPageTest extends PageTestBase { } @Test + public void should_generate_message_when_SourceFileName_not_present() + throws Exception { + page = new ClassPage(node, null, null, rootFolder, context); + page.render(); + + final Document doc = support.parse(output.getFile("Foo.html")); + assertEquals( + "Class files must be compiled with debug information to link with source files.", + support.findStr(doc, "/html/body/p[1]")); + } + + @Test + public void should_generate_message_when_SourceFileName_present_but_no_SourceFilePage() + throws Exception { + node.setSourceFileName("Foo.java"); + + page = new ClassPage(node, null, null, rootFolder, context); + page.render(); + + final Document doc = support.parse(output.getFile("Foo.html")); + assertEquals( + "Source file \"org/jacoco/example/Foo.java\" was not found during generation of report.", + support.findStr(doc, "/html/body/p[1]")); + } + + @Test + public void should_generate_message_with_default_package_when_SourceFileName_present_but_no_SourceFilePage() + 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.addMethod(m); + node.setSourceFileName("Foo.java"); + + page = new ClassPage(node, null, null, rootFolder, context); + page.render(); + + final Document doc = support.parse(output.getFile("Foo.html")); + assertEquals( + "Source file \"Foo.java\" was not found during generation of report.", + support.findStr(doc, "/html/body/p[1]")); + } + + @Test + public void should_not_generate_message_when_SourceFileName_and_SourceFilePage_present() + throws Exception { + node.setSourceFileName("Foo.java"); + + page = new ClassPage(node, null, new SourceLink(), rootFolder, context); + page.render(); + + final Document doc = support.parse(output.getFile("Foo.html")); + 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]")); + } + + @Test + public void should_generate_message_when_class_id_mismatch() + throws Exception { + node = new ClassCoverageImpl("Foo", 123, true); + 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("A different version of class was executed at runtime.", + support.findStr(doc, "/html/body/p[1]")); + } + + private class SourceLink implements ILinkable { + + public String getLink(final ReportOutputFolder base) { + return "Source.java.html"; + } + + public String getLinkLabel() { + return ""; + } + + public String getLinkStyle() { + return null; + } + + } + + @Test public void testGetFileName() throws IOException { page = new ClassPage(node, null, null, rootFolder, context); assertEquals("Foo.html", page.getFileName()); diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/MethodItemTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/MethodItemTest.java index ccd3a434..e9a030eb 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/MethodItemTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/MethodItemTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/NodePageTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/NodePageTest.java index 3adfaea5..65affac6 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/NodePageTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/NodePageTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/PackagePageTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/PackagePageTest.java index 2a0fd275..828f5f51 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/PackagePageTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/PackagePageTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -22,6 +22,8 @@ import org.jacoco.core.analysis.IClassCoverage; import org.jacoco.core.analysis.IPackageCoverage; import org.jacoco.core.analysis.ISourceFileCoverage; 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.PackageCoverageImpl; import org.jacoco.core.internal.analysis.SourceFileCoverageImpl; import org.jacoco.report.ISourceFileLocator; @@ -30,7 +32,7 @@ import org.junit.Test; import org.w3c.dom.Document; /** - * Unit tests for {@link PackageSourcePage}. + * Unit tests for {@link PackagePage}. */ public class PackagePageTest extends PageTestBase { @@ -57,15 +59,48 @@ public class PackagePageTest extends PageTestBase { } @Test + public void should_render_non_empty_classes() throws Exception { + final ClassCoverageImpl nonEmptyClass = new ClassCoverageImpl( + "example/NonEmptyClass", 0, false); + final MethodCoverageImpl nonEmptyMethod = new MethodCoverageImpl("m", + "()V", null); + nonEmptyMethod.increment(CounterImpl.COUNTER_1_0, + CounterImpl.COUNTER_0_0, 42); + nonEmptyClass.addMethod(nonEmptyMethod); + final ClassCoverageImpl emptyClass = new ClassCoverageImpl( + "example/EmptyClass", 0, false); + + node = new PackageCoverageImpl("example", + Arrays.<IClassCoverage> asList(emptyClass, nonEmptyClass), + Collections.<ISourceFileCoverage> emptySet()); + + page = new PackagePage(node, null, sourceLocator, rootFolder, context); + page.render(); + + final Document doc = support.parse(output.getFile("index.html")); + assertEquals("NonEmptyClass", support.findStr(doc, + "/html/body/table[1]/tbody/tr[1]/td[1]/a")); + assertEquals("1", + support.findStr(doc, "count(/html/body/table[1]/tbody/tr)")); + } + + @Test public void testContentsWithSource() throws Exception { - IClassCoverage class1 = new ClassCoverageImpl( + ClassCoverageImpl class1 = new ClassCoverageImpl( "org/jacoco/example/Foo1", 0x1000, false); - IClassCoverage class2 = new ClassCoverageImpl( + MethodCoverageImpl method1 = new MethodCoverageImpl("m", "()V", null); + method1.increment(CounterImpl.COUNTER_1_0, CounterImpl.COUNTER_0_0, 42); + class1.addMethod(method1); + ClassCoverageImpl class2 = new ClassCoverageImpl( "org/jacoco/example/Foo2", 0x2000, false); + MethodCoverageImpl method2 = new MethodCoverageImpl("m", "()V", null); + method2.increment(CounterImpl.COUNTER_1_0, CounterImpl.COUNTER_0_0, 42); + class2.addMethod(method2); ISourceFileCoverage src1 = new SourceFileCoverageImpl("Src1.java", "org/jacoco/example"); - node = new PackageCoverageImpl("org/jacoco/example", Arrays.asList( - class1, class2), Arrays.asList(src1)); + node = new PackageCoverageImpl("org/jacoco/example", + Arrays.<IClassCoverage> asList(class1, class2), + Arrays.asList(src1)); page = new PackagePage(node, null, sourceLocator, rootFolder, context); page.render(); @@ -93,12 +128,19 @@ public class PackagePageTest extends PageTestBase { @Test public void testContentsNoSource() throws Exception { - IClassCoverage class1 = new ClassCoverageImpl( + ClassCoverageImpl class1 = new ClassCoverageImpl( "org/jacoco/example/Foo1", 0x1000, false); - IClassCoverage class2 = new ClassCoverageImpl( + MethodCoverageImpl method1 = new MethodCoverageImpl("m", "()V", null); + method1.increment(CounterImpl.COUNTER_1_0, CounterImpl.COUNTER_0_0, 42); + class1.addMethod(method1); + ClassCoverageImpl class2 = new ClassCoverageImpl( "org/jacoco/example/Foo2", 0x2000, false); - node = new PackageCoverageImpl("org/jacoco/example", Arrays.asList( - class1, class2), Collections.<ISourceFileCoverage> emptyList()); + MethodCoverageImpl method2 = new MethodCoverageImpl("m", "()V", null); + method2.increment(CounterImpl.COUNTER_1_0, CounterImpl.COUNTER_0_0, 42); + class2.addMethod(method2); + node = new PackageCoverageImpl("org/jacoco/example", + Arrays.<IClassCoverage> asList(class1, class2), + Collections.<ISourceFileCoverage> emptyList()); page = new PackagePage(node, null, sourceLocator, rootFolder, context); page.render(); diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/PackageSourcePageTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/PackageSourcePageTest.java index c39a4982..96db3a59 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/PackageSourcePageTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/PackageSourcePageTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -24,6 +24,7 @@ import java.util.Collections; import org.jacoco.core.analysis.IClassCoverage; import org.jacoco.core.analysis.ISourceFileCoverage; +import org.jacoco.core.internal.analysis.CounterImpl; import org.jacoco.core.internal.analysis.PackageCoverageImpl; import org.jacoco.core.internal.analysis.SourceFileCoverageImpl; import org.jacoco.report.ISourceFileLocator; @@ -48,12 +49,16 @@ public class PackageSourcePageTest extends PageTestBase { @Override public void setup() throws Exception { super.setup(); - ISourceFileCoverage src1 = new SourceFileCoverageImpl("Src1.java", + SourceFileCoverageImpl src1 = new SourceFileCoverageImpl("Src1.java", "org/jacoco/example"); - ISourceFileCoverage src2 = new SourceFileCoverageImpl("Src2.java", + src1.increment(CounterImpl.COUNTER_1_0, + CounterImpl.COUNTER_0_0, 1); + SourceFileCoverageImpl src2 = new SourceFileCoverageImpl("Src2.java", "org/jacoco/example"); + src2.increment(CounterImpl.COUNTER_1_0, + CounterImpl.COUNTER_0_0, 1); node = new PackageCoverageImpl("org/jacoco/example", - Collections.<IClassCoverage> emptyList(), Arrays.asList(src1, + Collections.<IClassCoverage> emptyList(), Arrays.<ISourceFileCoverage>asList(src1, src2)); sourceLocator = new ISourceFileLocator() { @@ -109,6 +114,29 @@ public class PackageSourcePageTest extends PageTestBase { } @Test + public void should_render_non_empty_sources() throws Exception { + final ISourceFileCoverage emptySource = new SourceFileCoverageImpl( + "Empty.java", "example"); + final SourceFileCoverageImpl nonEmptySource = new SourceFileCoverageImpl( + "NonEmpty.java", "example"); + nonEmptySource.increment(CounterImpl.COUNTER_1_0, + CounterImpl.COUNTER_0_0, 1); + node = new PackageCoverageImpl("example", + Collections.<IClassCoverage> emptyList(), + Arrays.asList(emptySource, nonEmptySource)); + + page = new PackageSourcePage(node, null, sourceLocator, rootFolder, + context, packagePageLink); + page.render(); + + final Document doc = support.parse(output.getFile("index.source.html")); + assertEquals("NonEmpty.java", support.findStr(doc, + "/html/body/table[1]/tbody/tr[1]/td[1]/span")); + assertEquals("1", + support.findStr(doc, "count(/html/body/table[1]/tbody/tr)")); + } + + @Test public void testGetSourceFilePages() throws Exception { page = new PackageSourcePage(node, null, sourceLocator, rootFolder, context, packagePageLink); diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/PageTestBase.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/PageTestBase.java index 40cd983d..8a5574d0 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/PageTestBase.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/PageTestBase.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/ReportPageTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/ReportPageTest.java index 1d875f30..0f42bb48 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/ReportPageTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/ReportPageTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/SessionsPageTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/SessionsPageTest.java index 7983d8d9..2380d572 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/SessionsPageTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/SessionsPageTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/SourceFilePageTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/SourceFilePageTest.java index c0bc72e3..da433542 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/SourceFilePageTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/SourceFilePageTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/SourceHighlighterTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/SourceHighlighterTest.java index ca9d4d70..3ef347de 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/SourceHighlighterTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/SourceHighlighterTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -13,14 +13,13 @@ package org.jacoco.report.internal.html.page; import static org.junit.Assert.assertEquals; +import java.io.ByteArrayOutputStream; import java.io.StringReader; -import java.io.StringWriter; import java.util.Locale; import org.jacoco.core.analysis.ICoverageNode.ElementType; import org.jacoco.core.internal.analysis.CounterImpl; import org.jacoco.core.internal.analysis.SourceNodeImpl; -import org.jacoco.report.internal.html.HTMLDocument; import org.jacoco.report.internal.html.HTMLElement; import org.jacoco.report.internal.html.HTMLSupport; import org.jacoco.report.internal.html.resources.Styles; @@ -35,9 +34,9 @@ public class SourceHighlighterTest { private HTMLSupport htmlSupport; - private StringWriter buffer; + private ByteArrayOutputStream buffer; - private HTMLDocument html; + private HTMLElement html; private HTMLElement parent; @@ -49,8 +48,8 @@ public class SourceHighlighterTest { public void setup() throws Exception { htmlSupport = new HTMLSupport(); source = new SourceNodeImpl(ElementType.SOURCEFILE, "Foo.java"); - buffer = new StringWriter(); - html = new HTMLDocument(buffer, "UTF-8"); + buffer = new ByteArrayOutputStream(); + html = new HTMLElement(buffer, "UTF-8"); html.head().title(); parent = html.body(); sourceHighlighter = new SourceHighlighter(Locale.US); @@ -60,8 +59,7 @@ public class SourceHighlighterTest { public void testDefaultTabWidth() throws Exception { final String src = "\tA"; sourceHighlighter.render(parent, source, new StringReader(src)); - html.close(); - final Document doc = htmlSupport.parse(buffer.toString()); + final Document doc = parseDoc(); // Assert that we no longer replace tabs with spaces assertEquals("\tA\n", htmlSupport.findStr(doc, "//pre/text()")); @@ -70,8 +68,7 @@ public class SourceHighlighterTest { @Test public void testDefaultLanguage() throws Exception { sourceHighlighter.render(parent, source, new StringReader("")); - html.close(); - final Document doc = htmlSupport.parse(buffer.toString()); + final Document doc = parseDoc(); assertEquals("source lang-java linenums", htmlSupport.findStr(doc, "//pre/@class")); } @@ -80,8 +77,7 @@ public class SourceHighlighterTest { public void testSetLanguage() throws Exception { sourceHighlighter.setLanguage("scala"); sourceHighlighter.render(parent, source, new StringReader("")); - html.close(); - final Document doc = htmlSupport.parse(buffer.toString()); + final Document doc = parseDoc(); assertEquals("source lang-scala linenums", htmlSupport.findStr(doc, "//pre/@class")); } @@ -94,8 +90,7 @@ public class SourceHighlighterTest { source.increment(CounterImpl.COUNTER_0_1, CounterImpl.COUNTER_0_0, 2); source.increment(CounterImpl.COUNTER_0_1, CounterImpl.COUNTER_0_0, 3); sourceHighlighter.render(parent, source, new StringReader(src)); - html.close(); - final Document doc = htmlSupport.parse(buffer.toString()); + final Document doc = parseDoc(); assertEquals(Styles.NOT_COVERED, htmlSupport.findStr(doc, "//pre/span[text() = 'A']/@class")); assertEquals(Styles.PARTLY_COVERED, @@ -109,18 +104,16 @@ public class SourceHighlighterTest { @Test public void testHighlightNone() throws Exception { sourceHighlighter.highlight(parent, source.getLine(1), 1); - html.close(); - final Document doc = htmlSupport.parse(buffer.toString()); + final Document doc = parseDoc(); assertEquals("", htmlSupport.findStr(doc, "//pre")); } @Test public void testHighlightBranchesFC() throws Exception { - source.increment(CounterImpl.COUNTER_0_1, - CounterImpl.getInstance(0, 5), 1); + source.increment(CounterImpl.COUNTER_0_1, CounterImpl.getInstance(0, 5), + 1); sourceHighlighter.highlight(parent.pre(null), source.getLine(1), 1); - html.close(); - final Document doc = htmlSupport.parse(buffer.toString()); + final Document doc = parseDoc(); assertEquals("fc bfc", htmlSupport.findStr(doc, "//pre/span/@class")); assertEquals("All 5 branches covered.", htmlSupport.findStr(doc, "//pre/span/@title")); @@ -128,11 +121,10 @@ public class SourceHighlighterTest { @Test public void testHighlightBranchesPC() throws Exception { - source.increment(CounterImpl.COUNTER_0_1, - CounterImpl.getInstance(2, 3), 1); + source.increment(CounterImpl.COUNTER_0_1, CounterImpl.getInstance(2, 3), + 1); sourceHighlighter.highlight(parent.pre(null), source.getLine(1), 1); - html.close(); - final Document doc = htmlSupport.parse(buffer.toString()); + final Document doc = parseDoc(); assertEquals("pc bpc", htmlSupport.findStr(doc, "//pre/span/@class")); assertEquals("2 of 5 branches missed.", htmlSupport.findStr(doc, "//pre/span/@title")); @@ -140,14 +132,18 @@ public class SourceHighlighterTest { @Test public void testHighlightBranchesNC() throws Exception { - source.increment(CounterImpl.COUNTER_0_1, - CounterImpl.getInstance(5, 0), 1); + source.increment(CounterImpl.COUNTER_0_1, CounterImpl.getInstance(5, 0), + 1); sourceHighlighter.highlight(parent.pre(null), source.getLine(1), 1); - html.close(); - final Document doc = htmlSupport.parse(buffer.toString()); + final Document doc = parseDoc(); assertEquals("pc bnc", htmlSupport.findStr(doc, "//pre/span/@class")); assertEquals("All 5 branches missed.", htmlSupport.findStr(doc, "//pre/span/@title")); } + private Document parseDoc() throws Exception { + html.close(); + return htmlSupport.parse(buffer); + } + } diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/resources/ResourcesTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/resources/ResourcesTest.java index 9dd7dea9..1e61cebc 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/resources/ResourcesTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/resources/ResourcesTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/resources/StylesTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/resources/StylesTest.java index e0eea6e3..fdf54756 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/resources/StylesTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/resources/StylesTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/BarColumnTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/BarColumnTest.java index 97e7658e..8623eee7 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/BarColumnTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/BarColumnTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -26,7 +26,6 @@ import org.jacoco.core.analysis.ICoverageNode.ElementType; import org.jacoco.core.internal.analysis.CounterImpl; import org.jacoco.report.MemoryMultiReportOutput; import org.jacoco.report.internal.ReportOutputFolder; -import org.jacoco.report.internal.html.HTMLDocument; import org.jacoco.report.internal.html.HTMLElement; import org.jacoco.report.internal.html.HTMLSupport; import org.jacoco.report.internal.html.resources.Resources; @@ -46,7 +45,7 @@ public class BarColumnTest { private Resources resources; - private HTMLDocument doc; + private HTMLElement html; private HTMLElement td; @@ -59,9 +58,9 @@ public class BarColumnTest { output = new MemoryMultiReportOutput(); root = new ReportOutputFolder(output); resources = new Resources(root); - doc = new HTMLDocument(root.createFile("Test.html"), "UTF-8"); - doc.head().title(); - td = doc.body().table("somestyle").tr().td(); + html = new HTMLElement(root.createFile("Test.html"), "UTF-8"); + html.head().title(); + td = html.body().table("somestyle").tr().td(); support = new HTMLSupport(); column = new BarColumn(CounterEntity.LINE, Locale.ENGLISH); } @@ -76,14 +75,13 @@ public class BarColumnTest { public void testInit() throws Exception { final ITableItem i = createItem(6, 24); assertTrue(column.init(Arrays.asList(i), i.getNode())); - doc.close(); + html.close(); } @Test public void testFooter() throws Exception { column.footer(td, createNode(15, 5), resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("15 of 20", support.findStr(doc, "/html/body/table/tr/td/text()")); } @@ -94,8 +92,7 @@ public class BarColumnTest { final ITableItem i2 = createItem(6, 24); column.init(Arrays.asList(i1, i2), createNode(21, 29)); column.item(td, i1, resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("2", support.findStr(doc, "count(/html/body/table/tr[1]/td/img)")); @@ -105,16 +102,16 @@ public class BarColumnTest { support.findStr(doc, "/html/body/table/tr[1]/td/img[1]/@src")); assertEquals("15", support.findStr(doc, "/html/body/table/tr[1]/td/img[1]/@alt")); - assertEquals("60", - support.findStr(doc, "/html/body/table/tr[1]/td/img[1]/@width")); + assertEquals("60", support.findStr(doc, + "/html/body/table/tr[1]/td/img[1]/@width")); // green bar assertEquals("jacoco-resources/greenbar.gif", support.findStr(doc, "/html/body/table/tr[1]/td/img[2]/@src")); assertEquals("5", support.findStr(doc, "/html/body/table/tr[1]/td/img[2]/@alt")); - assertEquals("20", - support.findStr(doc, "/html/body/table/tr[1]/td/img[2]/@width")); + assertEquals("20", support.findStr(doc, + "/html/body/table/tr[1]/td/img[2]/@width")); } @Test @@ -122,8 +119,7 @@ public class BarColumnTest { final ITableItem i1 = createItem(20, 0); column.init(Arrays.asList(i1), createNode(20, 0)); column.item(td, i1, resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("1", support.findStr(doc, "count(/html/body/table/tr[1]/td/img)")); @@ -133,8 +129,8 @@ public class BarColumnTest { support.findStr(doc, "/html/body/table/tr[1]/td/img[1]/@src")); assertEquals("20", support.findStr(doc, "/html/body/table/tr[1]/td/img[1]/@alt")); - assertEquals("120", - support.findStr(doc, "/html/body/table/tr[1]/td/img[1]/@width")); + assertEquals("120", support.findStr(doc, + "/html/body/table/tr[1]/td/img[1]/@width")); } @Test @@ -142,8 +138,7 @@ public class BarColumnTest { final ITableItem i1 = createItem(00, 20); column.init(Arrays.asList(i1), createNode(00, 20)); column.item(td, i1, resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("1", support.findStr(doc, "count(/html/body/table/tr[1]/td/img)")); @@ -153,8 +148,8 @@ public class BarColumnTest { support.findStr(doc, "/html/body/table/tr[1]/td/img[1]/@src")); assertEquals("20", support.findStr(doc, "/html/body/table/tr[1]/td/img[1]/@alt")); - assertEquals("120", - support.findStr(doc, "/html/body/table/tr[1]/td/img[1]/@width")); + assertEquals("120", support.findStr(doc, + "/html/body/table/tr[1]/td/img[1]/@width")); } @Test @@ -162,8 +157,7 @@ public class BarColumnTest { final ITableItem i1 = createItem(00, 00); column.init(Arrays.asList(i1), createNode(00, 00)); column.item(td, i1, resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("0", support.findStr(doc, "count(/html/body/table/tr[1]/td/img)")); @@ -175,8 +169,7 @@ public class BarColumnTest { final ITableItem i1 = createItem(0, 123456); col.init(Arrays.asList(i1), createNode(00, 20)); col.item(td, i1, resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("123\u00a0456", support.findStr(doc, "/html/body/table/tr[1]/td/img[1]/@alt")); @@ -190,7 +183,7 @@ public class BarColumnTest { assertTrue(c.compare(i1, i2) < 0); assertTrue(c.compare(i2, i1) > 0); assertEquals(0, c.compare(i1, i1)); - doc.close(); + html.close(); } @Test @@ -201,7 +194,7 @@ public class BarColumnTest { assertTrue(c.compare(i1, i2) < 0); assertTrue(c.compare(i2, i1) > 0); assertEquals(0, c.compare(i1, i1)); - doc.close(); + html.close(); } private ITableItem createItem(final int missed, final int covered) { @@ -233,4 +226,9 @@ public class BarColumnTest { }; } + private Document parseDoc() throws Exception { + html.close(); + return support.parse(output.getFile("Test.html")); + } + } diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/CounterColumnTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/CounterColumnTest.java index 7c68e118..8311128f 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/CounterColumnTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/CounterColumnTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -28,7 +28,6 @@ import org.jacoco.core.analysis.ICoverageNode.ElementType; import org.jacoco.core.internal.analysis.CounterImpl; import org.jacoco.report.MemoryMultiReportOutput; import org.jacoco.report.internal.ReportOutputFolder; -import org.jacoco.report.internal.html.HTMLDocument; import org.jacoco.report.internal.html.HTMLElement; import org.jacoco.report.internal.html.HTMLSupport; import org.jacoco.report.internal.html.resources.Resources; @@ -48,7 +47,7 @@ public class CounterColumnTest { private Resources resources; - private HTMLDocument doc; + private HTMLElement html; private HTMLElement td; @@ -61,9 +60,9 @@ public class CounterColumnTest { output = new MemoryMultiReportOutput(); root = new ReportOutputFolder(output); resources = new Resources(root); - doc = new HTMLDocument(root.createFile("Test.html"), "UTF-8"); - doc.head().title(); - td = doc.body().table("somestyle").tr().td(); + html = new HTMLElement(root.createFile("Test.html"), "UTF-8"); + html.head().title(); + td = html.body().table("somestyle").tr().td(); support = new HTMLSupport(); locale = Locale.ENGLISH; } @@ -80,7 +79,7 @@ public class CounterColumnTest { locale); final ITableItem item = createItem(0, 3); assertTrue(column.init(Arrays.asList(item), item.getNode())); - doc.close(); + html.close(); } @Test @@ -89,7 +88,7 @@ public class CounterColumnTest { locale); final ITableItem item = createItem(0, 0); assertFalse(column.init(Arrays.asList(item), createNode(1, 0))); - doc.close(); + html.close(); } @Test @@ -99,8 +98,7 @@ public class CounterColumnTest { final ITableItem item = createItem(100, 50); column.init(Collections.singletonList(item), item.getNode()); column.item(td, item, resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("150", support.findStr(doc, "/html/body/table/tr/td[1]/text()")); } @@ -112,8 +110,7 @@ public class CounterColumnTest { final ITableItem item = createItem(100, 50); column.init(Collections.singletonList(item), item.getNode()); column.item(td, item, resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("100", support.findStr(doc, "/html/body/table/tr/td[1]/text()")); } @@ -125,8 +122,7 @@ public class CounterColumnTest { final ITableItem item = createItem(100, 50); column.init(Collections.singletonList(item), item.getNode()); column.item(td, item, resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("50", support.findStr(doc, "/html/body/table/tr/td[1]/text()")); } @@ -138,8 +134,7 @@ public class CounterColumnTest { final ITableItem item = createItem(1000, 0); column.init(Collections.singletonList(item), item.getNode()); column.item(td, item, resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("1.000", support.findStr(doc, "/html/body/table/tr/td[1]/text()")); } @@ -151,8 +146,7 @@ public class CounterColumnTest { final ITableItem item = createItem(20, 60); column.init(Collections.singletonList(item), item.getNode()); column.footer(td, item.getNode(), resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("80", support.findStr(doc, "/html/body/table/tr/td[1]/text()")); } @@ -167,7 +161,7 @@ public class CounterColumnTest { assertEquals(0, c.compare(i1, i1)); assertTrue(c.compare(i1, i2) > 0); assertTrue(c.compare(i2, i1) < 0); - doc.close(); + html.close(); } @Test @@ -180,7 +174,7 @@ public class CounterColumnTest { assertEquals(0, c.compare(i1, i1)); assertTrue(c.compare(i1, i2) > 0); assertTrue(c.compare(i2, i1) < 0); - doc.close(); + html.close(); } @Test @@ -193,7 +187,7 @@ public class CounterColumnTest { assertEquals(0, c.compare(i1, i1)); assertTrue(c.compare(i1, i2) > 0); assertTrue(c.compare(i2, i1) < 0); - doc.close(); + html.close(); } private ITableItem createItem(final int missed, final int covered) { @@ -224,4 +218,10 @@ public class CounterColumnTest { } }; } + + private Document parseDoc() throws Exception { + html.close(); + return support.parse(output.getFile("Test.html")); + } + } diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/LabelColumnTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/LabelColumnTest.java index adf4e243..f13ef470 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/LabelColumnTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/LabelColumnTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -21,7 +21,6 @@ import org.jacoco.core.analysis.ICoverageNode; import org.jacoco.core.analysis.ICoverageNode.ElementType; import org.jacoco.report.MemoryMultiReportOutput; import org.jacoco.report.internal.ReportOutputFolder; -import org.jacoco.report.internal.html.HTMLDocument; import org.jacoco.report.internal.html.HTMLElement; import org.jacoco.report.internal.html.HTMLSupport; import org.jacoco.report.internal.html.resources.Resources; @@ -41,7 +40,7 @@ public class LabelColumnTest { private Resources resources; - private HTMLDocument doc; + private HTMLElement html; private HTMLElement td; @@ -54,9 +53,9 @@ public class LabelColumnTest { output = new MemoryMultiReportOutput(); root = new ReportOutputFolder(output); resources = new Resources(root); - doc = new HTMLDocument(root.createFile("Test.html"), "UTF-8"); - doc.head().title(); - td = doc.body().table("somestyle").tr().td(); + html = new HTMLElement(root.createFile("Test.html"), "UTF-8"); + html.head().title(); + td = html.body().table("somestyle").tr().td(); support = new HTMLSupport(); column = new LabelColumn(); } @@ -70,15 +69,14 @@ public class LabelColumnTest { @Test public void testInit() throws Exception { assertTrue(column.init(null, null)); - doc.close(); + html.close(); } @Test public void testFooter() throws Exception { column.footer(td, new CoverageNodeImpl(ElementType.GROUP, "Foo"), resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("Total", support.findStr(doc, "/html/body/table/tr/td/text()")); } @@ -86,8 +84,7 @@ public class LabelColumnTest { @Test public void testItemWithoutLink() throws Exception { column.item(td, createItem("Abc", null), resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("Abc", support.findStr(doc, "/html/body/table/tr/td/span/text()")); assertEquals("el_group", @@ -97,8 +94,7 @@ public class LabelColumnTest { @Test public void testItemWithLink() throws Exception { column.item(td, createItem("Def", "def.html"), resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("Def", support.findStr(doc, "/html/body/table/tr/td/a/text()")); assertEquals("def.html", @@ -112,7 +108,7 @@ public class LabelColumnTest { final ITableItem i1 = createItem("abcdef", null); final ITableItem i2 = createItem("aBcDeF", null); assertEquals(0, column.getComparator().compare(i1, i2)); - doc.close(); + html.close(); } @Test @@ -121,11 +117,12 @@ public class LabelColumnTest { final ITableItem i2 = createItem("world", null); assertTrue(column.getComparator().compare(i1, i2) < 0); assertTrue(column.getComparator().compare(i2, i1) > 0); - doc.close(); + html.close(); } private ITableItem createItem(final String name, final String link) { - final ICoverageNode node = new CoverageNodeImpl(ElementType.GROUP, name); + final ICoverageNode node = new CoverageNodeImpl(ElementType.GROUP, + name); return new ITableItem() { public String getLinkLabel() { return name; @@ -145,4 +142,8 @@ public class LabelColumnTest { }; } + private Document parseDoc() throws Exception { + html.close(); + return support.parse(output.getFile("Test.html")); + } } diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/PercentageColumnTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/PercentageColumnTest.java index bff3d970..3028e005 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/PercentageColumnTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/PercentageColumnTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -25,7 +25,6 @@ import org.jacoco.core.analysis.ICoverageNode.ElementType; import org.jacoco.core.internal.analysis.CounterImpl; import org.jacoco.report.MemoryMultiReportOutput; import org.jacoco.report.internal.ReportOutputFolder; -import org.jacoco.report.internal.html.HTMLDocument; import org.jacoco.report.internal.html.HTMLElement; import org.jacoco.report.internal.html.HTMLSupport; import org.jacoco.report.internal.html.resources.Resources; @@ -45,7 +44,7 @@ public class PercentageColumnTest { private Resources resources; - private HTMLDocument doc; + private HTMLElement html; private HTMLElement td; @@ -58,9 +57,9 @@ public class PercentageColumnTest { output = new MemoryMultiReportOutput(); root = new ReportOutputFolder(output); resources = new Resources(root); - doc = new HTMLDocument(root.createFile("Test.html"), "UTF-8"); - doc.head().title(); - td = doc.body().table("somestyle").tr().td(); + html = new HTMLElement(root.createFile("Test.html"), "UTF-8"); + html.head().title(); + td = html.body().table("somestyle").tr().td(); support = new HTMLSupport(); column = new PercentageColumn(CounterEntity.LINE, Locale.ENGLISH); } @@ -74,15 +73,14 @@ public class PercentageColumnTest { @Test public void testInit() throws Exception { assertTrue(column.init(null, null)); - doc.close(); + html.close(); } @Test public void testItem1() throws Exception { final ITableItem item = createItem(100, 50); column.item(td, item, resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("33%", support.findStr(doc, "/html/body/table/tr/td[1]/text()")); } @@ -91,8 +89,7 @@ public class PercentageColumnTest { public void testItem2() throws Exception { final ITableItem item = createItem(0, 0); column.item(td, item, resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("n/a", support.findStr(doc, "/html/body/table/tr/td[1]/text()")); } @@ -101,8 +98,7 @@ public class PercentageColumnTest { public void testRounding() throws Exception { final ITableItem item = createItem(1, 199); column.item(td, item, resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("99%", support.findStr(doc, "/html/body/table/tr/td[1]/text()")); } @@ -113,8 +109,7 @@ public class PercentageColumnTest { Locale.FRENCH); final ITableItem item = createItem(0, 1000); column.item(td, item, resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); // After integration of JEP 252 into JDK9, CLDR locale data is used by // default, which results in usage of non-breaking space below, while // the legacy locale data uses regular space: @@ -126,8 +121,7 @@ public class PercentageColumnTest { public void testFooter1() throws Exception { final ITableItem item = createItem(20, 60); column.footer(td, item.getNode(), resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("75%", support.findStr(doc, "/html/body/table/tr")); } @@ -135,8 +129,7 @@ public class PercentageColumnTest { public void testFooter2() throws Exception { final ITableItem item = createItem(0, 0); column.footer(td, item.getNode(), resources, root); - doc.close(); - final Document doc = support.parse(output.getFile("Test.html")); + final Document doc = parseDoc(); assertEquals("n/a", support.findStr(doc, "/html/body/table/tr")); } @@ -148,7 +141,7 @@ public class PercentageColumnTest { assertTrue(c.compare(i1, i2) < 0); assertTrue(c.compare(i2, i1) > 0); assertEquals(0, c.compare(i1, i1)); - doc.close(); + html.close(); } private ITableItem createItem(final int missed, final int covered) { @@ -179,4 +172,9 @@ public class PercentageColumnTest { } }; } + + private Document parseDoc() throws Exception { + html.close(); + return support.parse(output.getFile("Test.html")); + } } diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/SortIndexTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/SortIndexTest.java index b3e18954..7ca47226 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/SortIndexTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/SortIndexTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/TableTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/TableTest.java index 4e6e2ed6..eed6923d 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/TableTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/TableTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -27,7 +27,6 @@ import org.jacoco.core.analysis.ICoverageNode.ElementType; import org.jacoco.core.internal.analysis.CounterImpl; import org.jacoco.report.MemoryMultiReportOutput; import org.jacoco.report.internal.ReportOutputFolder; -import org.jacoco.report.internal.html.HTMLDocument; import org.jacoco.report.internal.html.HTMLElement; import org.jacoco.report.internal.html.HTMLSupport; import org.jacoco.report.internal.html.resources.Resources; @@ -47,7 +46,7 @@ public class TableTest { private Resources resources; - private HTMLDocument doc; + private HTMLElement html; private HTMLElement body; @@ -58,9 +57,9 @@ public class TableTest { output = new MemoryMultiReportOutput(); root = new ReportOutputFolder(output); resources = new Resources(root); - doc = new HTMLDocument(root.createFile("Test.html"), "UTF-8"); - doc.head().title(); - body = doc.body(); + html = new HTMLElement(root.createFile("Test.html"), "UTF-8"); + html.head().title(); + body = html.body(); table = new Table(); } @@ -106,7 +105,7 @@ public class TableTest { createItem("B", 2), createItem("C", 3)); table.add("Header", null, recorder, false); table.render(body, items, createTotal("Sum", 6), resources, root); - doc.close(); + html.close(); assertEquals("init-footer-itemA-itemB-itemC-", recorder.toString()); } @@ -136,31 +135,35 @@ public class TableTest { final List<ITableItem> items = Arrays.asList(createItem("A", 1)); table.add("Header", null, column, false); table.render(body, items, createTotal("Sum", 1), resources, root); - doc.close(); + html.close(); } @Test(expected = IllegalStateException.class) public void testTwoDefaultSorts() throws IOException { - doc.close(); - table.add("Header1", null, new StubRenderer( - CounterComparator.TOTALITEMS.on(CounterEntity.CLASS)), true); - table.add("Header2", null, new StubRenderer( - CounterComparator.TOTALITEMS.on(CounterEntity.CLASS)), true); + html.close(); + table.add("Header1", null, + new StubRenderer( + CounterComparator.TOTALITEMS.on(CounterEntity.CLASS)), + true); + table.add("Header2", null, + new StubRenderer( + CounterComparator.TOTALITEMS.on(CounterEntity.CLASS)), + true); } @Test public void testSortIds() throws Exception { final List<ITableItem> items = Arrays.asList(createItem("C", 3), createItem("E", 4), createItem("A", 1), createItem("D", 2)); - table.add("Forward", null, new StubRenderer( - CounterComparator.TOTALITEMS.on(CounterEntity.CLASS)), false); - table.add( - "Reverse", - null, - new StubRenderer(CounterComparator.TOTALITEMS.reverse().on( - CounterEntity.CLASS)), false); + table.add("Forward", null, + new StubRenderer( + CounterComparator.TOTALITEMS.on(CounterEntity.CLASS)), + false); + table.add("Reverse", null, new StubRenderer( + CounterComparator.TOTALITEMS.reverse().on(CounterEntity.CLASS)), + false); table.render(body, items, createTotal("Sum", 6), resources, root); - doc.close(); + html.close(); final HTMLSupport support = new HTMLSupport(); final Document doc = support.parse(output.getFile("Test.html")); @@ -199,10 +202,12 @@ public class TableTest { final List<ITableItem> items = Arrays.asList(createItem("C", 3), createItem("E", 5), createItem("A", 1), createItem("D", 4), createItem("B", 2)); - table.add("Forward", null, new StubRenderer( - CounterComparator.TOTALITEMS.on(CounterEntity.CLASS)), true); + table.add("Forward", null, + new StubRenderer( + CounterComparator.TOTALITEMS.on(CounterEntity.CLASS)), + true); table.render(body, items, createTotal("Sum", 1), resources, root); - doc.close(); + html.close(); final HTMLSupport support = new HTMLSupport(); final Document doc = support.parse(output.getFile("Test.html")); @@ -222,7 +227,8 @@ public class TableTest { } private ITableItem createItem(final String name, final int count) { - final ICoverageNode node = new CoverageNodeImpl(ElementType.GROUP, name) { + final ICoverageNode node = new CoverageNodeImpl(ElementType.GROUP, + name) { { this.classCounter = CounterImpl.getInstance(count, 0); } diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/xml/LocalEntityResolver.java b/org.jacoco.report.test/src/org/jacoco/report/internal/xml/LocalEntityResolver.java index c67fdb1d..ae6fc91e 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/xml/LocalEntityResolver.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/xml/LocalEntityResolver.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/xml/XMLDocumentTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/xml/XMLDocumentTest.java deleted file mode 100644 index cd3d985a..00000000 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/xml/XMLDocumentTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.report.internal.xml; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.StringWriter; - -import org.junit.Test; - -/** - * Unit tests for {@link XMLDocument}. - */ -public class XMLDocumentTest { - - @Test - public void testNoDoctype() throws IOException { - StringWriter writer = new StringWriter(); - new XMLDocument("test", null, null, "UTF-8", false, writer).close(); - assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><test/>", - writer.toString()); - } - - @Test - public void testNoDoctypeStandalone() throws IOException { - StringWriter writer = new StringWriter(); - new XMLDocument("test", null, null, "UTF-8", true, writer).close(); - assertEquals( - "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" - + "<test/>", writer.toString()); - } - - @Test - public void testDoctype() throws IOException { - StringWriter writer = new StringWriter(); - new XMLDocument("test", "sample", "sample.dtd", "UTF-8", false, writer) - .close(); - assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - + "<!DOCTYPE test PUBLIC \"sample\" \"sample.dtd\"><test/>", - writer.toString()); - } - - @Test - public void testDoctypeStandalone() throws IOException { - StringWriter writer = new StringWriter(); - new XMLDocument("test", "sample", "sample.dtd", "UTF-8", true, writer) - .close(); - assertEquals( - "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" - + "<!DOCTYPE test PUBLIC \"sample\" \"sample.dtd\">" - + "<test/>", writer.toString()); - } - - @Test - public void testStream() throws IOException { - ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - new XMLDocument("test", null, null, "UTF-8", false, buffer).text( - "\u00CD\u307e").close(); - assertEquals( - "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test>\u00CD\u307e</test>", - buffer.toString("UTF-8")); - } - - @Test - public void testClose() throws IOException { - class CloseVerifier extends StringWriter { - - boolean closed = false; - - @Override - public void close() throws IOException { - closed = true; - super.close(); - } - } - CloseVerifier verifier = new CloseVerifier(); - new XMLDocument("test", null, null, "UTF-8", false, verifier).close(); - assertTrue(verifier.closed); - } - -} diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/xml/XMLElementTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/xml/XMLElementTest.java index 0ba4644e..5ed161c2 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/xml/XMLElementTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/xml/XMLElementTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -13,8 +13,8 @@ package org.jacoco.report.internal.xml; import static org.junit.Assert.assertEquals; +import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.StringWriter; import org.junit.Before; import org.junit.Test; @@ -24,123 +24,156 @@ import org.junit.Test; */ public class XMLElementTest { - private StringWriter buffer; + private static final String DECL = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; + + private ByteArrayOutputStream buffer; private XMLElement root; @Before - public void setUp() throws IOException { - buffer = new StringWriter(); - root = new XMLElement(buffer, "root"); - root.beginOpenTag(); + public void setup() throws IOException { + buffer = new ByteArrayOutputStream(); + root = new XMLElement("root", null, null, false, "UTF-8", buffer); + } + + @Test + public void init_should_write_doctype_when_given() throws IOException { + root = new XMLElement("root", "-//JACOCO//TEST", "test.dtd", false, + "UTF-8", buffer); + assertEquals(DECL + + "<!DOCTYPE root PUBLIC \"-//JACOCO//TEST\" \"test.dtd\"><root/>", + actual()); + } + + @Test + public void init_should_write_standalone_when_given() throws IOException { + root = new XMLElement("root", null, null, true, "UTF-8", buffer); + assertEquals( + "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><root/>", + actual()); } @Test - public void testEmptyNode() throws IOException { + public void close_should_emit_empty_element_when_no_children_exist() + throws IOException { + assertContent("<root/>"); + } + + @Test + public void close_should_be_allowed_multiple_times() throws IOException { root.close(); - // Second close has no effect: root.close(); - assertEquals("<root/>", buffer.toString()); + assertContent("<root/>"); } @Test(expected = IOException.class) - public void testAddAttributeToClosedNode() throws IOException { + public void attr_should_throw_exception_when_closed() throws IOException { root.close(); root.attr("attr", "value"); } @Test(expected = IOException.class) - public void testAddChildToClosedNode() throws IOException { + public void element_should_throw_exception_when_closed() + throws IOException { root.close(); root.element("child"); } @Test(expected = IOException.class) - public void testAddTextToClosedNode() throws IOException { + public void text_should_throw_exception_when_closed() throws IOException { root.close(); root.text("text"); } @Test - public void testNestedElement() throws IOException { + public void element_should_emit_nested_element() throws IOException { root.element("world"); - root.close(); - assertEquals("<root><world/></root>", buffer.toString()); + assertContent("<root><world/></root>"); } @Test - public void test2NestedElements() throws IOException { + public void element_should_allow_multiple_nested_elements() + throws IOException { root.element("world"); root.element("universe"); - root.close(); - assertEquals("<root><world/><universe/></root>", buffer.toString()); + assertContent("<root><world/><universe/></root>"); } @Test - public void testText() throws IOException { + public void text_should_emit_text() throws IOException { root.text("world"); - root.close(); - assertEquals("<root>world</root>", buffer.toString()); + assertContent("<root>world</root>"); } @Test - public void testMixedContent() throws IOException { + public void text_should_allow_mixing_with_elements() throws IOException { root.element("tag1"); root.text("world"); root.element("tag2"); - root.close(); - assertEquals("<root><tag1/>world<tag2/></root>", buffer.toString()); + assertContent("<root><tag1/>world<tag2/></root>"); } @Test - public void testQuotedText() throws IOException { + public void test_should_be_quoted() throws IOException { root.text("<black&white\">"); - root.close(); - assertEquals("<root><black&white"></root>", - buffer.toString()); + assertContent("<root><black&white"></root>"); } @Test - public void testNullAttributes() throws IOException { + public void attr_should_ignore_call_when_value_is_null() + throws IOException { root.attr("id", null); - root.close(); - assertEquals("<root/>", buffer.toString()); + assertContent("<root/>"); } @Test - public void testStringAttributes() throws IOException { - root.attr("id", "12345").attr("quote", "<\">"); - root.close(); - assertEquals("<root id=\"12345\" quote=\"<">\"/>", - buffer.toString()); + public void attr_should_emit_string_value() throws IOException { + root.attr("id", "12345"); + assertContent("<root id=\"12345\"/>"); } @Test - public void testIntAttributes() throws IOException { - root.attr("missed", 0).attr("total", 123); - root.close(); - assertEquals("<root missed=\"0\" total=\"123\"/>", buffer.toString()); + public void attr_should_quote_string_value() throws IOException { + root.attr("quote", "<\">"); + assertContent("<root quote=\"<">\"/>"); } @Test - public void testLongAttributes() throws IOException { - root.attr("min", Long.MIN_VALUE).attr("max", Long.MAX_VALUE); - root.close(); - assertEquals( - "<root min=\"-9223372036854775808\" max=\"9223372036854775807\"/>", - buffer.toString()); + public void attr_should_emit_int_value() throws IOException { + root.attr("missed", 0); + root.attr("total", 123); + assertContent("<root missed=\"0\" total=\"123\"/>"); + } + + @Test + public void attr_should_emit_long_value() throws IOException { + root.attr("min", Long.MIN_VALUE); + root.attr("max", Long.MAX_VALUE); + assertContent( + "<root min=\"-9223372036854775808\" max=\"9223372036854775807\"/>"); } @Test(expected = IOException.class) - public void testInvalidAttributeOutput1() throws IOException { + public void attr_should_throw_exception_when_text_was_added() + throws IOException { root.text("text"); root.attr("id", "12345"); } @Test(expected = IOException.class) - public void testInvalidAttributeOutput2() throws IOException { + public void attr_should_throw_exception_when_child_was_added() + throws IOException { root.element("child"); root.attr("id", "12345"); } + private void assertContent(String expected) throws IOException { + assertEquals(DECL + expected, actual()); + } + + private String actual() throws IOException { + root.close(); + return buffer.toString("UTF-8"); + } + } diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/xml/XMLGroupVisitorTest.java b/org.jacoco.report.test/src/org/jacoco/report/internal/xml/XMLGroupVisitorTest.java index fb8771ea..92f5c28f 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/xml/XMLGroupVisitorTest.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/xml/XMLGroupVisitorTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -13,26 +13,22 @@ package org.jacoco.report.internal.xml; import static org.junit.Assert.assertEquals; -import java.io.IOException; -import java.io.StringWriter; - -import javax.xml.parsers.ParserConfigurationException; +import java.io.ByteArrayOutputStream; import org.jacoco.report.ReportStructureTestDriver; import org.jacoco.report.xml.XMLFormatter; import org.junit.Before; import org.junit.Test; import org.w3c.dom.Document; -import org.xml.sax.SAXException; /** * Unit tests for {@link XMLGroupVisitor}. */ public class XMLGroupVisitorTest { - private XMLElement root; + private ReportElement root; - private StringWriter buffer; + private ByteArrayOutputStream buffer; private XMLSupport support; @@ -42,11 +38,9 @@ public class XMLGroupVisitorTest { @Before public void setup() throws Exception { - buffer = new StringWriter(); + buffer = new ByteArrayOutputStream(); support = new XMLSupport(XMLFormatter.class); - root = new XMLDocument("report", "-//JACOCO//DTD Report 1.0//EN", - "report.dtd", "UTF-8", true, buffer); - root.attr("name", "Report"); + root = new ReportElement("Report", buffer, "UTF-8"); handler = new XMLGroupVisitor(root, null); driver = new ReportStructureTestDriver(); } @@ -54,16 +48,14 @@ public class XMLGroupVisitorTest { @Test public void testVisitBundle() throws Exception { driver.sendBundle(handler); - root.close(); - final Document doc = getDocument(); + final Document doc = parseDoc(); assertEquals("bundle", support.findStr(doc, "//report/group/@name")); } @Test public void testVisitGroup() throws Exception { driver.sendGroup(handler); - root.close(); - final Document doc = getDocument(); + final Document doc = parseDoc(); assertEquals("group", support.findStr(doc, "//report/group/@name")); } @@ -71,15 +63,14 @@ public class XMLGroupVisitorTest { public void testVisitEnd() throws Exception { driver.sendBundle(handler); handler.visitEnd(); - root.close(); - final Document doc = getDocument(); + final Document doc = parseDoc(); assertEquals("2", support.findStr(doc, "//report/counter[@type='BRANCH']/@covered")); } - private Document getDocument() throws SAXException, IOException, - ParserConfigurationException { - return support.parse(buffer.toString()); + private Document parseDoc() throws Exception { + root.close(); + return support.parse(buffer); } } diff --git a/org.jacoco.report.test/src/org/jacoco/report/internal/xml/XMLSupport.java b/org.jacoco.report.test/src/org/jacoco/report/internal/xml/XMLSupport.java index c95e7b4b..fbf4af41 100644 --- a/org.jacoco.report.test/src/org/jacoco/report/internal/xml/XMLSupport.java +++ b/org.jacoco.report.test/src/org/jacoco/report/internal/xml/XMLSupport.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -14,8 +14,8 @@ package org.jacoco.report.internal.xml; import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -71,13 +71,13 @@ public class XMLSupport { return xpath; } - public Document parse(String document) throws SAXException, IOException, - ParserConfigurationException { - return builder.parse(new InputSource(new StringReader(document))); + public Document parse(ByteArrayOutputStream buffer) + throws SAXException, IOException, ParserConfigurationException { + return parse(buffer.toByteArray()); } - public Document parse(byte[] document) throws SAXException, IOException, - ParserConfigurationException { + public Document parse(byte[] document) + throws SAXException, IOException, ParserConfigurationException { return builder .parse(new InputSource(new ByteArrayInputStream(document))); } |