From 5b5d2b19627f80b7827897ad959ff18430c1dac6 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Fri, 24 Mar 2023 19:38:49 +0100 Subject: [PATCH] Move showDiffDetails property to ComparisonData This simplifies testing of comparators with different values of this property. --- .../ArtifactComparator.java | 24 +++++++++---------- .../internal/ContentsComparator.java | 10 ++------ .../internal/DefaultContentsComparator.java | 2 +- .../internal/TextComparator.java | 16 ++++++------- .../zipcomparator/internal/XmlComparator.java | 9 ++++--- 5 files changed, 27 insertions(+), 34 deletions(-) diff --git a/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/artifactcomparator/ArtifactComparator.java b/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/artifactcomparator/ArtifactComparator.java index 411180709c..dff561cd15 100644 --- a/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/artifactcomparator/ArtifactComparator.java +++ b/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/artifactcomparator/ArtifactComparator.java @@ -19,22 +19,22 @@ public interface ArtifactComparator { - public static class ComparisonData { + public static record ComparisonData(List ignoredPattern, boolean writeDelta, boolean showDiffDetails) { - public ComparisonData(List ignoredPattern, boolean writeDelta) { - this.ignoredPattern = ignoredPattern != null ? List.copyOf(ignoredPattern) : List.of(); - this.writeDelta = writeDelta; - } + /** + * System property that control if a detailed diff is desired or not, false + * (default) = no detailed diff is shown, true show detailed difference. + */ + private static final boolean SHOW_DIFF_DETAILS = Boolean.getBoolean("tycho.comparator.showDiff"); - private final List ignoredPattern; - private boolean writeDelta; - - public List ignoredPattern() { - return ignoredPattern; + public ComparisonData(List ignoredPattern, boolean writeDelta) { + this(ignoredPattern, writeDelta, SHOW_DIFF_DETAILS); } - public boolean writeDelta() { - return writeDelta; + public ComparisonData(List ignoredPattern, boolean writeDelta, boolean showDiffDetails) { + this.ignoredPattern = ignoredPattern != null ? List.copyOf(ignoredPattern) : List.of(); + this.writeDelta = writeDelta; + this.showDiffDetails = showDiffDetails; } } diff --git a/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/zipcomparator/internal/ContentsComparator.java b/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/zipcomparator/internal/ContentsComparator.java index dd8964068d..a72a8e4954 100644 --- a/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/zipcomparator/internal/ContentsComparator.java +++ b/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/zipcomparator/internal/ContentsComparator.java @@ -21,12 +21,6 @@ public interface ContentsComparator { - /** - * System property that control if a detailed diff is desired or not, false - * (default) = no detailed diff is shown, true show detailed difference. - */ - static final boolean SHOW_DIFF_DETAILS = Boolean.getBoolean("tycho.comparator.showDiff"); - /** * System property that controls the threshold size where a direct byte compare is performed * (default 5 mb) @@ -36,7 +30,7 @@ public interface ContentsComparator { /** * Computes the delta for the given {@link InputStream}s, the streams passed will support * mark/reset for repeated reads. - * + * * @param baseline * the baseline data * @param reactor @@ -51,7 +45,7 @@ public ArtifactDelta getDelta(ComparatorInputStream baseline, ComparatorInputStr /** * Check if this comparator matches the given name or extension - * + * * @param nameOrExtension * the extension or name to match * @return true if this comparator matches, false otherwise diff --git a/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/zipcomparator/internal/DefaultContentsComparator.java b/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/zipcomparator/internal/DefaultContentsComparator.java index 2996f78633..d406f036ed 100644 --- a/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/zipcomparator/internal/DefaultContentsComparator.java +++ b/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/zipcomparator/internal/DefaultContentsComparator.java @@ -30,7 +30,7 @@ public ArtifactDelta getDelta(ComparatorInputStream baseline, ComparatorInputStr throws IOException { if (isTextFile(baseline) && isTextFile(reactor)) { //If both items a certainly a text file, we compare them ignoring line endings - return TextComparator.compareText(baseline, reactor); + return TextComparator.compareText(baseline, reactor, data); } return ArtifactDelta.DEFAULT; } diff --git a/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/zipcomparator/internal/TextComparator.java b/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/zipcomparator/internal/TextComparator.java index d73adfa70f..cff3185c6a 100644 --- a/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/zipcomparator/internal/TextComparator.java +++ b/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/zipcomparator/internal/TextComparator.java @@ -6,7 +6,7 @@ * https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 - * + * * Contributors: * Christoph Läubrich - initial API and implementation *******************************************************************************/ @@ -39,21 +39,21 @@ public class TextComparator implements ContentsComparator { @Override public ArtifactDelta getDelta(ComparatorInputStream baseline, ComparatorInputStream reactor, ComparisonData data) throws IOException { - return compareText(baseline, reactor); + return compareText(baseline, reactor, data); } - public static ArtifactDelta compareText(ComparatorInputStream baseline, ComparatorInputStream reactor) - throws IOException { + public static ArtifactDelta compareText(ComparatorInputStream baseline, ComparatorInputStream reactor, + ComparisonData data) throws IOException { ByteIterator baselineIterator = new ByteIterator(baseline.asBytes()); ByteIterator reactorIterator = new ByteIterator(reactor.asBytes()); while (baselineIterator.hasNext() && reactorIterator.hasNext()) { if (baselineIterator.next() != reactorIterator.next()) { - return createDelta(ArtifactDelta.DEFAULT.getMessage(), baseline, reactor); + return createDelta(ArtifactDelta.DEFAULT.getMessage(), baseline, reactor, data); } } //now both need to be at the end of the stream if they are the same! if (baselineIterator.hasNext() || reactorIterator.hasNext()) { - return createDelta(ArtifactDelta.DEFAULT.getMessage(), baseline, reactor); + return createDelta(ArtifactDelta.DEFAULT.getMessage(), baseline, reactor, data); } return ArtifactDelta.NO_DIFFERENCE; } @@ -100,8 +100,8 @@ public boolean matches(String nameOrExtension) { } public static ArtifactDelta createDelta(String message, ComparatorInputStream baseline, - ComparatorInputStream reactor) { - if (SHOW_DIFF_DETAILS) { + ComparatorInputStream reactor, ComparisonData data) { + if (data.showDiffDetails()) { String detailed; try { List source = IOUtils.readLines(baseline.asNewStream(), StandardCharsets.UTF_8); diff --git a/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/zipcomparator/internal/XmlComparator.java b/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/zipcomparator/internal/XmlComparator.java index 688396f59d..7ef0b3e7a0 100644 --- a/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/zipcomparator/internal/XmlComparator.java +++ b/tycho-artifactcomparator/src/main/java/org/eclipse/tycho/zipcomparator/internal/XmlComparator.java @@ -6,7 +6,7 @@ * https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 - * + * * Contributors: * Christoph Läubrich - initial API and implementation *******************************************************************************/ @@ -43,21 +43,20 @@ public ArtifactDelta getDelta(ComparatorInputStream baseline, ComparatorInputStr Diff baselineDiff = computeDiff(baseline, reactor); if (baselineDiff.hasDifferences()) { String message = baselineDiff.fullDescription(); - return TextComparator.createDelta(message, baseline, reactor); + return TextComparator.createDelta(message, baseline, reactor, data); } return null; } catch (RuntimeException e) { - return TextComparator.createDelta(ArtifactDelta.DEFAULT.getMessage(), baseline, reactor); + return TextComparator.createDelta(ArtifactDelta.DEFAULT.getMessage(), baseline, reactor, data); } } private Diff computeDiff(InputStream baseline, InputStream reactor) { - Diff baselineDiff = DiffBuilder.compare(Input.fromStream(baseline))// + return DiffBuilder.compare(Input.fromStream(baseline))// .withTest(Input.fromStream(reactor))// .checkForSimilar()// .ignoreComments() // .ignoreWhitespace().build(); - return baselineDiff; } @Override