Home | History | Annotate | Download | only in table
      1 /*******************************************************************************
      2  * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors
      3  * All rights reserved. This program and the accompanying materials
      4  * are made available under the terms of the Eclipse Public License v1.0
      5  * which accompanies this distribution, and is available at
      6  * http://www.eclipse.org/legal/epl-v10.html
      7  *
      8  * Contributors:
      9  *    Marc R. Hoffmann - initial API and implementation
     10  *
     11  *******************************************************************************/
     12 package org.jacoco.report.internal.html.table;
     13 
     14 import static org.junit.Assert.assertEquals;
     15 import static org.junit.Assert.assertTrue;
     16 
     17 import java.io.IOException;
     18 
     19 import org.jacoco.core.analysis.CoverageNodeImpl;
     20 import org.jacoco.core.analysis.ICoverageNode;
     21 import org.jacoco.core.analysis.ICoverageNode.ElementType;
     22 import org.jacoco.report.MemoryMultiReportOutput;
     23 import org.jacoco.report.internal.ReportOutputFolder;
     24 import org.jacoco.report.internal.html.HTMLDocument;
     25 import org.jacoco.report.internal.html.HTMLElement;
     26 import org.jacoco.report.internal.html.HTMLSupport;
     27 import org.jacoco.report.internal.html.resources.Resources;
     28 import org.junit.After;
     29 import org.junit.Before;
     30 import org.junit.Test;
     31 import org.w3c.dom.Document;
     32 
     33 /**
     34  * Unit tests for {@link LabelColumn}.
     35  */
     36 public class LabelColumnTest {
     37 
     38 	private MemoryMultiReportOutput output;
     39 
     40 	private ReportOutputFolder root;
     41 
     42 	private Resources resources;
     43 
     44 	private HTMLDocument doc;
     45 
     46 	private HTMLElement td;
     47 
     48 	private HTMLSupport support;
     49 
     50 	private IColumnRenderer column;
     51 
     52 	@Before
     53 	public void setup() throws Exception {
     54 		output = new MemoryMultiReportOutput();
     55 		root = new ReportOutputFolder(output);
     56 		resources = new Resources(root);
     57 		doc = new HTMLDocument(root.createFile("Test.html"), "UTF-8");
     58 		doc.head().title();
     59 		td = doc.body().table("somestyle").tr().td();
     60 		support = new HTMLSupport();
     61 		column = new LabelColumn();
     62 	}
     63 
     64 	@After
     65 	public void teardown() throws IOException {
     66 		output.close();
     67 		output.assertAllClosed();
     68 	}
     69 
     70 	@Test
     71 	public void testInit() throws Exception {
     72 		assertTrue(column.init(null, null));
     73 		doc.close();
     74 	}
     75 
     76 	@Test
     77 	public void testFooter() throws Exception {
     78 		column.footer(td, new CoverageNodeImpl(ElementType.GROUP, "Foo"),
     79 				resources, root);
     80 		doc.close();
     81 		final Document doc = support.parse(output.getFile("Test.html"));
     82 		assertEquals("Total",
     83 				support.findStr(doc, "/html/body/table/tr/td/text()"));
     84 	}
     85 
     86 	@Test
     87 	public void testItemWithoutLink() throws Exception {
     88 		column.item(td, createItem("Abc", null), resources, root);
     89 		doc.close();
     90 		final Document doc = support.parse(output.getFile("Test.html"));
     91 		assertEquals("Abc",
     92 				support.findStr(doc, "/html/body/table/tr/td/span/text()"));
     93 		assertEquals("el_group",
     94 				support.findStr(doc, "/html/body/table/tr/td/span/@class"));
     95 	}
     96 
     97 	@Test
     98 	public void testItemWithLink() throws Exception {
     99 		column.item(td, createItem("Def", "def.html"), resources, root);
    100 		doc.close();
    101 		final Document doc = support.parse(output.getFile("Test.html"));
    102 		assertEquals("Def",
    103 				support.findStr(doc, "/html/body/table/tr/td/a/text()"));
    104 		assertEquals("def.html",
    105 				support.findStr(doc, "/html/body/table/tr/td/a/@href"));
    106 		assertEquals("el_group",
    107 				support.findStr(doc, "/html/body/table/tr/td/a/@class"));
    108 	}
    109 
    110 	@Test
    111 	public void testComparator1() throws Exception {
    112 		final ITableItem i1 = createItem("abcdef", null);
    113 		final ITableItem i2 = createItem("aBcDeF", null);
    114 		assertEquals(0, column.getComparator().compare(i1, i2));
    115 		doc.close();
    116 	}
    117 
    118 	@Test
    119 	public void testComparator2() throws Exception {
    120 		final ITableItem i1 = createItem("hello", null);
    121 		final ITableItem i2 = createItem("world", null);
    122 		assertTrue(column.getComparator().compare(i1, i2) < 0);
    123 		assertTrue(column.getComparator().compare(i2, i1) > 0);
    124 		doc.close();
    125 	}
    126 
    127 	private ITableItem createItem(final String name, final String link) {
    128 		final ICoverageNode node = new CoverageNodeImpl(ElementType.GROUP, name);
    129 		return new ITableItem() {
    130 			public String getLinkLabel() {
    131 				return name;
    132 			}
    133 
    134 			public String getLink(ReportOutputFolder base) {
    135 				return link;
    136 			}
    137 
    138 			public String getLinkStyle() {
    139 				return Resources.getElementStyle(node.getElementType());
    140 			}
    141 
    142 			public ICoverageNode getNode() {
    143 				return node;
    144 			}
    145 		};
    146 	}
    147 
    148 }
    149