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.html; 13 14 import static org.junit.Assert.assertEquals; 15 import static org.junit.Assert.assertSame; 16 import static org.junit.Assert.assertTrue; 17 18 import java.io.BufferedReader; 19 import java.io.IOException; 20 import java.io.InputStreamReader; 21 import java.util.Locale; 22 23 import org.jacoco.report.ILanguageNames; 24 import org.jacoco.report.MemoryMultiReportOutput; 25 import org.jacoco.report.ReportStructureTestDriver; 26 import org.junit.After; 27 import org.junit.Before; 28 import org.junit.Test; 29 30 /** 31 * Unit tests for {@link HTMLFormatter}. 32 */ 33 public class HTMLFormatterTest { 34 35 private ReportStructureTestDriver driver; 36 37 private HTMLFormatter formatter; 38 39 private MemoryMultiReportOutput output; 40 41 @Before 42 public void setup() { 43 driver = new ReportStructureTestDriver(); 44 formatter = new HTMLFormatter(); 45 output = new MemoryMultiReportOutput(); 46 } 47 48 @After 49 public void teardown() { 50 output.assertAllClosed(); 51 } 52 53 @Test 54 public void testStructureWithNestedGroups() throws IOException { 55 driver.sendNestedGroups(formatter.createVisitor(output)); 56 output.assertFile("index.html"); 57 output.assertFile("group1/index.html"); 58 output.assertFile("group1/group/index.html"); 59 output.assertFile("group1/group/bundle/index.html"); 60 output.assertFile("bundle/index.html"); 61 } 62 63 @Test 64 public void testStructureWithGroup() throws IOException { 65 driver.sendGroup(formatter.createVisitor(output)); 66 output.assertFile("index.html"); 67 output.assertFile("bundle/index.html"); 68 output.assertFile("bundle/org.jacoco.example/index.html"); 69 output.assertFile("bundle/org.jacoco.example/FooClass.html"); 70 } 71 72 @Test 73 public void testStructureWithBundleOnly() throws IOException { 74 driver.sendBundle(formatter.createVisitor(output)); 75 output.assertFile("index.html"); 76 output.assertFile("org.jacoco.example/index.html"); 77 output.assertFile("org.jacoco.example/FooClass.html"); 78 } 79 80 @Test 81 public void testDefaultEncoding() throws Exception { 82 driver.sendBundle(formatter.createVisitor(output)); 83 final BufferedReader reader = new BufferedReader(new InputStreamReader( 84 output.getFileAsStream("index.html"), "UTF-8")); 85 final String line = reader.readLine(); 86 assertTrue(line, 87 line.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"")); 88 } 89 90 @Test 91 public void testSetEncoding() throws Exception { 92 formatter.setOutputEncoding("UTF-16"); 93 driver.sendBundle(formatter.createVisitor(output)); 94 final BufferedReader reader = new BufferedReader(new InputStreamReader( 95 output.getFileAsStream("index.html"), "UTF-16")); 96 final String line = reader.readLine(); 97 assertTrue(line, 98 line.startsWith("<?xml version=\"1.0\" encoding=\"UTF-16\"")); 99 } 100 101 @Test 102 public void testGetLanguageNames() throws Exception { 103 ILanguageNames names = new ILanguageNames() { 104 public String getPackageName(String vmname) { 105 return null; 106 } 107 108 public String getQualifiedClassName(String vmname) { 109 return null; 110 } 111 112 public String getClassName(String vmname, String vmsignature, 113 String vmsuperclass, String[] vminterfaces) { 114 return null; 115 } 116 117 public String getMethodName(String vmclassname, 118 String vmmethodname, String vmdesc, String vmsignature) { 119 return null; 120 } 121 122 public String getQualifiedMethodName(String vmclassname, 123 String vmmethodname, String vmdesc, String vmsignature) { 124 return null; 125 } 126 }; 127 formatter.setLanguageNames(names); 128 assertSame(names, formatter.getLanguageNames()); 129 output.close(); 130 } 131 132 @Test 133 public void testGetFooterText() throws Exception { 134 formatter.setFooterText("Custom Footer"); 135 assertEquals("Custom Footer", formatter.getFooterText()); 136 output.close(); 137 } 138 139 @Test 140 public void testGetLocale() throws Exception { 141 formatter.setLocale(Locale.KOREAN); 142 assertEquals(Locale.KOREAN, formatter.getLocale()); 143 output.close(); 144 } 145 146 } 147