Skip to content

Instantly share code, notes, and snippets.

@brianstorti
Forked from cbaldin/FileTestOperations.java
Created September 3, 2011 05:26
Show Gist options
  • Save brianstorti/1190621 to your computer and use it in GitHub Desktop.
Save brianstorti/1190621 to your computer and use it in GitHub Desktop.
Assert File
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import junitx.framework.AssertionFailedError;
import org.junit.Assert;
public class FileTestOperations {
boolean fail;
public void assertFileWithTemplate(final String file, final String template) {
this.assertFileWithTemplate(new File(file), new File(template));
}
public void assertFileWithTemplate(final File file, final File template) {
Assert.assertNotNull(template);
Assert.assertNotNull(file);
Assert.assertTrue("File does not exist [" + template.getAbsolutePath() + "]", template.exists());
Assert.assertTrue("File does not exist [" + file.getAbsolutePath() + "]", file.exists());
Assert.assertTrue("Template file not readable", template.canRead());
Assert.assertTrue("Actual file not readable", file.canRead());
FileInputStream templateStream = null;
FileInputStream fileStream = null;
try {
try {
templateStream = new FileInputStream(template);
fileStream = new FileInputStream(file);
BufferedReader fileData = new BufferedReader(new InputStreamReader(fileStream));
BufferedReader templData = new BufferedReader(new InputStreamReader(templateStream));
asserLines(fileData, templData);
} finally {
templateStream.close();
fileStream.close();
}
} catch (IOException e) {
throw new AssertionFailedError(e);
}
if (fail) {
Assert.fail("Generated file is not equal provided template.");
}
}
private void assertLines(final BufferedReader fileData, final BufferedReader templData) throws IOException {
String fileLine;
String templateLine;
for (int lineNumber = 1; true; lineNumber++) {
fileLine = fileData.readLine();
templateLine = templData.readLine();
if (fileLine == null && templateLine == null) {
return;
}
this.verifyFileLengh(fileLine, templateLine, lineNumber);
if (!templateLine.equals(fileLine)) {
searchCaracterWithDiference(fileLine.toCharArray(), templateLine.toCharArray(), lineNumber);
}
}
}
private void searchCaracterWithDiference(char[] lineChars, char[] templChars, int lineNumber) {
int column;
for (int i = 0; i < templChars.length; i++) {
if (lineChars[i] != templChars[i]) {
column = i + 1;
System.out.println("Difference was found at line " + lineNumber + ", near column " + column + ". \"" + String.valueOf(lineChars[i]) + "\" expected, \"" + String.valueOf(templChars[i] + "\" received."));
fail = true;
}
}
}
private void verifyFileLengh(final String fileLine, final String templateLine, final int line) {
Assert.assertFalse("Generated file has less lines than template.", fileLine == null && templateLine != null);
Assert.assertFalse("Generated file has more lines than template", fileLine != null && templateLine == null);
Assert.assertFalse("Line " + line + " from the generated file is larger than template.", fileLine.length() > templateLine.length());
Assert.assertFalse("Line " + line + " from generated file is smaller than template.", fileLine.length() < templateLine.length());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment