Skip to content

Commit

Permalink
#408 update baseline file only if the baseline actually changed
Browse files Browse the repository at this point in the history
  • Loading branch information
DirkMahler committed Jul 14, 2024
1 parent 1e120ba commit d6c2767
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.SortedMap;
import java.util.TreeMap;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

Expand All @@ -12,6 +13,7 @@
* Note that the data structures rely on {@link SortedMap}s to preserve the order of entries. This is required for creating diffs between baseline files which are generated from these structures.
*/
@Getter
@EqualsAndHashCode
@ToString
public class Baseline {

Expand All @@ -29,6 +31,7 @@ public class Baseline {
* Represent a baseline for a specific {@link com.buschmais.jqassistant.core.rule.api.model.Concept} or {@link com.buschmais.jqassistant.core.rule.api.model.Constraint}.
*/
@Getter
@EqualsAndHashCode
@ToString
public static class RuleBaseline {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.buschmais.jqassistant.core.rule.api.model.ExecutableRule;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import static java.util.Collections.emptyList;

Expand All @@ -27,6 +28,7 @@
* <p>
*/
@RequiredArgsConstructor
@Slf4j
public class BaselineManager {

private final com.buschmais.jqassistant.core.analysis.api.configuration.Baseline configuration;
Expand All @@ -45,7 +47,8 @@ public void start() {
}

public void stop() {
if (configuration.enabled()) {
if (configuration.enabled() && !(optionalOldBaseline.isPresent() && newBaseline.equals(optionalOldBaseline.get()))) {
log.info("Detected updated baseline.");
baselineRepository.write(newBaseline);
}
}
Expand Down Expand Up @@ -80,18 +83,19 @@ private Boolean isExistingResult(ExecutableRule<?> executableRule, Row row, List
Baseline.RuleBaseline oldRuleBaseline = ruleBaseline.get(ruleId);
if (oldRuleBaseline != null && oldRuleBaseline.getRows()
.containsKey(rowKey)) {
add(ruleId, rowKey, columns, rows);
addToNewBaseline(ruleId, rowKey, columns, rows);
return true;
}
return false;
})
.orElseGet(() -> {
add(ruleId, rowKey, columns, rows);
addToNewBaseline(ruleId, rowKey, columns, rows);
return false;
});
}

private void add(String constraintId, String rowKey, Map<String, Column<?>> columns, Function<Baseline, SortedMap<String, Baseline.RuleBaseline>> rows) {
private void addToNewBaseline(String constraintId, String rowKey, Map<String, Column<?>> columns,
Function<Baseline, SortedMap<String, Baseline.RuleBaseline>> rows) {
Baseline.RuleBaseline newRuleBaseline = rows.apply(newBaseline)
.computeIfAbsent(constraintId, key -> new Baseline.RuleBaseline());
TreeMap<String, String> row = new TreeMap<>();
Expand All @@ -102,5 +106,4 @@ private void add(String constraintId, String rowKey, Map<String, Column<?>> colu
newRuleBaseline.getRows()
.put(rowKey, row);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.buschmais.jqassistant.core.shared.xml.JAXBHelper;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jqassistant.schema.baseline.v2.ColumnType;
import org.jqassistant.schema.baseline.v2.JqassistantBaseline;
import org.jqassistant.schema.baseline.v2.RowType;
Expand All @@ -15,6 +16,7 @@
import static java.util.Optional.of;

@RequiredArgsConstructor
@Slf4j
public class BaselineRepository {

private static final JAXBHelper<JqassistantBaseline> JAXB_HELPER = new JAXBHelper<>(JqassistantBaseline.class);
Expand All @@ -26,13 +28,17 @@ public class BaselineRepository {
public Optional<Baseline> read() {
File baselineFile = getFile();
if (baselineFile.exists()) {
log.info("Reading baseline from file '{}'.", baselineFile);
return of(read(baselineFile));
}
log.info("Baseline file '{}' does not exist yet.", baselineFile);
return empty();
}

public void write(Baseline baseline) {
write(baseline, getFile());
File baselineFile = getFile();
log.info("Writing baseline to file '{}'.", baselineFile);
write(baseline, baselineFile);
}

private File getFile() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void existingBaselineWithExistingConstraintViolation(ExecutableRule<?> executabl
assertThat(baselineManager.isExisting(executableRule, row)).isTrue();
baselineManager.stop();

verifyNewBaseline(executableRule, "1");
verify(baselineRepository, never()).write(any(Baseline.class));
}

@ParameterizedTest
Expand Down Expand Up @@ -141,7 +141,7 @@ void existingBaselineWithNewConstraintViolation(ExecutableRule<?> executableRule
assertThat(baselineManager.isExisting(executableRule, newRow)).isFalse();
baselineManager.stop();

verifyNewBaseline(executableRule, "1");
verify(baselineRepository, never()).write(any(Baseline.class));
}

@ParameterizedTest
Expand Down

0 comments on commit d6c2767

Please sign in to comment.