diff --git a/README.md b/README.md index 0cf16e4e..d9652600 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,4 @@ -# java-diff-utils - -## Status ## -[![Build Status](https://travis-ci.org/wumpz/java-diff-utils.svg?branch=master)](https://travis-ci.org/wumpz/java-diff-utils) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/7eba77f10bed4c2a8d08ac8dc8da4a86)](https://www.codacy.com/app/wumpz/java-diff-utils?utm_source=github.com&utm_medium=referral&utm_content=wumpz/java-diff-utils&utm_campaign=Badge_Grade) +# java-diff-utils [![Build Status](https://api.travis-ci.org/Kogoro/java-diff-utils.svg?branch=master)](https://travis-ci.org/Kogoro/java-diff-utils) ## Intro ## Diff Utils library is an OpenSource library for performing the comparison operations between texts: computing diffs, applying patches, generating unified diffs or parsing them, generating diff output for easy future displaying (like side-by-side view) and so on. @@ -50,8 +47,13 @@ This is a test ~senctence~**for diffutils**. But it can easily replaced by any other which is better for handing your texts. I have plan to add implementation of some in future. ### Changelog ### - * Version 2.2 + * Version 2.2-SNAPSHOT * Added chunk to support surroundings of differences + * Verify also looks at surroundings + * Deltas now can proof if another delta change the same chunk + * Chunks now can proof if another chunk change the same positions + * Version 2.1-SNAPSHOT + * included checkstyle source code conventions * Version 2.0 * switch to maven and removed other artifacts * changed groupid to **com.github.java-diff-utils** due to different forks at github @@ -68,6 +70,26 @@ But it can easily replaced by any other which is better for handing your texts. * Ant build script * Generate output in unified diff format (thanks for Bill James) +## Source Code conventions + +Recently a checkstyle process was integrated into the build process. JSqlParser follows the sun java format convention. There are no TABs allowed. Use spaces. + +```java +public static Patch diff(List original, List revised, + BiPredicate equalizer) throws DiffException { + if (equalizer != null) { + return DiffUtils.diff(original, revised, + new MyersDiff<>(equalizer)); + } + return DiffUtils.diff(original, revised, new MyersDiff<>()); +} +``` + +This is a valid piece of source code: +* blocks without braces are not allowed +* after control statements (if, while, for) a whitespace is expected +* the opening brace should be in the same line as the control statement + ### To Install ### **This jar is not yet to get at maven central.** diff --git a/java-diff-utils.iml b/java-diff-utils.iml index 5f6b53a8..1dc1f4d5 100644 --- a/java-diff-utils.iml +++ b/java-diff-utils.iml @@ -1,29 +1,29 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nb-configuration.xml b/nb-configuration.xml index f1692631..e8402d7c 100644 --- a/nb-configuration.xml +++ b/nb-configuration.xml @@ -1,18 +1,18 @@ + This file contains additional configuration written by modules in the NetBeans IDE. + The configuration is intended to be shared among all the users of project and + therefore it is assumed to be part of version control checkout. + Without this configuration present, some functionality in the IDE may be limited or fail altogether. + --> + Properties that influence various parts of the IDE, especially code formatting and the like. + You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up. + That way multiple projects can share the same settings (useful for formatting rules for example). + Any value defined here will override the pom.xml file value but is only applicable to the current project. + --> none false true diff --git a/pom.xml b/pom.xml index b36ac972..43843f14 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,5 @@ - + 4.0.0 com.github.java-diff-utils diffutils @@ -6,33 +7,34 @@ 2.2-SNAPSHOT java-diff-utils - The DiffUtils library for computing diffs, applying patches, generationg side-by-side view in Java. + The DiffUtils library for computing diffs, applying patches, generationg side-by-side view in Java. + https://github.com/wumpz/java-diff-utils 2009 - + --> scm:git:https://github.com/wumpz/java-diff-utils.git scm:git:ssh://git@github.com:wumpz/java-diff-utils.git https://github.com/wumpz/java-diff-utils.git diffutils-2.2 - - + + GitHub Issues https://github.com/wumpz/java-diff-utils/issues - + java-diff-utils - + Christopher Sontag @@ -51,7 +53,7 @@ juanco@suigeneris.org - + The Apache Software License, Version 2.0 @@ -108,7 +110,7 @@ - + org.apache.maven.plugins maven-compiler-plugin @@ -119,7 +121,7 @@ UTF-8 - + + + + + + + + + + + + + + + + + + + + + + + + + + + com.puppycrawl.tools + checkstyle + 6.19 + + diff --git a/src/main/java/com/github/difflib/DiffUtils.java b/src/main/java/com/github/difflib/DiffUtils.java index 4a8bed4f..2652b3a1 100644 --- a/src/main/java/com/github/difflib/DiffUtils.java +++ b/src/main/java/com/github/difflib/DiffUtils.java @@ -41,19 +41,17 @@ public final class DiffUtils { /** - * Computes the difference between the original and revised list of elements with default diff - * algorithm + * Computes the difference between the original and revised list of elements with default diff algorithm * - * @param original The original text. Must not be {@code null}. - * @param revised The revised text. Must not be {@code null}. + * @param original The original text. Must not be {@code null}. + * @param revised The revised text. Must not be {@code null}. * @param linesBeforeAfter - Amount of lines for before and after chunk content - * @return The patch describing the difference between the original and revised sequences. Never - * {@code null}. + * @return The patch describing the difference between the original and revised sequences. Never {@code null}. */ public static Patch diff(List original, List revised, int linesBeforeAfter) throws DiffException { return DiffUtils.diff(original, revised, new MyersDiff<>(), linesBeforeAfter); } - + /** * Computes the difference between the original and revised text. */ @@ -62,17 +60,14 @@ public static Patch diff(String originalText, String revisedText, int li } /** - * Computes the difference between the original and revised list of elements with default diff - * algorithm - * - * @param original The original text. Must not be {@code null}. - * @param revised The revised text. Must not be {@code null}. + * Computes the difference between the original and revised list of elements with default diff algorithm * - * @param equalizer the equalizer object to replace the default compare algorithm - * (Object.equals). If {@code null} the default equalizer of the default algorithm is used.. + * @param original The original text. Must not be {@code null}. + * @param revised The revised text. Must not be {@code null}. + * @param equalizer the equalizer object to replace the default compare algorithm (Object.equals). If {@code null} + * the default equalizer of the default algorithm is used.. * @param linesBeforeAfter - Amount of lines for before and after chunk content - * @return The patch describing the difference between the original and revised sequences. Never - * {@code null}. + * @return The patch describing the difference between the original and revised sequences. Never {@code null}. */ public static Patch diff(List original, List revised, BiPredicate equalizer, int linesBeforeAfter) throws DiffException { @@ -84,29 +79,26 @@ public static Patch diff(List original, List revised, } /** - * Computes the difference between the original and revised list of elements with default diff - * algorithm + * Computes the difference between the original and revised list of elements with default diff algorithm * - * @param original The original text. Must not be {@code null}. - * @param revised The revised text. Must not be {@code null}. - * @param algorithm The diff algorithm. Must not be {@code null}. + * @param original The original text. Must not be {@code null}. + * @param revised The revised text. Must not be {@code null}. + * @param algorithm The diff algorithm. Must not be {@code null}. * @param linesBeforeAfter - Amount of lines for before and after chunk content - * @return The patch describing the difference between the original and revised sequences. Never - * {@code null}. + * @return The patch describing the difference between the original and revised sequences. Never {@code null}. */ public static Patch diff(List original, List revised, DiffAlgorithm algorithm, int linesBeforeAfter) throws DiffException { - Objects.requireNonNull(original,"original must not be null"); - Objects.requireNonNull(revised,"revised must not be null"); - Objects.requireNonNull(algorithm,"algorithm must not be null"); + Objects.requireNonNull(original, "original must not be null"); + Objects.requireNonNull(revised, "revised must not be null"); + Objects.requireNonNull(algorithm, "algorithm must not be null"); return Patch.generate(original, revised, algorithm.diff(original, revised), linesBeforeAfter); } /** - * Computes the difference between the given texts inline. This one uses the "trick" to make out - * of texts lists of characters, like DiffRowGenerator does and merges those changes at the end - * together again. + * Computes the difference between the given texts inline. This one uses the "trick" to make out of texts lists of + * characters, like DiffRowGenerator does and merges those changes at the end together again. * * @param original * @param revised @@ -141,7 +133,7 @@ private static List compressLines(List lines, String delimiter) * Patch the original text with given patch * * @param original the original text - * @param patch the given patch + * @param patch the given patch * @return the revised text * @throws PatchFailedException if can't apply patch */ @@ -154,7 +146,7 @@ public static List patch(List original, Patch patch) * Unpatch the revised text for a given patch * * @param revised the revised text - * @param patch the given patch + * @param patch the given patch * @return the original text */ public static List unpatch(List revised, Patch patch) { diff --git a/src/main/java/com/github/difflib/UnifiedDiffUtils.java b/src/main/java/com/github/difflib/UnifiedDiffUtils.java index c48b050f..742584ee 100644 --- a/src/main/java/com/github/difflib/UnifiedDiffUtils.java +++ b/src/main/java/com/github/difflib/UnifiedDiffUtils.java @@ -19,16 +19,17 @@ import com.github.difflib.patch.Chunk; import com.github.difflib.patch.Delta; import com.github.difflib.patch.Patch; + import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** - * * @author toben */ public final class UnifiedDiffUtils { + private static final Pattern UNIFIED_DIFF_CHUNK_REGEXP = Pattern .compile("^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@$"); @@ -124,22 +125,22 @@ public static Patch parseUnifiedDiff(List diff) { return patch; } - + /** - * generateUnifiedDiff takes a Patch and some other arguments, returning the Unified Diff format - * text representing the Patch. + * generateUnifiedDiff takes a Patch and some other arguments, returning the Unified Diff format text representing + * the Patch. * - * @param original - Filename of the original (unrevised file) - * @param revised - Filename of the revised file + * @param original - Filename of the original (unrevised file) + * @param revised - Filename of the revised file * @param originalLines - Lines of the original file - * @param patch - Patch created by the diff() function - * @param contextSize - number of lines of context output around each difference in the file. + * @param patch - Patch created by the diff() function + * @param contextSize - number of lines of context output around each difference in the file. * @return List of strings representing the Unified Diff representation of the Patch argument. * @author Bill James (tankerbay@gmail.com) */ public static List generateUnifiedDiff(String original, - String revised, List originalLines, Patch patch, - int contextSize) { + String revised, List originalLines, Patch patch, + int contextSize) { if (!patch.getDeltas().isEmpty()) { List ret = new ArrayList<>(); ret.add("--- " + original); @@ -195,19 +196,18 @@ public static List generateUnifiedDiff(String original, } return new ArrayList<>(); } - + /** - * processDeltas takes a list of Deltas and outputs them together in a single block of - * Unified-Diff-format text. + * processDeltas takes a list of Deltas and outputs them together in a single block of Unified-Diff-format text. * - * @param origLines - the lines of the original file - * @param deltas - the Deltas to be output as a single block + * @param origLines - the lines of the original file + * @param deltas - the Deltas to be output as a single block * @param contextSize - the number of lines of context to place around block * @return * @author Bill James (tankerbay@gmail.com) */ private static List processDeltas(List origLines, - List> deltas, int contextSize) { + List> deltas, int contextSize) { List buffer = new ArrayList<>(); int origTotal = 0; // counter for total lines output from Original int revTotal = 0; // counter for total lines output from Original @@ -275,7 +275,7 @@ private static List processDeltas(List origLines, // Create and insert the block header, conforming to the Unified Diff // standard - StringBuffer header = new StringBuffer(); + StringBuilder header = new StringBuilder(); header.append("@@ -"); header.append(origStart); header.append(","); @@ -289,7 +289,7 @@ private static List processDeltas(List origLines, return buffer; } - + /** * getDeltaText returns the lines to be added to the Unified Diff text from the Delta parameter * @@ -307,7 +307,7 @@ private static List getDeltaText(Delta delta) { } return buffer; } - + private UnifiedDiffUtils() { } } diff --git a/src/main/java/com/github/difflib/algorithm/Change.java b/src/main/java/com/github/difflib/algorithm/Change.java index eb21d3fe..f57b4f8d 100644 --- a/src/main/java/com/github/difflib/algorithm/Change.java +++ b/src/main/java/com/github/difflib/algorithm/Change.java @@ -18,10 +18,10 @@ import com.github.difflib.patch.DeltaType; /** - * * @author toben */ public class Change { + public final DeltaType deltaType; public final int startOriginal; public final int endOriginal; diff --git a/src/main/java/com/github/difflib/algorithm/DiffAlgorithm.java b/src/main/java/com/github/difflib/algorithm/DiffAlgorithm.java index e6a20151..928c0985 100644 --- a/src/main/java/com/github/difflib/algorithm/DiffAlgorithm.java +++ b/src/main/java/com/github/difflib/algorithm/DiffAlgorithm.java @@ -20,35 +20,38 @@ package com.github.difflib.algorithm; import com.github.difflib.patch.Patch; -import java.util.*; + +import java.util.Arrays; +import java.util.List; /** * The general interface for computing diffs between two lists of elements of type T. + *

