Skip to content

Commit

Permalink
TNG#1045 : uses a function passed into constructor as a more precisel…
Browse files Browse the repository at this point in the history
…y defined

extension point of TextFileBasedViolationStore

Also marked public methods as final to limit the inheritance contract

Signed-off-by: danhaywood <dan@haywood-associates.co.uk>
  • Loading branch information
danhaywood committed Feb 5, 2023
1 parent 1ae112c commit 5a5c71e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 TNG Technology Consulting GmbH
* Copyright 2014-2023 TNG Technology Consulting GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,11 +23,13 @@
import java.util.List;
import java.util.Properties;
import java.util.UUID;
import java.util.function.Function;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.tngtech.archunit.PublicAPI;
Expand All @@ -54,13 +56,26 @@ public class TextFileBasedViolationStore implements ViolationStore {
private static final String ALLOW_STORE_UPDATE_PROPERTY_NAME = "default.allowStoreUpdate";
private static final String ALLOW_STORE_UPDATE_DEFAULT = "true";

private final Function<String, String> ruleFileNameStrategy;

private boolean storeCreationAllowed;
private boolean storeUpdateAllowed;
private File storeFolder;
private FileSyncedProperties storedRules;

public TextFileBasedViolationStore() {
this(description -> UUID.randomUUID().toString());
}

/**
* @param ruleFileNameStrategy - provides a plugin point to allow ways of deriving a rule file name to be created.
*/
public TextFileBasedViolationStore(Function<String,String> ruleFileNameStrategy) {
this.ruleFileNameStrategy = ruleFileNameStrategy;
}

@Override
public void initialize(Properties properties) {
public final void initialize(Properties properties) {
storeCreationAllowed = Boolean.parseBoolean(properties.getProperty(ALLOW_STORE_CREATION_PROPERTY_NAME, ALLOW_STORE_CREATION_DEFAULT));
storeUpdateAllowed = Boolean.parseBoolean(properties.getProperty(ALLOW_STORE_UPDATE_PROPERTY_NAME, ALLOW_STORE_UPDATE_DEFAULT));
String path = properties.getProperty(STORE_PATH_PROPERTY_NAME, STORE_PATH_DEFAULT);
Expand Down Expand Up @@ -93,12 +108,12 @@ private void checkInitialization(boolean initializationSuccessful, String messag
}

@Override
public boolean contains(ArchRule rule) {
public final boolean contains(ArchRule rule) {
return storedRules.containsKey(rule.getDescription());
}

@Override
public void save(ArchRule rule, List<String> violations) {
public final void save(ArchRule rule, List<String> violations) {
log.debug("Storing evaluated rule '{}' with {} violations: {}", rule.getDescription(), violations.size(), violations);
if (!storeUpdateAllowed) {
throw new StoreUpdateFailedException(String.format(
Expand Down Expand Up @@ -145,12 +160,9 @@ private String ensureRuleFileName(ArchRule rule) {
return ruleFileName;
}

/**
* This has <code>protected</code> visibility so that it can be more easily overridden in alternative
* implementations of {@link ViolationStore} based on this implementation.
*/
protected String newRuleFileName(String description) {
return UUID.randomUUID().toString();
@VisibleForTesting()
String newRuleFileName(String description) {
return this.ruleFileNameStrategy.apply(description);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
import java.util.List;
import java.util.Properties;

import com.google.common.collect.ImmutableList;
import com.google.common.io.Files;
import com.tngtech.archunit.lang.ArchRule;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import com.google.common.collect.ImmutableList;
import com.google.common.io.Files;
import com.tngtech.archunit.lang.ArchRule;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
Expand Down Expand Up @@ -119,6 +119,18 @@ public void stores_violations_with_line_breaks() {
assertThat(violations).as("stored violations").containsExactlyElementsOf(expected);
}

@Test
public void class_can_be_overridden_and_rule_file_name_strategy_supplied() {
class MySubclassOfTextFileBasedViolationStore extends TextFileBasedViolationStore {
public MySubclassOfTextFileBasedViolationStore() {
super(s -> s + " xxx");
}
};
TextFileBasedViolationStore store = new MySubclassOfTextFileBasedViolationStore();

assertThat(store.newRuleFileName("abc")).isEqualTo("abc xxx");
}

private Properties readProperties(File file) throws IOException {
Properties properties = new Properties();
try (FileInputStream inputStream = new FileInputStream(file)) {
Expand Down

0 comments on commit 5a5c71e

Please sign in to comment.