• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
13 
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertSame;
16 
17 import java.io.IOException;
18 
19 import org.jacoco.report.MemoryMultiReportOutput;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 
24 /**
25  * Unit tests for {@link ReportOutputFolder}.
26  */
27 public class ReportOutputFolderTest {
28 
29 	private MemoryMultiReportOutput output;
30 
31 	private ReportOutputFolder root;
32 
33 	@Before
setup()34 	public void setup() {
35 		output = new MemoryMultiReportOutput();
36 		root = new ReportOutputFolder(output);
37 	}
38 
39 	@After
teardown()40 	public void teardown() throws IOException {
41 		output.close();
42 		output.assertAllClosed();
43 	}
44 
45 	@Test
testFileInRoot()46 	public void testFileInRoot() throws IOException {
47 		root.createFile("test.html").close();
48 		output.assertSingleFile("test.html");
49 	}
50 
51 	@Test
testSubfolderInstance()52 	public void testSubfolderInstance() throws IOException {
53 		final ReportOutputFolder folder1 = root.subFolder("folder1");
54 		final ReportOutputFolder folder2 = root.subFolder("folder1");
55 		assertSame(folder1, folder2);
56 	}
57 
58 	@Test
testFileInSubFolder()59 	public void testFileInSubFolder() throws IOException {
60 		root.subFolder("folderA").subFolder("folderB").createFile("test.html")
61 				.close();
62 		output.assertSingleFile("folderA/folderB/test.html");
63 		output.close();
64 		output.assertAllClosed();
65 	}
66 
67 	@Test
testRelativeLinkInSameFolder()68 	public void testRelativeLinkInSameFolder() throws IOException {
69 		final ReportOutputFolder base = root.subFolder("f1").subFolder("f2");
70 		assertEquals("test.html", base.getLink(base, "test.html"));
71 	}
72 
73 	@Test
testRelativeLinkInParentFolder()74 	public void testRelativeLinkInParentFolder() throws IOException {
75 		final ReportOutputFolder base = root.subFolder("f1").subFolder("f2");
76 		assertEquals("../../test.html", root.getLink(base, "test.html"));
77 	}
78 
79 	@Test
testRelativeLinkInSubFolder()80 	public void testRelativeLinkInSubFolder() throws IOException {
81 		final ReportOutputFolder folder = root.subFolder("f1").subFolder("f2");
82 		assertEquals("f1/f2/test.html", folder.getLink(root, "test.html"));
83 	}
84 
85 	@Test
testRelativeLinkInSibling1()86 	public void testRelativeLinkInSibling1() throws IOException {
87 		final ReportOutputFolder folder = root.subFolder("f1").subFolder("f2");
88 		final ReportOutputFolder base = root.subFolder("g1").subFolder("g2");
89 		assertEquals("../../f1/f2/test.html", folder.getLink(base, "test.html"));
90 	}
91 
92 	@Test
testRelativeLinkInSibling2()93 	public void testRelativeLinkInSibling2() throws IOException {
94 		final ReportOutputFolder folder = root.subFolder("f1").subFolder("f2");
95 		final ReportOutputFolder base = root.subFolder("f1").subFolder("g2");
96 		assertEquals("../f2/test.html", folder.getLink(base, "test.html"));
97 	}
98 
99 	@Test(expected = IllegalArgumentException.class)
testInvalidRelativeLink()100 	public void testInvalidRelativeLink() throws IOException {
101 		final ReportOutputFolder folder = root.subFolder("f1").subFolder("f2");
102 		final ReportOutputFolder base = new ReportOutputFolder(
103 				new MemoryMultiReportOutput()).subFolder("g1");
104 		folder.getLink(base, "test.html");
105 	}
106 }
107