
To properly use the following file you will also need this build.xml.
package samples.junit; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.apache.log4j.ConsoleAppender; import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.SimpleLayout; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import samples.CustomResultHandler; import com.inet.pdfc.config.ConfigurationFactory; import com.inet.pdfc.config.IConfiguration; import com.inet.pdfc.config.PDFCProperty; import com.inet.pdfc.results.IResultHandler; import com.inet.pdfc.runner.BatchRunner; /** * This sample demonstrates how you can use JUnit to compare two folders * containing PDF files with pairwise identical names. * * This sample runs as a parameterized JUnit test for each PDF file. */ @RunWith(Parameterized.class) public class CompareTwoFoldersAsUnitTest { // the configuration containing various settings for i-net PDFC private static IConfiguration configuration; // the logger that is set and reseted during the JUnit test private static Logger logger = Logger.getRootLogger(); // the appender for showing log output on a console with a simple layout private static ConsoleAppender consoleAppender = new ConsoleAppender( new SimpleLayout() ); // the log level to be reseted after the test private static Level loglevel = logger.getLevel(); // the PDF file, that should be compared with a reference file private File currentPdfFile = null; // the reference file to compare with private File referencePdfFile = null; // The root folder of the PDF files that should be compared. // It must be set with the -DcurrentFolder=... java option private static final String SOURCE = System.getProperty( "currentFolder" ); // The root folder of the reference PDF files. // It must be set with the -DreferenceFolder=... java option private static final String REFERENCE = System.getProperty( "referenceFolder" ); // The root folder of the sources. private static String sourceRootDir; /** * This filter lists only PDF files. */ static final FileFilter PDFFILEFILTER = new FileFilter() { public boolean accept( File f ) { return f.isFile() && f.getName().endsWith( ".pdf" ); } }; /** * This filter lists only directories. */ static final FileFilter SUBDIRECTORYFILTER = new FileFilter() { public boolean accept( File f ) { return f.isDirectory(); } }; /** * The constructor sets the current PDF file that should be compared with the specified reference PDF file. * @param currentPdf the PDF file that should be compared * @param referencePdf the reference PDF file */ public CompareTwoFoldersAsUnitTest(String currentPdf, String referencePdf) { currentPdfFile = new File( currentPdf ); referencePdfFile = new File( referencePdf ); } /** * Initialize the test by enabling console output to the logger and setting the log level to display some informations. * These changes to the logger will be reseted. * In addition the create PNG image settings of the PDFC configuration will be disabled. * */ @BeforeClass public static void init() { logger.addAppender( consoleAppender ); logger.setLevel( Level.INFO ); configuration = ConfigurationFactory.getConfiguration(); configuration.putObject( PDFCProperty.CREATE_DIFFIMAGES, Boolean.FALSE ); // don't create PNG image for each page of the PDF file with differences configuration.putObject( PDFCProperty.CREATE_ORIGIMAGES, Boolean.FALSE ); // don't create PNG image for each page with the original content } /** * Resets the changes to the logger after this test has run. */ @AfterClass public static void shutdown() { logger.removeAppender( consoleAppender ); logger.setLevel( loglevel ); } /** * Creates a collection that contains a mapping for all PDF files in the source folder with their * corresponding reference PDF files. * This test will be executed for each entry in this collection. * @return a list with all source PDF files an their corresponding reference files. */ @Parameters public static Collection<Object[]> data() { List<Object[]> list = new java.util.ArrayList<Object[]>(); // the source directory must be set assertTrue( "Test directory not defined", SOURCE != null ); File mainDir = new File( SOURCE ); // it must be a directory assertTrue( "Test directory " + SOURCE + " is not a directory ", mainDir.isDirectory() ); try{ // get the root path of the sources sourceRootDir = mainDir.getCanonicalPath(); } catch( IOException e ) { fail( "Canonical representation of main directory"); } // add all founded PDF files in the source directory to the list list.addAll( preparePaths( mainDir ) ); assertTrue( "There are no PDF files in the source directory at " + SOURCE, list.size() != 0 ); return list; } /** * Makes recursive search of PDF files in current directory and in it's sub directories. * @param sourceDir the current directory to look for PDF files * @return list of all found PDF files */ private static List<Object[]> preparePaths( File sourceDir ) { List<Object[]> paramList = new ArrayList<Object[]>(); try{ String currentSourcePath = sourceDir.getCanonicalPath(); String subDirPath; subDirPath = currentSourcePath.substring( sourceRootDir.length() ); if( subDirPath.length() != 0 ) { if( subDirPath.charAt( 0 ) == File.separatorChar ) { subDirPath = subDirPath.substring( 1 ); } } // get all files using the filter File[] pdfFiles = sourceDir.listFiles( PDFFILEFILTER ); for( int i=0; i<pdfFiles.length; i++ ) { File pdfFile = pdfFiles[i]; String pdfName = pdfFile.getName(); if( pdfName.trim().length() != 0 ) { // set the path for the source file String sourceFilePath = currentSourcePath + File.separatorChar + pdfName; // set the path for the corresponding reference file String refListsPath = REFERENCE + File.separatorChar; if( subDirPath.length() != 0 ) { refListsPath = refListsPath + subDirPath + File.separatorChar; } refListsPath = refListsPath + pdfName; // add the source file with its corresponding reference file to the list. Object[] params = new String[]{refListsPath, sourceFilePath }; paramList.add( params ); } } // parse the source sub directories File[] subDirs = sourceDir.listFiles( SUBDIRECTORYFILTER ); for( int i=0; i<subDirs.length; i++ ) { File subDir = subDirs[i]; paramList.addAll( preparePaths(subDir ) ); } } catch( IOException e ) { e.printStackTrace(); } return paramList; } /** * Compares the current PDF file with it's reference file and checks if there are any differences. * @throws IOException if the canonical path of the current PDF file can not be obtained */ @Test public void compare() throws IOException { IResultHandler resultHandler = new CustomResultHandler( configuration ); // compare the current PDF file with it's reference (new BatchRunner( configuration, resultHandler )).runComparison( referencePdfFile, currentPdfFile ); // The same Custom Result Handler can be used for File- and for Folder- Comparison CustomResultHandler customResultHandler = (CustomResultHandler)resultHandler; int differenceCount = customResultHandler.getDifferenceCount( 0 ); assertTrue( differenceCount + " differences found in " + currentPdfFile.getCanonicalPath(), differenceCount == 0 ); } }