19 package org.sleuthkit.autopsy.integrationtesting;
21 import com.github.difflib.DiffUtils;
22 import com.github.difflib.patch.AbstractDelta;
23 import com.github.difflib.patch.Chunk;
24 import com.github.difflib.patch.Patch;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.util.List;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
32 import org.apache.commons.io.FileUtils;
33 import org.apache.commons.lang3.StringUtils;
34 import org.apache.commons.lang3.exception.ExceptionUtils;
35 import org.apache.commons.lang3.tuple.Pair;
65 super(message, exception);
71 private static final String[]
DIFF_BREAK =
new String[]{
"",
"",
""};
73 private static final String
NEW_LINE = System.getProperty(
"line.separator");
87 if (prevResult.isDirectory() && curResult.isDirectory()) {
88 final Map<String, File> prevFiles = FileUtils.listFiles(prevResult, null,
true).stream()
89 .collect(Collectors.toMap(f ->
getRelative(prevResult, f), f -> f, (f1, f2) -> f1));
91 final Map<String, File> curFiles = FileUtils.listFiles(curResult, null,
true).stream()
92 .collect(Collectors.toMap(f ->
getRelative(curResult, f), f -> f, (f1, f2) -> f1));
94 Map<String, Pair<File, File>> prevCurMapping = Stream.of(prevFiles, curFiles)
95 .flatMap((map) -> map.keySet().stream())
96 .collect(Collectors.toMap(k -> k, k -> Pair.of(prevFiles.get(k), curFiles.get(k)), (v1, v2) -> v1));
98 String fullDiff = prevCurMapping.entrySet().stream()
99 .map((entry) ->
getFileDiffs(entry.getValue().getLeft(), entry.getValue().getRight(), entry.getKey()))
100 .filter((val) -> val != null)
101 .collect(Collectors.joining(String.join(NEW_LINE, FILE_DIFF_BREAK)));
105 }
else if (prevResult.isFile() && curResult.isFile()) {
106 return getFileDiffs(prevResult, curResult, prevResult.toString() +
" / " + curResult.toString());
109 throw new DiffServiceException(String.format(
"%s and %s must be of same type (directory/file).", prevResult.toString(), curResult.toString()));
123 boolean hasOrig = (orig != null && orig.exists());
124 boolean hasCur = (cur != null && cur.exists());
125 if (!hasOrig && !hasCur) {
127 }
else if (!hasOrig && hasCur) {
129 }
else if (hasOrig && !hasCur) {
134 }
catch (IOException ex) {
136 orig.toString(), cur.toString(),
NEW_LINE, ExceptionUtils.getStackTrace(ex)));
142 return String.format(
"%d,%d", chunk.getPosition() + 1, chunk.getLines().size());
166 private List<String>
getLinesDiff(Chunk<String> orig, Chunk<String> cur) {
167 Stream<String> origPrefixed = orig.getLines().stream()
168 .map((line) -> ORIG_LINE_PREFIX + line);
170 Stream<String> curPrefixed = cur.getLines().stream()
171 .map((line) -> CUR_LINE_PREFIX + line);
173 return Stream.concat(origPrefixed, curPrefixed)
174 .collect(Collectors.toList());
179 List<String> linesDiff =
getLinesDiff(delta.getSource(), delta.getTarget());
181 return Stream.concat(Stream.of(lineNums), linesDiff.stream())
182 .collect(Collectors.joining(NEW_LINE)) + NEW_LINE;
195 private String
diffLines(List<String> orig, List<String> cur, String header) {
197 Patch<String> patch = DiffUtils.diff(orig, cur);
199 String diff = patch.getDeltas().stream()
201 .collect(Collectors.joining(String.join(NEW_LINE, DIFF_BREAK)));
203 if (StringUtils.isBlank(diff)) {
207 return (header != null)
208 ? header + NEW_LINE + diff
213 String divider =
"-----------------------------------------------------------";
214 return String.join(NEW_LINE, divider, remark, divider);
218 return rootDirectory.toURI().relativize(file.toURI()).getPath();
List< String > getLinesDiff(Chunk< String > orig, Chunk< String > cur)
static final String NEW_LINE
String getRelative(File rootDirectory, File file)
String getDiffLineNumString(Chunk<?> orig, Chunk<?> cur)
String getHeaderWithDivider(String remark)
static final String ORIG_LINE_PREFIX
static final String[] DIFF_BREAK
static final long serialVersionUID
static final String[] FILE_DIFF_BREAK
String getLinesDiffString(AbstractDelta< String > delta)
String getFileDiffs(File orig, File cur, String identifier)
static final String CUR_LINE_PREFIX
String diffLines(List< String > orig, List< String > cur, String header)
String getChunkLineNumString(Chunk<?> chunk)