forked from assertj/assertj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiff.java
More file actions
128 lines (113 loc) · 4.07 KB
/
Diff.java
File metadata and controls
128 lines (113 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* 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-2014 the original author or authors.
*/
package org.assertj.core.internal;
import static java.lang.String.format;
import static java.util.Collections.unmodifiableList;
import static org.assertj.core.util.Closeables.closeQuietly;
import static org.assertj.core.util.Objects.areEqual;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.assertj.core.util.VisibleForTesting;
/**
* Compares the contents of two files or two streams.
*
* @author David DIDIER
* @author Alex Ruiz
* @author Yvonne Wang
* @author Matthieu Baechler
* @author Olivier Michallat
* @author Joel Costigliola
*/
@VisibleForTesting
public class Diff {
private static final String EOF = "EOF";
@VisibleForTesting
public List<String> diff(InputStream actual, InputStream expected) throws IOException {
BufferedReader reader1 = null;
BufferedReader reader2 = null;
try {
reader1 = readerFor(actual);
reader2 = readerFor(expected);
return unmodifiableList(diff(reader1, reader2));
} finally {
closeQuietly(reader1);
closeQuietly(reader2);
}
}
@VisibleForTesting
public List<String> diff(File actual, File expected) throws IOException {
BufferedReader reader1 = null;
BufferedReader reader2 = null;
try {
reader1 = readerFor(actual);
reader2 = readerFor(expected);
return unmodifiableList(diff(reader1, reader2));
} finally {
closeQuietly(reader1);
closeQuietly(reader2);
}
}
@VisibleForTesting
public List<String> diff(File actual, String expected, Charset charset) throws IOException {
BufferedReader reader1 = null;
try {
reader1 = readerFor(actual, charset);
BufferedReader reader2 = readerFor(expected);
return unmodifiableList(diff(reader1, reader2));
} finally {
closeQuietly(reader1);
}
}
private BufferedReader readerFor(InputStream stream) {
return new BufferedReader(new InputStreamReader(stream));
}
private BufferedReader readerFor(InputStream stream, Charset charset) {
return new BufferedReader(new InputStreamReader(stream, charset));
}
private BufferedReader readerFor(File file) throws IOException {
return readerFor(new FileInputStream(file));
}
private BufferedReader readerFor(File file, Charset charset) throws IOException {
return readerFor(new FileInputStream(file), charset);
}
private BufferedReader readerFor(String string) {
return new BufferedReader(new StringReader(string));
}
private List<String> diff(BufferedReader actual, BufferedReader expected) throws IOException {
List<String> diffs = new ArrayList<>();
int lineNumber = 1;
while (true) {
String actualLine = actual.readLine();
String expectedLine = expected.readLine();
if (actualLine == null || expectedLine == null) {
if (expectedLine != null) diffs.add(output(lineNumber, EOF, expectedLine));
if (actualLine != null) diffs.add(output(lineNumber, actualLine, EOF));
return diffs;
} else if (!areEqual(actualLine, expectedLine)) {
diffs.add(output(lineNumber, actualLine, expectedLine));
}
lineNumber += 1;
}
}
private String output(int lineNumber, String actual, String expected) {
return format("line:<%d>, expected:<%s> but was:<%s>", lineNumber, expected, actual);
}
}