Package: FileComparisonUtils
FileComparisonUtils
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
FileComparisonUtils() |
|
|
|
|
|
||||||||||||||||||||
assertSameContents(File, File) |
|
|
|
|
|
||||||||||||||||||||
assertSameContents(List, List) |
|
|
|
|
|
||||||||||||||||||||
commonPrefix(String, String) |
|
|
|
|
|
||||||||||||||||||||
fileToStrings(File) |
|
|
|
|
|
||||||||||||||||||||
fileToStrings(InputStream) |
|
|
|
|
|
||||||||||||||||||||
fileToStrings(String) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
stringToStrings(String) |
|
|
|
|
|
Coverage
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * These Foolish Things - Miscellaneous utilities
6: * http://thesefoolishthings.tidalwave.it - git clone git@bitbucket.org:tidalwave/thesefoolishthings-src.git
7: * %%
8: * Copyright (C) 2009 - 2018 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: * $Id$
24: *
25: * *********************************************************************************************************************
26: * #L%
27: */
28: package it.tidalwave.util.test;
29:
30: import java.io.InputStream;
31: import javax.annotation.Nonnull;
32: import java.util.ArrayList;
33: import java.util.List;
34: import java.io.BufferedReader;
35: import java.io.ByteArrayInputStream;
36: import java.io.File;
37: import java.io.FileInputStream;
38: import java.io.IOException;
39: import java.io.InputStreamReader;
40: import org.incava.util.diff.Diff;
41: import org.incava.util.diff.Difference;
42: import org.slf4j.Logger;
43: import org.slf4j.LoggerFactory;
44: import static org.junit.Assert.*;
45:
46: /***********************************************************************************************************************
47: *
48: * @author Fabrizio Giudici
49: * @version $Id: $
50: *
51: **********************************************************************************************************************/
52: public final class FileComparisonUtils
53: {
54: private static final Logger log = LoggerFactory.getLogger(FileComparisonUtils.class);
55:
56: /*******************************************************************************************************************
57: *
58: *
59: ******************************************************************************************************************/
60: public static void assertSameContents (final @Nonnull File expectedFile, final @Nonnull File actualFile)
61: throws IOException
62: {
63: final String expectedPath = expectedFile.getAbsolutePath();
64: final String actualPath = actualFile.getAbsolutePath();
65: final String commonPath = commonPrefix(expectedPath, actualPath);
66: log.info("******** Comparing files:");
67: log.info(">>>> path is: {}", commonPath);
68: log.info(">>>> exp is: {}", expectedPath.substring(commonPath.length()));
69: log.info(">>>> act is: {}", actualPath.substring(commonPath.length()));
70: assertSameContents(fileToStrings(expectedFile), fileToStrings(actualFile));
71: }
72:
73: /*******************************************************************************************************************
74: *
75: *
76: ******************************************************************************************************************/
77: public static void assertSameContents (final @Nonnull List<String> expected, final @Nonnull List<String> actual)
78: {
79: final Diff diff = new Diff(expected, actual);
80: final List<Difference> differences = diff.diff();
81:
82:• if (!differences.isEmpty())
83: {
84: final StringBuilder buffer = new StringBuilder();
85:
86:• for (final Difference difference : differences)
87: {
88: int addedStart = difference.getAddedStart();
89: int addedEnd = difference.getAddedEnd();
90: int deletedStart = difference.getDeletedStart();
91: int deletedEnd = difference.getDeletedEnd();
92:
93:• if (addedStart >= 0)
94: {
95:• addedEnd = (addedEnd >= 0) ? addedEnd : actual.size() - 1;
96:
97:• for (int i = addedStart; i <= addedEnd; i++)
98: {
99: buffer.append(String.format("-act: %3d: *%s*\n", i + 1, actual.get(i)));
100: }
101: }
102:
103:• if (deletedStart >= 0)
104: {
105:• deletedEnd = (deletedEnd >= 0) ? deletedEnd : expected.size() - 1;
106:
107:• for (int i = deletedStart; i <= deletedEnd; i++)
108: {
109: buffer.append(String.format("+exp: %3d: *%s*\n", i + 1, expected.get(i)));
110: }
111: }
112: }
113:
114: log.info("{}", buffer);
115: fail("Unexpected contents:\n" + buffer);
116: }
117: }
118:
119: /*******************************************************************************************************************
120: *
121: *
122: ******************************************************************************************************************/
123: @Nonnull
124: public static List<String> stringToStrings (final @Nonnull String string)
125: throws IOException
126: {
127: //return Arrays.asList(string.split("\n"));
128: return fileToStrings(new ByteArrayInputStream(string.getBytes("UTF-8")));
129: }
130:
131: /*******************************************************************************************************************
132: *
133: *
134: ******************************************************************************************************************/
135: @Nonnull
136: public static List<String> fileToStrings (final @Nonnull File file)
137: throws IOException
138: {
139: return fileToStrings(new FileInputStream(file));
140: }
141:
142: /*******************************************************************************************************************
143: *
144: *
145: ******************************************************************************************************************/
146: @Nonnull
147: public static List<String> fileToStrings (final @Nonnull String path)
148: throws IOException
149: {
150: final InputStream is = FileComparisonUtils.class.getClassLoader().getResourceAsStream(path);
151:
152:• if (is == null)
153: {
154: throw new RuntimeException("Resource not found: " + path);
155: }
156:
157: return fileToStrings(is);
158: }
159:
160: /*******************************************************************************************************************
161: *
162: *
163: ******************************************************************************************************************/
164: @Nonnull
165: public static List<String> fileToStrings (final @Nonnull InputStream is)
166: throws IOException
167: {
168: final BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
169: final List<String> result = new ArrayList<String>();
170:
171: for (;;)
172: {
173: final String s = br.readLine();
174:
175:• if (s == null)
176: {
177: break;
178: }
179:
180: result.add(s);
181: }
182:
183: br.close();
184:
185: return result;
186: }
187:
188: /*******************************************************************************************************************
189: *
190: *
191: ******************************************************************************************************************/
192: @Nonnull
193: public static String commonPrefix (final @Nonnull String s1, final @Nonnull String s2)
194: {
195: final int min = Math.min(s1.length(), s2.length());
196: int latestSeenSlash = 0;
197:
198:• for (int i = 0; i < min; i++)
199: {
200:• if (s1.charAt(i) != s2.charAt(i))
201: {
202:• return (i == 0) ? "" : s1.substring(0, Math.min(latestSeenSlash + 1, min));
203: }
204: else
205: {
206:• if (s1.charAt(i) == File.separatorChar)
207: {
208: latestSeenSlash = i;
209: }
210: }
211: }
212:
213: return s1.substring(0, min);
214: }
215: }