aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2011-01-25 19:10:20 +0100
committersimonbrandhof <simon.brandhof@gmail.com>2011-01-25 19:26:43 +0100
commite7de7db7a3a3de0bf7961f0b5210156182622f10 (patch)
treea638d3b82ebf27f68184c527df61fff9923502ab /plugins
parentf3b88bb8e5a57a215e59dcc5c38f550433c9a1eb (diff)
downloadsonarqube-e7de7db7a3a3de0bf7961f0b5210156182622f10.tar.gz
sonarqube-e7de7db7a3a3de0bf7961f0b5210156182622f10.zip
Improve backward-compatibility when adding data to a non-indexed resource
Diffstat (limited to 'plugins')
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/JavaSourceImporter.java65
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidSensor.java15
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/JavaSourceImporterTest.java57
-rw-r--r--plugins/sonar-squid-java-plugin/test-resources/basic_handlers/javaFile.java31
-rw-r--r--plugins/sonar-squid-java-plugin/test-resources/basic_handlers/plsqlFile.pak83
5 files changed, 94 insertions, 157 deletions
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/JavaSourceImporter.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/JavaSourceImporter.java
index 87e7699ba88..0e7a1c1e3f4 100644
--- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/JavaSourceImporter.java
+++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/JavaSourceImporter.java
@@ -19,30 +19,73 @@
*/
package org.sonar.plugins.squid;
+import org.apache.commons.io.FileUtils;
+import org.sonar.api.CoreProperties;
import org.sonar.api.batch.*;
-import org.sonar.api.resources.Java;
-import org.sonar.api.resources.JavaFile;
-import org.sonar.api.resources.Resource;
+import org.sonar.api.resources.*;
+import org.sonar.api.utils.SonarException;
import org.sonar.java.api.JavaUtils;
-import java.io.File;
+import java.io.IOException;
+import java.nio.charset.Charset;
import java.util.List;
-@DependsUpon(classes=SquidSensor.class)
+@Phase(name = Phase.Name.PRE)
+@DependsUpon(classes = SquidSensor.class)
@DependedUpon(JavaUtils.BARRIER_AFTER_SQUID)
-public class JavaSourceImporter extends AbstractSourceImporter {
+public final class JavaSourceImporter implements Sensor {
- public JavaSourceImporter() {
- super(Java.INSTANCE);
+ /**
+ * {@inheritDoc}
+ */
+ public boolean shouldExecuteOnProject(Project project) {
+ return isEnabled(project) && Java.KEY.equals(project.getLanguageKey());
}
- @Override
- protected Resource createResource(File file, List<File> sourceDirs, boolean unitTest) {
- return file != null ? JavaFile.fromIOFile(file, sourceDirs, unitTest) : null;
+ /**
+ * {@inheritDoc}
+ */
+ public void analyse(Project project, SensorContext context) {
+ analyse(project.getFileSystem(), context);
+ }
+
+ void analyse(ProjectFileSystem fileSystem, SensorContext context) {
+ parseDirs(context, fileSystem.mainFiles(Java.INSTANCE), false, fileSystem.getSourceCharset());
+ parseDirs(context, fileSystem.testFiles(Java.INSTANCE), true, fileSystem.getSourceCharset());
+ }
+
+ void parseDirs(SensorContext context, List<InputFile> inputFiles, boolean unitTest, Charset sourcesEncoding) {
+ for (InputFile inputFile : inputFiles) {
+ JavaFile javaFile = JavaFile.fromRelativePath(inputFile.getRelativePath(), unitTest);
+ importSource(context, javaFile, inputFile, sourcesEncoding);
+ }
+ }
+
+ void importSource(SensorContext context, JavaFile javaFile, InputFile inputFile, Charset sourcesEncoding) {
+ try {
+ //if (!context.isIndexed(javaFile, true)) {
+ // See http://jira.codehaus.org/browse/SONAR-791
+ // Squid is the reference plugin to index files. If a file is not indexed,
+ // throw new SonarException("Invalid file: " + javaFile + ". Please check that Java source directories match root directories" +
+ // " as defined by packages.");
+ //}
+ String source = FileUtils.readFileToString(inputFile.getFile(), sourcesEncoding.name());
+ context.saveSource(javaFile, source);
+
+ } catch (IOException e) {
+ throw new SonarException("Unable to read and import the source file : '" + inputFile.getFile().getAbsolutePath() + "' with the charset : '"
+ + sourcesEncoding.name() + "'.", e);
+ }
+ }
+
+ boolean isEnabled(Project project) {
+ return project.getConfiguration().getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY,
+ CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE);
}
@Override
public String toString() {
return getClass().getSimpleName();
}
+
}
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidSensor.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidSensor.java
index c1b242e09de..c79a60d2559 100644
--- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidSensor.java
+++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidSensor.java
@@ -25,6 +25,7 @@ import org.sonar.api.batch.*;
import org.sonar.api.checks.AnnotationCheckFactory;
import org.sonar.api.checks.NoSonarFilter;
import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.resources.InputFile;
import org.sonar.api.resources.Java;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.Project;
@@ -82,22 +83,18 @@ public class SquidSensor implements Sensor {
}
private void browseTestSources(Project project, SensorContext context) {
- for (File testFile : getTestSourceFiles(project)) {
- context.index(JavaFile.fromIOFile(testFile, project.getFileSystem().getTestDirs(), true));
+ for (InputFile testFile : project.getFileSystem().testFiles(Java.INSTANCE)) {
+ context.index(JavaFile.fromRelativePath(testFile.getRelativePath(), true));
}
}
- private List<File> getTestSourceFiles(Project project) {
- return project.getFileSystem().getTestFiles(Java.INSTANCE);
- }
-
private List<File> getMainSourceFiles(Project project) {
return project.getFileSystem().getJavaSourceFiles();
}
private Collection<File> getMainBytecodeFiles(Project project) {
Collection<File> bytecodeFiles = projectClasspath.getElements();
- if ( !hasProjectBytecodeFiles(project)) {
+ if (!hasProjectBytecodeFiles(project)) {
File classesDir = project.getFileSystem().getBuildOutputDir();
if (classesDir != null && classesDir.exists()) {
bytecodeFiles.remove(classesDir);
@@ -107,11 +104,11 @@ public class SquidSensor implements Sensor {
}
private boolean hasProjectBytecodeFiles(Project project) {
- if ( !project.getConfiguration()
+ if (!project.getConfiguration()
.getBoolean(CoreProperties.DESIGN_SKIP_DESIGN_PROPERTY, CoreProperties.DESIGN_SKIP_DESIGN_DEFAULT_VALUE)) {
File classesDir = project.getFileSystem().getBuildOutputDir();
if (classesDir != null && classesDir.exists()) {
- return !FileUtils.listFiles(classesDir, new String[] { "class" }, true).isEmpty();
+ return !FileUtils.listFiles(classesDir, new String[]{"class"}, true).isEmpty();
}
}
return false;
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/JavaSourceImporterTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/JavaSourceImporterTest.java
index c6667a6920e..f90274d9964 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/JavaSourceImporterTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/JavaSourceImporterTest.java
@@ -19,42 +19,53 @@
*/
package org.sonar.plugins.squid;
-import org.apache.commons.io.FileUtils;
+import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
+import org.sonar.api.batch.SensorContext;
+import org.sonar.api.resources.InputFile;
import org.sonar.api.resources.JavaFile;
-import org.sonar.api.resources.JavaPackage;
-import org.sonar.api.resources.Resource;
-import org.sonar.api.resources.ResourceUtils;
+import org.sonar.api.utils.SonarException;
import java.io.File;
import java.io.IOException;
-import java.util.Arrays;
+import java.nio.charset.Charset;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.*;
public class JavaSourceImporterTest {
- @Test
- public void shouldDefineMainFile() throws IOException {
- JavaSourceImporter importer = new JavaSourceImporter();
- Resource clazz = importer.createResource(new File(newDir("source1"), "/MyClass.java"), Arrays.asList(newDir("source1")), false);
- assertThat(clazz, is(JavaFile.class));
- assertThat(clazz.getKey(), is(JavaPackage.DEFAULT_PACKAGE_NAME + ".MyClass"));
- assertThat(clazz.getName(), is("MyClass"));
+ private JavaSourceImporter importer;
+ private SensorContext context;
+ private InputFile inputFile;
+
+ @Before
+ public void init() {
+ File fileToImport = new File("test-resources/rules/UndocumentedApi.java");
+ inputFile = mock(InputFile.class);
+ when(inputFile.getRelativePath()).thenReturn("UndocumentedApi.java");
+ when(inputFile.getFile()).thenReturn(fileToImport);
+ when(inputFile.getFileBaseDir()).thenReturn(fileToImport.getParentFile());
+ importer = new JavaSourceImporter();
+ context = mock(SensorContext.class);
}
@Test
- public void shouldDefineTestFile() throws IOException {
- JavaSourceImporter importer = new JavaSourceImporter();
- Resource resource = importer.createResource(new File(newDir("tests"), "/MyClassTest.java"), Arrays.asList(newDir("tests")), true);
- assertThat(resource, is(JavaFile.class));
- assertThat(ResourceUtils.isUnitTestClass(resource), is(true));
+ public void shouldSetSource() throws IOException {
+ JavaFile javaFile = JavaFile.fromRelativePath("UndocumentedApi.java", true);
+ when(context.isIndexed(javaFile, true)).thenReturn(true);
+ importer.importSource(context, javaFile, inputFile, Charset.defaultCharset());
+
+ verify(context).saveSource(eq(javaFile), anyString());
}
- private File newDir(String relativePath) throws IOException {
- File target = new File("target", relativePath);
- FileUtils.forceMkdir(target);
- return target;
+ @Test(expected = SonarException.class)
+ @Ignore("see SONAR-791")
+ public void shouldFailWhenSquidDidNotIndexFile() throws IOException {
+ JavaFile javaFile = new JavaFile("Bar");
+ when(context.isIndexed(javaFile, true)).thenReturn(false);
+ importer.importSource(context, javaFile, inputFile, Charset.defaultCharset());
}
} \ No newline at end of file
diff --git a/plugins/sonar-squid-java-plugin/test-resources/basic_handlers/javaFile.java b/plugins/sonar-squid-java-plugin/test-resources/basic_handlers/javaFile.java
deleted file mode 100644
index 5ea06251f14..00000000000
--- a/plugins/sonar-squid-java-plugin/test-resources/basic_handlers/javaFile.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/**
- * Javadoc for this Car class
- * @author freddy
- *
- */
-public class Car {
-
- private int n; //C++ inline comment
- private String s;
-
- public AClass(int n, String s) {
- this.n = n;
- this.s = s;
- }
-
- /*
- *C style comment
- *
- */
- public int getN() {
- /*
- * Another C style comment
- */
- return n;
- }
-
- public String getS() {
- return s;
- }
-
-} \ No newline at end of file
diff --git a/plugins/sonar-squid-java-plugin/test-resources/basic_handlers/plsqlFile.pak b/plugins/sonar-squid-java-plugin/test-resources/basic_handlers/plsqlFile.pak
deleted file mode 100644
index f0e5a8535a5..00000000000
--- a/plugins/sonar-squid-java-plugin/test-resources/basic_handlers/plsqlFile.pak
+++ /dev/null
@@ -1,83 +0,0 @@
-
-CREATE OR REPLACE PACKAGE SONAR002 AS
-
--- ----------------------------------------------------------------------------
--- package version
-FUNCTION VERSION RETURN VARCHAR2;
- PRAGMA RESTRICT_REFERENCES (VERSION, WNDS, WNPS, RNPS);
-
-
-TYPE T_trans IS TABLE OF VARCHAR2(250) INDEX BY BINARY_INTEGER;
-A_trans T_trans;
-w_origine VARCHAR2(160);
-w_sqlerrm VARCHAR2(255);
-
-header_c_langue VARCHAR2(2);
-
-TYPE T_TAG IS RECORD ( NOM_TAG VARCHAR2(50)
- ,VALEUR VARCHAR2(32000));
-
-TYPE TT_TAGS IS TABLE OF T_TAG INDEX BY BINARY_INTEGER;
-
-tb_TAGS TT_TAGS;
-
-
-TYPE ControleData IS RECORD
- ( pourcent NUMBER,
- bande VARCHAR2(40),
- nb_ligne NUMBER,
- typ_avis VARCHAR2(25),
- nb_brique NUMBER,
- no_op operation.no_op%TYPE,
- no_op_pere operation.no_op%TYPE,
- no_op_doss operation.no_op%TYPE,
- c_typ_contrat operation.c_typ_contrat%TYPE,
- ref_op operation.ref_op%TYPE,
- c_op_param type_oper_param.c_op_param%TYPE,
- c_serv operation.c_serv%TYPE,
- c_dev operation.c_dev%TYPE,
- d_op operation.d_op%TYPE,
- c_op_param_role VARCHAR2(4),
- c_phase VARCHAR2(4),
- c_phase_real VARCHAR2(4),
- id_avis NUMBER,
- c_form VARCHAR2(10),
- c_teneur_envoi VARCHAR2(10),
- Spool VARCHAR2(320),
- spool_tmp VARCHAR2(320),
- old_elt VARCHAR2(10),
- new_elt VARCHAR2(10),
- old_ligne NUMBER,
- new_ligne NUMBER,
- old_ordre NUMBER,
- new_tag VARCHAR2(3),
- old_tag VARCHAR2(3),
- new_ordre NUMBER,
- mode_fusion VARCHAR2(1),
- TRACE VARCHAR2(80),
- taux_cent_mille NUMBER,
- caractere_taux VARCHAR2(1),
- chemin_avis VARCHAR2(20),
- nbr_dec_doss NUMBER,
- c_dev_doss VARCHAR2(3),
- mt_cpta_bloc_2 NUMBER,
- regr_aff_dest VARCHAR2(1),
- no_seq NUMBER(8),
- no_seq_pere NUMBER(8),
- id_envoi NUMBER(8),
- id_envoi_pere NUMBER(8),
- id_message NUMBER(8),
- generation VARCHAR2(1),
- c_uti VARCHAR2(6),
- c_uti_cre VARCHAR2(6),
- no_swift VARCHAR2(1),
- c_trait_202 VARCHAR2(5),
- envoi_pere_202 VARCHAR2(5),
- f_envoi_ga_java VARCHAR2(1),
- avec_templ_perso VARCHAR2(1),
- f_comm_prises VARCHAR2(1),
- liab_reduc_with_pmt VARCHAR2(1));
-Controle ControleData;
-
-END SONAR002;
-