• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
3  * This program and the accompanying materials are made available under
4  * the terms of the Eclipse Public License 2.0 which is available at
5  * http://www.eclipse.org/legal/epl-2.0
6  *
7  * SPDX-License-Identifier: EPL-2.0
8  *
9  * Contributors:
10  *    Marc R. Hoffmann - initial API and implementation
11  *
12  *******************************************************************************/
13 package org.jacoco.report.internal;
14 
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertSame;
17 
18 import java.io.IOException;
19 
20 import org.jacoco.report.MemoryMultiReportOutput;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Test;
24 
25 /**
26  * Unit tests for {@link ReportOutputFolder}.
27  */
28 public class ReportOutputFolderTest {
29 
30 	private MemoryMultiReportOutput output;
31 
32 	private ReportOutputFolder root;
33 
34 	@Before
setup()35 	public void setup() {
36 		output = new MemoryMultiReportOutput();
37 		root = new ReportOutputFolder(output);
38 	}
39 
40 	@After
teardown()41 	public void teardown() throws IOException {
42 		output.close();
43 		output.assertAllClosed();
44 	}
45 
46 	@Test
testFileInRoot()47 	public void testFileInRoot() throws IOException {
48 		root.createFile("test.html").close();
49 		output.assertSingleFile("test.html");
50 	}
51 
52 	@Test
testSubfolderInstance()53 	public void testSubfolderInstance() throws IOException {
54 		final ReportOutputFolder folder1 = root.subFolder("folder1");
55 		final ReportOutputFolder folder2 = root.subFolder("folder1");
56 		assertSame(folder1, folder2);
57 	}
58 
59 	@Test
testFileInSubFolder()60 	public void testFileInSubFolder() throws IOException {
61 		root.subFolder("folderA").subFolder("folderB").createFile("test.html")
62 				.close();
63 		output.assertSingleFile("folderA/folderB/test.html");
64 		output.close();
65 		output.assertAllClosed();
66 	}
67 
68 	@Test
testRelativeLinkInSameFolder()69 	public void testRelativeLinkInSameFolder() throws IOException {
70 		final ReportOutputFolder base = root.subFolder("f1").subFolder("f2");
71 		assertEquals("test.html", base.getLink(base, "test.html"));
72 	}
73 
74 	@Test
testRelativeLinkInParentFolder()75 	public void testRelativeLinkInParentFolder() throws IOException {
76 		final ReportOutputFolder base = root.subFolder("f1").subFolder("f2");
77 		assertEquals("../../test.html", root.getLink(base, "test.html"));
78 	}
79 
80 	@Test
testRelativeLinkInSubFolder()81 	public void testRelativeLinkInSubFolder() throws IOException {
82 		final ReportOutputFolder folder = root.subFolder("f1").subFolder("f2");
83 		assertEquals("f1/f2/test.html", folder.getLink(root, "test.html"));
84 	}
85 
86 	@Test
testRelativeLinkInSibling1()87 	public void testRelativeLinkInSibling1() throws IOException {
88 		final ReportOutputFolder folder = root.subFolder("f1").subFolder("f2");
89 		final ReportOutputFolder base = root.subFolder("g1").subFolder("g2");
90 		assertEquals("../../f1/f2/test.html",
91 				folder.getLink(base, "test.html"));
92 	}
93 
94 	@Test
testRelativeLinkInSibling2()95 	public void testRelativeLinkInSibling2() throws IOException {
96 		final ReportOutputFolder folder = root.subFolder("f1").subFolder("f2");
97 		final ReportOutputFolder base = root.subFolder("f1").subFolder("g2");
98 		assertEquals("../f2/test.html", folder.getLink(base, "test.html"));
99 	}
100 
101 	@Test(expected = IllegalArgumentException.class)
testInvalidRelativeLink()102 	public void testInvalidRelativeLink() throws IOException {
103 		final ReportOutputFolder folder = root.subFolder("f1").subFolder("f2");
104 		final ReportOutputFolder base = new ReportOutputFolder(
105 				new MemoryMultiReportOutput()).subFolder("g1");
106 		folder.getLink(base, "test.html");
107 	}
108 }
109