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; 13 14 import static org.junit.Assert.assertEquals; 15 import static org.junit.Assert.assertNotNull; 16 import static org.junit.Assert.assertNull; 17 18 import java.io.BufferedReader; 19 import java.io.File; 20 import java.io.FileOutputStream; 21 import java.io.IOException; 22 import java.io.OutputStreamWriter; 23 import java.io.Reader; 24 import java.io.Writer; 25 26 import org.junit.Before; 27 import org.junit.Rule; 28 import org.junit.Test; 29 import org.junit.rules.TemporaryFolder; 30 31 /** 32 * Unit tests for {@link DirectorySourceFileLocator}. 33 */ 34 public class DirectorySourceFileLocatorTest { 35 36 @Rule 37 public final TemporaryFolder sourceFolder = new TemporaryFolder(); 38 39 private ISourceFileLocator locator; 40 41 @Before 42 public void setup() { 43 locator = new DirectorySourceFileLocator(sourceFolder.getRoot(), 44 "UTF-8", 4); 45 } 46 47 @Test 48 public void testGetSourceFileNegative() throws IOException { 49 assertNull(locator.getSourceFile("org/jacoco/example", 50 "DoesNotExist.java")); 51 } 52 53 @Test 54 public void testGetSourceFile() throws IOException { 55 createFile("org/jacoco/example/Test.java"); 56 final Reader source = locator.getSourceFile("org/jacoco/example", 57 "Test.java"); 58 assertContent(source); 59 } 60 61 private void createFile(String path) throws IOException { 62 final File file = new File(sourceFolder.getRoot(), path); 63 file.getParentFile().mkdirs(); 64 final Writer writer = new OutputStreamWriter( 65 new FileOutputStream(file), "UTF-8"); 66 writer.write("Source"); 67 writer.close(); 68 } 69 70 private void assertContent(Reader source) throws IOException { 71 assertNotNull(source); 72 final BufferedReader buffer = new BufferedReader(source); 73 assertEquals("Source", buffer.readLine()); 74 assertNull(buffer.readLine()); 75 buffer.close(); 76 } 77 78 } 79