Skip to content

Commit

Permalink
Migrate jacoco coverrage to module
Browse files Browse the repository at this point in the history
  • Loading branch information
DamnClin committed Jul 13, 2022
1 parent cd4527e commit 751e45c
Show file tree
Hide file tree
Showing 21 changed files with 361 additions and 274 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ private GeneratorAction() {}
public static final String SPRINGBOOT_UNDERTOW = "springboot-undertow";
public static final String SPRINGBOOT_ACTUATOR = "springboot-actuator";

public static final String JACOCO_CHECK_MIN_COVERAGE = "jacoco-check-min-coverage";

public static final String REACT_CYPRESS = "react-cypress";

public static final String SPRINGBOOT_KAFKA = "springboot-kafka";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package tech.jhipster.lite.generator.server.javatool.jacoco.application;

import org.springframework.stereotype.Component;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.server.javatool.jacoco.domain.JacocoService;
import tech.jhipster.lite.generator.server.javatool.jacoco.domain.JacocoThresholdModuleFactory;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

@Component
public class JacocoApplicationService {

private final JacocoService jacocoService;
private final JacocoThresholdModuleFactory factory;

public JacocoApplicationService(JacocoService jacocoService) {
this.jacocoService = jacocoService;
public JacocoApplicationService() {
factory = new JacocoThresholdModuleFactory();
}

public void addCheckMinimumCoverage(Project project) {
jacocoService.addCheckMinimumCoverage(project);
public JHipsterModule buildModule(JHipsterModuleProperties properties) {
return factory.buildModule(properties);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package tech.jhipster.lite.generator.server.javatool.jacoco.domain;

import static tech.jhipster.lite.module.domain.JHipsterModule.*;

import tech.jhipster.lite.error.domain.Assert;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

public class JacocoThresholdModuleFactory {

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
Assert.notNull("properties", properties);

return moduleBuilder(properties)
.mandatoryReplacements()
.in("pom.xml")
.add(
lineAfterRegex(
"<outputDirectory>target\\/jacoco\\/<\\/outputDirectory>[\n\r]*\\s*<\\/configuration>[\n\r]*\\s*<\\/execution>\\s*$"
),
"""
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<dataFile>target/jacoco/allTest.exec</dataFile>
<rules>
<rule>
<element>CLASS</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>1.00</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>1.00</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>\
"""
)
.and()
.and()
.build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tech.jhipster.lite.generator.server.javatool.jacoco.infrastructure.primary;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.jhipster.lite.generator.server.javatool.jacoco.application.JacocoApplicationService;
import tech.jhipster.lite.module.infrastructure.primary.JHipsterModuleApiDoc;
import tech.jhipster.lite.module.infrastructure.primary.JHipsterModuleResource;

@Configuration
class JacocoThresholdModuleConfiguration {

@Bean
JHipsterModuleResource jacocoModuleThreshold(JacocoApplicationService jacoco) {
return JHipsterModuleResource
.builder()
.legacyUrl("/api/servers/java/jacoco-minimum-coverage")
.slug("jacoco-check-min-coverage")
.withoutProperties()
.apiDoc(new JHipsterModuleApiDoc("Java", "Add JaCoCo configuration to check minimum coverage"))
.factory(jacoco::buildModule);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import tech.jhipster.lite.module.domain.replacement.JHipsterModuleMandatoryReplacements.JHipsterModuleMandatoryReplacementsBuilder;
import tech.jhipster.lite.module.domain.replacement.JHipsterModuleOptionalReplacements;
import tech.jhipster.lite.module.domain.replacement.JHipsterModuleOptionalReplacements.JHipsterModuleOptionalReplacementsBuilder;
import tech.jhipster.lite.module.domain.replacement.RegexNeedleAfterReplacer;
import tech.jhipster.lite.module.domain.replacement.RegexNeedleBeforeReplacer;
import tech.jhipster.lite.module.domain.replacement.RegexReplacer;
import tech.jhipster.lite.module.domain.replacement.TextNeedleBeforeReplacer;
Expand Down Expand Up @@ -175,6 +176,10 @@ public static RegexNeedleBeforeReplacer lineBeforeRegex(String regex) {
return new RegexNeedleBeforeReplacer(Pattern.compile(regex, Pattern.MULTILINE));
}

public static RegexNeedleAfterReplacer lineAfterRegex(String regex) {
return new RegexNeedleAfterReplacer(Pattern.compile(regex, Pattern.MULTILINE));
}

public static PropertyKey propertyKey(String key) {
return new PropertyKey(key);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package tech.jhipster.lite.module.domain.replacement;

import java.util.function.BiFunction;
import java.util.regex.Pattern;
import tech.jhipster.lite.error.domain.Assert;
import tech.jhipster.lite.module.domain.JHipsterModule;

public record RegexNeedleAfterReplacer(Pattern pattern) implements ElementReplacer {
public RegexNeedleAfterReplacer {
Assert.notNull("pattern", pattern);
}

@Override
public boolean notMatchIn(String content) {
return !pattern().matcher(content).find();
}

@Override
public BiFunction<String, String, String> replacer() {
return (content, replacement) ->
linePattern().matcher(content).replaceAll(result -> result.group() + JHipsterModule.LINE_BREAK + replacement);
}

private Pattern linePattern() {
String stringPattern = searchMatcher();

if (isLinePattern(stringPattern)) {
return pattern();
}

return Pattern.compile(stringPattern + ".*$", pattern().flags());
}

private boolean isLinePattern(String stringPattern) {
return stringPattern.endsWith("$");
}

@Override
public String searchMatcher() {
return pattern().pattern();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private Pattern linePattern() {
return pattern();
}

return Pattern.compile("^.*" + stringPattern, Pattern.MULTILINE);
return Pattern.compile("^.*" + stringPattern, pattern().flags());
}

private boolean isLinePattern(String stringPattern) {
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/features/aop-logging.feature
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Feature: AOP logging

Scenario: Should apply AOP logging module
When I apply "aop-logging" module to default project with maven file
When I apply "aop-logging" module to default project with maven file
| packageName | tech.jhipster.chips |
| baseName | jhipster |
Then I should have files in "src/main/java/tech/jhipster/chips/technical/infrastructure/secondary/aop/logging"
Expand Down
5 changes: 5 additions & 0 deletions src/test/features/jacoco-threshold.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Feature: Jacoco threshold module

Scenario: Should apply jacoco threshol module
When I apply "jacoco-check-min-coverage" module to default project with maven file without properties
Then I should have "<value>COVEREDRATIO</value>" in "pom.xml"
11 changes: 8 additions & 3 deletions src/test/java/tech/jhipster/lite/generator/ModulesSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public void applyModuleForLastProject(String moduleSlug) {
post(applyModuleUrl(moduleSlug), buildModuleQuery(lastProjectFolder(), null));
}

@When("I apply {string} module to default project with maven file without properties")
public void applyModuleForDefaultProjectWithMavenFileWithoutProperties(String moduleSlug) {
applyModuleForDefaultProjectWithMavenFile(moduleSlug, null);
}

@When("I apply {string} module to default project with maven file")
public void applyModuleForDefaultProjectWithMavenFile(String moduleSlug, Map<String, Object> properties) {
String projectFolder = newTestFolder();
Expand Down Expand Up @@ -151,7 +156,7 @@ private static void addPackageJsonToProject(String folder) {
}

private static void addPomToProject(String folder) {
addFileToProject(folder, "src/test/resources/projects/maven/pom.xml", "pom.xml");
addFileToProject(folder, "src/test/resources/projects/init-maven/pom.xml", "pom.xml");
}

private static void addFileToProject(String folder, String source, String destination) {
Expand All @@ -162,9 +167,9 @@ private static void addFileToProject(String folder, String source, String destin
throw new AssertionError(e);
}

Path pomPath = folderPath.resolve(destination);
Path filePath = folderPath.resolve(destination);
try {
Files.copy(Paths.get(source), pomPath);
Files.copy(Paths.get(source), filePath);
} catch (IOException e) {
throw new AssertionError(e);
}
Expand Down

This file was deleted.

Loading

0 comments on commit 751e45c

Please sign in to comment.