|
| 1 | +/* |
| 2 | + * Sonar Scoverage Plugin |
| 3 | + * Copyright (C) 2013 Rado Buransky |
| 4 | + |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public |
| 17 | + * License along with this program; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 |
| 19 | + */ |
| 20 | +package com.buransky.plugins.scoverage.pathcleaner |
| 21 | + |
| 22 | +import org.junit.runner.RunWith |
| 23 | +import org.scalatest.mock.MockitoSugar |
| 24 | +import org.scalatest.junit.JUnitRunner |
| 25 | +import org.scalatest.FlatSpec |
| 26 | +import com.buransky.plugins.scoverage.pathcleaner.BruteForceSequenceMatcher.PathSeq |
| 27 | +import org.scalatest.Matchers |
| 28 | +import java.io.File |
| 29 | +import org.mockito.Mockito._ |
| 30 | + |
| 31 | +@RunWith(classOf[JUnitRunner]) |
| 32 | +class BruteForceSequenceMatcherSpec extends FlatSpec with Matchers with MockitoSugar { |
| 33 | + |
| 34 | + // file-map of all files under baseDir/sonar.sources organized by their filename |
| 35 | + val filesMap: Map[String, Seq[PathSeq]] = Map ( |
| 36 | + "rootTestFile.scala" -> List(List("testProject", "main", "rootTestFile.scala")), |
| 37 | + "nestedTestFile.scala" -> List(List("testProject", "main", "some", "folders", "nestedTestFile.scala")), |
| 38 | + "multiFile.scala" -> List( |
| 39 | + List("testProject", "main", "some", "multiFile.scala"), |
| 40 | + List("testProject", "main", "some", "folder", "multiFile.scala") |
| 41 | + ) |
| 42 | + ) |
| 43 | + |
| 44 | + // baseDir = testProject sonar.sources = main |
| 45 | + val testee = new BruteForceSequenceMatcherTestee("/testProject/main", filesMap) |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + behavior of "BruteForceSequenceMatcher with absolute report filenames" |
| 50 | + |
| 51 | + it should "provide just the filename for top level files" in { |
| 52 | + testee.getSourceRelativePath(List("testProject", "main", "rootTestFile.scala")).get shouldEqual List("rootTestFile.scala") |
| 53 | + } |
| 54 | + |
| 55 | + it should "provide the filename and the folders for nested files" in { |
| 56 | + testee.getSourceRelativePath(List("testProject", "main", "some", "folders", "nestedTestFile.scala")).get shouldEqual List("some", "folders", "nestedTestFile.scala") |
| 57 | + } |
| 58 | + |
| 59 | + it should "find the correct file if multiple files with same name exist" in { |
| 60 | + testee.getSourceRelativePath(List("testProject", "main", "some", "multiFile.scala")).get shouldEqual List("some", "multiFile.scala") |
| 61 | + testee.getSourceRelativePath(List("testProject", "main", "some", "folder", "multiFile.scala")).get shouldEqual List("some", "folder", "multiFile.scala") |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + behavior of "BruteForceSequenceMatcher with filenames relative to the base dir" |
| 68 | + |
| 69 | + it should "provide just the filename for top level files" in { |
| 70 | + testee.getSourceRelativePath(List("main", "rootTestFile.scala")).get shouldEqual List("rootTestFile.scala") |
| 71 | + } |
| 72 | + |
| 73 | + it should "provide the filename and the folders for nested files" in { |
| 74 | + testee.getSourceRelativePath(List("main", "some", "folders", "nestedTestFile.scala")).get shouldEqual List("some", "folders", "nestedTestFile.scala") |
| 75 | + } |
| 76 | + |
| 77 | + it should "find the correct file if multiple files with same name exist" in { |
| 78 | + testee.getSourceRelativePath(List("main", "some", "multiFile.scala")).get shouldEqual List("some", "multiFile.scala") |
| 79 | + testee.getSourceRelativePath(List("main", "some", "folder", "multiFile.scala")).get shouldEqual List("some", "folder", "multiFile.scala") |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + behavior of "BruteForceSequenceMatcher with filenames relative to the src dir" |
| 86 | + |
| 87 | + it should "provide just the filename for top level files" in { |
| 88 | + testee.getSourceRelativePath(List("rootTestFile.scala")).get shouldEqual List("rootTestFile.scala") |
| 89 | + } |
| 90 | + |
| 91 | + it should "provide the filename and the folders for nested files" in { |
| 92 | + testee.getSourceRelativePath(List("some", "folders", "nestedTestFile.scala")).get shouldEqual List("some", "folders", "nestedTestFile.scala") |
| 93 | + } |
| 94 | + |
| 95 | + it should "find the correct file if multiple files with same name exist" in { |
| 96 | + testee.getSourceRelativePath(List("some", "multiFile.scala")).get shouldEqual List("some", "multiFile.scala") |
| 97 | + testee.getSourceRelativePath(List("some", "folder", "multiFile.scala")).get shouldEqual List("some", "folder", "multiFile.scala") |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + class BruteForceSequenceMatcherTestee(absoluteSrcPath: String, filesMap: Map[String, Seq[PathSeq]]) |
| 104 | + extends BruteForceSequenceMatcher(mock[File], "") { |
| 105 | + |
| 106 | + def srcDir = { |
| 107 | + val dir = mock[File] |
| 108 | + when(dir.isAbsolute).thenReturn(true) |
| 109 | + when(dir.isDirectory).thenReturn(true) |
| 110 | + when(dir.getAbsolutePath).thenReturn(absoluteSrcPath) |
| 111 | + dir |
| 112 | + } |
| 113 | + |
| 114 | + override private[pathcleaner] def initSourceDir(): File = srcDir |
| 115 | + override private[pathcleaner] def initFilesMap(): Map[String, Seq[PathSeq]] = filesMap |
| 116 | + } |
| 117 | +} |
0 commit comments