Home | History | Annotate | Download | only in page
      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.page;
     13 
     14 import static org.junit.Assert.assertEquals;
     15 
     16 import java.io.StringReader;
     17 import java.io.StringWriter;
     18 import java.util.Locale;
     19 
     20 import org.jacoco.core.analysis.ICoverageNode.ElementType;
     21 import org.jacoco.core.internal.analysis.CounterImpl;
     22 import org.jacoco.core.internal.analysis.SourceNodeImpl;
     23 import org.jacoco.report.internal.html.HTMLDocument;
     24 import org.jacoco.report.internal.html.HTMLElement;
     25 import org.jacoco.report.internal.html.HTMLSupport;
     26 import org.jacoco.report.internal.html.resources.Styles;
     27 import org.junit.Before;
     28 import org.junit.Test;
     29 import org.w3c.dom.Document;
     30 
     31 /**
     32  * Unit tests for {@link SourceHighlighter}.
     33  */
     34 public class SourceHighlighterTest {
     35 
     36 	private HTMLSupport htmlSupport;
     37 
     38 	private StringWriter buffer;
     39 
     40 	private HTMLDocument html;
     41 
     42 	private HTMLElement parent;
     43 
     44 	private SourceHighlighter sourceHighlighter;
     45 
     46 	private SourceNodeImpl source;
     47 
     48 	@Before
     49 	public void setup() throws Exception {
     50 		htmlSupport = new HTMLSupport();
     51 		source = new SourceNodeImpl(ElementType.SOURCEFILE, "Foo.java");
     52 		buffer = new StringWriter();
     53 		html = new HTMLDocument(buffer, "UTF-8");
     54 		html.head().title();
     55 		parent = html.body();
     56 		sourceHighlighter = new SourceHighlighter(Locale.US);
     57 	}
     58 
     59 	@Test
     60 	public void testDefaultTabWidth() throws Exception {
     61 		final String src = "\tA";
     62 		sourceHighlighter.render(parent, source, new StringReader(src));
     63 		html.close();
     64 		final Document doc = htmlSupport.parse(buffer.toString());
     65 
     66 		// Assert that we no longer replace tabs with spaces
     67 		assertEquals("\tA\n", htmlSupport.findStr(doc, "//pre/text()"));
     68 	}
     69 
     70 	@Test
     71 	public void testDefaultLanguage() throws Exception {
     72 		sourceHighlighter.render(parent, source, new StringReader(""));
     73 		html.close();
     74 		final Document doc = htmlSupport.parse(buffer.toString());
     75 		assertEquals("source lang-java linenums",
     76 				htmlSupport.findStr(doc, "//pre/@class"));
     77 	}
     78 
     79 	@Test
     80 	public void testSetLanguage() throws Exception {
     81 		sourceHighlighter.setLanguage("scala");
     82 		sourceHighlighter.render(parent, source, new StringReader(""));
     83 		html.close();
     84 		final Document doc = htmlSupport.parse(buffer.toString());
     85 		assertEquals("source lang-scala linenums",
     86 				htmlSupport.findStr(doc, "//pre/@class"));
     87 	}
     88 
     89 	@Test
     90 	public void testHighlighting() throws Exception {
     91 		final String src = "A\nB\nC\nD";
     92 		source.increment(CounterImpl.COUNTER_1_0, CounterImpl.COUNTER_0_0, 1);
     93 		source.increment(CounterImpl.COUNTER_1_0, CounterImpl.COUNTER_0_0, 2);
     94 		source.increment(CounterImpl.COUNTER_0_1, CounterImpl.COUNTER_0_0, 2);
     95 		source.increment(CounterImpl.COUNTER_0_1, CounterImpl.COUNTER_0_0, 3);
     96 		sourceHighlighter.render(parent, source, new StringReader(src));
     97 		html.close();
     98 		final Document doc = htmlSupport.parse(buffer.toString());
     99 		assertEquals(Styles.NOT_COVERED,
    100 				htmlSupport.findStr(doc, "//pre/span[text() = 'A']/@class"));
    101 		assertEquals(Styles.PARTLY_COVERED,
    102 				htmlSupport.findStr(doc, "//pre/span[text() = 'B']/@class"));
    103 		assertEquals(Styles.FULLY_COVERED,
    104 				htmlSupport.findStr(doc, "//pre/span[text() = 'C']/@class"));
    105 		assertEquals("",
    106 				htmlSupport.findStr(doc, "//pre/span[text() = 'D']/@class"));
    107 	}
    108 
    109 	@Test
    110 	public void testHighlightNone() throws Exception {
    111 		sourceHighlighter.highlight(parent, source.getLine(1), 1);
    112 		html.close();
    113 		final Document doc = htmlSupport.parse(buffer.toString());
    114 		assertEquals("", htmlSupport.findStr(doc, "//pre"));
    115 	}
    116 
    117 	@Test
    118 	public void testHighlightBranchesFC() throws Exception {
    119 		source.increment(CounterImpl.COUNTER_0_1,
    120 				CounterImpl.getInstance(0, 5), 1);
    121 		sourceHighlighter.highlight(parent.pre(null), source.getLine(1), 1);
    122 		html.close();
    123 		final Document doc = htmlSupport.parse(buffer.toString());
    124 		assertEquals("fc bfc", htmlSupport.findStr(doc, "//pre/span/@class"));
    125 		assertEquals("All 5 branches covered.",
    126 				htmlSupport.findStr(doc, "//pre/span/@title"));
    127 	}
    128 
    129 	@Test
    130 	public void testHighlightBranchesPC() throws Exception {
    131 		source.increment(CounterImpl.COUNTER_0_1,
    132 				CounterImpl.getInstance(2, 3), 1);
    133 		sourceHighlighter.highlight(parent.pre(null), source.getLine(1), 1);
    134 		html.close();
    135 		final Document doc = htmlSupport.parse(buffer.toString());
    136 		assertEquals("pc bpc", htmlSupport.findStr(doc, "//pre/span/@class"));
    137 		assertEquals("2 of 5 branches missed.",
    138 				htmlSupport.findStr(doc, "//pre/span/@title"));
    139 	}
    140 
    141 	@Test
    142 	public void testHighlightBranchesNC() throws Exception {
    143 		source.increment(CounterImpl.COUNTER_0_1,
    144 				CounterImpl.getInstance(5, 0), 1);
    145 		sourceHighlighter.highlight(parent.pre(null), source.getLine(1), 1);
    146 		html.close();
    147 		final Document doc = htmlSupport.parse(buffer.toString());
    148 		assertEquals("pc bnc", htmlSupport.findStr(doc, "//pre/span/@class"));
    149 		assertEquals("All 5 branches missed.",
    150 				htmlSupport.findStr(doc, "//pre/span/@title"));
    151 	}
    152 
    153 }
    154