it.tidalwave.netbeans.util.test.AssertFileContents Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* OpenBlueSky - NetBeans Platform Enhancements
* ============================================
*
* Copyright (C) 2007-2010 by Tidalwave s.a.s.
* Project home page: http://openbluesky.kenai.com
*
***********************************************************************************************************************
*
* 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.
*
***********************************************************************************************************************
*
* $Id: AssertFileContents.java,v f17245cd751a 2010/03/14 17:14:28 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.netbeans.util.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import junit.framework.AssertionFailedError;
import org.incava.util.diff.Diff;
import org.incava.util.diff.Difference;
/***********************************************************************************************************************
*
* Adapted from:
* http://code.google.com/p/jatran/source/browse/trunk/src/main/org/incava/util/diff/FileDiff.java?spec=svn22&r=22
*
* @author Fabrizio Giudici
* @version $Id: AssertFileContents.java,v f17245cd751a 2010/03/14 17:14:28 fabrizio $
*
**********************************************************************************************************************/
public final class AssertFileContents
{
private static final String CLASS = AssertFileContents.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
public static void assertSameFiles (final String fromFile, final String toFile)
throws IOException
{
assertSameFiles(new File(fromFile), new File(toFile));
}
public static void assertSameFiles (final File f1, final File f2)
throws IOException
{
logger.info("Comparing " + f1.getCanonicalPath() + " and " + f2.getCanonicalPath());
final String[] aLines = read(f1);
final String[] bLines = read(f2);
final List differences = (new Diff(aLines, bLines)).diff();
if (differences.isEmpty())
{
logger.info(">>>> files are the same");
}
else
{
logger.info(">>>> files differ");
for (final Iterator i = differences.iterator(); i.hasNext(); )
{
final Difference diff = (Difference) i.next();
final int delStart = diff.getDeletedStart();
final int delEnd = diff.getDeletedEnd();
final int addStart = diff.getAddedStart();
final int addEnd = diff.getAddedEnd();
final String from = toString(delStart, delEnd);
final String to = toString(addStart, addEnd);
final String type = delEnd != Difference.NONE
&& addEnd != Difference.NONE ? "c"
: (delEnd == Difference.NONE ? "a" : "d");
logger.info(from + type + to);
if (delEnd != Difference.NONE)
{
printLines(delStart, delEnd, "<", aLines);
if (addEnd != Difference.NONE)
{
logger.info("---");
}
}
if (addEnd != Difference.NONE)
{
printLines(addStart, addEnd, ">", bLines);
}
}
throw new AssertionFailedError("Files differ: " + f1.getCanonicalPath() + " and " + f2.getCanonicalPath());
}
}
protected static void printLines (final int start, final int end, final String ind, final String[] lines)
{
for (int lnum = start; lnum <= end; ++lnum)
{
logger.info(ind + " " + lines[lnum]);
}
}
protected static String toString (final int start, final int end)
{
// adjusted, because file lines are one-indexed, not zero.
final StringBuilder buffer = new StringBuilder();
// match the line numbering from diff(1):
buffer.append(end == Difference.NONE ? start : (1 + start));
if (end != Difference.NONE && start != end)
{
buffer.append(",").append(1 + end);
}
return buffer.toString();
}
@Nonnull
@edu.umd.cs.findbugs.annotations.SuppressWarnings("OS_OPEN_STREAM_EXCEPTION_PATH")
private static String[] read (final @Nonnull File file)
throws IOException
{
final BufferedReader br = new BufferedReader(new FileReader(file));
final List contents = new ArrayList();
String in;
while ((in = br.readLine()) != null)
{
contents.add(in);
}
br.close();
return contents.toArray(new String[contents.size()]);
}
}