Skip to content

Commit

Permalink
#92 improves code quality (reduce code duplication)
Browse files Browse the repository at this point in the history
  • Loading branch information
jycr committed Jun 20, 2023
1 parent 0ec23b6 commit 36268bf
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 145 deletions.
1 change: 1 addition & 0 deletions ecocode-rules-specifications/src/main/assembly/java.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<includes>
<include>io/ecocode/rules/*.class</include>
<include>io/ecocode/rules/java/*.*</include>
</includes>
<outputDirectory/>
Expand Down
1 change: 1 addition & 0 deletions ecocode-rules-specifications/src/main/assembly/php.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<includes>
<include>io/ecocode/rules/*.class</include>
<include>io/ecocode/rules/php/*.*</include>
</includes>
<outputDirectory/>
Expand Down
1 change: 1 addition & 0 deletions ecocode-rules-specifications/src/main/assembly/python.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<includes>
<include>io/ecocode/rules/*.class</include>
<include>io/ecocode/rules/python/*.*</include>
</includes>
<outputDirectory/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.ecocode.rules;

import org.sonar.api.SonarEdition;
import org.sonar.api.SonarProduct;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarRuntime;
import org.sonar.api.utils.Version;

/**
* This class is used to reduce duplication code and to pass SonarCloud Quality Gate
* //TODO: Remove this class when repository will be split
*/
public final class Common {
private static final Version SONARQUBE_RUNTIME_VERSION = Version.create(9, 8);

/**
* Base compatibility version of plugins
*/
public static final SonarRuntime SONARQUBE_RUNTIME = new SonarRuntime() {
@Override
public Version getApiVersion() {
return SONARQUBE_RUNTIME_VERSION;
}

@Override
public SonarProduct getProduct() {
return SonarProduct.SONARQUBE;
}

@Override
public SonarQubeSide getSonarQubeSide() {
return SonarQubeSide.SCANNER;
}

@Override
public SonarEdition getEdition() {
return SonarEdition.COMMUNITY;
}
};

private Common() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.ecocode.rules;

import org.junit.jupiter.api.Test;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarProduct;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarRuntime;
import org.sonar.api.utils.Version;

import static io.ecocode.rules.Common.SONARQUBE_RUNTIME;
import static org.assertj.core.api.Assertions.assertThat;

class CommonTest {
private static final Version MINIMAL_SONARQUBE_VERSION_COMPATIBILITY = Version.create(9, 8);

@Test
void testPluginCompatibility() {
final SonarRuntime sonarRuntime = SONARQUBE_RUNTIME;

assertThat(MINIMAL_SONARQUBE_VERSION_COMPATIBILITY.isGreaterThanOrEqual(sonarRuntime.getApiVersion()))
.describedAs("Plugin must be compatible with SonarQube 9.8")
.isTrue();
assertThat(sonarRuntime.getProduct())
.describedAs("Plugin should applied to SonarQube")
.isEqualTo(SonarProduct.SONARQUBE);
assertThat(sonarRuntime.getEdition())
.describedAs("Plugin should be compatible with Community Edition")
.isEqualTo(SonarEdition.COMMUNITY);
assertThat(sonarRuntime.getSonarQubeSide())
.describedAs("Plugin should be executed by scanner")
.isEqualTo(SonarQubeSide.SCANNER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,15 @@
import fr.greencodeinitiative.java.checks.OptimizeReadFileExceptions;
import fr.greencodeinitiative.java.checks.UnnecessarilyAssignValuesToVariables;
import fr.greencodeinitiative.java.checks.UseCorrectForLoop;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarProduct;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarRuntime;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.utils.Version;
import org.sonar.plugins.java.api.JavaCheck;
import org.sonarsource.analyzer.commons.RuleMetadataLoader;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static io.ecocode.rules.Common.SONARQUBE_RUNTIME;
import static io.ecocode.rules.java.JavaRulesSpecificationsRepository.LANGUAGE;
import static io.ecocode.rules.java.JavaRulesSpecificationsRepository.NAME;
import static io.ecocode.rules.java.JavaRulesSpecificationsRepository.REPOSITORY_KEY;
Expand Down Expand Up @@ -84,28 +80,6 @@ public class JavaRulesDefinition implements RulesDefinition {
);

static final List<Class<? extends JavaCheck>> ANNOTATED_RULE_TEST_CLASSES = Collections.emptyList();
private static final Version SONARQUBE_RUNTIME_VERSION = Version.create(9, 8);
static final SonarRuntime SONARQUBE_RUNTIME = new SonarRuntime() {
@Override
public Version getApiVersion() {
return SONARQUBE_RUNTIME_VERSION;
}

@Override
public SonarProduct getProduct() {
return SonarProduct.SONARQUBE;
}

@Override
public SonarQubeSide getSonarQubeSide() {
return SonarQubeSide.SCANNER;
}

@Override
public SonarEdition getEdition() {
return SonarEdition.COMMUNITY;
}
};

@Override
public void define(Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import static fr.greencodeinitiative.java.JavaRulesDefinition.ANNOTATED_RULE_CLASSES;
import static fr.greencodeinitiative.java.JavaRulesDefinition.ANNOTATED_RULE_TEST_CLASSES;
import static fr.greencodeinitiative.java.JavaRulesDefinition.SONARQUBE_RUNTIME;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

Expand Down Expand Up @@ -104,21 +103,4 @@ private static void assertAllRuleParametersHaveDescription(Repository repository
}
}
}

@Test
void testPluginCompatibility() {
SonarRuntime sonarRuntime = SONARQUBE_RUNTIME;
assertThat(MINIMAL_SONARQUBE_VERSION_COMPATIBILITY.isGreaterThanOrEqual(sonarRuntime.getApiVersion()))
.describedAs("Plugin must be compatible with SonarQube 9.8")
.isTrue();
assertThat(sonarRuntime.getProduct())
.describedAs("Plugin should applied to SonarQube")
.isEqualTo(SonarProduct.SONARQUBE);
assertThat(sonarRuntime.getEdition())
.describedAs("Plugin should be compatible with Community Edition")
.isEqualTo(SonarEdition.COMMUNITY);
assertThat(sonarRuntime.getSonarQubeSide())
.describedAs("Plugin should be executed by scanner")
.isEqualTo(SonarQubeSide.SCANNER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,13 @@

import java.util.List;

import static io.ecocode.rules.Common.SONARQUBE_RUNTIME;
import static io.ecocode.rules.php.PhpRulesSpecificationsRepository.LANGUAGE;
import static io.ecocode.rules.php.PhpRulesSpecificationsRepository.NAME;
import static io.ecocode.rules.php.PhpRulesSpecificationsRepository.REPOSITORY_KEY;
import static io.ecocode.rules.php.PhpRulesSpecificationsRepository.RESOURCE_BASE_PATH;

public class PhpRuleRepository implements RulesDefinition, PHPCustomRuleRepository {
private static final Version SONARQUBE_RUNTIME_VERSION = Version.create(9, 8);
static final SonarRuntime SONARQUBE_RUNTIME = new SonarRuntime() {
@Override
public Version getApiVersion() {
return SONARQUBE_RUNTIME_VERSION;
}

@Override
public SonarProduct getProduct() {
return SonarProduct.SONARQUBE;
}

@Override
public SonarQubeSide getSonarQubeSide() {
return SonarQubeSide.SCANNER;
}

@Override
public SonarEdition getEdition() {
return SonarEdition.COMMUNITY;
}
};

@Override
public void define(Context context) {
NewRepository repository = context.createRepository(REPOSITORY_KEY, LANGUAGE).setName(NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.sonar.api.Plugin;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarProduct;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarRuntime;
import org.sonar.api.internal.PluginContextImpl;
import org.sonar.api.utils.Version;

import static fr.greencodeinitiative.php.PhpRuleRepository.SONARQUBE_RUNTIME;
import static io.ecocode.rules.Common.SONARQUBE_RUNTIME;
import static org.assertj.core.api.Assertions.assertThat;

class PhpPluginTest {
Expand All @@ -47,21 +43,4 @@ void test() {
new PHPPlugin().define(context);
assertThat(context.getExtensions()).hasSize(1);
}

@Test
void testPluginCompatibility() {
SonarRuntime sonarRuntime = context.getRuntime();
assertThat(MINIMAL_SONARQUBE_VERSION_COMPATIBILITY.isGreaterThanOrEqual(sonarRuntime.getApiVersion()))
.describedAs("Plugin must be compatible with SonarQube 9.8")
.isTrue();
assertThat(sonarRuntime.getProduct())
.describedAs("Plugin should applied to SonarQube")
.isEqualTo(SonarProduct.SONARQUBE);
assertThat(sonarRuntime.getEdition())
.describedAs("Plugin should be compatible with Community Edition")
.isEqualTo(SonarEdition.COMMUNITY);
assertThat(sonarRuntime.getSonarQubeSide())
.describedAs("Plugin should be executed by scanner")
.isEqualTo(SonarQubeSide.SCANNER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,20 @@
import fr.greencodeinitiative.python.checks.AvoidUnoptimizedVectorImagesCheck;
import fr.greencodeinitiative.python.checks.DetectUnoptimizedImageFormat;
import fr.greencodeinitiative.python.checks.NoFunctionCallWhenDeclaringForLoop;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarProduct;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarRuntime;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.utils.Version;
import org.sonar.plugins.python.api.PythonCustomRuleRepository;
import org.sonarsource.analyzer.commons.RuleMetadataLoader;

import java.util.Arrays;
import java.util.List;

import static io.ecocode.rules.Common.SONARQUBE_RUNTIME;
import static io.ecocode.rules.python.PythonRulesSpecificationsRepository.LANGUAGE;
import static io.ecocode.rules.python.PythonRulesSpecificationsRepository.NAME;
import static io.ecocode.rules.python.PythonRulesSpecificationsRepository.REPOSITORY_KEY;
import static io.ecocode.rules.python.PythonRulesSpecificationsRepository.RESOURCE_BASE_PATH;

public class PythonRuleRepository implements RulesDefinition, PythonCustomRuleRepository {
private static final Version SONARQUBE_RUNTIME_VERSION = Version.create(9, 8);
static final SonarRuntime SONARQUBE_RUNTIME = new SonarRuntime() {
@Override
public Version getApiVersion() {
return SONARQUBE_RUNTIME_VERSION;
}

@Override
public SonarProduct getProduct() {
return SonarProduct.SONARQUBE;
}

@Override
public SonarQubeSide getSonarQubeSide() {
return SonarQubeSide.SCANNER;
}

@Override
public SonarEdition getEdition() {
return SonarEdition.COMMUNITY;
}
};

@Override
public void define(Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,15 @@
*/
package fr.greencodeinitiative.python;

import static fr.greencodeinitiative.python.PythonRuleRepository.SONARQUBE_RUNTIME;
import static org.assertj.core.api.Assertions.assertThat;

import io.ecocode.rules.python.PythonRulesSpecificationsRepository;
import org.assertj.core.api.SoftAssertions;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarProduct;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarRuntime;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.utils.Version;

public class PythonRuleRepositoryTest {
private static final Version MINIMAL_SONARQUBE_VERSION_COMPATIBILITY = Version.create(9, 8);
import static org.assertj.core.api.Assertions.assertThat;

public class PythonRuleRepositoryTest {
private PythonRuleRepository pythonRuleRepository;
private RulesDefinition.Context context;
private RulesDefinition.Repository repository;
Expand Down Expand Up @@ -80,23 +72,5 @@ public void testAllRuleParametersHaveDescription() {
.flatMap(repository -> repository.rules().stream())
.flatMap(rule -> rule.params().stream())
.forEach(param -> assertThat(param.description()).as("description for " + param.key()).isNotEmpty());
;
}

@Test
public void testPluginCompatibility() {
SonarRuntime sonarRuntime = SONARQUBE_RUNTIME;
assertThat(MINIMAL_SONARQUBE_VERSION_COMPATIBILITY.isGreaterThanOrEqual(sonarRuntime.getApiVersion()))
.describedAs("Plugin must be compatible with SonarQube 9.8")
.isTrue();
assertThat(sonarRuntime.getProduct())
.describedAs("Plugin should applied to SonarQube")
.isEqualTo(SonarProduct.SONARQUBE);
assertThat(sonarRuntime.getEdition())
.describedAs("Plugin should be compatible with Community Edition")
.isEqualTo(SonarEdition.COMMUNITY);
assertThat(sonarRuntime.getSonarQubeSide())
.describedAs("Plugin should be executed by scanner")
.isEqualTo(SonarQubeSide.SCANNER);
}
}

0 comments on commit 36268bf

Please sign in to comment.