aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfmallet <freddy.mallet@gmail.com>2010-09-10 08:31:11 +0000
committerfmallet <freddy.mallet@gmail.com>2010-09-10 08:31:11 +0000
commite321995f8db5f15e278193f6035d9b0b522e3bde (patch)
treef7e8c2197dc918ae1074c569c46a85b572b2db78
parent41b2388801a713aa58d4ef53e034923392e70f48 (diff)
downloadsonarqube-e321995f8db5f15e278193f6035d9b0b522e3bde.tar.gz
sonarqube-e321995f8db5f15e278193f6035d9b0b522e3bde.zip
fix SONAR-1781 : The use of org.sonar.api.batch.AbstractSourceImporter is error prone when importing Unit Test source code
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/AbstractSourceImporter.java3
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/File.java43
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/AbstractSourceImporterTest.java75
3 files changed, 81 insertions, 40 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/AbstractSourceImporter.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/AbstractSourceImporter.java
index f5a179e328d..662bfbad2e5 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/AbstractSourceImporter.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/AbstractSourceImporter.java
@@ -96,6 +96,9 @@ public abstract class AbstractSourceImporter implements Sensor {
org.sonar.api.resources.File resource = org.sonar.api.resources.File.fromIOFile(file, sourceDirs);
if (resource != null) {
resource.setLanguage(language);
+ if (unitTest) {
+ resource.setQualifier(Resource.QUALIFIER_UNIT_TEST_CLASS);
+ }
}
return resource;
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/File.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/File.java
index 5d7fb94d212..e8d2b02cafc 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/File.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/File.java
@@ -19,14 +19,15 @@
*/
package org.sonar.api.resources;
+import java.util.List;
+
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.sonar.api.utils.WildcardPattern;
-import java.util.List;
-
/**
* This class is an implementation of a resource of type FILE
+ *
* @since 1.10
*/
public class File extends Resource<Directory> {
@@ -35,12 +36,12 @@ public class File extends Resource<Directory> {
private String filename;
private Language language;
private Directory parent;
+ private String qualifier = Resource.QUALIFIER_FILE;
/**
- * File in project. Key is the path relative to project source directories. It is not the absolute path
- * and it does not include the path to source directories. Example : <code>new File("org/sonar/foo.sql")</code>. The
- * absolute path may be c:/myproject/src/main/sql/org/sonar/foo.sql. Project root is c:/myproject and source dir
- * is src/main/sql.
+ * File in project. Key is the path relative to project source directories. It is not the absolute path and it does not include the path
+ * to source directories. Example : <code>new File("org/sonar/foo.sql")</code>. The absolute path may be
+ * c:/myproject/src/main/sql/org/sonar/foo.sql. Project root is c:/myproject and source dir is src/main/sql.
*/
public File(String key) {
if (key == null) {
@@ -88,11 +89,11 @@ public class File extends Resource<Directory> {
this.language = language;
}
- /**
- * {@inheritDoc}
- *
- * @see Resource#getParent()
- */
+ /**
+ * {@inheritDoc}
+ *
+ * @see Resource#getParent()
+ */
public Directory getParent() {
if (parent == null) {
parent = new Directory(directoryKey);
@@ -112,7 +113,7 @@ public class File extends Resource<Directory> {
/**
* {@inheritDoc}
- *
+ *
* @see Resource#matchFilePattern(String)
*/
public boolean matchFilePattern(String antPattern) {
@@ -140,7 +141,7 @@ public class File extends Resource<Directory> {
/**
* {@inheritDoc}
- *
+ *
* @see Resource#getName()
*/
public String getName() {
@@ -149,7 +150,7 @@ public class File extends Resource<Directory> {
/**
* {@inheritDoc}
- *
+ *
* @see Resource#getLongName()
*/
public String getLongName() {
@@ -158,7 +159,7 @@ public class File extends Resource<Directory> {
/**
* {@inheritDoc}
- *
+ *
* @see Resource#getDescription()
*/
public String getDescription() {
@@ -167,7 +168,7 @@ public class File extends Resource<Directory> {
/**
* {@inheritDoc}
- *
+ *
* @see Resource#getLanguage()
*/
public Language getLanguage() {
@@ -189,10 +190,16 @@ public class File extends Resource<Directory> {
}
/**
- * @return QUALIFIER_FILE
+ * Returns the qualifier associated to this File. Should be QUALIFIER_FILE or
+ *
+ * @return QUALIFIER_UNIT_TEST_CLASS
*/
public String getQualifier() {
- return Resource.QUALIFIER_FILE;
+ return qualifier;
+ }
+
+ public void setQualifier(String qualifier) {
+ this.qualifier = qualifier;
}
@Override
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/AbstractSourceImporterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/AbstractSourceImporterTest.java
index 69749dd25db..db0de75b8dd 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/AbstractSourceImporterTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/AbstractSourceImporterTest.java
@@ -19,6 +19,28 @@
*/
package org.sonar.api.batch;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.anyObject;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.MapConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;
@@ -29,23 +51,13 @@ import org.junit.Before;
import org.junit.Test;
import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;
import org.sonar.api.CoreProperties;
-import org.sonar.api.resources.*;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.nio.charset.Charset;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Matchers.anyObject;
-import static org.mockito.Matchers.argThat;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.anyString;
-import static org.mockito.Mockito.*;
+import org.sonar.api.resources.DefaultProjectFileSystem;
+import org.sonar.api.resources.Java;
+import org.sonar.api.resources.JavaFile;
+import org.sonar.api.resources.Language;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.ProjectFileSystem;
+import org.sonar.api.resources.Resource;
public class AbstractSourceImporterTest {
@@ -55,8 +67,8 @@ public class AbstractSourceImporterTest {
@Before
public void setup() throws UnsupportedEncodingException {
- aClaess = new String(new byte[]{65, 67, 108, 97, -61, -88, 115, 115, 40, 41}, CharEncoding.UTF_8);
- explicacao = new String(new byte[]{101, 120, 112, 108, 105, 99, 97, -61, -89, -61, -93, 111, 40, 41}, CharEncoding.UTF_8);
+ aClaess = new String(new byte[] { 65, 67, 108, 97, -61, -88, 115, 115, 40, 41 }, CharEncoding.UTF_8);
+ explicacao = new String(new byte[] { 101, 120, 112, 108, 105, 99, 97, -61, -89, -61, -93, 111, 40, 41 }, CharEncoding.UTF_8);
importer = new FakeSourceImporter();
}
@@ -72,7 +84,8 @@ public class AbstractSourceImporterTest {
Project pom = mock(Project.class);
Configuration config = mock(Configuration.class);
when(pom.getConfiguration()).thenReturn(config);
- when(config.getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY, CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE)).thenReturn(Boolean.FALSE);
+ when(config.getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY, CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE)).thenReturn(
+ Boolean.FALSE);
assertFalse(importer.isEnabled(pom));
assertFalse(importer.shouldExecuteOnProject(pom));
@@ -81,6 +94,7 @@ public class AbstractSourceImporterTest {
@Test
public void doNotSaveSourceIfNullResource() throws IOException {
AbstractSourceImporter nullImporter = new AbstractSourceImporter(Java.INSTANCE) {
+
@Override
protected Resource createResource(File file, List<File> sourceDirs, boolean unitTest) {
return null;
@@ -89,7 +103,7 @@ public class AbstractSourceImporterTest {
SensorContext context = mock(SensorContext.class);
ProjectFileSystem fileSystem = mock(ProjectFileSystem.class);
- when(fileSystem.getSourceFiles(Java.INSTANCE)).thenReturn(Arrays.<File>asList(new File("Foo.java"), new File("Bar.java")));
+ when(fileSystem.getSourceFiles(Java.INSTANCE)).thenReturn(Arrays.<File> asList(new File("Foo.java"), new File("Bar.java")));
nullImporter.analyse(fileSystem, context);
verify(context, never()).saveSource((Resource) anyObject(), anyString());
@@ -133,10 +147,10 @@ public class AbstractSourceImporterTest {
when(fileSystem.getSourceFiles((Language) anyObject())).thenReturn(Arrays.asList(
new File("test-resources/org/sonar/api/batch/AbstractSourceImporterTest/encoding/" + testFile)));
-
importer.analyse(project, context);
verify(context).saveSource(eq(FakeSourceImporter.TEST_RESOURCE), argThat(new BaseMatcher<String>() {
+
public boolean matches(Object arg0) {
String source = (String) arg0;
return source.contains(aClaess) && source.contains(explicacao);
@@ -147,6 +161,23 @@ public class AbstractSourceImporterTest {
}));
}
+ @Test
+ public void testCreateUnitTestResource() {
+ AbstractSourceImporter importer = new AbstractSourceImporter(Java.INSTANCE) {
+ };
+
+ File unitTestFile = new File("test/UnitTest.java");
+ File unitTestDir = new File("test");
+ List<File> unitTestDirs = new ArrayList<File>();
+ unitTestDirs.add(unitTestDir);
+
+ Resource unitTest = importer.createResource(unitTestFile, unitTestDirs, true);
+ assertThat(unitTest.getQualifier(), is("UTS"));
+
+ Resource srcTest = importer.createResource(unitTestFile, unitTestDirs, false);
+ assertThat(srcTest.getQualifier(), is("FIL"));
+ }
+
private static class FakeSourceImporter extends AbstractSourceImporter {
private final static Resource TEST_RESOURCE = new JavaFile("Test");