Home | History | Annotate | Download | only in table
      1 /*******************************************************************************
      2  * Copyright (c) 2009, 2017 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 import java.util.Comparator;
     19 import java.util.Locale;
     20 
     21 import org.jacoco.core.analysis.CoverageNodeImpl;
     22 import org.jacoco.core.analysis.ICoverageNode;
     23 import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
     24 import org.jacoco.core.analysis.ICoverageNode.ElementType;
     25 import org.jacoco.core.internal.analysis.CounterImpl;
     26 import org.jacoco.report.MemoryMultiReportOutput;
     27 import org.jacoco.report.internal.ReportOutputFolder;
     28 import org.jacoco.report.internal.html.HTMLDocument;
     29 import org.jacoco.report.internal.html.HTMLElement;
     30 import org.jacoco.report.internal.html.HTMLSupport;
     31 import org.jacoco.report.internal.html.resources.Resources;
     32 import org.junit.After;
     33 import org.junit.Before;
     34 import org.junit.Test;
     35 import org.w3c.dom.Document;
     36 
     37 /**
     38  * Unit tests for {@link PercentageColumn}.
     39  */
     40 public class PercentageColumnTest {
     41 
     42 	private MemoryMultiReportOutput output;
     43 
     44 	private ReportOutputFolder root;
     45 
     46 	private Resources resources;
     47 
     48 	private HTMLDocument doc;
     49 
     50 	private HTMLElement td;
     51 
     52 	private HTMLSupport support;
     53 
     54 	private IColumnRenderer column;
     55 
     56 	@Before
     57 	public void setup() throws Exception {
     58 		output = new MemoryMultiReportOutput();
     59 		root = new ReportOutputFolder(output);
     60 		resources = new Resources(root);
     61 		doc = new HTMLDocument(root.createFile("Test.html"), "UTF-8");
     62 		doc.head().title();
     63 		td = doc.body().table("somestyle").tr().td();
     64 		support = new HTMLSupport();
     65 		column = new PercentageColumn(CounterEntity.LINE, Locale.ENGLISH);
     66 	}
     67 
     68 	@After
     69 	public void teardown() throws IOException {
     70 		output.close();
     71 		output.assertAllClosed();
     72 	}
     73 
     74 	@Test
     75 	public void testInit() throws Exception {
     76 		assertTrue(column.init(null, null));
     77 		doc.close();
     78 	}
     79 
     80 	@Test
     81 	public void testItem1() throws Exception {
     82 		final ITableItem item = createItem(100, 50);
     83 		column.item(td, item, resources, root);
     84 		doc.close();
     85 		final Document doc = support.parse(output.getFile("Test.html"));
     86 		assertEquals("33%",
     87 				support.findStr(doc, "/html/body/table/tr/td[1]/text()"));
     88 	}
     89 
     90 	@Test
     91 	public void testItem2() throws Exception {
     92 		final ITableItem item = createItem(0, 0);
     93 		column.item(td, item, resources, root);
     94 		doc.close();
     95 		final Document doc = support.parse(output.getFile("Test.html"));
     96 		assertEquals("n/a",
     97 				support.findStr(doc, "/html/body/table/tr/td[1]/text()"));
     98 	}
     99 
    100 	@Test
    101 	public void testRounding() throws Exception {
    102 		final ITableItem item = createItem(1, 199);
    103 		column.item(td, item, resources, root);
    104 		doc.close();
    105 		final Document doc = support.parse(output.getFile("Test.html"));
    106 		assertEquals("99%",
    107 				support.findStr(doc, "/html/body/table/tr/td[1]/text()"));
    108 	}
    109 
    110 	@Test
    111 	public void testLocale() throws Exception {
    112 		IColumnRenderer column = new PercentageColumn(CounterEntity.LINE,
    113 				Locale.FRENCH);
    114 		final ITableItem item = createItem(0, 1000);
    115 		column.item(td, item, resources, root);
    116 		doc.close();
    117 		final Document doc = support.parse(output.getFile("Test.html"));
    118 		assertEquals("100 %",
    119 				support.findStr(doc, "/html/body/table/tr/td[1]/text()"));
    120 	}
    121 
    122 	@Test
    123 	public void testFooter1() throws Exception {
    124 		final ITableItem item = createItem(20, 60);
    125 		column.footer(td, item.getNode(), resources, root);
    126 		doc.close();
    127 		final Document doc = support.parse(output.getFile("Test.html"));
    128 		assertEquals("75%", support.findStr(doc, "/html/body/table/tr"));
    129 	}
    130 
    131 	@Test
    132 	public void testFooter2() throws Exception {
    133 		final ITableItem item = createItem(0, 0);
    134 		column.footer(td, item.getNode(), resources, root);
    135 		doc.close();
    136 		final Document doc = support.parse(output.getFile("Test.html"));
    137 		assertEquals("n/a", support.findStr(doc, "/html/body/table/tr"));
    138 	}
    139 
    140 	@Test
    141 	public void testComparator() throws Exception {
    142 		final Comparator<ITableItem> c = column.getComparator();
    143 		final ITableItem i1 = createItem(50, 50);
    144 		final ITableItem i2 = createItem(800, 200);
    145 		assertTrue(c.compare(i1, i2) < 0);
    146 		assertTrue(c.compare(i2, i1) > 0);
    147 		assertEquals(0, c.compare(i1, i1));
    148 		doc.close();
    149 	}
    150 
    151 	private ITableItem createItem(final int missed, final int covered) {
    152 		final ICoverageNode node = createNode(missed, covered);
    153 		return new ITableItem() {
    154 			public String getLinkLabel() {
    155 				return "Foo";
    156 			}
    157 
    158 			public String getLink(ReportOutputFolder base) {
    159 				return null;
    160 			}
    161 
    162 			public String getLinkStyle() {
    163 				return Resources.getElementStyle(node.getElementType());
    164 			}
    165 
    166 			public ICoverageNode getNode() {
    167 				return node;
    168 			}
    169 		};
    170 	}
    171 
    172 	private CoverageNodeImpl createNode(final int missed, final int covered) {
    173 		return new CoverageNodeImpl(ElementType.GROUP, "Foo") {
    174 			{
    175 				this.lineCounter = CounterImpl.getInstance(missed, covered);
    176 			}
    177 		};
    178 	}
    179 }
    180