JGitSourceReferencesProvider implements SourceReferencesProvider { String getRelativePath(File subDir, File parentDir) throws MojoExecutionException { URI subDirUri; URI relativeUri; try { subDirUri = subDir.getCanonicalFile().toURI(); relativeUri = parentDir.getCanonicalFile().toURI().relativize(subDirUri); } catch (IOException e) { throw new MojoExecutionException(e.getMessage(), e); } if (relativeUri.equals(subDirUri)) { throw new MojoExecutionException(subDir + " is not a subdir of " + parentDir); } String relative = relativeUri.getPath(); if (relative.startsWith("/")) { relative = relative.substring(1); } if (relative.endsWith("/")) { relative = relative.substring(0, relative.length() - 1); } return relative; }  @Override String getSourceReferencesHeader(MavenProject project, ScmUrl scmUrl);  }
@Test public void testGetRelativePathWithDots() throws MojoExecutionException { JGitSourceReferencesProvider provider = new JGitSourceReferencesProvider(); File projectBasedir = new File("/foo/../test/bar/baz"); File repoRoot = new File("/test/foo/../bar"); assertEquals("baz", provider.getRelativePath(projectBasedir, repoRoot)); }
@Test public void testGetRelativePathWithSpaces() throws MojoExecutionException { JGitSourceReferencesProvider provider = new JGitSourceReferencesProvider(); File projectBasedir = new File("/foo/test me/bar/baz boo"); File repoRoot = new File("/foo/test me/"); assertEquals("bar/baz boo", provider.getRelativePath(projectBasedir, repoRoot)); }
@Test @EnabledOnOs(OS.WINDOWS) public void testGetRelativePathMixedCaseOnWindows() throws MojoExecutionException { JGitSourceReferencesProvider provider = new JGitSourceReferencesProvider(); File projectBasedir = new File("C:/bar/baz/test/me"); File repoRoot = new File("c:/bar/baz"); assertEquals("test/me", provider.getRelativePath(projectBasedir, repoRoot)); }
@Test public void testGetRelativePathNoCommonBasedir() throws MojoExecutionException { JGitSourceReferencesProvider provider = new JGitSourceReferencesProvider(); File projectBasedir = new File("/foo/test/bar"); File repoRoot = new File("/baz"); assertThrows(MojoExecutionException.class, () -> provider.getRelativePath(projectBasedir, repoRoot)); }
@Test public void testGetRelativePathMixedCaseOnWindows() throws MojoExecutionException { assumeThat(File.separator, is("\\")); JGitSourceReferencesProvider provider = new JGitSourceReferencesProvider(); File projectBasedir = new File("C:/bar/baz/test/me"); File repoRoot = new File("c:/bar/baz"); assertEquals("test/me", provider.getRelativePath(projectBasedir, repoRoot)); }
@Test(expected = MojoExecutionException.class) public void testGetRelativePathNoCommonBasedir() throws MojoExecutionException { JGitSourceReferencesProvider provider = new JGitSourceReferencesProvider(); File projectBasedir = new File("/foo/test/bar"); File repoRoot = new File("/baz"); provider.getRelativePath(projectBasedir, repoRoot); }