Skip to content

Commit

Permalink
#408 allow to enable/disable of BaselineManager
Browse files Browse the repository at this point in the history
  • Loading branch information
DirkMahler committed Jul 11, 2024
1 parent ecbabbe commit a9dc709
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.buschmais.jqassistant.core.analysis.api.baseline.BaselineManager;
import com.buschmais.jqassistant.core.analysis.api.baseline.BaselineRepository;
import com.buschmais.jqassistant.core.analysis.api.configuration.Analyze;
import com.buschmais.jqassistant.core.analysis.api.configuration.Baseline;
import com.buschmais.jqassistant.core.analysis.impl.AnalyzerImpl;
import com.buschmais.jqassistant.core.analysis.spi.AnalyzerPluginRepository;
import com.buschmais.jqassistant.core.report.api.ReportContext;
Expand Down Expand Up @@ -51,8 +52,9 @@ public void run(CliConfiguration configuration, Options options) throws CliExecu
.report(), reportContext);
InMemoryReportPlugin inMemoryReportPlugin = new InMemoryReportPlugin(new CompositeReportPlugin(reportPlugins));
try {
BaselineRepository baselineRepository = new BaselineRepository(analyze.baseline(), getRulesDirectory(analyze.rule()));
BaselineManager baselineManager = new BaselineManager(analyze.baseline(), baselineRepository.read());
Baseline baselineConfiguration = analyze.baseline();
BaselineRepository baselineRepository = new BaselineRepository(baselineConfiguration, getRulesDirectory(analyze.rule()));
BaselineManager baselineManager = new BaselineManager(baselineConfiguration, baselineRepository);
Analyzer analyzer = new AnalyzerImpl(analyze, pluginRepository.getClassLoader(), store, pluginRepository.getAnalyzerPluginRepository()
.getRuleInterpreterPlugins(emptyMap()), baselineManager, inMemoryReportPlugin);
RuleSet availableRules = getAvailableRules(analyze
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,32 @@ public class BaselineManager {

private final com.buschmais.jqassistant.core.analysis.api.configuration.Baseline configuration;

private final Optional<Baseline> optionalOldBaseline;
private final BaselineRepository baselineRepository;

private final Baseline newBaseline = new Baseline();
private Optional<Baseline> optionalOldBaseline = null;

private Baseline newBaseline = null;

public void start() {
if (configuration.enabled()) {
optionalOldBaseline = baselineRepository.read();
newBaseline = new Baseline();
}
}

public void stop() {
if (configuration.enabled()) {
baselineRepository.write(newBaseline);
}
}

public boolean isNew(ExecutableRule<?> executableRule, Row row) {
if (!configuration.enabled()) {
return true;
}
if (optionalOldBaseline == null) {
throw new IllegalStateException("Baseline manager has not been started yet");
}
String ruleId = executableRule.getId();
String rowKey = row.getKey();
Map<String, Column<?>> columns = row.getColumns();
Expand Down Expand Up @@ -75,7 +96,4 @@ private void add(String constraintId, String rowKey, Map<String, Column<?>> colu
.put(rowKey, row);
}

public Baseline getNewBaseline() {
return newBaseline;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Stream;

import com.buschmais.jqassistant.core.report.api.model.Column;
Expand All @@ -11,16 +12,21 @@
import com.buschmais.jqassistant.core.rule.api.model.Constraint;
import com.buschmais.jqassistant.core.rule.api.model.ExecutableRule;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class BaselineManagerTest {
Expand All @@ -36,44 +42,87 @@ private static Stream<Arguments> rules() {
@Mock
private com.buschmais.jqassistant.core.analysis.api.configuration.Baseline configuration;

@Mock
private BaselineRepository baselineRepository;

@Captor
private ArgumentCaptor<Baseline> baselineArgumentCaptor;

private BaselineManager baselineManager;

@BeforeEach
void setUp() {
this.baselineManager = new BaselineManager(configuration, baselineRepository);
}

@ParameterizedTest
@MethodSource("rules")
void baselineDisabled(ExecutableRule<?> executableRule) {
doReturn(false).when(configuration)
.enabled();
Row row = Row.builder()
.key("1")
.columns(Map.of("c1", Column.builder()
.label("1")
.build()))
.build();

baselineManager.start();
assertThat(baselineManager.isNew(executableRule, row)).isTrue();
baselineManager.stop();

verify(baselineRepository, never()).read();
verify(baselineRepository, never()).write(any(Baseline.class));
}

@ParameterizedTest
@MethodSource("rules")
void noBaselineWithNewConstraintViolation(ExecutableRule<?> executableRule) {
doReturn(true).when(configuration)
.enabled();
doReturn(empty()).when(baselineRepository)
.read();
Row row = Row.builder()
.key("1")
.columns(Map.of("c1", Column.builder()
.label("1")
.build()))
.build();
BaselineManager baselineManager = new BaselineManager(configuration,empty());

baselineManager.start();
assertThat(baselineManager.isNew(executableRule, row)).isTrue();
baselineManager.stop();

verifyNewBaseline(executableRule, baselineManager.getNewBaseline()
.getConstraints(), "1");
verifyNewBaseline(executableRule, baseline -> baseline.getConstraints(), "1");
}

@ParameterizedTest
@MethodSource("rules")
void existingBaselineWithExistingConstraintViolation(ExecutableRule<?> executableRule) {
doReturn(true).when(configuration)
.enabled();
Baseline oldBaseline = createOldBaseline(executableRule, "1");
doReturn(of(oldBaseline)).when(baselineRepository)
.read();
Row row = Row.builder()
.key("1")
.columns(Map.of("c1", Column.builder()
.label("1")
.build()))
.build();
BaselineManager baselineManager = new BaselineManager(configuration, of(oldBaseline));

baselineManager.start();
assertThat(baselineManager.isNew(executableRule, row)).isFalse();
baselineManager.stop();

verifyNewBaseline(executableRule, baselineManager.getNewBaseline()
.getConstraints(), "1");
verifyNewBaseline(executableRule, baseline -> baseline.getConstraints(), "1");
}

@ParameterizedTest
@MethodSource("rules")
void existingBaselineWithNewConstraintViolation(ExecutableRule<?> executableRule) {
doReturn(true).when(configuration)
.enabled();
Baseline oldBaseline = createOldBaseline(executableRule, "1");
Row oldRow = Row.builder()
.key("1")
Expand All @@ -87,31 +136,37 @@ void existingBaselineWithNewConstraintViolation(ExecutableRule<?> executableRule
.label("2")
.build()))
.build();
BaselineManager baselineManager = new BaselineManager(configuration, of(oldBaseline));
doReturn(of(oldBaseline)).when(baselineRepository)
.read();

baselineManager.start();
assertThat(baselineManager.isNew(executableRule, oldRow)).isFalse();
assertThat(baselineManager.isNew(executableRule, newRow)).isTrue();
baselineManager.stop();

verifyNewBaseline(executableRule, baselineManager.getNewBaseline()
.getConstraints(), "1");
verifyNewBaseline(executableRule, baseline -> baseline.getConstraints(), "1");
}

@ParameterizedTest
@MethodSource("rules")
void existingBaselineWithRemovedConstraintViolation(ExecutableRule<?> executableRule) {
doReturn(true).when(configuration)
.enabled();
Baseline oldBaseline = createOldBaseline(executableRule, "1", "2");
Row row = Row.builder()
.key("1")
.columns(Map.of("c1", Column.builder()
.label("1")
.build()))
.build();
BaselineManager baselineManager = new BaselineManager(configuration, of(oldBaseline));
doReturn(of(oldBaseline)).when(baselineRepository)
.read();

baselineManager.start();
assertThat(baselineManager.isNew(executableRule, row)).isFalse();
baselineManager.stop();

verifyNewBaseline(executableRule, baselineManager.getNewBaseline()
.getConstraints(), "1");
verifyNewBaseline(executableRule, baseline -> baseline.getConstraints(), "1");
}

private static Baseline createOldBaseline(ExecutableRule<?> rule, String... rowKeys) {
Expand All @@ -128,10 +183,14 @@ private static Baseline createOldBaseline(ExecutableRule<?> rule, String... rowK
return oldBaseline;
}

private static void verifyNewBaseline(ExecutableRule<?> executableRule, SortedMap<String, Baseline.RuleBaseline> rulebaseLines, String... expectedRowKeys) {
assertThat(rulebaseLines).hasSize(1)
private void verifyNewBaseline(ExecutableRule<?> executableRule, Function<Baseline, SortedMap<String, Baseline.RuleBaseline>> rulebaseLinesFunction,
String... expectedRowKeys) {
verify(baselineRepository).write(baselineArgumentCaptor.capture());
Baseline newBaseline = baselineArgumentCaptor.getValue();
SortedMap<String, Baseline.RuleBaseline> ruleBaselines = rulebaseLinesFunction.apply(newBaseline);
assertThat(ruleBaselines).hasSize(1)
.containsKey(executableRule.getId());
Baseline.RuleBaseline ruleBaseline = rulebaseLines.get(executableRule.getId());
Baseline.RuleBaseline ruleBaseline = ruleBaselines.get(executableRule.getId());
assertThat(ruleBaseline).isNotNull();
SortedMap<String, SortedMap<String, String>> rows = ruleBaseline.getRows();
assertThat(rows).hasSize(expectedRowKeys.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.buschmais.jqassistant.core.analysis.api.baseline.BaselineManager;
import com.buschmais.jqassistant.core.analysis.api.baseline.BaselineRepository;
import com.buschmais.jqassistant.core.analysis.api.configuration.Analyze;
import com.buschmais.jqassistant.core.analysis.api.configuration.Baseline;
import com.buschmais.jqassistant.core.analysis.impl.AnalyzerImpl;
import com.buschmais.jqassistant.core.report.api.ReportContext;
import com.buschmais.jqassistant.core.report.api.ReportPlugin;
Expand Down Expand Up @@ -266,10 +267,10 @@ protected Scanner getScanner(Map<String, Object> properties) {
private Analyzer getAnalyzer(Map<String, String> parameters) throws RuleException {
ConfigurationBuilder configurationBuilder = createConfigurationBuilder().with(Analyze.class, Analyze.RULE_PARAMETERS, parameters);
Configuration configuration = createConfiguration(configurationBuilder);
BaselineRepository baselineRepository = new BaselineRepository(configuration.analyze()
.baseline(), getRulesDirectory());
BaselineManager baselineManager = new BaselineManager(configuration.analyze()
.baseline(), baselineRepository.read());
Baseline baselineConfiguration = configuration.analyze()
.baseline();
BaselineRepository baselineRepository = new BaselineRepository(baselineConfiguration, getRulesDirectory());
BaselineManager baselineManager = new BaselineManager(baselineConfiguration, baselineRepository);
return new AnalyzerImpl(configuration.analyze(), pluginRepository.getClassLoader(), store, getRuleInterpreterPlugins(), baselineManager, reportPlugin);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.buschmais.jqassistant.core.analysis.api.baseline.BaselineManager;
import com.buschmais.jqassistant.core.analysis.api.baseline.BaselineRepository;
import com.buschmais.jqassistant.core.analysis.api.configuration.Analyze;
import com.buschmais.jqassistant.core.analysis.api.configuration.Baseline;
import com.buschmais.jqassistant.core.analysis.impl.AnalyzerImpl;
import com.buschmais.jqassistant.core.analysis.spi.AnalyzerPluginRepository;
import com.buschmais.jqassistant.core.report.api.ReportContext;
Expand Down Expand Up @@ -73,8 +74,9 @@ private void analyze(Store store, MojoExecutionContext mojoExecutionContext) thr
InMemoryReportPlugin inMemoryReportPlugin = new InMemoryReportPlugin(new CompositeReportPlugin(reportPlugins));

try {
BaselineRepository baselineRepository = new BaselineRepository(analyze.baseline(), mojoExecutionContext.getRuleDirectory());
BaselineManager baselineManager = new BaselineManager(analyze.baseline(), baselineRepository.read());
Baseline baselineConfiguration = analyze.baseline();
BaselineRepository baselineRepository = new BaselineRepository(baselineConfiguration, mojoExecutionContext.getRuleDirectory());
BaselineManager baselineManager = new BaselineManager(baselineConfiguration, baselineRepository);
Analyzer analyzer = new AnalyzerImpl(configuration.analyze(), pluginRepository.getClassLoader(), store,
analyzerPluginRepository.getRuleInterpreterPlugins(emptyMap()), baselineManager, inMemoryReportPlugin);
analyzer.execute(ruleSet, ruleSelection);
Expand Down

0 comments on commit a9dc709

Please sign in to comment.