diff --git a/src/main/java/pl/touk/sputnik/Main.java b/src/main/java/pl/touk/sputnik/Main.java index fbafb45c..b6a215b1 100644 --- a/src/main/java/pl/touk/sputnik/Main.java +++ b/src/main/java/pl/touk/sputnik/Main.java @@ -29,20 +29,19 @@ public static void main(String[] args) { System.exit(1); } - ConfigurationHolder.initFromFile(commandLine.getOptionValue(CliOption.CONF.getCommandLineParam())); - Configuration configuration = ConfigurationHolder.instance(); + Configuration configuration = ConfigurationBuilder.initFromFile(commandLine.getOptionValue(CliOption.CONF.getCommandLineParam())); configuration.updateWithCliOptions(commandLine); ConnectorFacade facade = getConnectorFacade(configuration); - new Engine(facade).run(); + new Engine(facade, configuration).run(); } private static ConnectorFacade getConnectorFacade(Configuration configuration) { ConnectorType connectorType = ConnectorType.getValidConnectorType(configuration.getProperty(GeneralOption.CONNECTOR_TYPE)); ConnectorFacade facade = null; try { - facade = ConnectorFacadeFactory.INSTANCE.build(connectorType); - facade.validate(ConfigurationHolder.instance()); + facade = ConnectorFacadeFactory.INSTANCE.build(connectorType, configuration); + facade.validate(configuration); } catch (GeneralOptionNotSupportedException e) { System.out.println(e.getMessage()); System.exit(2); diff --git a/src/main/java/pl/touk/sputnik/configuration/ConfigurationHolder.java b/src/main/java/pl/touk/sputnik/configuration/ConfigurationBuilder.java similarity index 71% rename from src/main/java/pl/touk/sputnik/configuration/ConfigurationHolder.java rename to src/main/java/pl/touk/sputnik/configuration/ConfigurationBuilder.java index adeecf8b..ccfa34e6 100644 --- a/src/main/java/pl/touk/sputnik/configuration/ConfigurationHolder.java +++ b/src/main/java/pl/touk/sputnik/configuration/ConfigurationBuilder.java @@ -15,25 +15,23 @@ import com.google.common.io.Resources; @Slf4j -public class ConfigurationHolder { +public class ConfigurationBuilder { - private static Configuration INSTANCE; - - public static void initFromFile(String configurationFilename) { + public static Configuration initFromFile(String configurationFilename) { notBlank(configurationFilename, "You need to provide filename with configuration properties"); log.info("Initializing configuration properties from file {}", configurationFilename); try (FileReader resourceReader = new FileReader(configurationFilename)){ Properties properties = new Properties(); properties.load(resourceReader); - initFromProperties(properties); + return initFromProperties(properties); } catch (IOException e) { log.error("Configuration initialization failed", e); throw new RuntimeException("Configuration file " + configurationFilename + " cannot be loaded"); } } - public static void initFromResource(String configurationResource) { + public static Configuration initFromResource(String configurationResource) { notBlank(configurationResource, "You need to provide url with configuration properties"); log.info("Initializing configuration properties from url {}", configurationResource); @@ -41,27 +39,16 @@ public static void initFromResource(String configurationResource) { try (Reader resourceStream = charSource.openStream()) { Properties properties = new Properties(); properties.load(resourceStream); - initFromProperties(properties); + return initFromProperties(properties); } catch (IOException e) { log.error("Configuration initialization failed", e); throw new RuntimeException(e); } } - public static void initFromProperties(Properties properties) { - setInstance(new Configuration(properties)); - } - - public static void reset() { - INSTANCE = null; + public static Configuration initFromProperties(Properties properties) { + return new Configuration(properties); } - public static Configuration instance() { - return INSTANCE; - } - - private static void setInstance(Configuration instance) { - INSTANCE = instance; - } } diff --git a/src/main/java/pl/touk/sputnik/connector/ConnectorDetails.java b/src/main/java/pl/touk/sputnik/connector/ConnectorDetails.java index 891c2df5..f104877f 100644 --- a/src/main/java/pl/touk/sputnik/connector/ConnectorDetails.java +++ b/src/main/java/pl/touk/sputnik/connector/ConnectorDetails.java @@ -1,7 +1,7 @@ package pl.touk.sputnik.connector; import lombok.Getter; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.GeneralOption; import static org.apache.commons.lang3.Validate.notBlank; @@ -17,18 +17,18 @@ public class ConnectorDetails { private String useHttps; private boolean isHttps; - public ConnectorDetails() { - buildFromConfiguration(); + public ConnectorDetails(Configuration configuration) { + buildFromConfiguration(configuration); validate(); } - private void buildFromConfiguration() { - host = ConfigurationHolder.instance().getProperty(GeneralOption.HOST); - port = Integer.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.PORT)); - path = ConfigurationHolder.instance().getProperty(GeneralOption.PATH); - username = ConfigurationHolder.instance().getProperty(GeneralOption.USERNAME); - password = ConfigurationHolder.instance().getProperty(GeneralOption.PASSWORD); - useHttps = ConfigurationHolder.instance().getProperty(GeneralOption.USE_HTTPS); + private void buildFromConfiguration(Configuration configuration) { + host = configuration.getProperty(GeneralOption.HOST); + port = Integer.valueOf(configuration.getProperty(GeneralOption.PORT)); + path = configuration.getProperty(GeneralOption.PATH); + username = configuration.getProperty(GeneralOption.USERNAME); + password = configuration.getProperty(GeneralOption.PASSWORD); + useHttps = configuration.getProperty(GeneralOption.USE_HTTPS); isHttps = Boolean.parseBoolean(useHttps); } diff --git a/src/main/java/pl/touk/sputnik/connector/ConnectorFacadeFactory.java b/src/main/java/pl/touk/sputnik/connector/ConnectorFacadeFactory.java index 2cb94c3b..8017806d 100644 --- a/src/main/java/pl/touk/sputnik/connector/ConnectorFacadeFactory.java +++ b/src/main/java/pl/touk/sputnik/connector/ConnectorFacadeFactory.java @@ -2,6 +2,7 @@ import org.jetbrains.annotations.NotNull; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.GeneralOptionNotSupportedException; import pl.touk.sputnik.connector.gerrit.GerritFacadeBuilder; import pl.touk.sputnik.connector.stash.StashFacadeBuilder; @@ -13,12 +14,12 @@ public class ConnectorFacadeFactory { StashFacadeBuilder stashFacadeBuilder = new StashFacadeBuilder(); @NotNull - public ConnectorFacade build(@NotNull ConnectorType type) { + public ConnectorFacade build(@NotNull ConnectorType type, Configuration configuration) { switch (type) { case STASH: - return stashFacadeBuilder.build(); + return stashFacadeBuilder.build(configuration); case GERRIT: - return gerritFacadeBuilder.build(); + return gerritFacadeBuilder.build(configuration); default: throw new GeneralOptionNotSupportedException("Connector " + type.getName() + " is not supported"); } diff --git a/src/main/java/pl/touk/sputnik/connector/gerrit/GerritFacadeBuilder.java b/src/main/java/pl/touk/sputnik/connector/gerrit/GerritFacadeBuilder.java index 95ac9381..6664c68e 100644 --- a/src/main/java/pl/touk/sputnik/connector/gerrit/GerritFacadeBuilder.java +++ b/src/main/java/pl/touk/sputnik/connector/gerrit/GerritFacadeBuilder.java @@ -8,7 +8,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.http.impl.client.HttpClientBuilder; import org.jetbrains.annotations.NotNull; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.CliOption; import pl.touk.sputnik.connector.ConnectorDetails; import pl.touk.sputnik.connector.http.HttpHelper; @@ -21,9 +21,9 @@ public class GerritFacadeBuilder { private HttpHelper httpHelper = new HttpHelper(); @NotNull - public GerritFacade build() { - ConnectorDetails connectorDetails = new ConnectorDetails(); - GerritPatchset gerritPatchset = buildGerritPatchset(); + public GerritFacade build(Configuration configuration) { + ConnectorDetails connectorDetails = new ConnectorDetails(configuration); + GerritPatchset gerritPatchset = buildGerritPatchset(configuration); GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory(); String hostUri = httpHelper.buildHttpHostUri(connectorDetails); @@ -46,9 +46,9 @@ public HttpClientBuilder extend(HttpClientBuilder httpClientBuilder, GerritAuthD } @NotNull - private GerritPatchset buildGerritPatchset() { - String changeId = ConfigurationHolder.instance().getProperty(CliOption.CHANGE_ID); - String revisionId = ConfigurationHolder.instance().getProperty(CliOption.REVISION_ID); + private GerritPatchset buildGerritPatchset(Configuration configuration) { + String changeId = configuration.getProperty(CliOption.CHANGE_ID); + String revisionId = configuration.getProperty(CliOption.REVISION_ID); notBlank(changeId, "You must provide non blank Gerrit change Id"); notBlank(revisionId, "You must provide non blank Gerrit revision Id"); diff --git a/src/main/java/pl/touk/sputnik/connector/stash/StashFacade.java b/src/main/java/pl/touk/sputnik/connector/stash/StashFacade.java index cde6e0a0..c425125a 100644 --- a/src/main/java/pl/touk/sputnik/connector/stash/StashFacade.java +++ b/src/main/java/pl/touk/sputnik/connector/stash/StashFacade.java @@ -8,7 +8,6 @@ import net.minidev.json.JSONObject; import org.jetbrains.annotations.NotNull; import pl.touk.sputnik.configuration.Configuration; -import pl.touk.sputnik.configuration.ConfigurationHolder; import pl.touk.sputnik.configuration.GeneralOption; import pl.touk.sputnik.configuration.GeneralOptionNotSupportedException; import pl.touk.sputnik.connector.ConnectorFacade; @@ -32,9 +31,11 @@ public class StashFacade implements ConnectorFacade { private StashConnector stashConnector; private ObjectMapper objectMapper = new ObjectMapper(); + private final Configuration configuration; - public StashFacade(@NotNull StashConnector stashConnector) { + public StashFacade(@NotNull StashConnector stashConnector, Configuration configuration) { this.stashConnector = stashConnector; + this.configuration = configuration; } @Override @@ -84,7 +85,7 @@ public void setReview(@NotNull Review review) { } private void sendFileComments(Review review) { - boolean commentOnlyChangedLines = Boolean.parseBoolean(ConfigurationHolder.instance().getProperty(GeneralOption.COMMENT_ONLY_CHANGED_LINES)); + boolean commentOnlyChangedLines = Boolean.parseBoolean(configuration.getProperty(GeneralOption.COMMENT_ONLY_CHANGED_LINES)); for (ReviewFile reviewFile : review.getFiles()) { SingleFileChanges changes = changesForSingleFile(reviewFile.getReviewFilename()); diff --git a/src/main/java/pl/touk/sputnik/connector/stash/StashFacadeBuilder.java b/src/main/java/pl/touk/sputnik/connector/stash/StashFacadeBuilder.java index 56a6bcd9..2045e707 100644 --- a/src/main/java/pl/touk/sputnik/connector/stash/StashFacadeBuilder.java +++ b/src/main/java/pl/touk/sputnik/connector/stash/StashFacadeBuilder.java @@ -6,7 +6,7 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.jetbrains.annotations.NotNull; import pl.touk.sputnik.configuration.CliOption; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.GeneralOption; import pl.touk.sputnik.connector.ConnectorDetails; import pl.touk.sputnik.connector.http.HttpConnector; @@ -19,22 +19,22 @@ public class StashFacadeBuilder { private HttpHelper httpHelper = new HttpHelper(); @NotNull - public StashFacade build() { - ConnectorDetails connectorDetails = new ConnectorDetails(); - StashPatchset stashPatchset = buildStashPatchset(); + public StashFacade build(Configuration configuration) { + ConnectorDetails connectorDetails = new ConnectorDetails(configuration); + StashPatchset stashPatchset = buildStashPatchset(configuration); HttpHost httpHost = httpHelper.buildHttpHost(connectorDetails); HttpClientContext httpClientContext = httpHelper.buildClientContext(httpHost, new BasicScheme()); CloseableHttpClient closeableHttpClient = httpHelper.buildClient(httpHost, connectorDetails); - return new StashFacade(new StashConnector(new HttpConnector(closeableHttpClient, httpClientContext, connectorDetails.getPath()), stashPatchset)); + return new StashFacade(new StashConnector(new HttpConnector(closeableHttpClient, httpClientContext, connectorDetails.getPath()), stashPatchset), configuration); } @NotNull - public StashPatchset buildStashPatchset() { - String pullRequestId = ConfigurationHolder.instance().getProperty(CliOption.PULL_REQUEST_ID); - String repositorySlug = ConfigurationHolder.instance().getProperty(GeneralOption.REPOSITORY_SLUG); - String projectKey = ConfigurationHolder.instance().getProperty(GeneralOption.PROJECT_KEY); + public StashPatchset buildStashPatchset(Configuration configuration) { + String pullRequestId = configuration.getProperty(CliOption.PULL_REQUEST_ID); + String repositorySlug = configuration.getProperty(GeneralOption.REPOSITORY_SLUG); + String projectKey = configuration.getProperty(GeneralOption.PROJECT_KEY); notBlank(pullRequestId, "You must provide non blank Stash pull request id"); notBlank(repositorySlug, "You must provide non blank Stash repository slug"); diff --git a/src/main/java/pl/touk/sputnik/engine/Engine.java b/src/main/java/pl/touk/sputnik/engine/Engine.java index 910ae5c0..28ca40d8 100644 --- a/src/main/java/pl/touk/sputnik/engine/Engine.java +++ b/src/main/java/pl/touk/sputnik/engine/Engine.java @@ -1,11 +1,13 @@ package pl.touk.sputnik.engine; import lombok.extern.slf4j.Slf4j; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.connector.ConnectorFacade; import pl.touk.sputnik.engine.visitor.AfterReviewVisitor; import pl.touk.sputnik.engine.visitor.BeforeReviewVisitor; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewFile; +import pl.touk.sputnik.review.ReviewFormatterFactory; import pl.touk.sputnik.review.ReviewProcessor; import java.util.List; @@ -14,26 +16,28 @@ public class Engine { private final ConnectorFacade facade; + private final Configuration config; - public Engine(ConnectorFacade facade) { + public Engine(ConnectorFacade facade, Configuration configuration) { this.facade = facade; + this.config = configuration; } public void run() { List reviewFiles = facade.listFiles(); - Review review = new Review(reviewFiles); + Review review = new Review(reviewFiles, ReviewFormatterFactory.get(config)); - for (BeforeReviewVisitor beforeReviewVisitor : new VisitorBuilder().buildBeforeReviewVisitors()) { + for (BeforeReviewVisitor beforeReviewVisitor : new VisitorBuilder().buildBeforeReviewVisitors(config)) { beforeReviewVisitor.beforeReview(review); } - List processors = new ProcessorBuilder().buildProcessors(); + List processors = ProcessorBuilder.buildProcessors(config); ReviewRunner reviewRunner = new ReviewRunner(review); for (ReviewProcessor processor : processors) { reviewRunner.review(processor); } - for (AfterReviewVisitor afterReviewVisitor : new VisitorBuilder().buildAfterReviewVisitors()) { + for (AfterReviewVisitor afterReviewVisitor : new VisitorBuilder().buildAfterReviewVisitors(config)) { afterReviewVisitor.afterReview(review); } diff --git a/src/main/java/pl/touk/sputnik/engine/ProcessorBuilder.java b/src/main/java/pl/touk/sputnik/engine/ProcessorBuilder.java index 16bc9d37..95ae2af6 100644 --- a/src/main/java/pl/touk/sputnik/engine/ProcessorBuilder.java +++ b/src/main/java/pl/touk/sputnik/engine/ProcessorBuilder.java @@ -1,7 +1,7 @@ package pl.touk.sputnik.engine; import org.jetbrains.annotations.NotNull; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.GeneralOption; import pl.touk.sputnik.processor.checkstyle.CheckstyleProcessor; import pl.touk.sputnik.processor.codenarc.CodeNarcProcessor; @@ -19,31 +19,31 @@ public class ProcessorBuilder { @NotNull - public List buildProcessors() { + public static List buildProcessors(Configuration configuration) { List processors = new ArrayList<>(); - if (Boolean.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.CHECKSTYLE_ENABLED))) { - processors.add(new CheckstyleProcessor()); + if (Boolean.valueOf(configuration.getProperty(GeneralOption.CHECKSTYLE_ENABLED))) { + processors.add(new CheckstyleProcessor(configuration)); } - if (Boolean.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.PMD_ENABLED))) { - processors.add(new PmdProcessor()); + if (Boolean.valueOf(configuration.getProperty(GeneralOption.PMD_ENABLED))) { + processors.add(new PmdProcessor(configuration)); } - if (Boolean.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.FINDBUGS_ENABLED))) { - processors.add(new FindBugsProcessor()); + if (Boolean.valueOf(configuration.getProperty(GeneralOption.FINDBUGS_ENABLED))) { + processors.add(new FindBugsProcessor(configuration)); } - if (Boolean.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.SCALASTYLE_ENABLED))) { - processors.add(new ScalastyleProcessor()); + if (Boolean.valueOf(configuration.getProperty(GeneralOption.SCALASTYLE_ENABLED))) { + processors.add(new ScalastyleProcessor(configuration)); } - if (Boolean.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.CODE_NARC_ENABLED))) { - processors.add(new CodeNarcProcessor()); + if (Boolean.valueOf(configuration.getProperty(GeneralOption.CODE_NARC_ENABLED))) { + processors.add(new CodeNarcProcessor(configuration)); } - if (Boolean.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.JSLINT_ENABLED))) { - processors.add(new JsLintProcessor()); + if (Boolean.valueOf(configuration.getProperty(GeneralOption.JSLINT_ENABLED))) { + processors.add(new JsLintProcessor(configuration)); } - if (Boolean.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.JSHINT_ENABLED))) { - processors.add(new JsHintProcessor()); + if (Boolean.valueOf(configuration.getProperty(GeneralOption.JSHINT_ENABLED))) { + processors.add(new JsHintProcessor(configuration)); } - if (Boolean.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.SONAR_ENABLED))) { - processors.add(new SonarProcessor()); + if (Boolean.valueOf(configuration.getProperty(GeneralOption.SONAR_ENABLED))) { + processors.add(new SonarProcessor(configuration)); } return processors; } diff --git a/src/main/java/pl/touk/sputnik/engine/ReviewRunner.java b/src/main/java/pl/touk/sputnik/engine/ReviewRunner.java index 15e810b6..0bb337cd 100644 --- a/src/main/java/pl/touk/sputnik/engine/ReviewRunner.java +++ b/src/main/java/pl/touk/sputnik/engine/ReviewRunner.java @@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.exception.ExceptionUtils; import org.jetbrains.annotations.NotNull; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewException; import pl.touk.sputnik.review.ReviewProcessor; diff --git a/src/main/java/pl/touk/sputnik/engine/VisitorBuilder.java b/src/main/java/pl/touk/sputnik/engine/VisitorBuilder.java index 57df6633..7edb3825 100644 --- a/src/main/java/pl/touk/sputnik/engine/VisitorBuilder.java +++ b/src/main/java/pl/touk/sputnik/engine/VisitorBuilder.java @@ -5,7 +5,7 @@ import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.lang3.BooleanUtils; import org.jetbrains.annotations.NotNull; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.GeneralOption; import pl.touk.sputnik.engine.visitor.*; import pl.touk.sputnik.engine.visitor.score.NoScore; @@ -27,42 +27,42 @@ public class VisitorBuilder { private static final String SCOREPASSIFNOERRORS = "SCOREPASSIFNOERRORS"; @NotNull - public List buildBeforeReviewVisitors() { + public List buildBeforeReviewVisitors(Configuration configuration) { List beforeReviewVisitors = new ArrayList<>(); - if (!BooleanUtils.toBoolean(ConfigurationHolder.instance().getProperty(GeneralOption.PROCESS_TEST_FILES))) { + if (!BooleanUtils.toBoolean(configuration.getProperty(GeneralOption.PROCESS_TEST_FILES))) { beforeReviewVisitors.add(new FilterOutTestFilesVisitor()); } return beforeReviewVisitors; } @NotNull - public List buildAfterReviewVisitors() { + public List buildAfterReviewVisitors(Configuration configuration) { List afterReviewVisitors = new ArrayList<>(); - String passingComment = ConfigurationHolder.instance().getProperty(GeneralOption.MESSAGE_SCORE_PASSING_COMMENT); + String passingComment = configuration.getProperty(GeneralOption.MESSAGE_SCORE_PASSING_COMMENT); afterReviewVisitors.add(new SummaryMessageVisitor(passingComment)); - int maxNumberOfComments = NumberUtils.toInt(ConfigurationHolder.instance().getProperty(GeneralOption.MAX_NUMBER_OF_COMMENTS), 0); + int maxNumberOfComments = NumberUtils.toInt(configuration.getProperty(GeneralOption.MAX_NUMBER_OF_COMMENTS), 0); if (maxNumberOfComments > 0) { afterReviewVisitors.add(new LimitCommentVisitor(maxNumberOfComments)); } - afterReviewVisitors.add(buildScoreAfterReviewVisitor()); + afterReviewVisitors.add(buildScoreAfterReviewVisitor(configuration)); return afterReviewVisitors; } @NotNull - private AfterReviewVisitor buildScoreAfterReviewVisitor() { + private AfterReviewVisitor buildScoreAfterReviewVisitor(Configuration configuration) { Map passingScore = ImmutableMap.of( - ConfigurationHolder.instance().getProperty(GeneralOption.SCORE_PASSING_KEY), - Short.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.SCORE_PASSING_VALUE)) + configuration.getProperty(GeneralOption.SCORE_PASSING_KEY), + Short.valueOf(configuration.getProperty(GeneralOption.SCORE_PASSING_VALUE)) ); Map failingScore = ImmutableMap.of( - ConfigurationHolder.instance().getProperty(GeneralOption.SCORE_FAILING_KEY), - Short.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.SCORE_FAILING_VALUE)) + configuration.getProperty(GeneralOption.SCORE_FAILING_KEY), + Short.valueOf(configuration.getProperty(GeneralOption.SCORE_FAILING_VALUE)) ); - String scoreStrategy = ConfigurationHolder.instance().getProperty(GeneralOption.SCORE_STRATEGY); + String scoreStrategy = configuration.getProperty(GeneralOption.SCORE_STRATEGY); notBlank(scoreStrategy); switch(scoreStrategy.toUpperCase()) { diff --git a/src/main/java/pl/touk/sputnik/processor/checkstyle/CheckstyleProcessor.java b/src/main/java/pl/touk/sputnik/processor/checkstyle/CheckstyleProcessor.java index cfc80e37..fa8b0ba2 100644 --- a/src/main/java/pl/touk/sputnik/processor/checkstyle/CheckstyleProcessor.java +++ b/src/main/java/pl/touk/sputnik/processor/checkstyle/CheckstyleProcessor.java @@ -5,10 +5,11 @@ import com.puppycrawl.tools.checkstyle.PropertiesExpander; import com.puppycrawl.tools.checkstyle.api.AuditListener; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.GeneralOption; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewException; @@ -22,9 +23,11 @@ import java.util.Properties; @Slf4j +@AllArgsConstructor public class CheckstyleProcessor implements ReviewProcessor { private static final String SOURCE_NAME = "Checkstyle"; private final CollectorListener collectorListener = new CollectorListener(); + private final Configuration configuration; @Nullable @Override @@ -64,7 +67,7 @@ private Checker createChecker(@NotNull AuditListener auditListener) { @Nullable private String getConfigurationFilename() { - String configurationFile = ConfigurationHolder.instance().getProperty(GeneralOption.CHECKSTYLE_CONFIGURATION_FILE); + String configurationFile = configuration.getProperty(GeneralOption.CHECKSTYLE_CONFIGURATION_FILE); log.info("Using Checkstyle configuration file {}", configurationFile); return configurationFile; } diff --git a/src/main/java/pl/touk/sputnik/processor/codenarc/CodeNarcProcessor.java b/src/main/java/pl/touk/sputnik/processor/codenarc/CodeNarcProcessor.java index 0e18e036..a4ae1f58 100644 --- a/src/main/java/pl/touk/sputnik/processor/codenarc/CodeNarcProcessor.java +++ b/src/main/java/pl/touk/sputnik/processor/codenarc/CodeNarcProcessor.java @@ -1,10 +1,12 @@ package pl.touk.sputnik.processor.codenarc; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.codenarc.CodeNarcRunner; import org.codenarc.results.Results; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewProcessor; import pl.touk.sputnik.review.ReviewResult; @@ -14,12 +16,14 @@ import java.util.List; @Slf4j +@AllArgsConstructor public class CodeNarcProcessor implements ReviewProcessor { private static final String PROCESSOR_NAME = "CodeNarc"; private final CodeNarcRunnerBuilder codeNarcRunnerBuilder = new CodeNarcRunnerBuilder(); private final ResultParser resultParser = new ResultParser(); + private final Configuration configuration; @Nullable @Override @@ -28,7 +32,7 @@ public ReviewResult process(@NotNull Review review) { if (noFilesToReview(reviewFiles)) { return new ReviewResult(); } - CodeNarcRunner codeNarcRunner = codeNarcRunnerBuilder.prepareCodeNarcRunner(reviewFiles); + CodeNarcRunner codeNarcRunner = codeNarcRunnerBuilder.prepareCodeNarcRunner(reviewFiles, configuration); Results results = codeNarcRunner.execute(); return resultParser.parseResults(results); } diff --git a/src/main/java/pl/touk/sputnik/processor/codenarc/CodeNarcRunnerBuilder.java b/src/main/java/pl/touk/sputnik/processor/codenarc/CodeNarcRunnerBuilder.java index d620f026..27f46222 100644 --- a/src/main/java/pl/touk/sputnik/processor/codenarc/CodeNarcRunnerBuilder.java +++ b/src/main/java/pl/touk/sputnik/processor/codenarc/CodeNarcRunnerBuilder.java @@ -3,24 +3,25 @@ import org.codenarc.CodeNarcRunner; import org.codenarc.analyzer.FilesystemSourceAnalyzer; import org.codenarc.analyzer.SourceAnalyzer; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import org.jetbrains.annotations.NotNull; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.GeneralOption; import java.util.List; class CodeNarcRunnerBuilder { - public CodeNarcRunner prepareCodeNarcRunner(List reviewFiles) { + public CodeNarcRunner prepareCodeNarcRunner(List reviewFiles, @NotNull Configuration configuration) { CodeNarcRunner codeNarcRunner = new CodeNarcRunner(); - codeNarcRunner.setRuleSetFiles(ConfigurationHolder.instance().getProperty(GeneralOption.CODE_NARC_RULESET)); - codeNarcRunner.setSourceAnalyzer(createSourceAnalyzer(reviewFiles)); + codeNarcRunner.setRuleSetFiles(configuration.getProperty(GeneralOption.CODE_NARC_RULESET)); + codeNarcRunner.setSourceAnalyzer(createSourceAnalyzer(reviewFiles, configuration)); return codeNarcRunner; } - private SourceAnalyzer createSourceAnalyzer(List reviewFiles) { + private SourceAnalyzer createSourceAnalyzer(List reviewFiles, @NotNull Configuration configuration) { FilesystemSourceAnalyzer sourceAnalyzer = new FilesystemSourceAnalyzer(); sourceAnalyzer.setBaseDirectory("."); sourceAnalyzer.setIncludes(createFileList(reviewFiles)); - sourceAnalyzer.setExcludes(ConfigurationHolder.instance().getProperty(GeneralOption.CODE_NARC_EXCLUDES)); + sourceAnalyzer.setExcludes(configuration.getProperty(GeneralOption.CODE_NARC_EXCLUDES)); return sourceAnalyzer; } diff --git a/src/main/java/pl/touk/sputnik/processor/findbugs/FindBugsProcessor.java b/src/main/java/pl/touk/sputnik/processor/findbugs/FindBugsProcessor.java index ae090a24..cafc0ec3 100644 --- a/src/main/java/pl/touk/sputnik/processor/findbugs/FindBugsProcessor.java +++ b/src/main/java/pl/touk/sputnik/processor/findbugs/FindBugsProcessor.java @@ -1,7 +1,5 @@ package pl.touk.sputnik.processor.findbugs; -import java.util.List; - import edu.umd.cs.findbugs.ClassScreener; import edu.umd.cs.findbugs.DetectorFactoryCollection; import edu.umd.cs.findbugs.FindBugs2; @@ -15,14 +13,13 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.GeneralOption; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewException; import pl.touk.sputnik.review.ReviewProcessor; import pl.touk.sputnik.review.ReviewResult; import pl.touk.sputnik.review.filter.JavaFilter; -import pl.touk.sputnik.review.locator.BuildDirLocator; import pl.touk.sputnik.review.locator.BuildDirLocatorFactory; import pl.touk.sputnik.review.transformer.ClassNameTransformer; @@ -30,11 +27,11 @@ public class FindBugsProcessor implements ReviewProcessor { private static final String SOURCE_NAME = "FindBugs"; private final CollectorBugReporter collectorBugReporter; - private final BuildDirLocator buildDirLocator; + private final Configuration config; - public FindBugsProcessor() { + public FindBugsProcessor(@NotNull Configuration configuration) { collectorBugReporter = createBugReporter(); - buildDirLocator = BuildDirLocatorFactory.create(); + config = configuration; } @Nullable @@ -90,7 +87,7 @@ public CollectorBugReporter createBugReporter() { @NotNull private Project createProject(@NotNull Review review) { Project project = new Project(); - for (String buildDir : buildDirLocator.getBuildDirs(review)) { + for (String buildDir : BuildDirLocatorFactory.create(config).getBuildDirs(review)) { project.addFile(buildDir); } for (String sourceDir : review.getSourceDirs()) { @@ -110,14 +107,14 @@ private IClassScreener createClassScreener(@NotNull Review review) { @Nullable private String getIncludeFilterFilename() { - String includeFilterFilename = ConfigurationHolder.instance().getProperty(GeneralOption.FINDBUGS_INCLUDE_FILTER); + String includeFilterFilename = config.getProperty(GeneralOption.FINDBUGS_INCLUDE_FILTER); log.info("Using FindBugs include filter file {}", includeFilterFilename); return includeFilterFilename; } @Nullable private String getExcludeFilterFilename() { - String excludeFilterFilename = ConfigurationHolder.instance().getProperty(GeneralOption.FINDBUGS_EXCLUDE_FILTER); + String excludeFilterFilename = config.getProperty(GeneralOption.FINDBUGS_EXCLUDE_FILTER); log.info("Using FindBugs exclude filter file {}", excludeFilterFilename); return excludeFilterFilename; } diff --git a/src/main/java/pl/touk/sputnik/processor/jshint/JsHintProcessor.java b/src/main/java/pl/touk/sputnik/processor/jshint/JsHintProcessor.java index 0837e2c2..7b38eb0a 100644 --- a/src/main/java/pl/touk/sputnik/processor/jshint/JsHintProcessor.java +++ b/src/main/java/pl/touk/sputnik/processor/jshint/JsHintProcessor.java @@ -5,10 +5,12 @@ import java.io.IOException; import java.util.List; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.jetbrains.annotations.NotNull; import pl.gildur.jshint4j.Error; import pl.gildur.jshint4j.JsHint; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.GeneralOption; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewException; @@ -23,10 +25,14 @@ import com.google.common.io.CharStreams; @Slf4j +@AllArgsConstructor public class JsHintProcessor implements ReviewProcessor { private static final String SOURCE_NAME = "JSHint"; + @NotNull + private final Configuration config; + @Override public ReviewResult process(Review review) { String configuration = readConfiguration(); @@ -66,7 +72,7 @@ private String readConfiguration() { } private String getConfigurationFileName() { - String configurationFile = ConfigurationHolder.instance().getProperty(GeneralOption.JSHINT_CONFIGURATION_FILE); + String configurationFile = config.getProperty(GeneralOption.JSHINT_CONFIGURATION_FILE); log.info("Using JSHint configuration file {}", configurationFile); return configurationFile; } diff --git a/src/main/java/pl/touk/sputnik/processor/jslint/JsLintProcessor.java b/src/main/java/pl/touk/sputnik/processor/jslint/JsLintProcessor.java index 18f4e46c..74c4c32c 100644 --- a/src/main/java/pl/touk/sputnik/processor/jslint/JsLintProcessor.java +++ b/src/main/java/pl/touk/sputnik/processor/jslint/JsLintProcessor.java @@ -1,7 +1,9 @@ package pl.touk.sputnik.processor.jslint; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import org.jetbrains.annotations.NotNull; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.GeneralOption; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewException; @@ -27,9 +29,12 @@ import com.googlecode.jslint4java.Option; @Slf4j +@AllArgsConstructor public class JsLintProcessor implements ReviewProcessor { private static final String SOURCE_NAME = "JSLint"; + @NotNull + private final Configuration config; @Override public ReviewResult process(Review review) { @@ -109,7 +114,7 @@ private Properties loadProperties(String configurationFileName) { } private String getConfigurationFileName() { - String configurationFile = ConfigurationHolder.instance().getProperty(GeneralOption.JSLINT_CONFIGURATION_FILE); + String configurationFile = config.getProperty(GeneralOption.JSLINT_CONFIGURATION_FILE); log.info("Using JSLint configuration file {}", configurationFile); return configurationFile; } diff --git a/src/main/java/pl/touk/sputnik/processor/pmd/CollectorRenderer.java b/src/main/java/pl/touk/sputnik/processor/pmd/CollectorRenderer.java index 67513694..97a1a660 100644 --- a/src/main/java/pl/touk/sputnik/processor/pmd/CollectorRenderer.java +++ b/src/main/java/pl/touk/sputnik/processor/pmd/CollectorRenderer.java @@ -9,24 +9,32 @@ import net.sourceforge.pmd.util.datasource.DataSource; import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; +import pl.touk.sputnik.configuration.ConfigurationBuilder; import pl.touk.sputnik.configuration.GeneralOption; import pl.touk.sputnik.review.ReviewResult; import pl.touk.sputnik.review.Severity; import pl.touk.sputnik.review.Violation; import java.io.IOException; +import java.util.Properties; @Slf4j public class CollectorRenderer extends AbstractRenderer { private static final String SPUTNIK_PMD_COLLECT_RENDERER = "Sputnik PMD Collect Renderer"; private static final char LINE_SEPARATOR = '\n'; + private final Configuration configuration; @Getter private final ReviewResult reviewResult = new ReviewResult(); - public CollectorRenderer() { + public CollectorRenderer(Properties properties) { + this(ConfigurationBuilder.initFromProperties(properties)); + } + + public CollectorRenderer(Configuration configuration) { super(SPUTNIK_PMD_COLLECT_RENDERER, SPUTNIK_PMD_COLLECT_RENDERER); + this.configuration = configuration; } @Override @@ -41,7 +49,7 @@ public void startFileAnalysis(DataSource dataSource) { @Override public void renderFileReport(Report report) throws IOException { - boolean showDetails = Boolean.valueOf(ConfigurationHolder.instance().getProperty(GeneralOption.PMD_SHOW_VIOLATION_DETAILS)); + boolean showDetails = Boolean.valueOf(configuration.getProperty(GeneralOption.PMD_SHOW_VIOLATION_DETAILS)); for (RuleViolation ruleViolation : report) { String violationDescription = showDetails ? renderViolationDetails(ruleViolation) :ruleViolation.getDescription(); diff --git a/src/main/java/pl/touk/sputnik/processor/pmd/PmdProcessor.java b/src/main/java/pl/touk/sputnik/processor/pmd/PmdProcessor.java index 36691cba..2fff9761 100644 --- a/src/main/java/pl/touk/sputnik/processor/pmd/PmdProcessor.java +++ b/src/main/java/pl/touk/sputnik/processor/pmd/PmdProcessor.java @@ -15,7 +15,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.GeneralOption; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewException; @@ -36,6 +36,13 @@ public class PmdProcessor implements ReviewProcessor { private static final char PMD_INPUT_PATH_SEPARATOR = ','; private Renderer renderer; + @NotNull + private final Configuration config; + + public PmdProcessor(Configuration configuration) { + config = configuration; + } + @Nullable @Override public ReviewResult process(@NotNull Review review) { @@ -65,7 +72,7 @@ public String getName() { @Nullable private String getRulesets() { - String ruleSets = ConfigurationHolder.instance().getProperty(GeneralOption.PMD_RULESETS); + String ruleSets = config.getProperty(GeneralOption.PMD_RULESETS); log.info("Using PMD rulesets {}", ruleSets); return ruleSets; } diff --git a/src/main/java/pl/touk/sputnik/processor/scalastyle/ScalastyleProcessor.java b/src/main/java/pl/touk/sputnik/processor/scalastyle/ScalastyleProcessor.java index ea963bc0..8fde4abd 100644 --- a/src/main/java/pl/touk/sputnik/processor/scalastyle/ScalastyleProcessor.java +++ b/src/main/java/pl/touk/sputnik/processor/scalastyle/ScalastyleProcessor.java @@ -1,6 +1,7 @@ package pl.touk.sputnik.processor.scalastyle; import com.google.common.collect.Lists; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -14,7 +15,7 @@ import org.scalastyle.ScalastyleConfiguration; import org.scalastyle.StartFile; import org.scalastyle.StyleError; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.GeneralOption; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewProcessor; @@ -30,16 +31,20 @@ import java.util.List; @Slf4j +@AllArgsConstructor public class ScalastyleProcessor implements ReviewProcessor { private static final String SOURCE_NAME = "Scalastyle"; private final MessageHelper messageHelper = new MessageHelper(ClassLoader.getSystemClassLoader()); + @NotNull + private final Configuration config; + @Nullable @Override @SuppressWarnings("unchecked") public ReviewResult process(@NotNull Review review) { - String scalastyleConfigFile = ConfigurationHolder.instance().getProperty(GeneralOption.SCALASTYLE_CONFIGURATION_FILE); + String scalastyleConfigFile = config.getProperty(GeneralOption.SCALASTYLE_CONFIGURATION_FILE); ScalastyleConfiguration configuration = ScalastyleConfiguration.readFromXml(scalastyleConfigFile); List messages = new ScalastyleChecker().checkFilesAsJava(configuration, toFileSpec(review.getFiles(new ScalaFilter(), new IOFileTransformer()))); return toReviewResult(messages); diff --git a/src/main/java/pl/touk/sputnik/processor/sonar/SonarProcessor.java b/src/main/java/pl/touk/sputnik/processor/sonar/SonarProcessor.java index 31b6f7a7..61dfe0aa 100644 --- a/src/main/java/pl/touk/sputnik/processor/sonar/SonarProcessor.java +++ b/src/main/java/pl/touk/sputnik/processor/sonar/SonarProcessor.java @@ -12,6 +12,7 @@ import com.google.common.annotations.VisibleForTesting; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewException; import pl.touk.sputnik.review.ReviewFile; @@ -25,13 +26,15 @@ public class SonarProcessor implements ReviewProcessor { private static final String PROCESSOR_NAME = "Sonar"; private SonarRunnerBuilder sonarRunnerBuilder; + private final Configuration configuration; - public SonarProcessor() { - this(new SonarRunnerBuilder()); + public SonarProcessor(@NotNull final Configuration configuration) { + this(new SonarRunnerBuilder(), configuration); } - public SonarProcessor(SonarRunnerBuilder sonarRunnerBuilder) { + public SonarProcessor(SonarRunnerBuilder sonarRunnerBuilder, @NotNull final Configuration configuration) { this.sonarRunnerBuilder = sonarRunnerBuilder; + this.configuration = configuration; } @Nullable @@ -42,7 +45,7 @@ public ReviewResult process(@NotNull Review review) { } try { - SonarRunner runner = sonarRunnerBuilder.prepareRunner(review); + SonarRunner runner = sonarRunnerBuilder.prepareRunner(review, configuration); File resultFile = runner.run(); SonarResultParser parser = new SonarResultParser(resultFile); return filterResults(parser.parseResults(), review); diff --git a/src/main/java/pl/touk/sputnik/processor/sonar/SonarRunner.java b/src/main/java/pl/touk/sputnik/processor/sonar/SonarRunner.java index 274ed9b8..6d7e4b04 100644 --- a/src/main/java/pl/touk/sputnik/processor/sonar/SonarRunner.java +++ b/src/main/java/pl/touk/sputnik/processor/sonar/SonarRunner.java @@ -5,7 +5,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.sonar.runner.api.EmbeddedRunner; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.GeneralOption; import java.io.File; @@ -20,6 +20,7 @@ public class SonarRunner { private final List files; private final EmbeddedRunner sonarEmbeddedRunner; + private final Configuration configuration; @VisibleForTesting static final String OUTPUT_DIR = ".sonar"; @@ -36,7 +37,7 @@ public class SonarRunner { @VisibleForTesting Properties loadBaseProperties() throws IOException { final Properties props = new Properties(); - for (final String property: StringUtils.split(ConfigurationHolder.instance().getProperty(GeneralOption.SONAR_PROPERTIES), ',')){ + for (final String property: StringUtils.split(configuration.getProperty(GeneralOption.SONAR_PROPERTIES), ',')){ final File propertyFile = new File(StringUtils.strip(property)); log.info("Loading {}", propertyFile.getAbsolutePath()); props.load(new FileInputStream(propertyFile)); @@ -71,7 +72,7 @@ void setAdditionalProperties(Properties props) { props.put(SonarProperties.SCM_STAT_ENABLED, "false"); props.put(SonarProperties.ISSUEASSIGN_PLUGIN, "false"); props.put(SonarProperties.EXPORT_PATH, OUTPUT_FILE); - props.put(SonarProperties.VERBOSE, ConfigurationHolder.instance().getProperty(GeneralOption.SONAR_VERBOSE)); + props.put(SonarProperties.VERBOSE, configuration.getProperty(GeneralOption.SONAR_VERBOSE)); props.put(SonarProperties.WORKDIR, OUTPUT_DIR); props.put(SonarProperties.PROJECT_BASEDIR, "."); } diff --git a/src/main/java/pl/touk/sputnik/processor/sonar/SonarRunnerBuilder.java b/src/main/java/pl/touk/sputnik/processor/sonar/SonarRunnerBuilder.java index 965cc23d..6e5832ec 100644 --- a/src/main/java/pl/touk/sputnik/processor/sonar/SonarRunnerBuilder.java +++ b/src/main/java/pl/touk/sputnik/processor/sonar/SonarRunnerBuilder.java @@ -5,13 +5,14 @@ import org.sonar.runner.api.EmbeddedRunner; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewFile; import com.google.common.collect.Lists; class SonarRunnerBuilder { - public SonarRunner prepareRunner(Review review) { + public SonarRunner prepareRunner(Review review, Configuration configuration) { List files = Lists.newArrayList(); /* @@ -30,7 +31,7 @@ public SonarRunner prepareRunner(Review review) { for (ReviewFile file : review.getFiles()) { files.add("**/" + new File(file.getReviewFilename()).getName()); } - SonarRunner sonarRunner = new SonarRunner(files, EmbeddedRunner.create()); + SonarRunner sonarRunner = new SonarRunner(files, EmbeddedRunner.create(), configuration); return sonarRunner; } } diff --git a/src/main/java/pl/touk/sputnik/review/Review.java b/src/main/java/pl/touk/sputnik/review/Review.java index 2a075e48..cb12c780 100644 --- a/src/main/java/pl/touk/sputnik/review/Review.java +++ b/src/main/java/pl/touk/sputnik/review/Review.java @@ -12,7 +12,11 @@ import pl.touk.sputnik.review.filter.FileFilter; import pl.touk.sputnik.review.transformer.FileTransformer; -import java.util.*; +import java.util.ArrayList; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.List; +import java.util.Map; @Slf4j @Getter @@ -36,11 +40,11 @@ public class Review { private List messages = new ArrayList<>(); private Map scores = new HashMap<>(); - private ReviewFormatter formatter; + private final ReviewFormatter formatter; - public Review(@NotNull List files) { + public Review(@NotNull List files, ReviewFormatter reviewFormatter) { this.files = files; - this.formatter = ReviewFormatterFactory.get(); + this.formatter = reviewFormatter; } @NotNull diff --git a/src/main/java/pl/touk/sputnik/review/ReviewFormatter.java b/src/main/java/pl/touk/sputnik/review/ReviewFormatter.java index b2a1c43c..6ef8e05b 100644 --- a/src/main/java/pl/touk/sputnik/review/ReviewFormatter.java +++ b/src/main/java/pl/touk/sputnik/review/ReviewFormatter.java @@ -7,7 +7,6 @@ import java.text.MessageFormat; - public class ReviewFormatter { private String commentFormat; diff --git a/src/main/java/pl/touk/sputnik/review/ReviewFormatterFactory.java b/src/main/java/pl/touk/sputnik/review/ReviewFormatterFactory.java index 644ae687..51567a7c 100644 --- a/src/main/java/pl/touk/sputnik/review/ReviewFormatterFactory.java +++ b/src/main/java/pl/touk/sputnik/review/ReviewFormatterFactory.java @@ -1,11 +1,10 @@ package pl.touk.sputnik.review; -import pl.touk.sputnik.configuration.ConfigurationHolder; - +import pl.touk.sputnik.configuration.Configuration; public class ReviewFormatterFactory { - static ReviewFormatter get() { - return new ReviewFormatter(ConfigurationHolder.instance()); + public static ReviewFormatter get(Configuration configuration) { + return new ReviewFormatter(configuration); } } diff --git a/src/main/java/pl/touk/sputnik/review/ReviewProcessor.java b/src/main/java/pl/touk/sputnik/review/ReviewProcessor.java index 152a6c86..b9171383 100644 --- a/src/main/java/pl/touk/sputnik/review/ReviewProcessor.java +++ b/src/main/java/pl/touk/sputnik/review/ReviewProcessor.java @@ -2,6 +2,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import pl.touk.sputnik.configuration.Configuration; public interface ReviewProcessor { diff --git a/src/main/java/pl/touk/sputnik/review/locator/BuildDirLocatorFactory.java b/src/main/java/pl/touk/sputnik/review/locator/BuildDirLocatorFactory.java index 4c83d25e..53a598a8 100644 --- a/src/main/java/pl/touk/sputnik/review/locator/BuildDirLocatorFactory.java +++ b/src/main/java/pl/touk/sputnik/review/locator/BuildDirLocatorFactory.java @@ -2,7 +2,7 @@ import lombok.extern.slf4j.Slf4j; import pl.touk.sputnik.configuration.BuildTool; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.GeneralOption; import static org.apache.commons.lang3.Validate.notBlank; @@ -10,8 +10,8 @@ @Slf4j public class BuildDirLocatorFactory { - public static BuildDirLocator create() { - String buildTool = ConfigurationHolder.instance().getProperty(GeneralOption.BUILD_TOOL); + public static BuildDirLocator create(Configuration configuration) { + String buildTool = configuration.getProperty(GeneralOption.BUILD_TOOL); notBlank(buildTool); switch (BuildTool.valueOf(buildTool.toUpperCase())) { diff --git a/src/test/java/pl/touk/sputnik/CliOptionsTest.java b/src/test/java/pl/touk/sputnik/CliOptionsTest.java index 1dd6db9c..75910eb8 100644 --- a/src/test/java/pl/touk/sputnik/CliOptionsTest.java +++ b/src/test/java/pl/touk/sputnik/CliOptionsTest.java @@ -25,9 +25,9 @@ public void shouldExecuteGerritReview() throws Exception { CommandLine commandLine = fixture.parse(args); // then - _(commandLine).hasOption(CliOption.CONF.getCommandLineParam()).withValue(SAMPLE_CONFIG); - _(commandLine).hasOption(CliOption.CHANGE_ID.getCommandLineParam()).withValue(SAMPLE_CHANGE_ID); - _(commandLine).hasOption(CliOption.REVISION_ID.getCommandLineParam()).withValue(SAMPLE_REVISION_ID); + cliAssert(commandLine).hasOption(CliOption.CONF.getCommandLineParam()).withValue(SAMPLE_CONFIG); + cliAssert(commandLine).hasOption(CliOption.CHANGE_ID.getCommandLineParam()).withValue(SAMPLE_CHANGE_ID); + cliAssert(commandLine).hasOption(CliOption.REVISION_ID.getCommandLineParam()).withValue(SAMPLE_REVISION_ID); } @Test @@ -39,14 +39,14 @@ public void shouldExecuteStashReview() throws Exception { CommandLine commandLine = fixture.parse(args); // then - _(commandLine).hasOption(CliOption.PULL_REQUEST_ID.getCommandLineParam()).withValue(SAMPLE_PULL_REQUEST_ID); + cliAssert(commandLine).hasOption(CliOption.PULL_REQUEST_ID.getCommandLineParam()).withValue(SAMPLE_PULL_REQUEST_ID); } private String[] toArgs(String argsFormat, String... substitutions) { return String.format(argsFormat, (Object[]) substitutions).split(" "); } - private CliAssert _(CommandLine cli) { + private CliAssert cliAssert(CommandLine cli) { return new CliAssert(cli); } diff --git a/src/test/java/pl/touk/sputnik/ReviewBuilder.java b/src/test/java/pl/touk/sputnik/ReviewBuilder.java index eb9d9b92..64bef8e0 100644 --- a/src/test/java/pl/touk/sputnik/ReviewBuilder.java +++ b/src/test/java/pl/touk/sputnik/ReviewBuilder.java @@ -1,17 +1,19 @@ package pl.touk.sputnik; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.review.Comment; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewFile; +import pl.touk.sputnik.review.ReviewFormatterFactory; import java.util.Arrays; import java.util.List; public class ReviewBuilder { - public static Review buildReview() { + public static Review buildReview(Configuration configuration) { List reviewFiles = Arrays.asList(buildReviewFile(1), buildReviewFile(2), buildReviewFile(3), buildReviewFile(4)); - Review review = new Review(reviewFiles); + Review review = new Review(reviewFiles, ReviewFormatterFactory.get(configuration)); review.setTotalViolationCount(8); review.getMessages().add("Total 8 violations found"); review.getScores().put("Code-Review", (short) 1); diff --git a/src/test/java/pl/touk/sputnik/TestEnvironment.java b/src/test/java/pl/touk/sputnik/TestEnvironment.java index 13ee9178..9d1afc6c 100644 --- a/src/test/java/pl/touk/sputnik/TestEnvironment.java +++ b/src/test/java/pl/touk/sputnik/TestEnvironment.java @@ -2,11 +2,13 @@ import com.google.common.collect.ImmutableList; import com.google.common.io.Resources; -import org.junit.After; import org.junit.Before; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; +import pl.touk.sputnik.configuration.ConfigurationBuilder; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewFile; +import pl.touk.sputnik.review.ReviewFormatter; +import pl.touk.sputnik.review.ReviewFormatterFactory; import java.io.File; @@ -14,14 +16,13 @@ public abstract class TestEnvironment { + protected Configuration config; + protected ReviewFormatter formatter; + @Before public void setUp() throws Exception { - ConfigurationHolder.initFromResource("test.properties"); - } - - @After - public void tearDown() throws Exception { - ConfigurationHolder.reset(); + config = ConfigurationBuilder.initFromResource("test.properties"); + formatter = ReviewFormatterFactory.get(config); } protected Review review() { @@ -29,15 +30,15 @@ protected Review review() { } protected Review review(String filename) { - return new Review(ImmutableList.of(new ReviewFile(Resources.getResource(filename).getFile()))); + return new Review(ImmutableList.of(new ReviewFile(Resources.getResource(filename).getFile())), formatter); } protected Review nonexistantReview() { - return new Review(ImmutableList.of(new ReviewFile("test"))); + return new Review(ImmutableList.of(new ReviewFile("test")), formatter); } protected Review nonexistantReview(String filename){ - return new Review(ImmutableList.of(new ReviewFile(filename))); + return new Review(ImmutableList.of(new ReviewFile(filename)), formatter); } protected File getResourceAsFile(String resourceName) { diff --git a/src/test/java/pl/touk/sputnik/configuration/ConfigurationHolderTest.java b/src/test/java/pl/touk/sputnik/configuration/ConfigurationBuilderTest.java similarity index 59% rename from src/test/java/pl/touk/sputnik/configuration/ConfigurationHolderTest.java rename to src/test/java/pl/touk/sputnik/configuration/ConfigurationBuilderTest.java index b66305f9..c3456602 100644 --- a/src/test/java/pl/touk/sputnik/configuration/ConfigurationHolderTest.java +++ b/src/test/java/pl/touk/sputnik/configuration/ConfigurationBuilderTest.java @@ -8,49 +8,49 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class ConfigurationHolderTest { +public class ConfigurationBuilderTest { @Test(expected = IllegalArgumentException.class) public void shouldFailWhenConfigFilenameIsEmpty() { - ConfigurationHolder.initFromFile(""); + ConfigurationBuilder.initFromFile(""); } @Test(expected = RuntimeException.class) public void shouldFailWhenConfigFileDoesNotExist() { - ConfigurationHolder.initFromFile("wrong.properties"); + ConfigurationBuilder.initFromFile("wrong.properties"); } @Test public void shouldReadPropertiesFromFile() { - ConfigurationHolder.initFromResource("sample-test.properties"); + Configuration config = ConfigurationBuilder.initFromResource("sample-test.properties"); - assertThat(ConfigurationHolder.instance().getProperty(GeneralOption.PORT)).isEqualTo("9999"); + assertThat(config.getProperty(GeneralOption.PORT)).isEqualTo("9999"); } @Test public void shouldOverrideSystemProperties() { System.setProperty(GeneralOption.USERNAME.getKey(), "userala"); - ConfigurationHolder.initFromResource("sample-test.properties"); + Configuration config = ConfigurationBuilder.initFromResource("sample-test.properties"); - assertThat(ConfigurationHolder.instance().getProperty(GeneralOption.USERNAME)).isEqualTo("userala"); + assertThat(config.getProperty(GeneralOption.USERNAME)).isEqualTo("userala"); } @Test public void shouldReturnNotOverridedSystemProperties() { System.setProperty("some.system.property", "1234"); - ConfigurationHolder.initFromResource("sample-test.properties"); + Configuration config = ConfigurationBuilder.initFromResource("sample-test.properties"); - assertThat(ConfigurationHolder.instance().getProperty(GeneralOption.PORT)).isEqualTo("9999"); + assertThat(config.getProperty(GeneralOption.PORT)).isEqualTo("9999"); } @Test public void shouldUpdateWithCliOptions() { - ConfigurationHolder.initFromResource("sample-test.properties"); + Configuration config = ConfigurationBuilder.initFromResource("sample-test.properties"); CommandLine commandLineMock = buildCommandLine(); - ConfigurationHolder.instance().updateWithCliOptions(commandLineMock); + config.updateWithCliOptions(commandLineMock); - assertThat(ConfigurationHolder.instance().getProperty(CliOption.CHANGE_ID)).isEqualTo("99999"); + assertThat(config.getProperty(CliOption.CHANGE_ID)).isEqualTo("99999"); } private CommandLine buildCommandLine() { diff --git a/src/test/java/pl/touk/sputnik/configuration/ConfigurationSetup.java b/src/test/java/pl/touk/sputnik/configuration/ConfigurationSetup.java index 93105a59..241e93d0 100644 --- a/src/test/java/pl/touk/sputnik/configuration/ConfigurationSetup.java +++ b/src/test/java/pl/touk/sputnik/configuration/ConfigurationSetup.java @@ -6,7 +6,7 @@ public class ConfigurationSetup { @SafeVarargs - public final void setUp(final Map... propertyMaps) { + public final Configuration setUp(final Map... propertyMaps) { Properties properties = new Properties(); for (Map map: propertyMaps) { for (Map.Entry entry : map.entrySet()) { @@ -14,7 +14,7 @@ public final void setUp(final Map... propertyMaps) { } } - ConfigurationHolder.initFromProperties(properties); + return ConfigurationBuilder.initFromProperties(properties); } } diff --git a/src/test/java/pl/touk/sputnik/connector/ConnectorFacadeFactoryTest.java b/src/test/java/pl/touk/sputnik/connector/ConnectorFacadeFactoryTest.java index 294c4a18..a6bebbc6 100644 --- a/src/test/java/pl/touk/sputnik/connector/ConnectorFacadeFactoryTest.java +++ b/src/test/java/pl/touk/sputnik/connector/ConnectorFacadeFactoryTest.java @@ -3,6 +3,7 @@ import org.junit.Before; import org.junit.Test; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.connector.gerrit.GerritFacade; import pl.touk.sputnik.connector.gerrit.GerritFacadeBuilder; @@ -24,11 +25,12 @@ public void shouldBuildConnector() { // given GerritFacadeBuilder gerritFacadeBuilderMock = mock(GerritFacadeBuilder.class); GerritFacade gerritFacadeMock = mock(GerritFacade.class); - when(gerritFacadeBuilderMock.build()).thenReturn(gerritFacadeMock); + Configuration config = mock(Configuration.class); + when(gerritFacadeBuilderMock.build(config)).thenReturn(gerritFacadeMock); connectorFacadeFactory.gerritFacadeBuilder = gerritFacadeBuilderMock; // when - ConnectorFacade facade = connectorFacadeFactory.build(ConnectorType.GERRIT); + ConnectorFacade facade = connectorFacadeFactory.build(ConnectorType.GERRIT, config); // then assertThat(facade).isEqualTo(gerritFacadeMock); diff --git a/src/test/java/pl/touk/sputnik/connector/FacadeConfigUtil.java b/src/test/java/pl/touk/sputnik/connector/FacadeConfigUtil.java index f3acd59c..f9a8bca1 100644 --- a/src/test/java/pl/touk/sputnik/connector/FacadeConfigUtil.java +++ b/src/test/java/pl/touk/sputnik/connector/FacadeConfigUtil.java @@ -4,9 +4,9 @@ import java.util.Map; public final class FacadeConfigUtil { - public static Integer HTTP_PORT = 8089; - public static Integer HTTPS_PORT = 8443; - public static String PATH = "/review"; + public static final Integer HTTP_PORT = 8089; + public static final Integer HTTPS_PORT = 8443; + public static final String PATH = "/review"; public static Map getHttpConfig(final String connectorType) { return new HashMap() {{ diff --git a/src/test/java/pl/touk/sputnik/connector/gerrit/GerritFacadeTest.java b/src/test/java/pl/touk/sputnik/connector/gerrit/GerritFacadeTest.java index 23215bad..975c5080 100644 --- a/src/test/java/pl/touk/sputnik/connector/gerrit/GerritFacadeTest.java +++ b/src/test/java/pl/touk/sputnik/connector/gerrit/GerritFacadeTest.java @@ -23,7 +23,8 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; +import pl.touk.sputnik.configuration.ConfigurationBuilder; import pl.touk.sputnik.configuration.ConfigurationSetup; import pl.touk.sputnik.configuration.GeneralOptionNotSupportedException; import pl.touk.sputnik.connector.ConnectorFacade; @@ -52,7 +53,7 @@ public class GerritFacadeTest { @Test public void shouldNotAllowCommentOnlyChangedLines() { // given - new ConfigurationSetup().setUp(ImmutableMap.of( + Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of( "cli.changeId", "abc", "cli.revisionId", "def", "global.commentOnlyChangedLines", Boolean.toString(true))); @@ -60,8 +61,8 @@ public void shouldNotAllowCommentOnlyChangedLines() { ConnectorFacadeFactory connectionFacade = new ConnectorFacadeFactory(); // when - ConnectorFacade gerritFacade = connectionFacade.build(ConnectorType.GERRIT); - catchException(gerritFacade).validate(ConfigurationHolder.instance()); + ConnectorFacade gerritFacade = connectionFacade.build(ConnectorType.GERRIT, config); + catchException(gerritFacade).validate(config); // then assertThat(caughtException()).isInstanceOf(GeneralOptionNotSupportedException.class).hasMessage( diff --git a/src/test/java/pl/touk/sputnik/connector/gerrit/ReviewInputBuilderTest.java b/src/test/java/pl/touk/sputnik/connector/gerrit/ReviewInputBuilderTest.java index f1a47ddf..1168bad3 100644 --- a/src/test/java/pl/touk/sputnik/connector/gerrit/ReviewInputBuilderTest.java +++ b/src/test/java/pl/touk/sputnik/connector/gerrit/ReviewInputBuilderTest.java @@ -3,7 +3,8 @@ import com.google.gerrit.extensions.api.changes.ReviewInput; import org.junit.Test; import pl.touk.sputnik.ReviewBuilder; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; +import pl.touk.sputnik.configuration.ConfigurationBuilder; import pl.touk.sputnik.review.Review; import static org.assertj.core.api.Assertions.assertThat; @@ -12,8 +13,8 @@ public class ReviewInputBuilderTest { @Test public void shouldBuildReviewInput() { - ConfigurationHolder.initFromResource("test.properties"); - Review review = ReviewBuilder.buildReview(); + Configuration config = ConfigurationBuilder.initFromResource("test.properties"); + Review review = ReviewBuilder.buildReview(config); ReviewInput reviewInput = new ReviewInputBuilder().toReviewInput(review); diff --git a/src/test/java/pl/touk/sputnik/connector/stash/StashFacadeHttpsTest.java b/src/test/java/pl/touk/sputnik/connector/stash/StashFacadeHttpsTest.java index 6525c5a5..a6b31e61 100644 --- a/src/test/java/pl/touk/sputnik/connector/stash/StashFacadeHttpsTest.java +++ b/src/test/java/pl/touk/sputnik/connector/stash/StashFacadeHttpsTest.java @@ -6,6 +6,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.ConfigurationSetup; import pl.touk.sputnik.connector.FacadeConfigUtil; import pl.touk.sputnik.review.ReviewFile; @@ -34,8 +35,8 @@ public class StashFacadeHttpsTest { @Before public void setUp() { - new ConfigurationSetup().setUp(FacadeConfigUtil.getHttpsConfig("stash"), STASH_PATCHSET_MAP); - stashFacade = new StashFacadeBuilder().build(); + Configuration config = new ConfigurationSetup().setUp(FacadeConfigUtil.getHttpsConfig("stash"), STASH_PATCHSET_MAP); + stashFacade = new StashFacadeBuilder().build(config); } @Test diff --git a/src/test/java/pl/touk/sputnik/connector/stash/StashFacadeTest.java b/src/test/java/pl/touk/sputnik/connector/stash/StashFacadeTest.java index 245b0e5c..03f0b28d 100644 --- a/src/test/java/pl/touk/sputnik/connector/stash/StashFacadeTest.java +++ b/src/test/java/pl/touk/sputnik/connector/stash/StashFacadeTest.java @@ -1,16 +1,5 @@ package pl.touk.sputnik.connector.stash; -import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; -import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; -import static com.github.tomakehurst.wiremock.client.WireMock.get; -import static com.github.tomakehurst.wiremock.client.WireMock.post; -import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; -import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; -import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; -import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; -import static com.github.tomakehurst.wiremock.client.WireMock.verify; -import static org.assertj.core.api.Assertions.assertThat; - import com.github.tomakehurst.wiremock.client.UrlMatchingStrategy; import com.github.tomakehurst.wiremock.junit.WireMockRule; import com.google.common.collect.ImmutableList; @@ -19,16 +8,21 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.ConfigurationSetup; import pl.touk.sputnik.connector.FacadeConfigUtil; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewFile; +import pl.touk.sputnik.review.ReviewFormatterFactory; import pl.touk.sputnik.review.Severity; import pl.touk.sputnik.review.Violation; import java.util.List; import java.util.Map; +import static com.github.tomakehurst.wiremock.client.WireMock.*; +import static org.assertj.core.api.Assertions.assertThat; + public class StashFacadeTest { private static String SOME_PULL_REQUEST_ID = "12314"; @@ -42,14 +36,15 @@ public class StashFacadeTest { ); private StashFacade stashFacade; + private Configuration config; @Rule public WireMockRule wireMockRule = new WireMockRule(FacadeConfigUtil.HTTP_PORT); @Before public void setUp() { - new ConfigurationSetup().setUp(FacadeConfigUtil.getHttpConfig("stash"), STASH_PATCHSET_MAP); - stashFacade = new StashFacadeBuilder().build(); + config = new ConfigurationSetup().setUp(FacadeConfigUtil.getHttpConfig("stash"), STASH_PATCHSET_MAP); + stashFacade = new StashFacadeBuilder().build(config); } @Test @@ -89,7 +84,7 @@ public void shouldNotAddTheSameCommentMoreThanOnce() throws Exception { "%s/rest/api/1.0/projects/%s/repos/%s/pull-requests/%s/comments", FacadeConfigUtil.PATH, SOME_PROJECT_KEY, SOME_REPOSITORY, SOME_PULL_REQUEST_ID)), "/json/stash-diff-empty.json"); - Review review = new Review(ImmutableList.of(new ReviewFile(filename))); + Review review = new Review(ImmutableList.of(new ReviewFile(filename)), ReviewFormatterFactory.get(config)); review.addError("scalastyle", new Violation(filename, 1, "error message", Severity.ERROR)); review.getMessages().add("Total 1 violations found"); diff --git a/src/test/java/pl/touk/sputnik/engine/ProcessorBuilderTest.java b/src/test/java/pl/touk/sputnik/engine/ProcessorBuilderTest.java index 8b216808..f5d7f288 100644 --- a/src/test/java/pl/touk/sputnik/engine/ProcessorBuilderTest.java +++ b/src/test/java/pl/touk/sputnik/engine/ProcessorBuilderTest.java @@ -2,6 +2,7 @@ import com.google.common.collect.ImmutableMap; import org.junit.Test; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.ConfigurationSetup; import pl.touk.sputnik.configuration.GeneralOption; @@ -13,14 +14,14 @@ public class ProcessorBuilderTest { @Test public void shouldNotBuildAnyProcessor() { - new ConfigurationSetup().setUp(Collections.emptyMap()); + Configuration config = new ConfigurationSetup().setUp(Collections.emptyMap()); - assertThat(new ProcessorBuilder().buildProcessors()).isEmpty(); + assertThat(ProcessorBuilder.buildProcessors(config)).isEmpty(); } @Test public void shouldBuildDisabledProcessors() { - new ConfigurationSetup().setUp(ImmutableMap.of( + Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of( GeneralOption.CHECKSTYLE_ENABLED.getKey(), "false", GeneralOption.FINDBUGS_ENABLED.getKey(), "false", GeneralOption.PMD_ENABLED.getKey(), "false", @@ -28,12 +29,12 @@ public void shouldBuildDisabledProcessors() { GeneralOption.CODE_NARC_ENABLED.getKey(), "false" )); - assertThat(new ProcessorBuilder().buildProcessors()).isEmpty(); + assertThat(ProcessorBuilder.buildProcessors(config)).isEmpty(); } @Test public void shouldBuildAllProcessors() { - new ConfigurationSetup().setUp(ImmutableMap.of( + Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of( GeneralOption.CHECKSTYLE_ENABLED.getKey(), "true", GeneralOption.FINDBUGS_ENABLED.getKey(), "true", GeneralOption.PMD_ENABLED.getKey(), "true", @@ -41,6 +42,6 @@ public void shouldBuildAllProcessors() { GeneralOption.CODE_NARC_ENABLED.getKey(), "true" )); - assertThat(new ProcessorBuilder().buildProcessors()).hasSize(5); + assertThat(ProcessorBuilder.buildProcessors(config)).hasSize(5); } } \ No newline at end of file diff --git a/src/test/java/pl/touk/sputnik/engine/ReviewRunnerTest.java b/src/test/java/pl/touk/sputnik/engine/ReviewRunnerTest.java index a0b25a37..65245cee 100644 --- a/src/test/java/pl/touk/sputnik/engine/ReviewRunnerTest.java +++ b/src/test/java/pl/touk/sputnik/engine/ReviewRunnerTest.java @@ -6,6 +6,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewException; import pl.touk.sputnik.review.ReviewProcessor; @@ -30,6 +31,9 @@ public class ReviewRunnerTest { @Mock private ReviewProcessor reviewProcessorMock; + @Mock + private Configuration config; + @InjectMocks private ReviewRunner reviewRunner; diff --git a/src/test/java/pl/touk/sputnik/engine/VisitorBuilderTest.java b/src/test/java/pl/touk/sputnik/engine/VisitorBuilderTest.java index 7fb88145..f15cdab3 100644 --- a/src/test/java/pl/touk/sputnik/engine/VisitorBuilderTest.java +++ b/src/test/java/pl/touk/sputnik/engine/VisitorBuilderTest.java @@ -2,6 +2,7 @@ import com.google.common.collect.ImmutableMap; import org.junit.Test; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.ConfigurationSetup; import pl.touk.sputnik.configuration.GeneralOption; import pl.touk.sputnik.engine.visitor.AfterReviewVisitor; @@ -23,27 +24,27 @@ public class VisitorBuilderTest { @Test public void shouldNotBuildBeforeVisitors() { - new ConfigurationSetup().setUp(Collections.emptyMap()); + Configuration config = new ConfigurationSetup().setUp(Collections.emptyMap()); - assertThat(new VisitorBuilder().buildBeforeReviewVisitors()).isEmpty(); + assertThat(new VisitorBuilder().buildBeforeReviewVisitors(config)).isEmpty(); } @Test public void shouldNotBuildDisabledBeforeVisitors() { - new ConfigurationSetup().setUp(ImmutableMap.of( + Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of( GeneralOption.PROCESS_TEST_FILES.getKey(), "true" )); - assertThat(new VisitorBuilder().buildBeforeReviewVisitors()).isEmpty(); + assertThat(new VisitorBuilder().buildBeforeReviewVisitors(config)).isEmpty(); } @Test public void shouldBuildBeforeVisitors() { - new ConfigurationSetup().setUp(ImmutableMap.of( + Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of( GeneralOption.PROCESS_TEST_FILES.getKey(), "false" )); - assertThat(new VisitorBuilder().buildBeforeReviewVisitors()) + assertThat(new VisitorBuilder().buildBeforeReviewVisitors(config)) .hasSize(1) .extracting("class") .containsExactly(FilterOutTestFilesVisitor.class); @@ -51,9 +52,9 @@ public void shouldBuildBeforeVisitors() { @Test public void shouldBuildAfterVisitors() { - new ConfigurationSetup().setUp(Collections.emptyMap()); + Configuration config = new ConfigurationSetup().setUp(Collections.emptyMap()); - assertThat(new VisitorBuilder().buildAfterReviewVisitors()) + assertThat(new VisitorBuilder().buildAfterReviewVisitors(config)) .hasSize(2) .extracting("class") .containsExactly(SummaryMessageVisitor.class, ScoreAlwaysPass.class); @@ -61,11 +62,11 @@ public void shouldBuildAfterVisitors() { @Test public void shouldNotBuildDisabledAfterVisitors() { - new ConfigurationSetup().setUp(ImmutableMap.of( + Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of( GeneralOption.MAX_NUMBER_OF_COMMENTS.getKey(), "0" )); - assertThat(new VisitorBuilder().buildAfterReviewVisitors()) + assertThat(new VisitorBuilder().buildAfterReviewVisitors(config)) .hasSize(2) .extracting("class") .containsExactly(SummaryMessageVisitor.class, ScoreAlwaysPass.class); @@ -73,11 +74,11 @@ public void shouldNotBuildDisabledAfterVisitors() { @Test public void shouldBuildFilterOutCommentVisitor() { - new ConfigurationSetup().setUp(ImmutableMap.of( + Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of( GeneralOption.MAX_NUMBER_OF_COMMENTS.getKey(), "50" )); - assertThat(new VisitorBuilder().buildAfterReviewVisitors()) + assertThat(new VisitorBuilder().buildAfterReviewVisitors(config)) .hasSize(3) .extracting("class") .containsExactly(SummaryMessageVisitor.class, LimitCommentVisitor.class, ScoreAlwaysPass.class); @@ -85,11 +86,11 @@ public void shouldBuildFilterOutCommentVisitor() { @Test public void shouldBuildNoScoreVisitor() { - new ConfigurationSetup().setUp(ImmutableMap.of( + Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of( GeneralOption.SCORE_STRATEGY.getKey(), "NOscore" )); - assertThat(new VisitorBuilder().buildAfterReviewVisitors()) + assertThat(new VisitorBuilder().buildAfterReviewVisitors(config)) .hasSize(2) .extracting("class") .containsExactly(SummaryMessageVisitor.class, NoScore.class); @@ -97,13 +98,13 @@ public void shouldBuildNoScoreVisitor() { @Test public void shouldBuildScoreAlwaysPassVisitor() { - new ConfigurationSetup().setUp(ImmutableMap.of( + Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of( GeneralOption.SCORE_STRATEGY.getKey(), "scoreAlwaysPass", GeneralOption.SCORE_PASSING_KEY.getKey(), "Verified", GeneralOption.SCORE_PASSING_VALUE.getKey(), "2" )); - List afterReviewVisitors = new VisitorBuilder().buildAfterReviewVisitors(); + List afterReviewVisitors = new VisitorBuilder().buildAfterReviewVisitors(config); assertThat(afterReviewVisitors) .hasSize(2) @@ -114,7 +115,7 @@ public void shouldBuildScoreAlwaysPassVisitor() { @Test public void shouldBuildScorePassIfEmptyVisitor() { - new ConfigurationSetup().setUp(ImmutableMap.of( + Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of( GeneralOption.SCORE_STRATEGY.getKey(), "SCOREPASSIFEMPTY", GeneralOption.SCORE_PASSING_KEY.getKey(), "Verified", GeneralOption.SCORE_PASSING_VALUE.getKey(), "3", @@ -122,7 +123,7 @@ public void shouldBuildScorePassIfEmptyVisitor() { GeneralOption.SCORE_FAILING_VALUE.getKey(), "-3" )); - List afterReviewVisitors = new VisitorBuilder().buildAfterReviewVisitors(); + List afterReviewVisitors = new VisitorBuilder().buildAfterReviewVisitors(config); assertThat(afterReviewVisitors) .hasSize(2) @@ -134,7 +135,7 @@ public void shouldBuildScorePassIfEmptyVisitor() { @Test public void shouldBuildScorePassIfNoErrorsVisitor() { - new ConfigurationSetup().setUp(ImmutableMap.of( + Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of( GeneralOption.SCORE_STRATEGY.getKey(), "SCOREPassIfNoErrors", GeneralOption.SCORE_PASSING_KEY.getKey(), "Code-Review", GeneralOption.SCORE_PASSING_VALUE.getKey(), "1", @@ -142,7 +143,7 @@ public void shouldBuildScorePassIfNoErrorsVisitor() { GeneralOption.SCORE_FAILING_VALUE.getKey(), "-2" )); - List afterReviewVisitors = new VisitorBuilder().buildAfterReviewVisitors(); + List afterReviewVisitors = new VisitorBuilder().buildAfterReviewVisitors(config); assertThat(afterReviewVisitors) .hasSize(2) @@ -154,11 +155,11 @@ public void shouldBuildScorePassIfNoErrorsVisitor() { @Test public void shouldBuildDefaultScoreAlwaysPassIfStrategyIsUnknown() { - new ConfigurationSetup().setUp(ImmutableMap.of( + Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of( GeneralOption.SCORE_STRATEGY.getKey(), "mySimpleStrategy" )); - List afterReviewVisitors = new VisitorBuilder().buildAfterReviewVisitors(); + List afterReviewVisitors = new VisitorBuilder().buildAfterReviewVisitors(config); assertThat(afterReviewVisitors) .hasSize(2) diff --git a/src/test/java/pl/touk/sputnik/engine/visitor/FilterOutTestFilesVisitorTest.java b/src/test/java/pl/touk/sputnik/engine/visitor/FilterOutTestFilesVisitorTest.java index 60c3d9f1..0ca7f9ed 100644 --- a/src/test/java/pl/touk/sputnik/engine/visitor/FilterOutTestFilesVisitorTest.java +++ b/src/test/java/pl/touk/sputnik/engine/visitor/FilterOutTestFilesVisitorTest.java @@ -1,10 +1,14 @@ package pl.touk.sputnik.engine.visitor; import org.junit.Test; +import pl.touk.sputnik.configuration.Configuration; +import pl.touk.sputnik.configuration.ConfigurationSetup; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewFile; +import pl.touk.sputnik.review.ReviewFormatterFactory; import java.util.Arrays; +import java.util.Collections; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -14,7 +18,8 @@ public class FilterOutTestFilesVisitorTest { @Test public void shouldFilterOutTestFiles() { - Review review = new Review(Arrays.asList(createReviewFile(true), createReviewFile(false))); + Configuration config = new ConfigurationSetup().setUp(Collections.emptyMap()); + Review review = new Review(Arrays.asList(createReviewFile(true), createReviewFile(false)), ReviewFormatterFactory.get(config)); new FilterOutTestFilesVisitor().beforeReview(review); diff --git a/src/test/java/pl/touk/sputnik/engine/visitor/LimitCommentVisitorTest.java b/src/test/java/pl/touk/sputnik/engine/visitor/LimitCommentVisitorTest.java index 046a86b4..f42792b3 100644 --- a/src/test/java/pl/touk/sputnik/engine/visitor/LimitCommentVisitorTest.java +++ b/src/test/java/pl/touk/sputnik/engine/visitor/LimitCommentVisitorTest.java @@ -2,15 +2,20 @@ import org.junit.Test; import pl.touk.sputnik.ReviewBuilder; +import pl.touk.sputnik.configuration.Configuration; +import pl.touk.sputnik.configuration.ConfigurationSetup; import pl.touk.sputnik.review.Review; +import java.util.Collections; + import static org.assertj.core.api.Assertions.assertThat; public class LimitCommentVisitorTest { @Test public void shouldNotLimitCommentsIfCountIsBelowMaximumCount() { - Review review = ReviewBuilder.buildReview(); + Configuration config = new ConfigurationSetup().setUp(Collections.emptyMap()); + Review review = ReviewBuilder.buildReview(config); new LimitCommentVisitor(10).afterReview(review); @@ -24,7 +29,8 @@ public void shouldNotLimitCommentsIfCountIsBelowMaximumCount() { @Test public void shouldLimitCommentsIfCountIsHigherMaximumCount() { - Review review = ReviewBuilder.buildReview(); + Configuration config = new ConfigurationSetup().setUp(Collections.emptyMap()); + Review review = ReviewBuilder.buildReview(config); new LimitCommentVisitor(3).afterReview(review); diff --git a/src/test/java/pl/touk/sputnik/engine/visitor/SummaryMessageVisitorTest.java b/src/test/java/pl/touk/sputnik/engine/visitor/SummaryMessageVisitorTest.java index 32bad4f2..880bdc80 100644 --- a/src/test/java/pl/touk/sputnik/engine/visitor/SummaryMessageVisitorTest.java +++ b/src/test/java/pl/touk/sputnik/engine/visitor/SummaryMessageVisitorTest.java @@ -1,14 +1,12 @@ package pl.touk.sputnik.engine.visitor; import org.junit.Test; +import pl.touk.sputnik.TestEnvironment; import pl.touk.sputnik.review.Review; -import pl.touk.sputnik.review.ReviewFile; - -import java.util.Collections; import static org.assertj.core.api.Assertions.assertThat; -public class SummaryMessageVisitorTest { +public class SummaryMessageVisitorTest extends TestEnvironment { private static final String TOTAL_8_VIOLATIONS_FOUND = "Total 8 violations found"; private static final String PROBLEM_SOURCE = "PMD"; @@ -17,7 +15,7 @@ public class SummaryMessageVisitorTest { @Test public void shouldAddSummaryMessage() { - Review review = new Review(Collections.emptyList()); + Review review = review(); review.setTotalViolationCount(8); new SummaryMessageVisitor("Perfect").afterReview(review); @@ -27,7 +25,7 @@ public void shouldAddSummaryMessage() { @Test public void shouldAddPerfectMessageIfThereAreNoViolationsFound() { - Review review = new Review(Collections.emptyList()); + Review review = review(); review.setTotalViolationCount(0); new SummaryMessageVisitor("Perfect").afterReview(review); @@ -37,7 +35,7 @@ public void shouldAddPerfectMessageIfThereAreNoViolationsFound() { @Test public void shouldAddProblemMessagesPerfectMessageIfThereAreNoViolationsFound() { - Review review = new Review(Collections.emptyList()); + Review review = review(); review.setTotalViolationCount(8); review.addProblem(PROBLEM_SOURCE, PROBLEM_MESSAGE); diff --git a/src/test/java/pl/touk/sputnik/engine/visitor/score/NoScoreTest.java b/src/test/java/pl/touk/sputnik/engine/visitor/score/NoScoreTest.java index 4ae06e5d..c9c8e07f 100644 --- a/src/test/java/pl/touk/sputnik/engine/visitor/score/NoScoreTest.java +++ b/src/test/java/pl/touk/sputnik/engine/visitor/score/NoScoreTest.java @@ -1,18 +1,16 @@ package pl.touk.sputnik.engine.visitor.score; import org.junit.Test; +import pl.touk.sputnik.TestEnvironment; import pl.touk.sputnik.review.Review; -import pl.touk.sputnik.review.ReviewFile; - -import java.util.Collections; import static org.assertj.core.api.Assertions.assertThat; -public class NoScoreTest { +public class NoScoreTest extends TestEnvironment { @Test public void shouldAddNoScoreToReview() { - Review review = new Review(Collections.emptyList()); + Review review = review(); new NoScore().afterReview(review); diff --git a/src/test/java/pl/touk/sputnik/engine/visitor/score/ScoreAlwaysPassTest.java b/src/test/java/pl/touk/sputnik/engine/visitor/score/ScoreAlwaysPassTest.java index e47f836b..1567b393 100644 --- a/src/test/java/pl/touk/sputnik/engine/visitor/score/ScoreAlwaysPassTest.java +++ b/src/test/java/pl/touk/sputnik/engine/visitor/score/ScoreAlwaysPassTest.java @@ -2,19 +2,23 @@ import com.google.common.collect.ImmutableMap; import org.junit.Test; +import pl.touk.sputnik.TestEnvironment; +import pl.touk.sputnik.configuration.Configuration; +import pl.touk.sputnik.configuration.ConfigurationSetup; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewFile; +import pl.touk.sputnik.review.ReviewFormatter; import java.util.Collections; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; -public class ScoreAlwaysPassTest { +public class ScoreAlwaysPassTest extends TestEnvironment { @Test public void shouldAddScoreToReview() { - Review review = new Review(Collections.emptyList()); + Review review = review(); new ScoreAlwaysPass(ImmutableMap.of("Sputnik-Pass", (short) 1)).afterReview(review); diff --git a/src/test/java/pl/touk/sputnik/processor/checkstyle/CheckstyleProcessorTest.java b/src/test/java/pl/touk/sputnik/processor/checkstyle/CheckstyleProcessorTest.java index 82d547d0..fe02d44c 100644 --- a/src/test/java/pl/touk/sputnik/processor/checkstyle/CheckstyleProcessorTest.java +++ b/src/test/java/pl/touk/sputnik/processor/checkstyle/CheckstyleProcessorTest.java @@ -1,19 +1,30 @@ package pl.touk.sputnik.processor.checkstyle; +import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import pl.touk.sputnik.TestEnvironment; +import pl.touk.sputnik.configuration.Configuration; +import pl.touk.sputnik.configuration.ConfigurationSetup; +import pl.touk.sputnik.processor.scalastyle.ScalastyleProcessor; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewResult; +import java.util.Collections; + import static org.assertj.core.api.Assertions.assertThat; public class CheckstyleProcessorTest extends TestEnvironment { - private final CheckstyleProcessor fixture = new CheckstyleProcessor(); + private CheckstyleProcessor fixture; @Mock private Review review; + @Before + public void setUp() throws Exception { + super.setUp(); + fixture = new CheckstyleProcessor(config); + } @Test public void shouldReturnBasicSunViolationsOnSimpleClass() { diff --git a/src/test/java/pl/touk/sputnik/processor/codenarc/CodeNarcProcessorTest.java b/src/test/java/pl/touk/sputnik/processor/codenarc/CodeNarcProcessorTest.java index 4cc7f9fb..26619e4f 100644 --- a/src/test/java/pl/touk/sputnik/processor/codenarc/CodeNarcProcessorTest.java +++ b/src/test/java/pl/touk/sputnik/processor/codenarc/CodeNarcProcessorTest.java @@ -1,10 +1,15 @@ package pl.touk.sputnik.processor.codenarc; -import org.junit.After; import org.junit.Before; import org.junit.Test; -import pl.touk.sputnik.configuration.ConfigurationHolder; -import pl.touk.sputnik.review.*; +import pl.touk.sputnik.configuration.Configuration; +import pl.touk.sputnik.configuration.ConfigurationBuilder; +import pl.touk.sputnik.review.Review; +import pl.touk.sputnik.review.ReviewFile; +import pl.touk.sputnik.review.ReviewFormatterFactory; +import pl.touk.sputnik.review.ReviewResult; +import pl.touk.sputnik.review.Severity; +import pl.touk.sputnik.review.Violation; import java.util.ArrayList; import java.util.List; @@ -12,7 +17,7 @@ import static org.assertj.core.api.Assertions.assertThat; public class CodeNarcProcessorTest { - private final CodeNarcProcessor sut = new CodeNarcProcessor(); + private CodeNarcProcessor sut; private static final String CONFIGURATION_WITH_BASIC_AND_IMPORT_RULE_SET_AND_EXCLUDE = "codeNarc/configuration/configurationWithBasicAndImportRuleSetAndExclude.properties"; private static final String CONFIGURATION_WITH_BASIC_RULE_SET = "codeNarc/configuration/configurationWithBasicRuleSet.properties"; @@ -25,16 +30,13 @@ public class CodeNarcProcessorTest { private final String REVIEW_FILE_WITHOUT_VIOLATIONS = "src/test/resources/codeNarc/testFiles/FileWithoutViolations.groovy"; private final String REVIEW_FILE_WITH_IMPORT_VIOLATION = "src/test/resources/codeNarc/testFiles/FileWithImportViolation.groovy"; private final String REVIEW_FILE_WITH_NOT_GROOVY_EXTENSION = "src/test/resources/wrongExtension.java"; - private final String REVIEW_FILE_WITHOUT_EXTENSION = "src/test/resources/withoutExtension"; + + private Configuration config; @Before public void setUp() throws Exception { - ConfigurationHolder.initFromResource(CONFIGURATION_WITH_BASIC_RULE_SET); - } - - @After - public void tearDown() throws Exception { - ConfigurationHolder.reset(); + config = ConfigurationBuilder.initFromResource(CONFIGURATION_WITH_BASIC_RULE_SET); + sut = new CodeNarcProcessor(config); } @Test @@ -104,7 +106,8 @@ public void shouldReturnViolationsFromManyFiles() { @Test public void shouldReturnViolationsUsingImportRuleSet() { - ConfigurationHolder.initFromResource(CONFIGURATION_WITH_IMPORT_RULE_SET); + config = ConfigurationBuilder.initFromResource(CONFIGURATION_WITH_IMPORT_RULE_SET); + sut = new CodeNarcProcessor(config); Review review = getReview(REVIEW_FILE_WITH_IMPORT_VIOLATION, REVIEW_FILE_WITH_ONE_VIOLATION); ReviewResult result = sut.process(review); @@ -120,7 +123,8 @@ public void shouldReturnViolationsUsingImportRuleSet() { @Test public void shouldReturnViolationsUsingDefaultRuleSetFromResources() { - ConfigurationHolder.initFromResource(CONFIGURATION_WITHOUT_RULE_SET); + config = ConfigurationBuilder.initFromResource(CONFIGURATION_WITHOUT_RULE_SET); + sut = new CodeNarcProcessor(config); Review review = getReview(REVIEW_FILE_WITH_ONE_VIOLATION_PER_EACH_SEVERITY); ReviewResult result = sut.process(review); @@ -137,7 +141,8 @@ public void shouldReturnViolationsUsingDefaultRuleSetFromResources() { @Test public void shouldReturnViolationsUsingImportAndBasicRuleSets() { - ConfigurationHolder.initFromResource(CONFIGURATION_WITH_BASIC_AND_IMPORT_RULE_SET); + config = ConfigurationBuilder.initFromResource(CONFIGURATION_WITH_BASIC_AND_IMPORT_RULE_SET); + sut = new CodeNarcProcessor(config); Review review = getReview(REVIEW_FILE_WITH_ONE_VIOLATION, REVIEW_FILE_WITH_IMPORT_VIOLATION); ReviewResult result = sut.process(review); @@ -155,7 +160,8 @@ public void shouldReturnViolationsUsingImportAndBasicRuleSets() { @Test public void shouldNotReturnViolationsFromExcudedFiles() { - ConfigurationHolder.initFromResource(CONFIGURATION_WITH_BASIC_AND_IMPORT_RULE_SET_AND_EXCLUDE); + config = ConfigurationBuilder.initFromResource(CONFIGURATION_WITH_BASIC_AND_IMPORT_RULE_SET_AND_EXCLUDE); + sut = new CodeNarcProcessor(config); Review review = getReview(REVIEW_FILE_WITH_ONE_VIOLATION, REVIEW_FILE_WITHOUT_VIOLATIONS, REVIEW_FILE_WITH_ONE_VIOLATION_PER_EACH_SEVERITY); ReviewResult result = sut.process(review); @@ -193,6 +199,6 @@ private Review getReview(String... filePaths) { for (String filePath : filePaths) { files.add(new ReviewFile(filePath)); } - return new Review(files); + return new Review(files, ReviewFormatterFactory.get(config)); } } \ No newline at end of file diff --git a/src/test/java/pl/touk/sputnik/processor/findbugs/FindBugsProcessorTest.java b/src/test/java/pl/touk/sputnik/processor/findbugs/FindBugsProcessorTest.java index ce92e8cc..bfa4ae27 100644 --- a/src/test/java/pl/touk/sputnik/processor/findbugs/FindBugsProcessorTest.java +++ b/src/test/java/pl/touk/sputnik/processor/findbugs/FindBugsProcessorTest.java @@ -10,14 +10,12 @@ import pl.touk.sputnik.configuration.ConfigurationSetup; import pl.touk.sputnik.configuration.GeneralOption; import pl.touk.sputnik.review.Review; -import pl.touk.sputnik.review.ReviewException; import pl.touk.sputnik.review.ReviewFile; +import pl.touk.sputnik.review.ReviewFormatterFactory; import pl.touk.sputnik.review.ReviewResult; import java.util.List; -import static com.googlecode.catchexception.CatchException.catchException; -import static com.googlecode.catchexception.CatchException.caughtException; import static org.assertj.core.api.Assertions.assertThat; @RunWith(MockitoJUnitRunner.class) @@ -27,15 +25,15 @@ public class FindBugsProcessorTest extends TestEnvironment { @Before public void setUp() throws Exception { - new ConfigurationSetup().setUp(ImmutableMap.of(GeneralOption.BUILD_TOOL.getKey(), "gradle")); - findBugsProcessor = new FindBugsProcessor(); + config = new ConfigurationSetup().setUp(ImmutableMap.of(GeneralOption.BUILD_TOOL.getKey(), "gradle")); + findBugsProcessor = new FindBugsProcessor(config); } @Test public void shouldReturnBasicViolationsOnEmptyClass() { //given List files = ImmutableList.of(new ReviewFile("src/test/java/toreview/TestClass.java")); - Review review = new Review(files); + Review review = new Review(files, ReviewFormatterFactory.get(config)); //when ReviewResult reviewResult = findBugsProcessor.process(review); diff --git a/src/test/java/pl/touk/sputnik/processor/jshint/JsHintProcessorTest.java b/src/test/java/pl/touk/sputnik/processor/jshint/JsHintProcessorTest.java index 24b409c2..b90e5bce 100644 --- a/src/test/java/pl/touk/sputnik/processor/jshint/JsHintProcessorTest.java +++ b/src/test/java/pl/touk/sputnik/processor/jshint/JsHintProcessorTest.java @@ -1,38 +1,34 @@ package pl.touk.sputnik.processor.jshint; -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.After; +import com.google.common.collect.ImmutableList; +import com.google.common.io.Resources; import org.junit.Before; import org.junit.Test; - -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; +import pl.touk.sputnik.configuration.ConfigurationBuilder; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewFile; +import pl.touk.sputnik.review.ReviewFormatterFactory; import pl.touk.sputnik.review.ReviewResult; -import com.google.common.collect.ImmutableList; -import com.google.common.io.Resources; +import static org.assertj.core.api.Assertions.assertThat; public class JsHintProcessorTest { - private final JsHintProcessor fixture = new JsHintProcessor(); + private JsHintProcessor fixture; + private Configuration config; @Before public void setUp() throws Exception { - ConfigurationHolder.initFromResource("jshint/sputnik/noConfigurationFile.properties"); - } - - @After - public void tearDown() throws Exception { - ConfigurationHolder.reset(); + config = ConfigurationBuilder.initFromResource("jshint/sputnik/noConfigurationFile.properties"); + fixture = new JsHintProcessor(config); } @Test public void shouldReturnEmptyResultWhenNoFilesToReview() { // given - Review review = new Review(ImmutableList.of(new ReviewFile("test"))); + Review review = new Review(ImmutableList.of(new ReviewFile("test")), ReviewFormatterFactory.get(config)); // when ReviewResult reviewResult = fixture.process(review); @@ -45,7 +41,7 @@ public void shouldReturnEmptyResultWhenNoFilesToReview() { @Test public void shouldReturnNoViolationsOnSimpleFunction() { // given - Review review = new Review(ImmutableList.of(new ReviewFile(Resources.getResource("js/test.js").getFile()))); + Review review = new Review(ImmutableList.of(new ReviewFile(Resources.getResource("js/test.js").getFile())), ReviewFormatterFactory.get(config)); // when ReviewResult reviewResult = fixture.process(review); @@ -58,8 +54,9 @@ public void shouldReturnNoViolationsOnSimpleFunction() { @Test public void shouldReturnOneViolationWithConfigurationOnSimpleFunction() { // given - ConfigurationHolder.initFromResource("jshint/sputnik/withConfigurationFile.properties"); - Review review = new Review(ImmutableList.of(new ReviewFile(Resources.getResource("js/test.js").getFile()))); + config = ConfigurationBuilder.initFromResource("jshint/sputnik/withConfigurationFile.properties"); + fixture = new JsHintProcessor(config); + Review review = new Review(ImmutableList.of(new ReviewFile(Resources.getResource("js/test.js").getFile())), ReviewFormatterFactory.get(config)); // when ReviewResult reviewResult = fixture.process(review); diff --git a/src/test/java/pl/touk/sputnik/processor/jslint/JsLintProcessorTest.java b/src/test/java/pl/touk/sputnik/processor/jslint/JsLintProcessorTest.java index 0fa49401..cfa9f562 100644 --- a/src/test/java/pl/touk/sputnik/processor/jslint/JsLintProcessorTest.java +++ b/src/test/java/pl/touk/sputnik/processor/jslint/JsLintProcessorTest.java @@ -1,39 +1,32 @@ package pl.touk.sputnik.processor.jslint; +import com.google.common.collect.ImmutableList; +import com.google.common.io.Resources; +import org.junit.Before; +import org.junit.Test; import pl.touk.sputnik.TestEnvironment; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.ConfigurationBuilder; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewFile; +import pl.touk.sputnik.review.ReviewFormatterFactory; import pl.touk.sputnik.review.ReviewResult; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.google.common.collect.ImmutableList; -import com.google.common.io.Resources; - import static org.assertj.core.api.Assertions.assertThat; public class JsLintProcessorTest extends TestEnvironment { - private final JsLintProcessor fixture = new JsLintProcessor(); - + private JsLintProcessor fixture; @Before public void setUp() throws Exception { - ConfigurationHolder.initFromResource("jslint/sputnik/noConfigurationFile.properties"); - } - - @After - public void tearDown() throws Exception { - ConfigurationHolder.reset(); + config = ConfigurationBuilder.initFromResource("jslint/sputnik/noConfigurationFile.properties"); + fixture = new JsLintProcessor(config); } @Test public void shouldReturnEmptyResultWhenNoFilesToReview() { // given - Review review = new Review(ImmutableList.of(new ReviewFile("test"))); + Review review = new Review(ImmutableList.of(new ReviewFile("test")), ReviewFormatterFactory.get(config)); // when ReviewResult reviewResult = fixture.process(review); @@ -46,7 +39,7 @@ public void shouldReturnEmptyResultWhenNoFilesToReview() { @Test public void shouldReturnNoViolationsOnSimpleFunction() { // given - Review review = new Review(ImmutableList.of(new ReviewFile(Resources.getResource("js/test.js").getFile()))); + Review review = new Review(ImmutableList.of(new ReviewFile(Resources.getResource("js/test.js").getFile())), ReviewFormatterFactory.get(config)); // when ReviewResult reviewResult = fixture.process(review); @@ -64,8 +57,8 @@ public void shouldReturnNoViolationsOnSimpleFunction() { @Test public void shouldReturnOneViolationWithConfigurationOnSimpleFunction() { // given - ConfigurationHolder.initFromResource("jslint/sputnik/withConfigurationFile.properties"); - Review review = new Review(ImmutableList.of(new ReviewFile(Resources.getResource("js/test.js").getFile()))); + config = ConfigurationBuilder.initFromResource("jslint/sputnik/withConfigurationFile.properties"); + Review review = new Review(ImmutableList.of(new ReviewFile(Resources.getResource("js/test.js").getFile())), ReviewFormatterFactory.get(config)); // when ReviewResult reviewResult = fixture.process(review); diff --git a/src/test/java/pl/touk/sputnik/processor/pmd/CollectorRendererTest.java b/src/test/java/pl/touk/sputnik/processor/pmd/CollectorRendererTest.java index cb691fd0..f096b23c 100644 --- a/src/test/java/pl/touk/sputnik/processor/pmd/CollectorRendererTest.java +++ b/src/test/java/pl/touk/sputnik/processor/pmd/CollectorRendererTest.java @@ -9,6 +9,7 @@ import org.jetbrains.annotations.NotNull; import org.junit.Test; import pl.touk.sputnik.TestEnvironment; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.ConfigurationSetup; import pl.touk.sputnik.configuration.GeneralOption; import pl.touk.sputnik.review.Severity; @@ -16,6 +17,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @@ -24,7 +26,7 @@ public class CollectorRendererTest extends TestEnvironment { - private CollectorRenderer renderer = new CollectorRenderer(); + private CollectorRenderer renderer; private static final String VIOLATION_DESCRIPTION = "this is bug!"; private static final String RULE_DESCRIPTION = "...and should be fixed"; @@ -33,9 +35,9 @@ public class CollectorRendererTest extends TestEnvironment { @Test public void shouldReportViolationWithDetails() throws IOException { - new ConfigurationSetup().setUp(ImmutableMap.of(GeneralOption.PMD_SHOW_VIOLATION_DETAILS.getKey(), "true")); - Rule rule = createRule(RULE_DESCRIPTION, EXTERNAL_INFO_URL, RulePriority.HIGH); - Report report = createReportWithVolation(createRuleViolation(rule, VIOLATION_DESCRIPTION)); + Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of(GeneralOption.PMD_SHOW_VIOLATION_DETAILS.getKey(), "true")); + Rule rule = createRule(RULE_DESCRIPTION, EXTERNAL_INFO_URL, RulePriority.HIGH, config); + Report report = createReportWithVolation(createRuleViolation(rule, VIOLATION_DESCRIPTION, config), config); renderer.renderFileReport(report); @@ -46,9 +48,9 @@ public void shouldReportViolationWithDetails() throws IOException { @Test public void shouldReportViolationWithoutDetails() throws IOException { - new ConfigurationSetup().setUp(ImmutableMap.of(GeneralOption.PMD_SHOW_VIOLATION_DETAILS.getKey(), "false")); - Rule rule = createRule(RULE_DESCRIPTION, EXTERNAL_INFO_URL, RulePriority.MEDIUM); - Report report = createReportWithVolation(createRuleViolation(rule, VIOLATION_DESCRIPTION)); + Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of(GeneralOption.PMD_SHOW_VIOLATION_DETAILS.getKey(), "false")); + Rule rule = createRule(RULE_DESCRIPTION, EXTERNAL_INFO_URL, RulePriority.MEDIUM, config); + Report report = createReportWithVolation(createRuleViolation(rule, VIOLATION_DESCRIPTION, config), config); renderer.renderFileReport(report); @@ -57,7 +59,8 @@ public void shouldReportViolationWithoutDetails() throws IOException { } @NotNull - private Rule createRule(String ruleDescription, String externalInfoUrl, RulePriority priority) { + private Rule createRule(String ruleDescription, String externalInfoUrl, RulePriority priority, @NotNull Configuration config) { + renderer = new CollectorRenderer(config); Rule rule = mock(Rule.class); when(rule.getDescription()).thenReturn(ruleDescription); when(rule.getExternalInfoUrl()).thenReturn(externalInfoUrl); @@ -67,7 +70,8 @@ private Rule createRule(String ruleDescription, String externalInfoUrl, RulePrio } @NotNull - private RuleViolation createRuleViolation(@NotNull Rule rule, String violationDescription) { + private RuleViolation createRuleViolation(@NotNull Rule rule, String violationDescription, @NotNull Configuration config) { + renderer = new CollectorRenderer(config); RuleViolation violation = mock(RuleViolation.class); when(violation.getRule()).thenReturn(rule); when(violation.getDescription()).thenReturn(violationDescription); @@ -76,7 +80,8 @@ private RuleViolation createRuleViolation(@NotNull Rule rule, String violationDe } @NotNull - private Report createReportWithVolation(@NotNull RuleViolation violation) { + private Report createReportWithVolation(@NotNull RuleViolation violation, @NotNull Configuration config) { + renderer = new CollectorRenderer(config); Report report = mock(Report.class); List list = new ArrayList(); list.add(violation); diff --git a/src/test/java/pl/touk/sputnik/processor/pmd/PmdProcessorTest.java b/src/test/java/pl/touk/sputnik/processor/pmd/PmdProcessorTest.java index c22ac76e..a3002075 100644 --- a/src/test/java/pl/touk/sputnik/processor/pmd/PmdProcessorTest.java +++ b/src/test/java/pl/touk/sputnik/processor/pmd/PmdProcessorTest.java @@ -1,18 +1,32 @@ package pl.touk.sputnik.processor.pmd; +import org.junit.Before; import org.junit.Test; import pl.touk.sputnik.TestEnvironment; +import pl.touk.sputnik.configuration.Configuration; +import pl.touk.sputnik.configuration.ConfigurationBuilder; +import pl.touk.sputnik.configuration.ConfigurationSetup; +import pl.touk.sputnik.processor.jslint.JsLintProcessor; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewException; import pl.touk.sputnik.review.ReviewResult; + +import java.util.Collections; + import static com.googlecode.catchexception.CatchException.catchException; import static com.googlecode.catchexception.CatchException.caughtException; import static org.assertj.core.api.Assertions.assertThat; public class PmdProcessorTest extends TestEnvironment { - private final PmdProcessor fixture = new PmdProcessor(); + private PmdProcessor fixture; + + @Before + public void setUp() throws Exception { + super.setUp(); + fixture = new PmdProcessor(config); + } @Test public void shouldReturnPmdViolations() { diff --git a/src/test/java/pl/touk/sputnik/processor/scalastyle/ScalastyleProcessorTest.java b/src/test/java/pl/touk/sputnik/processor/scalastyle/ScalastyleProcessorTest.java index 33319738..44b434fa 100644 --- a/src/test/java/pl/touk/sputnik/processor/scalastyle/ScalastyleProcessorTest.java +++ b/src/test/java/pl/touk/sputnik/processor/scalastyle/ScalastyleProcessorTest.java @@ -1,14 +1,25 @@ package pl.touk.sputnik.processor.scalastyle; +import org.junit.Before; import org.junit.Test; import pl.touk.sputnik.TestEnvironment; +import pl.touk.sputnik.configuration.Configuration; +import pl.touk.sputnik.configuration.ConfigurationSetup; import pl.touk.sputnik.review.ReviewResult; +import java.util.Collections; + import static org.assertj.core.api.Assertions.assertThat; public class ScalastyleProcessorTest extends TestEnvironment { - private final ScalastyleProcessor fixture = new ScalastyleProcessor(); + private ScalastyleProcessor fixture; + + @Before + public void setUp() throws Exception { + super.setUp(); + fixture = new ScalastyleProcessor(config); + } @Test public void shouldReturnScalastyleViolations() { diff --git a/src/test/java/pl/touk/sputnik/processor/sonar/SonarProcessorTest.java b/src/test/java/pl/touk/sputnik/processor/sonar/SonarProcessorTest.java index fe313ca2..e572a908 100644 --- a/src/test/java/pl/touk/sputnik/processor/sonar/SonarProcessorTest.java +++ b/src/test/java/pl/touk/sputnik/processor/sonar/SonarProcessorTest.java @@ -1,22 +1,21 @@ package pl.touk.sputnik.processor.sonar; -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.File; -import java.io.IOException; - -import org.junit.Test; - import com.google.common.collect.ImmutableList; -import com.google.common.io.Resources; - +import org.junit.Test; import pl.touk.sputnik.TestEnvironment; +import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewFile; +import pl.touk.sputnik.review.ReviewFormatterFactory; import pl.touk.sputnik.review.ReviewResult; import pl.touk.sputnik.review.Severity; import pl.touk.sputnik.review.Violation; +import java.io.File; +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + public class SonarProcessorTest extends TestEnvironment { @Test @@ -29,9 +28,9 @@ public void shouldFilterResultFiles() { ReviewFile r1 = new ReviewFile("src/t/f.cs"); ReviewFile r2 = new ReviewFile("src/t/f2.cs"); - Review review = new Review(ImmutableList.of(r1, r2)); + Review review = new Review(ImmutableList.of(r1, r2), ReviewFormatterFactory.get(config)); - ReviewResult filteredResults = new SonarProcessor().filterResults(results, review); + ReviewResult filteredResults = new SonarProcessor(config).filterResults(results, review); assertThat(filteredResults.getViolations()) .extracting("filenameOrJavaClassName") .containsExactly("src/t/f.cs", "src/t/f2.cs"); @@ -40,14 +39,14 @@ public void shouldFilterResultFiles() { @Test public void shouldReportViolations() { SonarProcessor processor = new SonarProcessor(new SonarRunnerBuilder() { - public SonarRunner prepareRunner(Review review) { - return new SonarRunner(null, null) { + public SonarRunner prepareRunner(Review review, Configuration configuration) { + return new SonarRunner(null, null, null) { public File run() throws IOException { return getResourceAsFile("json/sonar-result.json"); } }; } - }); + }, config); ReviewResult result = processor.process(nonexistantReview("src/module2/dir/file2.cs")); assertThat(result.getViolations()).hasSize(3); } diff --git a/src/test/java/pl/touk/sputnik/processor/sonar/SonarRunnerTest.java b/src/test/java/pl/touk/sputnik/processor/sonar/SonarRunnerTest.java index 890ba70f..971f8b6e 100644 --- a/src/test/java/pl/touk/sputnik/processor/sonar/SonarRunnerTest.java +++ b/src/test/java/pl/touk/sputnik/processor/sonar/SonarRunnerTest.java @@ -7,8 +7,6 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.PrintWriter; -import java.util.Arrays; import java.util.List; import java.util.Properties; @@ -21,7 +19,8 @@ import org.mockito.runners.MockitoJUnitRunner; import org.sonar.runner.api.EmbeddedRunner; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; +import pl.touk.sputnik.configuration.ConfigurationBuilder; import com.google.common.collect.ImmutableList; @@ -33,17 +32,18 @@ public class SonarRunnerTest { private static final String PROPERTY_1 = "sonar-property1"; private static final String PROPERTY_2 = "sonar-property2"; + private Configuration config; + @Mock private EmbeddedRunner sonarRunner; @Before public void setUp() throws FileNotFoundException { - ConfigurationHolder.initFromResource("test-sonar.properties"); + config = ConfigurationBuilder.initFromResource("test-sonar.properties"); } @After public void tearDown() { - ConfigurationHolder.reset(); new File(PROPERTY_1).delete(); new File(PROPERTY_2).delete(); } @@ -57,7 +57,7 @@ private void writeValidConfigFiles() throws IOException { public void shouldRun() throws IOException { writeValidConfigFiles(); List files = ImmutableList.of("file"); - SonarRunner runner = new SonarRunner(files, sonarRunner); + SonarRunner runner = new SonarRunner(files, sonarRunner, config); runner.run(); verify(sonarRunner).addProperties(any(Properties.class)); verify(sonarRunner).execute(); @@ -67,7 +67,7 @@ public void shouldRun() throws IOException { public void shouldLoadBaseProperties() throws IOException{ writeValidConfigFiles(); List files = ImmutableList.of("file"); - SonarRunner runner = new SonarRunner(files, sonarRunner); + SonarRunner runner = new SonarRunner(files, sonarRunner, config); Properties properties = runner.loadBaseProperties(); assertThat(properties.getProperty("sonar.foo")).isEqualTo("bar"); assertThat(properties.getProperty("sonar.bar")).isEqualTo("bazz"); @@ -77,7 +77,7 @@ public void shouldLoadBaseProperties() throws IOException{ public void shouldSetBaseSonarConfig() throws IOException{ writeValidConfigFiles(); List files = ImmutableList.of("first", "second"); - SonarRunner runner = new SonarRunner(files, sonarRunner); + SonarRunner runner = new SonarRunner(files, sonarRunner, config); Properties props = new Properties(); runner.setAdditionalProperties(props); assertThat(props.getProperty(SonarProperties.INCLUDE_FILES)).contains("first"); @@ -95,7 +95,7 @@ public void shouldSetBaseSonarConfig() throws IOException{ @Test(expected=IOException.class) public void shouldThrowWhenNoSonarFiles() throws IOException { List files = ImmutableList.of("first", "second"); - SonarRunner runner = new SonarRunner(files, sonarRunner); + SonarRunner runner = new SonarRunner(files, sonarRunner, config); runner.loadBaseProperties(); } } diff --git a/src/test/java/pl/touk/sputnik/review/locator/MavenBuildDirLocatorTest.java b/src/test/java/pl/touk/sputnik/review/locator/MavenBuildDirLocatorTest.java index dc26f939..96fcd799 100644 --- a/src/test/java/pl/touk/sputnik/review/locator/MavenBuildDirLocatorTest.java +++ b/src/test/java/pl/touk/sputnik/review/locator/MavenBuildDirLocatorTest.java @@ -1,21 +1,24 @@ package pl.touk.sputnik.review.locator; -import static org.assertj.core.api.Assertions.assertThat; - import com.google.common.collect.ImmutableList; -import org.junit.BeforeClass; +import org.junit.Before; import org.junit.Test; -import pl.touk.sputnik.configuration.ConfigurationHolder; +import pl.touk.sputnik.configuration.Configuration; +import pl.touk.sputnik.configuration.ConfigurationBuilder; import pl.touk.sputnik.review.Review; import pl.touk.sputnik.review.ReviewFile; +import pl.touk.sputnik.review.ReviewFormatterFactory; + +import static org.assertj.core.api.Assertions.assertThat; public class MavenBuildDirLocatorTest { private MavenBuildDirLocator locator = new MavenBuildDirLocator(); + private Configuration config; - @BeforeClass - public static void setUp() { - ConfigurationHolder.initFromResource("test.properties"); + @Before + public void setUp() { + config = ConfigurationBuilder.initFromResource("test.properties"); } @Test @@ -38,7 +41,7 @@ public void shouldReturnMainJavaBuildDirectory() { private Review review(String file) { - return new Review(ImmutableList.of(new ReviewFile(file))); + return new Review(ImmutableList.of(new ReviewFile(file)), ReviewFormatterFactory.get(config)); } } \ No newline at end of file diff --git a/src/test/resources/test.properties b/src/test/resources/test.properties index 456d71dd..b7d2dd8b 100644 --- a/src/test/resources/test.properties +++ b/src/test/resources/test.properties @@ -12,4 +12,4 @@ findbugs.includeFilter= findbugs.excludeFilter= jslint.enabled=true scalastyle.configurationFile=src/test/resources/scalastyle.xml -jshint.enabled=true +jshint.enabled=true \ No newline at end of file