+ * T The type of the compared elements in the 'lines'. * * @author Dmitry Naumenko - * @param T The type of the compared elements in the 'lines'. */ public interface DiffAlgorithm { /** - * Computes the difference between the original sequence and the revised sequence and returns it - * as a {@link Patch} object. + * Computes the difference between the original sequence and the revised sequence and returns it as a {@link Patch} + * object. * * @param original The original sequence. Must not be {@code null}. - * @param revised The revised sequence. Must not be {@code null}. + * @param revised The revised sequence. Must not be {@code null}. * @return The patch representing the diff of the given sequences. Never {@code null}. */ - public default List diff(T[] original, T[] revised) throws DiffException { + default List diff(T[] original, T[] revised) throws DiffException { return diff(Arrays.asList(original), Arrays.asList(revised)); } /** - * Computes the difference between the original sequence and the revised sequence and returns it - * as a {@link Patch} object. + * Computes the difference between the original sequence and the revised sequence and returns it as a {@link Patch} + * object. * * @param original The original sequence. Must not be {@code null}. - * @param revised The revised sequence. Must not be {@code null}. + * @param revised The revised sequence. Must not be {@code null}. * @return The patch representing the diff of the given sequences. Never {@code null}. */ - public List diff(List original, List revised) throws DiffException; + List diff(List original, List revised) throws DiffException; } diff --git a/src/main/java/com/github/difflib/algorithm/DifferentiationFailedException.java b/src/main/java/com/github/difflib/algorithm/DifferentiationFailedException.java index 132288f5..80c881e5 100644 --- a/src/main/java/com/github/difflib/algorithm/DifferentiationFailedException.java +++ b/src/main/java/com/github/difflib/algorithm/DifferentiationFailedException.java @@ -19,12 +19,9 @@ */ package com.github.difflib.algorithm; -import com.github.difflib.algorithm.DiffException; - /** - * Thrown whenever the differencing engine cannot produce the differences between two revisions of - * ta text. - + * Thrown whenever the differencing engine cannot produce the differences between two revisions of ta text. + * * @see MyersDiff * @see difflib.DiffAlgorithm */ diff --git a/src/main/java/com/github/difflib/algorithm/jgit/HistogramDiff.java b/src/main/java/com/github/difflib/algorithm/jgit/HistogramDiff.java index 978f56e9..2fa38203 100644 --- a/src/main/java/com/github/difflib/algorithm/jgit/HistogramDiff.java +++ b/src/main/java/com/github/difflib/algorithm/jgit/HistogramDiff.java @@ -19,18 +19,18 @@ import com.github.difflib.algorithm.DiffAlgorithm; import com.github.difflib.algorithm.DiffException; import com.github.difflib.patch.DeltaType; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; import org.eclipse.jgit.diff.Edit; import org.eclipse.jgit.diff.EditList; import org.eclipse.jgit.diff.Sequence; import org.eclipse.jgit.diff.SequenceComparator; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + /** - * HistorgramDiff using JGit - Library. This one is much more performant than the - * orginal Myers implementation. - * + * HistorgramDiff using JGit - Library. This one is much more performant than the orginal Myers implementation. + * * @author toben */ public class HistogramDiff implements DiffAlgorithm { @@ -43,19 +43,19 @@ public List diff(List original, List revised) throws DiffException diffList.addAll(new org.eclipse.jgit.diff.HistogramDiff().diff(new DataListComparator<>(), new DataList<>(original), new DataList<>(revised))); List patch = new ArrayList<>(); for (Edit edit : diffList) { - DeltaType type = DeltaType.EQUAL; + DeltaType type = DeltaType.EQUAL; switch (edit.getType()) { case DELETE: - type = DeltaType.DELETE; + type = DeltaType.DELETE; break; case INSERT: - type = DeltaType.INSERT; + type = DeltaType.INSERT; break; case REPLACE: - type = DeltaType.CHANGE; + type = DeltaType.CHANGE; break; } - patch.add(new Change(type,edit.getBeginA(), edit.getEndA(), edit.getBeginB(), edit.getEndB())); + patch.add(new Change(type, edit.getBeginA(), edit.getEndA(), edit.getBeginB(), edit.getEndB())); } return patch; } diff --git a/src/main/java/com/github/difflib/algorithm/myers/MyersDiff.java b/src/main/java/com/github/difflib/algorithm/myers/MyersDiff.java index 6907f681..7b3b2998 100644 --- a/src/main/java/com/github/difflib/algorithm/myers/MyersDiff.java +++ b/src/main/java/com/github/difflib/algorithm/myers/MyersDiff.java @@ -20,11 +20,12 @@ package com.github.difflib.algorithm.myers; import com.github.difflib.algorithm.Change; -import com.github.difflib.algorithm.DifferentiationFailedException; import com.github.difflib.algorithm.DiffAlgorithm; import com.github.difflib.algorithm.DiffException; +import com.github.difflib.algorithm.DifferentiationFailedException; import com.github.difflib.patch.DeltaType; import com.github.difflib.patch.Patch; + import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -35,23 +36,21 @@ */ public final class MyersDiff implements DiffAlgorithm { - private final BiPredicate DEFAULT_EQUALIZER = Object::equals; - private final BiPredicate equalizer; - + private final BiPredicate DEFAULT_EQUALIZER = Object::equals; + private final BiPredicate equalizer; public MyersDiff() { equalizer = DEFAULT_EQUALIZER; } - - public MyersDiff(final BiPredicate equalizer) { + public MyersDiff(final BiPredicate equalizer) { Objects.requireNonNull(equalizer, "equalizer must not be null"); this.equalizer = equalizer; } /** * {@inheritDoc} - * + *

* Return empty diff if get the error while procession the difference. */ @Override @@ -64,12 +63,11 @@ public List diff(final List original, final List revised) throws D } /** - * Computes the minimum diffpath that expresses de differences between the - * original and revised sequences, according to Gene Myers differencing - * algorithm. + * Computes the minimum diffpath that expresses de differences between the original and revised sequences, according + * to Gene Myers differencing algorithm. * * @param orig The original sequence. - * @param rev The revised sequence. + * @param rev The revised sequence. * @return A minimum {@link PathNode Path} accross the differences graph. * @throws DifferentiationFailedException if a diff path could not be found. */ @@ -134,12 +132,10 @@ private PathNode buildPath(final List orig, final List rev) /** * Constructs a {@link Patch} from a difference path. * - * @param path The path. - * @param orig The original sequence. - * @param rev The revised sequence. + * @param actualPath The path. + * @param orig The original sequence. + * @param rev The revised sequence. * @return A {@link Patch} script corresponding to the path. - * @throws DifferentiationFailedException if a {@link Patch} could not be - * built from the given path. */ private List buildRevision(PathNode actualPath, List orig, List rev) { Objects.requireNonNull(actualPath, "path is null"); diff --git a/src/main/java/com/github/difflib/algorithm/myers/PathNode.java b/src/main/java/com/github/difflib/algorithm/myers/PathNode.java index c60b4829..856485cf 100644 --- a/src/main/java/com/github/difflib/algorithm/myers/PathNode.java +++ b/src/main/java/com/github/difflib/algorithm/myers/PathNode.java @@ -23,10 +23,6 @@ * A node in a diffpath. * * @author Juanco Anez - * - * @see DiffNode - * @see Snake - * */ public final class PathNode { @@ -44,14 +40,14 @@ public final class PathNode { public final PathNode prev; public final boolean snake; - + public final boolean bootstrap; /** * Concatenates a new path node with an existing diffpath. * - * @param i The position in the original sequence for the new node. - * @param j The position in the revised sequence for the new node. + * @param i The position in the original sequence for the new node. + * @param j The position in the revised sequence for the new node. * @param prev The previous node in the path. */ public PathNode(int i, int j, boolean snake, boolean bootstrap, PathNode prev) { @@ -61,7 +57,7 @@ public PathNode(int i, int j, boolean snake, boolean bootstrap, PathNode prev) { if (snake) { this.prev = prev; } else { - this.prev = (prev == null ? null : prev.previousSnake()); + this.prev = prev == null ? null : prev.previousSnake(); } this.snake = snake; } @@ -82,11 +78,10 @@ public boolean isBootstrap() { } /** - * Skips sequences of {@link DiffNode DiffNodes} until a {@link Snake} or bootstrap node is - * found, or the end of the path is reached. + * Skips sequences of {@link PathNode PathNode} until a snake or bootstrap node is found, or the end of the + * path is reached. * - * @return The next first {@link Snake} or bootstrap node in the path, or null if - * none found. + * @return The next first snake or bootstrap node in the path, or null if none found. */ public final PathNode previousSnake() { if (isBootstrap()) { diff --git a/src/main/java/com/github/difflib/patch/ChangeDelta.java b/src/main/java/com/github/difflib/patch/ChangeDelta.java index 34a8bd6b..970d5736 100644 --- a/src/main/java/com/github/difflib/patch/ChangeDelta.java +++ b/src/main/java/com/github/difflib/patch/ChangeDelta.java @@ -22,10 +22,9 @@ import java.util.List; /** - * Describes the change-delta between original and revised texts. + * Describes the change-delta between original and revised texts.

T The type of the compared elements in the 'lines'. * * @author Dmitry Naumenko - * @param T The type of the compared elements in the 'lines'. */ public final class ChangeDelta extends Delta { @@ -33,7 +32,7 @@ public final class ChangeDelta extends Delta { * Creates a change delta with the two given chunks. * * @param original The original chunk. Must not be {@code null}. - * @param revised The original chunk. Must not be {@code null}. + * @param revised The original chunk. Must not be {@code null}. */ public ChangeDelta(Chunk original, Chunk revised) { super(DeltaType.CHANGE, original, revised); @@ -70,7 +69,6 @@ public void restore(List target) { @Override public String toString() { - return "[ChangeDelta, position: " + getOriginal().getPosition() + ", lines: " - + getOriginal().getLines() + " to " + getRevised().getLines() + "]"; + return "[ChangeDelta, position: " + getOriginal().getPosition() + ", lines: " + getOriginal().getLines() + " to " + getRevised().getLines() + "]"; } } diff --git a/src/main/java/com/github/difflib/patch/Chunk.java b/src/main/java/com/github/difflib/patch/Chunk.java index e4e1d9be..068beed3 100644 --- a/src/main/java/com/github/difflib/patch/Chunk.java +++ b/src/main/java/com/github/difflib/patch/Chunk.java @@ -24,15 +24,10 @@ import java.util.List; /** - * Holds the information about the part of text involved in the diff process - * - *

- * Text is represented as Object[] because the diff engine is capable of handling more - * than plain ascci. In fact, arrays or lists of any type that implements - * {@link java.lang.Object#hashCode hashCode()} and {@link java.lang.Object#equals equals()} - * correctly can be subject to differencing using this library. - *

- * T The type of the compared elements in the 'lines'. + * Holds the information about the part of text involved in the diff process

Text is represented as Object[] because the diff engine is capable + * of handling more than plain ascci. In fact, arrays or lists of any type that implements {@link java.lang.Object#hashCode hashCode()} and + * {@link java.lang.Object#equals equals()} correctly can be subject to differencing using this library.

T The type of the compared elements in the + * 'lines'. * * @author Dmitry Naumenko * @author Christopher Sontag @@ -48,7 +43,7 @@ public final class Chunk { * Creates a chunk and saves a copy of affected lines * * @param position the start position - * @param lines the affected lines + * @param lines the affected lines */ public Chunk(int position, List lines) { this.position = position; @@ -59,7 +54,7 @@ public Chunk(int position, List lines) { * Creates a chunk and saves a copy of affected lines * * @param position the start position - * @param lines the affected lines + * @param lines the affected lines */ public Chunk(int position, T[] lines) { this.position = position; @@ -77,10 +72,31 @@ public void verify(List target) throws PatchFailedException { } for (int i = 0; i < size(); i++) { if (!target.get(position + i).equals(lines.get(i))) { - throw new PatchFailedException( - "Incorrect Chunk: the chunk content doesn't match the target"); + throw new PatchFailedException("Incorrect Chunk: the chunk content doesn't match the target"); + } + } + for (int i = before.size(); i > 0; i--) { + if (!before.get(before.size() - i).equals(target.get(position - i))) { + throw new PatchFailedException("Incorrect Chunk: the chunk's surroundings (before) doesn't match the target"); } } + for (int i = 0; i < after.size(); i++) { + if (!after.get(i).equals(target.get(last() + i + 1))) { + throw new PatchFailedException("Incorrect Chunk: the chunk's surroundings (after) doesn't match the target"); + } + } + } + + /** + * Verifies that this chunk does not affect the same positions as the given chunk. + * + * @param other the chunk to be proof against this chunk. + * @throws PatchFailedException if both deltas affect the same chunk. + */ + public void verify(Chunk other) throws PatchFailedException { + if (Math.max(last(), other.last()) - Math.min(getPosition(), other.getPosition()) < (size() + other.size())) { + throw new PatchFailedException("Multiple chunks would override the same target position"); + } } /** @@ -136,7 +152,6 @@ public void setAfter(List after) { /* * (non-Javadoc) - * * @see java.lang.Object#hashCode() */ @Override @@ -151,7 +166,6 @@ public int hashCode() { /* * (non-Javadoc) - * * @see java.lang.Object#equals(java.lang.Object) */ @Override @@ -173,14 +187,12 @@ public boolean equals(Object obj) { } else if (!lines.equals(other.lines)) { return false; } - if (!before.equals(other.before)) return false; - if (!after.equals(other.after)) return false; - return position == other.position; + return before.equals(other.before) && after.equals(other.after) && position == other.position; } @Override public String toString() { - return "[position: " + position + ", size: " + size() + ", lines: " + lines + "]"; + return "[position: " + position + ", size: " + size() + ", lines: " + lines + ", before: " + before + ", after: " + after + "]"; } } diff --git a/src/main/java/com/github/difflib/patch/DeleteDelta.java b/src/main/java/com/github/difflib/patch/DeleteDelta.java index f65d8668..fd0bb155 100644 --- a/src/main/java/com/github/difflib/patch/DeleteDelta.java +++ b/src/main/java/com/github/difflib/patch/DeleteDelta.java @@ -23,9 +23,10 @@ /** * Describes the delete-delta between original and revised texts. + *

+ * T The type of the compared elements in the 'lines'. * * @author Dmitry Naumenko - * @param T The type of the compared elements in the 'lines'. */ public final class DeleteDelta extends Delta { @@ -33,7 +34,7 @@ public final class DeleteDelta extends Delta { * Creates a change delta with the two given chunks. * * @param original The original chunk. Must not be {@code null}. - * @param revised The original chunk. Must not be {@code null}. + * @param revised The original chunk. Must not be {@code null}. */ public DeleteDelta(Chunk original, Chunk revised) { super(DeltaType.DELETE, original, revised); @@ -48,7 +49,7 @@ public void applyTo(List target) throws PatchFailedException { target.remove(position); } } - + @Override public void restore(List target) { int position = this.getRevised().getPosition(); diff --git a/src/main/java/com/github/difflib/patch/Delta.java b/src/main/java/com/github/difflib/patch/Delta.java index 090f4ba1..4af3d3a4 100644 --- a/src/main/java/com/github/difflib/patch/Delta.java +++ b/src/main/java/com/github/difflib/patch/Delta.java @@ -19,13 +19,15 @@ */ package com.github.difflib.patch; -import java.util.*; +import java.util.List; +import java.util.Objects; /** * Describes the delta between original and revised texts. + *

+ * T The type of the compared elements in the 'lines'. * * @author Dmitry Naumenko - * @param T The type of the compared elements in the 'lines'. */ public abstract class Delta { @@ -37,13 +39,13 @@ public abstract class Delta { * Construct the delta for original and revised chunks * * @param original Chunk describing the original text. Must not be {@code null}. - * @param revised Chunk describing the revised text. Must not be {@code null}. + * @param revised Chunk describing the revised text. Must not be {@code null}. */ public Delta(DeltaType deltaType, Chunk original, Chunk revised) { Objects.requireNonNull(deltaType, "deltaType must not be null"); Objects.requireNonNull(original, "original must not be null"); Objects.requireNonNull(revised, "revised must not be null"); - + this.deltaType = deltaType; this.original = original; this.revised = revised; @@ -59,11 +61,21 @@ public void verify(List target) throws PatchFailedException { getOriginal().verify(target); } + /** + * Verifies that this delta does not affect the same chunk as the given delta. + * + * @param delta the delta to be proof against this delta. + * @throws PatchFailedException if both deltas affect the same chunk. + */ + public void verify(Delta delta) throws PatchFailedException { + getOriginal().verify(delta.getOriginal()); + } + /** * Applies this delta as the patch for a given target * * @param target the given target - * @throws PatchFailedException + * @throws PatchFailedException if a error occured */ public abstract void applyTo(List target) throws PatchFailedException; @@ -121,13 +133,10 @@ public boolean equals(Object obj) { return false; } if (revised == null) { - if (other.revised != null) { - return false; - } - } else if (!revised.equals(other.revised)) { - return false; + return other.revised == null; + } else { + return revised.equals(other.revised); } - return true; } } diff --git a/src/main/java/com/github/difflib/patch/DeltaType.java b/src/main/java/com/github/difflib/patch/DeltaType.java index 64e550b2..8ea775fd 100644 --- a/src/main/java/com/github/difflib/patch/DeltaType.java +++ b/src/main/java/com/github/difflib/patch/DeltaType.java @@ -17,7 +17,6 @@ /** * Specifies the type of the delta. - * */ public enum DeltaType { /** @@ -31,8 +30,7 @@ public enum DeltaType { /** * An insert into the original. */ - INSERT, - + INSERT, /** * An do nothing. */ diff --git a/src/main/java/com/github/difflib/patch/InsertDelta.java b/src/main/java/com/github/difflib/patch/InsertDelta.java index 3783c3dd..2f8a62d3 100644 --- a/src/main/java/com/github/difflib/patch/InsertDelta.java +++ b/src/main/java/com/github/difflib/patch/InsertDelta.java @@ -23,9 +23,10 @@ /** * Describes the add-delta between original and revised texts. + *

+ * T The type of the compared elements in the 'lines'. * * @author Dmitry Naumenko - * @param T The type of the compared elements in the 'lines'. */ public final class InsertDelta extends Delta { @@ -33,7 +34,7 @@ public final class InsertDelta extends Delta { * Creates an insert delta with the two given chunks. * * @param original The original chunk. Must not be {@code null}. - * @param revised The original chunk. Must not be {@code null}. + * @param revised The original chunk. Must not be {@code null}. */ public InsertDelta(Chunk original, Chunk revised) { super(DeltaType.INSERT, original, revised); @@ -48,7 +49,7 @@ public void applyTo(List target) throws PatchFailedException { target.add(position + i, lines.get(i)); } } - + @Override public void restore(List target) { int position = getRevised().getPosition(); diff --git a/src/main/java/com/github/difflib/patch/Patch.java b/src/main/java/com/github/difflib/patch/Patch.java index 6d586d8b..2ec6d42c 100644 --- a/src/main/java/com/github/difflib/patch/Patch.java +++ b/src/main/java/com/github/difflib/patch/Patch.java @@ -22,7 +22,6 @@ import com.github.difflib.algorithm.Change; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.ListIterator; @@ -30,8 +29,9 @@ /** * Describes the patch holding all deltas between the original and revised texts. - * + *

* T The type of the compared elements in the 'lines'. + * * @author Dmitry Naumenko * @author Christopher Sontag */ @@ -88,6 +88,21 @@ public void addDelta(Delta delta) { deltas.add(delta); } + /** + * Get the list of computed deltas + * + * @return the deltas + */ + public List> getDeltas() { + deltas.sort(comparing(d -> d.getOriginal().getPosition())); + return deltas; + } + + @Override + public String toString() { + return "Patch{" + "deltas=" + deltas + '}'; + } + public static Patch generate(List original, List revised, List changes, int surroundingLines) { Patch patch = new Patch<>(changes.size()); for (Change change : changes) { @@ -131,18 +146,4 @@ public static Patch generate(List original, List revised, ListDmitry Naumenko */ @@ -73,7 +72,7 @@ public String getNewLine() { /* * (non-Javadoc) - * + * * @see java.lang.Object#hashCode() */ @Override @@ -88,7 +87,7 @@ public int hashCode() { /* * (non-Javadoc) - * + * * @see java.lang.Object#equals(java.lang.Object) */ @Override @@ -118,9 +117,7 @@ public boolean equals(Object obj) { return false; } if (tag == null) { - if (other.tag != null) { - return false; - } + return other.tag == null; } else if (!tag.equals(other.tag)) { return false; } diff --git a/src/main/java/com/github/difflib/text/StringUtils.java b/src/main/java/com/github/difflib/text/StringUtils.java index 4166b311..36344502 100644 --- a/src/main/java/com/github/difflib/text/StringUtils.java +++ b/src/main/java/com/github/difflib/text/StringUtils.java @@ -20,6 +20,7 @@ package com.github.difflib.text; import java.util.List; + import static java.util.stream.Collectors.toList; final class StringUtils { @@ -53,7 +54,7 @@ public static List wrapText(List list, int columnWidth) { /** * Wrap the text with the given column width * - * @param line the text + * @param line the text * @param columnWidth the given column * @return the wrapped text */ diff --git a/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java b/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java index 966b525c..f553b9ef 100644 --- a/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java +++ b/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java @@ -6,7 +6,6 @@ import org.junit.Test; import java.io.BufferedReader; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; @@ -18,9 +17,9 @@ public class GenerateUnifiedDiffTest { - private static List fileToLines(String filename) throws FileNotFoundException, IOException { + private static List fileToLines(String filename) throws IOException { List lines = new ArrayList<>(); - String line = ""; + String line; try (BufferedReader in = new BufferedReader(new FileReader(filename))) { while ((line = in.readLine()) != null) { lines.add(line); @@ -107,7 +106,7 @@ public void testDiffWithHeaderLineInText() throws DiffException { } private void verify(List origLines, List revLines, - String originalFile, String revisedFile) throws DiffException { + String originalFile, String revisedFile) throws DiffException { Patch patch = DiffUtils.diff(origLines, revLines, 1); List unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff(originalFile, revisedFile, origLines, patch, 10); @@ -115,7 +114,7 @@ private void verify(List origLines, List revLines, Patch fromUnifiedPatch = UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff); List patchedLines; try { - patchedLines = (List) fromUnifiedPatch.applyTo(origLines); + patchedLines = fromUnifiedPatch.applyTo(origLines); assertTrue(revLines.size() == patchedLines.size()); for (int i = 0; i < revLines.size(); i++) { String l1 = revLines.get(i); diff --git a/src/test/java/com/github/difflib/TestConstants.java b/src/test/java/com/github/difflib/TestConstants.java index 7abb9a4d..bd675d3c 100644 --- a/src/test/java/com/github/difflib/TestConstants.java +++ b/src/test/java/com/github/difflib/TestConstants.java @@ -4,12 +4,12 @@ * Test constants * * @author simon.mittermueller@gmail.com - * */ public final class TestConstants { + public static final String BASE_FOLDER_RESOURCES = "target/test-classes/"; /** - * The base folder containing the test files. Ends with {@link #FS}. + * The base folder containing the test files. Ends with '/'. */ public static final String MOCK_FOLDER = BASE_FOLDER_RESOURCES + "/mocks/"; diff --git a/src/test/java/com/github/difflib/algorithm/jgit/HistogramDiffTest.java b/src/test/java/com/github/difflib/algorithm/jgit/HistogramDiffTest.java index cee1d8be..dadcf151 100644 --- a/src/test/java/com/github/difflib/algorithm/jgit/HistogramDiffTest.java +++ b/src/test/java/com/github/difflib/algorithm/jgit/HistogramDiffTest.java @@ -27,26 +27,25 @@ import static org.junit.Assert.assertNotNull; /** - * * @author toben */ public class HistogramDiffTest { - + public HistogramDiffTest() { } - + @BeforeClass public static void setUpClass() { } - + @AfterClass public static void tearDownClass() { } - + @Before public void setUp() { } - + @After public void tearDown() { } @@ -56,14 +55,14 @@ public void tearDown() { */ @Test public void testDiff() throws DiffException, PatchFailedException { - List orgList = Arrays.asList("A","B","C","A","B","B","A"); - List revList = Arrays.asList("C","B","A","B","A","C"); + List orgList = Arrays.asList("A", "B", "C", "A", "B", "B", "A"); + List revList = Arrays.asList("C", "B", "A", "B", "A", "C"); final Patch patch = Patch.generate(orgList, revList, new HistogramDiff().diff(orgList, revList), 1); System.out.println(patch); assertNotNull(patch); assertEquals(3, patch.getDeltas().size()); assertEquals("Patch{deltas=[[DeleteDelta, position: 0, lines: [A, B]], [DeleteDelta, position: 3, lines: [A, B]], [InsertDelta, position: 7, lines: [B, A, C]]]}", patch.toString()); - + List patched = patch.applyTo(orgList); assertEquals(revList, patched); } diff --git a/src/test/java/com/github/difflib/algorithm/jgit/LRHistogramDiffTest.java b/src/test/java/com/github/difflib/algorithm/jgit/LRHistogramDiffTest.java index 50c9e058..1cc36191 100644 --- a/src/test/java/com/github/difflib/algorithm/jgit/LRHistogramDiffTest.java +++ b/src/test/java/com/github/difflib/algorithm/jgit/LRHistogramDiffTest.java @@ -30,31 +30,29 @@ import static org.junit.Assert.assertEquals; /** - * * @author toben */ public class LRHistogramDiffTest { - + public LRHistogramDiffTest() { } - + @BeforeClass public static void setUpClass() { } - + @AfterClass public static void tearDownClass() { } - + @Before public void setUp() { } - + @After public void tearDown() { } - @Test public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOException, DiffException, PatchFailedException { ZipFile zip = new ZipFile(TestConstants.MOCK_FOLDER + "/large_dataset1.zip"); @@ -62,11 +60,11 @@ public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOExcept List revised = readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb"))); Patch patch = Patch.generate(original, revised, new HistogramDiff().diff(original, revised), 1); - + assertEquals(34, patch.getDeltas().size()); - + List created = patch.applyTo(original); assertArrayEquals(revised.toArray(), created.toArray()); } - + } diff --git a/src/test/java/com/github/difflib/algorithm/myers/MyersDiffTest.java b/src/test/java/com/github/difflib/algorithm/myers/MyersDiffTest.java index c564581b..2f87236a 100644 --- a/src/test/java/com/github/difflib/algorithm/myers/MyersDiffTest.java +++ b/src/test/java/com/github/difflib/algorithm/myers/MyersDiffTest.java @@ -26,7 +26,6 @@ import static org.junit.Assert.assertNotNull; /** - * * @author tw */ public class MyersDiffTest { diff --git a/src/test/java/com/github/difflib/examples/ApplyPatch.java b/src/test/java/com/github/difflib/examples/ApplyPatch.java index 4eca7d36..226234e9 100644 --- a/src/test/java/com/github/difflib/examples/ApplyPatch.java +++ b/src/test/java/com/github/difflib/examples/ApplyPatch.java @@ -5,6 +5,7 @@ import com.github.difflib.UnifiedDiffUtils; import com.github.difflib.patch.Patch; import com.github.difflib.patch.PatchFailedException; + import java.io.File; import java.io.IOException; import java.nio.file.Files; diff --git a/src/test/java/com/github/difflib/patch/ChunkTest.java b/src/test/java/com/github/difflib/patch/ChunkTest.java new file mode 100644 index 00000000..90140ccc --- /dev/null +++ b/src/test/java/com/github/difflib/patch/ChunkTest.java @@ -0,0 +1,40 @@ +package com.github.difflib.patch; + +import com.github.difflib.DiffUtils; +import com.github.difflib.algorithm.DiffException; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertTrue; + +public class ChunkTest { + + @Test + public void testChunk_verify() throws DiffException { + final List insertTest_from = Arrays.asList("hhh", "lll"); + final List insertTest_to = Arrays.asList("hhh", "jjj", "kkk", "lll"); + + final Patch patch = DiffUtils.diff(insertTest_from, insertTest_to, 1); + assertTrue(patch.getDeltas().size() > 0); + Delta delta = patch.getDeltas().get(0); + + PatchFailedException exception = null; + try { + delta.verify(insertTest_from); + } catch (PatchFailedException e) { + exception = e; + } + assertTrue(exception == null); + + exception = null; + try { + delta.verify(insertTest_to); + } catch (PatchFailedException e) { + exception = e; + } + assertTrue(exception != null); + } + +} diff --git a/src/test/java/com/github/difflib/patch/PatchTest.java b/src/test/java/com/github/difflib/patch/PatchTest.java index 15111117..ec1ab7fc 100644 --- a/src/test/java/com/github/difflib/patch/PatchTest.java +++ b/src/test/java/com/github/difflib/patch/PatchTest.java @@ -7,8 +7,7 @@ import java.util.Arrays; import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; public class PatchTest { @@ -50,4 +49,27 @@ public void testPatch_Change() throws DiffException { fail(e.getMessage()); } } + + @Test + public void testPatch_ChunkVerify() throws DiffException { + final List changeTest_from = Arrays.asList("aaa", "ddd"); + final List changeTest_to = Arrays.asList("aaa", "bbb", "ccc", "ddd"); + + final Patch patchOne = DiffUtils.diff(changeTest_from, changeTest_to, 1); + assertTrue(patchOne.getDeltas().size() > 0); + final Patch patchTwo = DiffUtils.diff(changeTest_from, changeTest_to, 1); + assertTrue(patchTwo.getDeltas().size() > 0); + + for (Delta deltaOne : patchOne.getDeltas()) { + for (Delta deltaTwo : patchTwo.getDeltas()) { + PatchFailedException exception = null; + try { + deltaOne.verify(deltaTwo); + } catch (PatchFailedException e) { + exception = e; + } + assertTrue(exception != null); + } + } + } } diff --git a/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java b/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java index 616c189e..bfa3d150 100644 --- a/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java +++ b/src/test/java/com/github/difflib/text/DiffRowGeneratorTest.java @@ -1,13 +1,13 @@ package com.github.difflib.text; -import com.github.difflib.text.DiffRow; -import com.github.difflib.text.DiffRowGenerator; import com.github.difflib.algorithm.DiffException; +import org.junit.Test; + import java.util.Arrays; import java.util.List; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import org.junit.Test; public class DiffRowGeneratorTest { @@ -24,7 +24,7 @@ public void testGenerator_Default() throws DiffException { assertEquals(3, rows.size()); } - + @Test public void testGenerator_Default2() throws DiffException { String first = "anything \n \nother"; @@ -83,14 +83,14 @@ private void print(List diffRows) { System.out.println(row); } } - + @Test public void testGeneratorWithWordWrap() throws DiffException { String first = "anything \n \nother"; String second = "anything\n\nother"; DiffRowGenerator generator = DiffRowGenerator.create() - .columnWidth(5) + .columnWidth(5) .build(); List rows = generator.generateDiffRows(split(first), split(second)); print(rows); @@ -100,7 +100,7 @@ public void testGeneratorWithWordWrap() throws DiffException { assertEquals("[CHANGE, ,]", rows.get(1).toString()); assertEquals("[EQUAL,other,other]", rows.get(2).toString()); } - + @Test public void testGeneratorWithMerge() throws DiffException { String first = "anything \n \nother"; @@ -118,20 +118,20 @@ public void testGeneratorWithMerge() throws DiffException { assertEquals("[CHANGE, ,]", rows.get(1).toString()); assertEquals("[EQUAL,other,other]", rows.get(2).toString()); } - + @Test public void testGeneratorWithMerge2() throws DiffException { DiffRowGenerator generator = DiffRowGenerator.create() .showInlineDiffs(true) .mergeOriginalRevised(true) .build(); - List rows = generator.generateDiffRows(Arrays.asList("Test"),Arrays.asList("ester")); + List rows = generator.generateDiffRows(Arrays.asList("Test"), Arrays.asList("ester")); print(rows); assertEquals(1, rows.size()); assertEquals("[CHANGE,Tester,ester]", rows.get(0).toString()); } - + @Test public void testGeneratorWithMerge3() throws DiffException { String first = "test\nanything \n \nother"; @@ -152,7 +152,7 @@ public void testGeneratorWithMerge3() throws DiffException { assertEquals("[INSERT,test,test]", rows.get(4).toString()); assertEquals("[INSERT,test2,test2]", rows.get(5).toString()); } - + @Test public void testGeneratorWithMergeByWord4() throws DiffException { DiffRowGenerator generator = DiffRowGenerator.create() @@ -160,13 +160,13 @@ public void testGeneratorWithMergeByWord4() throws DiffException { .mergeOriginalRevised(true) .inlineDiffByWord(true) .build(); - List rows = generator.generateDiffRows(Arrays.asList("Test"),Arrays.asList("ester")); + List rows = generator.generateDiffRows(Arrays.asList("Test"), Arrays.asList("ester")); print(rows); assertEquals(1, rows.size()); assertEquals("[CHANGE,Testester,ester]", rows.get(0).toString()); } - + @Test public void testGeneratorWithMergeByWord5() throws DiffException { DiffRowGenerator generator = DiffRowGenerator.create() @@ -175,20 +175,20 @@ public void testGeneratorWithMergeByWord5() throws DiffException { .inlineDiffByWord(true) .columnWidth(80) .build(); - List rows = generator.generateDiffRows(Arrays.asList("Test feature"),Arrays.asList("ester feature best")); + List rows = generator.generateDiffRows(Arrays.asList("Test feature"), Arrays.asList("ester feature best")); print(rows); assertEquals(1, rows.size()); assertEquals("[CHANGE,Testester
feature best,ester feature best]", rows.get(0).toString()); } - + @Test public void testSplitString() { List list = DiffRowGenerator.splitStringPreserveDelimiter("test,test2"); assertEquals(3, list.size()); assertEquals("[test, ,, test2]", list.toString()); } - + @Test public void testSplitString2() { List list = DiffRowGenerator.splitStringPreserveDelimiter("test , test2"); @@ -196,7 +196,7 @@ public void testSplitString2() { assertEquals(5, list.size()); assertEquals("[test, , ,, , test2]", list.toString()); } - + @Test public void testSplitString3() { List list = DiffRowGenerator.splitStringPreserveDelimiter("test,test2,"); @@ -204,8 +204,7 @@ public void testSplitString3() { assertEquals(4, list.size()); assertEquals("[test, ,, test2, ,]", list.toString()); } - - + @Test public void testGeneratorExample1() throws DiffException { DiffRowGenerator generator = DiffRowGenerator.create() @@ -218,13 +217,13 @@ public void testGeneratorExample1() throws DiffException { List rows = generator.generateDiffRows( Arrays.asList("This is a test senctence."), Arrays.asList("This is a test for diffutils.")); - - System.out.println(rows.get(0).getOldLine()); - + + System.out.println(rows.get(0).getOldLine()); + assertEquals(1, rows.size()); assertEquals("This is a test ~senctence~**for diffutils**.", rows.get(0).getOldLine()); } - + @Test public void testGeneratorExample2() throws DiffException { DiffRowGenerator generator = DiffRowGenerator.create() @@ -236,25 +235,25 @@ public void testGeneratorExample2() throws DiffException { List rows = generator.generateDiffRows( Arrays.asList("This is a test senctence.", "This is the second line.", "And here is the finish."), Arrays.asList("This is a test for diffutils.", "This is the second line.")); - + System.out.println("|original|new|"); System.out.println("|--------|---|"); for (DiffRow row : rows) { System.out.println("|" + row.getOldLine() + "|" + row.getNewLine() + "|"); } - + assertEquals(3, rows.size()); assertEquals("This is a test ~senctence~.", rows.get(0).getOldLine()); assertEquals("This is a test **for diffutils**.", rows.get(0).getNewLine()); } - + @Test public void testGeneratorUnchanged() throws DiffException { String first = "anything \n \nother"; String second = "anything\n\nother"; DiffRowGenerator generator = DiffRowGenerator.create() - .columnWidth(5) + .columnWidth(5) .reportLinesUnchanged(true) .build(); List rows = generator.generateDiffRows(split(first), split(second)); diff --git a/src/test/java/com/github/difflib/text/StringUtilsTest.java b/src/test/java/com/github/difflib/text/StringUtilsTest.java index ad8f1db2..82bd0645 100644 --- a/src/test/java/com/github/difflib/text/StringUtilsTest.java +++ b/src/test/java/com/github/difflib/text/StringUtilsTest.java @@ -15,12 +15,13 @@ */ package com.github.difflib.text; -import java.util.Collections; import org.junit.Test; -import static org.junit.Assert.*; + +import java.util.Collections; + +import static org.junit.Assert.assertEquals; /** - * * @author tw */ public class StringUtilsTest { @@ -38,7 +39,7 @@ public void testHtmlEntites() { */ @Test public void testNormalize_String() { - assertEquals(" test",StringUtils.normalize("\ttest")); + assertEquals(" test", StringUtils.normalize("\ttest")); } /** @@ -46,7 +47,7 @@ public void testNormalize_String() { */ @Test public void testNormalize_List() { - assertEquals(Collections.singletonList(" test"),StringUtils.normalize(Collections.singletonList("\ttest"))); + assertEquals(Collections.singletonList(" test"), StringUtils.normalize(Collections.singletonList("\ttest"))); } /** @@ -58,10 +59,10 @@ public void testWrapText_String_int() { assertEquals("tes
t", StringUtils.wrapText("test", 3)); assertEquals("test", StringUtils.wrapText("test", 10)); } - + @Test(expected = IllegalArgumentException.class) public void testWrapText_String_int_zero() { assertEquals("test", StringUtils.wrapText("test", -1)); } - + }