com.fitbur.assertj.internal.Diff Maven / Gradle / Ivy
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2016 the original author or authors.
*/
package com.fitbur.assertj.internal;
import static java.nio.file.Files.newBufferedReader;
import static java.util.Collections.unmodifiableList;
import static com.fitbur.assertj.util.Closeables.closeQuietly;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import com.fitbur.assertj.util.VisibleForTesting;
import com.fitbur.assertj.util.diff.Delta;
import com.fitbur.assertj.util.diff.DiffUtils;
import com.fitbur.assertj.util.diff.Patch;
/**
* Compares the contents of two files, inputStreams or paths.
*
* @author David DIDIER
* @author Alex Ruiz
* @author Yvonne Wang
* @author Matthieu Baechler
* @author Olivier Michallat
* @author Joel Costigliola
*/
@VisibleForTesting
public class Diff {
@VisibleForTesting
public List> diff(InputStream actual, InputStream expected) throws IOException {
return diff(readerFor(actual), readerFor(expected));
}
@VisibleForTesting
public List> diff(File actual, Charset actualCharset, File expected, Charset expectedCharset) throws IOException {
return diff(actual.toPath(), actualCharset, expected.toPath(), expectedCharset);
}
@VisibleForTesting
public List> diff(Path actual, Charset actualCharset, Path expected, Charset expectedCharset) throws IOException {
return diff(newBufferedReader(actual, actualCharset), newBufferedReader(expected, expectedCharset));
}
@VisibleForTesting
public List> diff(File actual, String expected, Charset charset) throws IOException {
return diff(actual.toPath(), expected, charset);
}
@VisibleForTesting
public List> diff(Path actual, String expected, Charset charset) throws IOException {
return diff(newBufferedReader(actual, charset), readerFor(expected));
}
private BufferedReader readerFor(InputStream stream) {
return new BufferedReader(new InputStreamReader(stream, Charset.defaultCharset()));
}
private BufferedReader readerFor(String string) {
return new BufferedReader(new StringReader(string));
}
private List> diff(BufferedReader actual, BufferedReader expected) throws IOException {
try {
List actualLines = linesFromBufferedReader(actual);
List expectedLines = linesFromBufferedReader(expected);
Patch patch = DiffUtils.diff(expectedLines, actualLines);
return unmodifiableList(patch.getDeltas());
} finally {
closeQuietly(actual, expected);
}
}
private List linesFromBufferedReader(BufferedReader reader) throws IOException {
String line;
List lines = new ArrayList<>();
while ((line = reader.readLine()) != null) {
lines.add(line);
}
return lines;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy