import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.sonar.api.batch.SensorContext;
+import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.WildcardPattern;
+import javax.annotation.CheckForNull;
+
/**
* @since 1.10
* Extends JavaPackage to allow smooth migration from JavaPackage to Directory
}
/**
- * @deprecated since 4.2 use {@link #create(String, String)}
+ * @deprecated since 4.2 use {@link #fromIOFile(java.io.File, Project)}
*/
@Deprecated
public Directory(String relativePathFromSourceDir) {
}
/**
- * @deprecated since 4.2 use {@link #create(String, String)}
+ * @deprecated since 4.2 use {@link #fromIOFile(java.io.File, Project)}
*/
@Deprecated
public Directory(String relativePathFromSourceDir, Language language) {
return normalizedKey;
}
+ /**
+ * Creates a {@link Directory} from an absolute {@link java.io.File} and a module.
+ * The returned {@link Directory} can be then passed for example to
+ * {@link SensorContext#saveMeasure(Resource, org.sonar.api.measures.Measure)}.
+ * @param dir absolute path to a directory
+ * @param module
+ * @return null if the directory is not under module basedir.
+ * @since 4.2
+ */
+ @CheckForNull
+ public static Directory fromIOFile(java.io.File dir, Project module) {
+ String relativePathFromBasedir = new PathResolver().relativePath(module.getFileSystem().getBasedir(), dir);
+ if (relativePathFromBasedir != null) {
+ return Directory.create(relativePathFromBasedir);
+ }
+ return null;
+ }
+
/**
* Create a Directory that is partially initialized. But that's enough to call for example
* {@link SensorContext#saveMeasure(Resource, org.sonar.api.measures.Measure)} when resources are already indexed.
* Internal use only.
- * @since 4.2
*/
- public static Directory create(String relativePathFromBaseDir) {
+ static Directory create(String relativePathFromBaseDir) {
Directory d = new Directory();
String normalizedPath = normalize(relativePathFromBaseDir);
d.setKey(normalizedPath);
}
/**
- * Creates a SonarQube File from an absolute Java IO File and a module.
- * The returned SonarQube File can be then passed for example to
+ * Creates a {@link File} from an absolute {@link java.io.File} and a module.
+ * The returned {@link File} can be then passed for example to
* {@link SensorContext#saveMeasure(Resource, org.sonar.api.measures.Measure)}.
* @param file absolute path to a file
* @param module
* Create a File that is partially initialized. But that's enough to call for example
* {@link SensorContext#saveMeasure(Resource, org.sonar.api.measures.Measure)} when resources are already indexed.
* Internal use only.
- * @since 4.2
*/
- public static File create(String relativePathFromBasedir) {
+ private static File create(String relativePathFromBasedir) {
File file = new File();
String normalizedPath = normalize(relativePathFromBasedir);
file.setKey(normalizedPath);
*/
package org.sonar.api.resources;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.IOException;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
public class DirectoryTest {
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Test
+ public void createFromIoFileShouldComputeCorrectKey() throws IOException {
+ java.io.File baseDir = temp.newFolder();
+ Project project = mock(Project.class);
+ ProjectFileSystem fileSystem = mock(ProjectFileSystem.class);
+ when(project.getFileSystem()).thenReturn(fileSystem);
+ when(fileSystem.getBasedir()).thenReturn(baseDir);
+ Resource dir = Directory.fromIOFile(new java.io.File(baseDir, "src/foo/bar/"), project);
+ assertThat(dir.getKey(), is("src/foo/bar"));
+ }
+
@Test
public void shouldStartBySlashAndNotEndBySlash() {
Resource dir = Directory.create("src/foo/bar/", " /foo/bar/ ");
*/
package org.sonar.api.resources;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.IOException;
import static org.fest.assertions.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
public class FileTest {
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Test
+ public void createFromIoFileShouldComputeCorrectKey() throws IOException {
+ java.io.File baseDir = temp.newFolder();
+ Project project = mock(Project.class);
+ ProjectFileSystem fileSystem = mock(ProjectFileSystem.class);
+ when(project.getFileSystem()).thenReturn(fileSystem);
+ when(fileSystem.getBasedir()).thenReturn(baseDir);
+ Resource file = File.fromIOFile(new java.io.File(baseDir, "src/foo/bar/toto.sql"), project);
+ assertThat(file.getKey(), is("src/foo/bar/toto.sql"));
+ }
+
@Test
public void trimKeyAndName() {
File file = new File(" foo/bar/ ", " toto.sql ");