diff --git a/source/java/org/alfresco/filesys/repo/CIFSContentComparator.java b/source/java/org/alfresco/filesys/repo/CIFSContentComparator.java index d5f4464101..9e8f3bf0a3 100644 --- a/source/java/org/alfresco/filesys/repo/CIFSContentComparator.java +++ b/source/java/org/alfresco/filesys/repo/CIFSContentComparator.java @@ -5,7 +5,6 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -18,6 +17,7 @@ import org.alfresco.util.TempFileProvider; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.hslf.HSLFSlideShow; import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.poifs.filesystem.EntryUtils; import org.apache.poi.poifs.filesystem.FilteringDirectoryNode; @@ -50,6 +50,7 @@ public class CIFSContentComparator implements ContentComparator { customComparators.put("application/vnd.ms-project", new MPPContentComparator()); customComparators.put("application/vnd.ms-excel", new XLSContentComparator()); + customComparators.put("application/vnd.ms-powerpoint", new PPTContentComparator()); } @Override @@ -128,6 +129,22 @@ public class CIFSContentComparator implements ContentComparator } } + private boolean isContentIdentical(NPOIFSFileSystem fs1, NPOIFSFileSystem fs2, Collection excludes) throws IOException + { + DirectoryEntry de1 = fs1.getRoot(); + DirectoryEntry de2 = fs2.getRoot(); + + FilteringDirectoryNode fs1Filtered = new FilteringDirectoryNode(de1, excludes); + FilteringDirectoryNode fs2Filtered = new FilteringDirectoryNode(de2, excludes); + + boolean retVal = EntryUtils.areDirectoriesIdentical(fs1Filtered, fs2Filtered); + if(logger.isDebugEnabled()) + { + logger.debug("returning equal="+ retVal); + } + return retVal; + } + // Comparator for MS Project private class MPPContentComparator implements ContentComparator { @@ -162,22 +179,10 @@ public class CIFSContentComparator implements ContentComparator excludes.add("Props9"); leftIs = existingContent.getContentInputStream(); - NPOIFSFileSystem fs2 = new NPOIFSFileSystem(leftIs); - NPOIFSFileSystem fs1 = new NPOIFSFileSystem(newFile); + NPOIFSFileSystem fs1 = new NPOIFSFileSystem(leftIs); + NPOIFSFileSystem fs2 = new NPOIFSFileSystem(newFile); - DirectoryEntry de1 = fs1.getRoot(); - DirectoryEntry de2 = fs2.getRoot(); - - FilteringDirectoryNode fs1Filtered = new FilteringDirectoryNode(de1, excludes); - FilteringDirectoryNode fs2Filtered = new FilteringDirectoryNode(de2, excludes); - - boolean retVal = EntryUtils.areDirectoriesIdentical(fs1Filtered, fs2Filtered); - if(logger.isDebugEnabled()) - { - logger.debug("returning equal="+ retVal); - } - - return retVal; + return isContentIdentical(fs1, fs2, excludes); } catch (ContentIOException ce) { @@ -241,7 +246,8 @@ public class CIFSContentComparator implements ContentComparator tpm1 = TempFileProvider.createTempFile("CIFSContentComparator1", "xls"); tpm2 = TempFileProvider.createTempFile("CIFSContentComparator2", "xls"); - HSSFWorkbook wb1 = new HSSFWorkbook(existingContent.getContentInputStream()); + leftIs = existingContent.getContentInputStream(); + HSSFWorkbook wb1 = new HSSFWorkbook(leftIs); HSSFWorkbook wb2 = new HSSFWorkbook(new FileInputStream(newFile)); wb1.writeProtectWorkbook("", "CIFSContentComparator"); wb2.writeProtectWorkbook("", "CIFSContentComparator"); @@ -249,23 +255,126 @@ public class CIFSContentComparator implements ContentComparator wb1.write(new FileOutputStream(tpm1)); wb2.write(new FileOutputStream(tpm2)); + NPOIFSFileSystem fs1 = new NPOIFSFileSystem(tpm1); + NPOIFSFileSystem fs2 = new NPOIFSFileSystem(tpm2); - NPOIFSFileSystem fs2 = new NPOIFSFileSystem(tpm1); - NPOIFSFileSystem fs1 = new NPOIFSFileSystem(tpm2); - - DirectoryEntry de1 = fs1.getRoot(); - DirectoryEntry de2 = fs2.getRoot(); - - FilteringDirectoryNode fs1Filtered = new FilteringDirectoryNode(de1, excludes); - FilteringDirectoryNode fs2Filtered = new FilteringDirectoryNode(de2, excludes); - - boolean retVal = EntryUtils.areDirectoriesIdentical(fs1Filtered, fs2Filtered); - if(logger.isDebugEnabled()) + return isContentIdentical(fs1, fs2, excludes); + } + catch (ContentIOException ce) + { + logger.debug("Unable to compare contents", ce); + return false; + } + catch (IOException e) + { + logger.debug("Unable to compare contents", e); + return false; + } + finally + { + if(tpm1 != null) + { + try + { + tpm1.delete(); + } + catch (Exception e) + { + // ignore + } + } + if(tpm2 != null) + { + try + { + tpm2.delete(); + } + catch (Exception e) + { + // ignore + } + } + if(leftIs != null) { - logger.debug("returning equal="+ retVal); + try + { + leftIs.close(); + } + catch (IOException e) + { + // Ignore + } } + } + } + } - return retVal; + // Comparator for MS PowerPoint + private class PPTContentComparator implements ContentComparator + { + + @Override + public boolean isContentEqual(ContentReader existingContent, File newFile) + { + long fileSizesDifference = newFile.length() - existingContent.getSize(); + + if(logger.isDebugEnabled()) + { + logger.debug("comparing two powerpoint files size:" + existingContent.getSize() + ", and " + newFile.length()); + } + + File tpm1 = null; + File tpm2 = null; + InputStream leftIs = null; + try + { + if(fileSizesDifference != 0) + { + // ALF-18793 + // Experience has shown that the size of opened/closed file increases to 3072 bytes. + // (That occurs only in case if the file has been created on one MS PowerPoint instance and opened/closed on another + // due to change of lastEditUsername property (if they are different)). + if (fileSizesDifference > 3072 && fileSizesDifference < 0) + { + logger.debug("powerpoint files are different size"); + // Different size + return false; + } + + Collection excludes = new HashSet(); + + leftIs = existingContent.getContentInputStream(); + HSLFSlideShow slideShow1 = new HSLFSlideShow(leftIs); + HSLFSlideShow slideShow2 = new HSLFSlideShow(new FileInputStream(newFile)); + + String lastEditUsername1 = slideShow1.getCurrentUserAtom().getLastEditUsername(); + String lastEditUsername2 = slideShow2.getCurrentUserAtom().getLastEditUsername(); + + if (lastEditUsername1.equals(lastEditUsername2)) + { + logger.debug("powerpoint files are different size"); + // Different size + return false; + } + else + { + //make sure that nothing has been changed except lastEditUsername + + tpm1 = TempFileProvider.createTempFile("CIFSContentComparator1", "ppt"); + tpm2 = TempFileProvider.createTempFile("CIFSContentComparator2", "ppt"); + + slideShow1.write(new FileOutputStream(tpm1)); + slideShow1.write(new FileOutputStream(tpm2)); + + NPOIFSFileSystem fs1 = new NPOIFSFileSystem(tpm1); + NPOIFSFileSystem fs2 = new NPOIFSFileSystem(tpm2); + + return isContentIdentical(fs1, fs2, excludes); + } + + } + + return true; } catch (ContentIOException ce) { @@ -315,8 +424,4 @@ public class CIFSContentComparator implements ContentComparator } } } - - - - } diff --git a/source/test-java/org/alfresco/filesys/repo/CIFSContentComparatorTest.java b/source/test-java/org/alfresco/filesys/repo/CIFSContentComparatorTest.java index e7a20193a4..880f9d50a0 100644 --- a/source/test-java/org/alfresco/filesys/repo/CIFSContentComparatorTest.java +++ b/source/test-java/org/alfresco/filesys/repo/CIFSContentComparatorTest.java @@ -114,13 +114,13 @@ public class CIFSContentComparatorTest extends TestCase contentComparator.init(); ClassPathResource file0Resource = new ClassPathResource("filesys/ContentComparatorTest0.mpp"); - assertNotNull("unable to find test resource filesys/filesys/ContentComparatorTest0.mpp", file0Resource); + assertNotNull("unable to find test resource filesys/ContentComparatorTest0.mpp", file0Resource); ClassPathResource file1Resource = new ClassPathResource("filesys/ContentComparatorTest1.mpp"); - assertNotNull("unable to find test resource filesys/filesys/ContentComparatorTest1.mpp", file1Resource); + assertNotNull("unable to find test resource filesys/ContentComparatorTest1.mpp", file1Resource); ClassPathResource file2Resource = new ClassPathResource("filesys/ContentComparatorTest2.mpp"); - assertNotNull("unable to find test resource filesys/filesys/ContentComparatorTest2.mpp", file1Resource); + assertNotNull("unable to find test resource filesys/ContentComparatorTest2.mpp", file1Resource); File textFile = TempFileProvider.createTempFile("testCIFSContentComparator","txt"); FileOutputStream os1 = new FileOutputStream(textFile); @@ -180,10 +180,10 @@ public class CIFSContentComparatorTest extends TestCase contentComparator.init(); ClassPathResource file0Resource = new ClassPathResource("filesys/ContentComparatorTest0.mpp"); - assertNotNull("unable to find test resource filesys/filesys/ContentComparatorTest0.mpp", file0Resource); + assertNotNull("unable to find test resource filesys/ContentComparatorTest0.mpp", file0Resource); ClassPathResource file1Resource = new ClassPathResource("filesys/ContentComparatorTest1.mpp"); - assertNotNull("unable to find test resource filesys/filesys/ContentComparatorTest1.mpp", file1Resource); + assertNotNull("unable to find test resource filesys/ContentComparatorTest1.mpp", file1Resource); /** * Compare trivially different project files, should ignore trivial differences and be equal @@ -211,19 +211,19 @@ public class CIFSContentComparatorTest extends TestCase contentComparator.init(); ClassPathResource file0Resource = new ClassPathResource("filesys/ContentComparatorTestExcel2003-1.xls"); - assertNotNull("unable to find test resource filesys/filesys/ContentComparatorTestExcel2003-1.xls", file0Resource); + assertNotNull("unable to find test resource filesys/ContentComparatorTestExcel2003-1.xls", file0Resource); ClassPathResource file1Resource = new ClassPathResource("filesys/ContentComparatorTestExcel2003-2.xls"); - assertNotNull("unable to find test resource filesys/filesys/ContentComparatorTestExcel2003-2.xls", file1Resource); + assertNotNull("unable to find test resource filesys/ContentComparatorTestExcel2003-2.xls", file1Resource); ClassPathResource file3Resource = new ClassPathResource("filesys/ContentComparatorTestExcel2003-3.xls"); - assertNotNull("unable to find test resource filesys/filesys/ContentComparatorTestExcel2003-3.xls", file3Resource); + assertNotNull("unable to find test resource filesys/ContentComparatorTestExcel2003-3.xls", file3Resource); ClassPathResource file4Resource = new ClassPathResource("filesys/ContentComparatorTestExcel2003-4.xls"); - assertNotNull("unable to find test resource filesys/filesys/ContentComparatorTestExcel2003-4.xls", file4Resource); + assertNotNull("unable to find test resource filesys/ContentComparatorTestExcel2003-4.xls", file4Resource); ClassPathResource file5Resource = new ClassPathResource("filesys/ContentComparatorTestExcel2003-5.xls"); - assertNotNull("unable to find test resource filesys/filesys/ContentComparatorTestExcel2003-5.xls", file5Resource); + assertNotNull("unable to find test resource filesys/ContentComparatorTestExcel2003-5.xls", file5Resource); /** * Compare trivially different excel files, should ignore trivial differences and be equal @@ -267,4 +267,111 @@ public class CIFSContentComparatorTest extends TestCase assertTrue("compare trivially different xls files, should be equal", result); } } + + /** + * Open and close of a PowerPoint 2003 file changes last edit username property hence changes file size. + * + * Test File 0 is created on initial PowerPoint instance + * Test File 1 file is edited on initial PowerPoint instance so that the file size has been increased by less than 3073 bytes. + * Test File 2 has been opened and closed on target PowerPoint instance hence lastEditUserName has been changed. + * Test File 3 has been opened edited and closed on target PowerPoint instance hence lastEditUserName has been changed + * but the difference of sizes is still less than 3073 bytes. + * Test File 4 has been edited on initial PowerPoint instance so that the file size has been reduced. + * Test File 5 has been edited on initial PowerPoint instance so that the file size has been increased by more than 3072 bytes. + * + * @throws Exception + */ + public void testDiffPowerPoint2003Files() throws Exception + { + CIFSContentComparator contentComparator = new CIFSContentComparator(); + contentComparator.init(); + + ClassPathResource file0Resource = new ClassPathResource("filesys/ContentComparatorTestPowerPoint2003-0-initial.ppt"); + assertNotNull("unable to find test resource filesys/ContentComparatorTestPowerPoint2003-0-initial.ppt", file0Resource); + + ClassPathResource file1Resource = new ClassPathResource("filesys/ContentComparatorTestPowerPoint2003-1-edited-lt-3073bytes.ppt"); + assertNotNull("unable to find test resource filesys/ContentComparatorTestPowerPoint2003-1-edited-lt-3073bytes.ppt", file1Resource); + + ClassPathResource file2Resource = new ClassPathResource("filesys/ContentComparatorTestPowerPoint2003-2-opened-closed.ppt"); + assertNotNull("unable to find test resource filesys/ContentComparatorTestPowerPoint2003-2-opened-closed.ppt", file2Resource); + + ClassPathResource file3Resource = new ClassPathResource("filesys/ContentComparatorTestPowerPoint2003-3-opened-edited-closed.ppt"); + assertNotNull("unable to find test resource filesys/ContentComparatorTestPowerPoint2003-3-opened-edited-closed.ppt", file3Resource); + + ClassPathResource file4Resource = new ClassPathResource("filesys/ContentComparatorTestPowerPoint2003-4-edited-lt-0bytes.ppt"); + assertNotNull("unable to find test resource filesys/ContentComparatorTestPowerPoint2003-4-edited-lt-0bytes.ppt", file4Resource); + + ClassPathResource file5Resource = new ClassPathResource("filesys/ContentComparatorTestPowerPoint2003-5-edited-gt-3072bytes.ppt"); + assertNotNull("unable to find test resource filesys/ContentComparatorTestPowerPoint2003-5-edited-gt-3072bytes.ppt", file5Resource); + + /** + * Compare different powerpoint files, should not be ignored + */ + { + File file0 = file0Resource.getFile(); + File file1 = file1Resource.getFile(); + + ContentReader reader = new FileContentReader(file0); + reader.setMimetype("application/vnd.ms-powerpoint"); + reader.setEncoding("UTF-8"); + boolean result = contentComparator.isContentEqual(reader, file1); + assertTrue("compare different powerpoint files, should not be equal", !result); + } + + /** + * Compare trivially different powerpoint files, should ignore trivial differences and be equal + */ + { + File file0 = file0Resource.getFile(); + File file2 = file2Resource.getFile(); + + ContentReader reader = new FileContentReader(file0); + reader.setMimetype("application/vnd.ms-powerpoint"); + reader.setEncoding("UTF-8"); + boolean result = contentComparator.isContentEqual(reader, file2); + assertTrue("compare trivially different powerpoint files, should be equal", result); + } + + /** + * Compare different powerpoint files, should not be ignored + */ + { + File file0 = file0Resource.getFile(); + File file3 = file1Resource.getFile(); + + ContentReader reader = new FileContentReader(file0); + reader.setMimetype("application/vnd.ms-powerpoint"); + reader.setEncoding("UTF-8"); + boolean result = contentComparator.isContentEqual(reader, file3); + assertTrue("compare different powerpoint files, should not be equal", !result); + } + + /** + * Compare different powerpoint files, should not be ignored + */ + { + File file0 = file0Resource.getFile(); + File file4 = file1Resource.getFile(); + + ContentReader reader = new FileContentReader(file0); + reader.setMimetype("application/vnd.ms-powerpoint"); + reader.setEncoding("UTF-8"); + boolean result = contentComparator.isContentEqual(reader, file4); + assertTrue("compare different powerpoint files, should not be equal", !result); + } + + /** + * Compare different powerpoint files, should not be ignored + */ + { + File file0 = file0Resource.getFile(); + File file5 = file1Resource.getFile(); + + ContentReader reader = new FileContentReader(file0); + reader.setMimetype("application/vnd.ms-powerpoint"); + reader.setEncoding("UTF-8"); + boolean result = contentComparator.isContentEqual(reader, file5); + assertTrue("compare different powerpoint files, should not be equal", !result); + } + } } diff --git a/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-0-initial.ppt b/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-0-initial.ppt new file mode 100644 index 0000000000..c15ca57a6d Binary files /dev/null and b/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-0-initial.ppt differ diff --git a/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-1-edited-lt-3073bytes.ppt b/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-1-edited-lt-3073bytes.ppt new file mode 100644 index 0000000000..347933d9a6 Binary files /dev/null and b/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-1-edited-lt-3073bytes.ppt differ diff --git a/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-2-opened-closed.ppt b/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-2-opened-closed.ppt new file mode 100644 index 0000000000..315c3c7ddb Binary files /dev/null and b/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-2-opened-closed.ppt differ diff --git a/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-3-opened-edited-closed.ppt b/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-3-opened-edited-closed.ppt new file mode 100644 index 0000000000..05519ee818 Binary files /dev/null and b/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-3-opened-edited-closed.ppt differ diff --git a/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-4-edited-gt-3072bytes.ppt b/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-4-edited-gt-3072bytes.ppt new file mode 100644 index 0000000000..b0b03296ab Binary files /dev/null and b/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-4-edited-gt-3072bytes.ppt differ diff --git a/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-5-edited-lt-0bytes.ppt b/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-5-edited-lt-0bytes.ppt new file mode 100644 index 0000000000..c040d93213 Binary files /dev/null and b/source/test-resources/filesys/ContentComparatorTestPowerPoint2003-5-edited-lt-0bytes.ppt differ