Home | History | Annotate | Download | only in report
      1 /*******************************************************************************
      2  * Copyright (c) 2009, 2015 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.IOException;
     19 import java.io.Reader;
     20 import java.util.HashMap;
     21 import java.util.Map;
     22 
     23 import org.junit.Before;
     24 import org.junit.Test;
     25 
     26 /**
     27  * Unit tests for {@link MultiSourceFileLocator}.
     28  */
     29 public class MultiSourceFileLocatorTest {
     30 
     31 	private MultiSourceFileLocator locator;
     32 
     33 	@Before
     34 	public void setup() {
     35 		locator = new MultiSourceFileLocator(7);
     36 	}
     37 
     38 	@Test
     39 	public void testGetTabWidth() throws IOException {
     40 		assertEquals(7, locator.getTabWidth());
     41 	}
     42 
     43 	@Test
     44 	public void testEmpty() throws IOException {
     45 		assertNull(locator.getSourceFile("org/jacoco/example", "Test.java"));
     46 	}
     47 
     48 	@Test
     49 	public void testNohit() throws IOException {
     50 		final StubLocator loc1 = new StubLocator();
     51 		locator.add(loc1);
     52 		final StubLocator loc2 = new StubLocator();
     53 		locator.add(loc2);
     54 		final StubLocator loc3 = new StubLocator();
     55 		locator.add(loc3);
     56 
     57 		assertNull(locator.getSourceFile("org/jacoco/example", "Test.java"));
     58 	}
     59 
     60 	@Test
     61 	public void testHit() throws IOException {
     62 		final StubLocator loc1 = new StubLocator();
     63 		loc1.put("org/jacoco/example/Test1.java", '1');
     64 		locator.add(loc1);
     65 		final StubLocator loc2 = new StubLocator();
     66 		loc2.put("org/jacoco/example/Test2.java", '2');
     67 		locator.add(loc2);
     68 		final StubLocator loc3 = new StubLocator();
     69 		loc3.put("org/jacoco/example/Test3.java", '3');
     70 		locator.add(loc3);
     71 
     72 		final Reader source = locator.getSourceFile("org/jacoco/example",
     73 				"Test2.java");
     74 		assertNotNull(source);
     75 		assertEquals('2', source.read());
     76 	}
     77 
     78 	@Test
     79 	public void testPrecedence() throws IOException {
     80 		final StubLocator loc1 = new StubLocator();
     81 		loc1.put("org/jacoco/example/TestX.java", '1');
     82 		locator.add(loc1);
     83 		final StubLocator loc2 = new StubLocator();
     84 		loc2.put("org/jacoco/example/Test.java", '2');
     85 		locator.add(loc2);
     86 		final StubLocator loc3 = new StubLocator();
     87 		loc3.put("org/jacoco/example/Test.java", '3');
     88 		locator.add(loc3);
     89 
     90 		final Reader source = locator.getSourceFile("org/jacoco/example",
     91 				"Test.java");
     92 		assertNotNull(source);
     93 		assertEquals('2', source.read());
     94 	}
     95 
     96 	private static class StubLocator implements ISourceFileLocator {
     97 
     98 		private final Map<String, Reader> sources = new HashMap<String, Reader>();
     99 
    100 		void put(String path, char content) {
    101 			sources.put(path, new StubReader(content));
    102 		}
    103 
    104 		public Reader getSourceFile(String packageName, String fileName)
    105 				throws IOException {
    106 			return sources.get(packageName + "/" + fileName);
    107 		}
    108 
    109 		public int getTabWidth() {
    110 			return 0;
    111 		}
    112 	}
    113 
    114 	private static class StubReader extends Reader {
    115 
    116 		private final char content;
    117 
    118 		StubReader(char content) {
    119 			this.content = content;
    120 		}
    121 
    122 		@Override
    123 		public int read(char[] cbuf, int off, int len) throws IOException {
    124 			for (int idx = 0; idx < len; idx++) {
    125 				cbuf[off + idx] = content;
    126 			}
    127 			return len;
    128 		}
    129 
    130 		@Override
    131 		public void close() throws IOException {
    132 		}
    133 
    134 	}
    135 
    136 }
    137