diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-06-23 21:31:56 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-06-25 23:42:50 +0200 |
commit | 70b6899988da0d2ba0a39b846e4f1bd3fa27304f (patch) | |
tree | 1ac093a87e0fba6b07c6feb6aceae89bdd9663cf /tests/plugins/batch-plugin | |
parent | 5dd574819854e9ce7e2f4e181e78153a7ecbf828 (diff) | |
download | sonarqube-70b6899988da0d2ba0a39b846e4f1bd3fa27304f.tar.gz sonarqube-70b6899988da0d2ba0a39b846e4f1bd3fa27304f.zip |
Move integration tests to directory tests/
Diffstat (limited to 'tests/plugins/batch-plugin')
10 files changed, 489 insertions, 0 deletions
diff --git a/tests/plugins/batch-plugin/pom.xml b/tests/plugins/batch-plugin/pom.xml new file mode 100644 index 00000000000..98b839b6bde --- /dev/null +++ b/tests/plugins/batch-plugin/pom.xml @@ -0,0 +1,37 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.sonarsource.sonarqube.tests</groupId> + <artifactId>plugins</artifactId> + <version>6.5-SNAPSHOT</version> + </parent> + + <artifactId>batch-plugin</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>sonar-plugin</packaging> + <name>SonarQube Integration Tests :: Plugins :: Batch</name> + <description>Main plugin for batch tests</description> + + <dependencies> + <dependency> + <groupId>org.sonarsource.sonarqube</groupId> + <artifactId>sonar-plugin-api</artifactId> + <version>${apiVersion}</version> + <scope>provided</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId> + <artifactId>sonar-packaging-maven-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <pluginClass>com.sonarsource.BatchPlugin</pluginClass> + </configuration> + </plugin> + </plugins> + </build> +</project> diff --git a/tests/plugins/batch-plugin/src/main/java/com/sonarsource/BatchPlugin.java b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/BatchPlugin.java new file mode 100644 index 00000000000..08f533ecea0 --- /dev/null +++ b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/BatchPlugin.java @@ -0,0 +1,47 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package com.sonarsource; + +import com.sonarsource.decimal_scale_of_measures.DecimalScaleMeasureComputer; +import com.sonarsource.decimal_scale_of_measures.DecimalScaleMetric; +import com.sonarsource.decimal_scale_of_measures.DecimalScaleProperty; +import com.sonarsource.decimal_scale_of_measures.DecimalScaleSensor; +import org.sonar.api.Plugin; + +import static java.util.Arrays.asList; + +public class BatchPlugin implements Plugin { + + @Override + public void define(Context context) { + context.addExtensions(asList( + // SONAR-6939 decimal_scale_of_measures + DecimalScaleMeasureComputer.class, + DecimalScaleMetric.class, + DecimalScaleSensor.class, + DecimalScaleProperty.definition(), + + DumpSettingsInitializer.class, + RaiseMessageException.class, + TempFolderExtension.class, + WaitingSensor.class + )); + } +} diff --git a/tests/plugins/batch-plugin/src/main/java/com/sonarsource/DumpSettingsInitializer.java b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/DumpSettingsInitializer.java new file mode 100644 index 00000000000..5149d72779c --- /dev/null +++ b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/DumpSettingsInitializer.java @@ -0,0 +1,68 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package com.sonarsource; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map.Entry; +import java.util.Set; +import java.util.TreeMap; +import org.sonar.api.Properties; +import org.sonar.api.Property; +import org.sonar.api.PropertyType; +import org.sonar.api.batch.Initializer; +import org.sonar.api.config.Settings; +import org.sonar.api.resources.Project; + +@Properties({ + @Property( + key = DumpSettingsInitializer.SONAR_SHOW_SETTINGS, + type = PropertyType.STRING, + name = "Property to decide if it should output settings", + multiValues = true, + defaultValue = "") +}) +public class DumpSettingsInitializer extends Initializer { + + public static final String SONAR_SHOW_SETTINGS = "sonar.showSettings"; + private Settings settings; + + public DumpSettingsInitializer(Settings settings) { + this.settings = settings; + } + + @Override + public boolean shouldExecuteOnProject(Project project) { + return true; + } + + @Override + public void execute(Project project) { + Set<String> settingsToDump = new HashSet<>(Arrays.asList(settings.getStringArray(SONAR_SHOW_SETTINGS))); + if (!settingsToDump.isEmpty()) { + TreeMap<String, String> treemap = new TreeMap<String, String>(settings.getProperties()); + for (Entry<String, String> prop : treemap.entrySet()) { + if (settingsToDump.contains(prop.getKey())) { + System.out.println(" o " + project.getKey() + ":" + prop.getKey() + " = " + prop.getValue()); + } + } + } + } +} diff --git a/tests/plugins/batch-plugin/src/main/java/com/sonarsource/RaiseMessageException.java b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/RaiseMessageException.java new file mode 100644 index 00000000000..a1acf47fb39 --- /dev/null +++ b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/RaiseMessageException.java @@ -0,0 +1,45 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package com.sonarsource; + +import org.sonar.api.batch.Sensor; +import org.sonar.api.batch.SensorContext; +import org.sonar.api.config.Settings; +import org.sonar.api.resources.Project; +import org.sonar.api.utils.MessageException; + +public class RaiseMessageException implements Sensor { + + private final Settings settings; + + public RaiseMessageException(Settings settings) { + this.settings = settings; + } + + @Override + public boolean shouldExecuteOnProject(Project project) { + return settings.getBoolean("raiseMessageException"); + } + + @Override + public void analyse(Project project, SensorContext sensorContext) { + throw MessageException.of("Error message from plugin"); + } +}
\ No newline at end of file diff --git a/tests/plugins/batch-plugin/src/main/java/com/sonarsource/TempFolderExtension.java b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/TempFolderExtension.java new file mode 100644 index 00000000000..1549471ff0f --- /dev/null +++ b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/TempFolderExtension.java @@ -0,0 +1,60 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package com.sonarsource; + +import org.sonar.api.Properties; +import org.sonar.api.Property; +import org.sonar.api.PropertyType; +import org.sonar.api.batch.Initializer; +import org.sonar.api.config.Settings; +import org.sonar.api.resources.Project; +import org.sonar.api.utils.TempFolder; + +@Properties({ + @Property( + key = TempFolderExtension.CREATE_TEMP_FILES, + type = PropertyType.BOOLEAN, + name = "Property to decide if it should create temp files", + defaultValue = "false") +}) +public class TempFolderExtension extends Initializer { + + public static final String CREATE_TEMP_FILES = "sonar.createTempFiles"; + private Settings settings; + private TempFolder tempFolder; + + public TempFolderExtension(Settings settings, TempFolder tempFolder) { + this.settings = settings; + this.tempFolder = tempFolder; + } + + @Override + public boolean shouldExecuteOnProject(Project project) { + return true; + } + + @Override + public void execute(Project project) { + if (settings.getBoolean(CREATE_TEMP_FILES)) { + System.out.println("Creating temp directory: " + tempFolder.newDir("sonar-it").getAbsolutePath()); + System.out.println("Creating temp file: " + tempFolder.newFile("sonar-it", ".txt").getAbsolutePath()); + } + } +} diff --git a/tests/plugins/batch-plugin/src/main/java/com/sonarsource/WaitingSensor.java b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/WaitingSensor.java new file mode 100644 index 00000000000..a498e8534aa --- /dev/null +++ b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/WaitingSensor.java @@ -0,0 +1,48 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package com.sonarsource; + +import org.sonar.api.batch.Sensor; +import org.sonar.api.batch.SensorContext; +import org.sonar.api.config.Settings; +import org.sonar.api.resources.Project; + +public class WaitingSensor implements Sensor { + private Settings settings; + + public WaitingSensor(Settings settings) { + this.settings = settings; + } + + @Override + public boolean shouldExecuteOnProject(Project project) { + return settings.getBoolean("sonar.it.enableWaitingSensor"); + } + + @Override + public void analyse(Project module, SensorContext context) { + try { + Thread.sleep(10_000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + +} diff --git a/tests/plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleMeasureComputer.java b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleMeasureComputer.java new file mode 100644 index 00000000000..38140cd597f --- /dev/null +++ b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleMeasureComputer.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package com.sonarsource.decimal_scale_of_measures; + +import org.sonar.api.ce.measure.Measure; +import org.sonar.api.ce.measure.MeasureComputer; + +public class DecimalScaleMeasureComputer implements MeasureComputer { + + @Override + public MeasureComputerDefinition define(MeasureComputerDefinitionContext defContext) { + return defContext.newDefinitionBuilder() + // Output metrics must contains at least one metric + .setOutputMetrics(DecimalScaleMetric.KEY) + + .build(); + } + + @Override + public void compute(MeasureComputerContext context) { + if (context.getMeasure(DecimalScaleMetric.KEY) == null) { + Iterable<Measure> childMeasures = context.getChildrenMeasures(DecimalScaleMetric.KEY); + int count = 0; + double total = 0.0; + for (Measure childMeasure : childMeasures) { + count++; + total += childMeasure.getDoubleValue(); + } + double value = 0.0; + if (count > 0) { + value = total / count; + } + context.addMeasure(DecimalScaleMetric.KEY, value); + } + } +} diff --git a/tests/plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleMetric.java b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleMetric.java new file mode 100644 index 00000000000..aa2454b2a89 --- /dev/null +++ b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleMetric.java @@ -0,0 +1,44 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package com.sonarsource.decimal_scale_of_measures; + +import java.util.Collections; +import java.util.List; +import org.sonar.api.measures.Metric; +import org.sonar.api.measures.Metrics; + +public class DecimalScaleMetric implements Metrics { + + public static final String KEY = "decimal_scale"; + + private static final Metric METRIC = new Metric.Builder(KEY, "Decimal Scale", Metric.ValueType.FLOAT) + .setDescription("Numeric metric with overridden decimal scale") + .setDecimalScale(4) + .create(); + + @Override + public List getMetrics() { + return Collections.singletonList(definition()); + } + + public static Metric definition() { + return METRIC; + } +} diff --git a/tests/plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleProperty.java b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleProperty.java new file mode 100644 index 00000000000..c070434eeec --- /dev/null +++ b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleProperty.java @@ -0,0 +1,32 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package com.sonarsource.decimal_scale_of_measures; + +import org.sonar.api.PropertyType; +import org.sonar.api.config.PropertyDefinition; + +public class DecimalScaleProperty { + + public static final String KEY = "sonar.scanner.feedDecimalScaleMetric"; + + public static PropertyDefinition definition() { + return PropertyDefinition.builder(KEY).name("Enable test decimal_scale_of_measures").type(PropertyType.BOOLEAN).defaultValue(String.valueOf(false)).build(); + } +} diff --git a/tests/plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleSensor.java b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleSensor.java new file mode 100644 index 00000000000..788627bb920 --- /dev/null +++ b/tests/plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleSensor.java @@ -0,0 +1,55 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package com.sonarsource.decimal_scale_of_measures; + +import org.sonar.api.batch.Sensor; +import org.sonar.api.batch.SensorContext; +import org.sonar.api.batch.fs.FilePredicate; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.resources.Project; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; + +public class DecimalScaleSensor implements Sensor { + private static final Logger LOG = Loggers.get(DecimalScaleSensor.class); + + @Override + public boolean shouldExecuteOnProject(Project project) { + return true; + } + + @Override + public void analyse(Project module, SensorContext context) { + if (context.settings().getBoolean(DecimalScaleProperty.KEY)) { + FilePredicate all = context.fileSystem().predicates().all(); + Iterable<InputFile> files = context.fileSystem().inputFiles(all); + double value = 0.0001; + for (InputFile file : files) { + LOG.info("Value for {}: {}", file.relativePath(), value); + context.newMeasure() + .on(file) + .forMetric(DecimalScaleMetric.definition()) + .withValue(value) + .save(); + value += 0.0001; + } + } + } +} |