Modified: pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/metadata/TestSynchronizedMetadataValidation.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/metadata/TestSynchronizedMetadataValidation.java?rev=1453416&r1=1453415&r2=1453416&view=diff ============================================================================== --- pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/metadata/TestSynchronizedMetadataValidation.java (original) +++ pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/metadata/TestSynchronizedMetadataValidation.java Wed Mar 6 16:46:35 2013 @@ -41,535 +41,580 @@ import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; + /** * Test Class of SynchronizedMetaDataValidation (for 6-7-3 Isartor Tests) * * @author Germain Costenobel * */ -public class TestSynchronizedMetadataValidation { +public class TestSynchronizedMetadataValidation +{ + + protected PDDocument doc; + protected PDDocumentInformation dico; + protected XMPMetadata metadata; + protected String title, author, subject, keywords, creator, producer; + protected Calendar creationDate, modifyDate; + protected static SynchronizedMetaDataValidation sync; + protected List<ValidationError> ve; + + @BeforeClass + public static void initSynchronizedMetadataValidation() + { + sync = new SynchronizedMetaDataValidation(); + } + + @Before + public void initNewDocumentInformation() throws Exception + { + + try + { + doc = new PDDocument(); + dico = doc.getDocumentInformation(); + metadata = XMPMetadata.createXMPMetadata(); + } + catch (IOException e) + { + throw new Exception("Failed to create temporary test PDF/XMP Document"); + } + + } + + /** + * Check detection of a null Document + * + * @throws ValidationException + */ + @Test(expected = ValidationException.class) + public void TestNullDocument() throws ValidationException + { + sync.validateMetadataSynchronization(null, metadata); + } + + /** + * Check detection of null metadata + * + * @throws ValidationException + */ + @Test(expected = ValidationException.class) + public void TestNullMetaData() throws ValidationException + { + sync.validateMetadataSynchronization(doc, null); + } - protected PDDocument doc; - protected PDDocumentInformation dico; - protected XMPMetadata metadata; - protected String title, author, subject, keywords, creator, producer; - protected Calendar creationDate, modifyDate; - protected static SynchronizedMetaDataValidation sync; - protected List<ValidationError> ve; - - @BeforeClass - public static void initSynchronizedMetadataValidation() { - sync = new SynchronizedMetaDataValidation(); - } - - @Before - public void initNewDocumentInformation() throws Exception { - - try { - doc = new PDDocument(); - dico = doc.getDocumentInformation(); - metadata = XMPMetadata.createXMPMetadata(); - } catch (IOException e) { - throw new Exception("Failed to create temporary test PDF/XMP Document"); - } - - } - - /** - * Check detection of a null Document - * - * @throws ValidationException - */ - @Test(expected = ValidationException.class) - public void TestNullDocument() throws ValidationException { - sync.validateMetadataSynchronization(null, metadata); - } - - /** - * Check detection of null metadata - * - * @throws ValidationException - */ - @Test(expected = ValidationException.class) - public void TestNullMetaData() throws ValidationException { - sync.validateMetadataSynchronization(doc, null); - } - - /** - * Check the detection of a PDF document without any information - * - * @throws Exception - */ - @Test - public void TestDocumentWithoutInformation() throws Exception { - try { - ve = sync.validateMetadataSynchronization(doc, metadata); - // Test without any information - Assert.assertEquals(0, ve.size()); - } catch (ValidationException e) { - throw new Exception(e.getMessage()); - } - } - - /** - * Check the detection of a completely empty XMP document (without any - * schemas) - * - * @throws Exception - */ - @Test - public void testEmptyXMP() throws Exception { - title = "TITLE"; - author = "AUTHOR(S)"; - subject = "SUBJECTS"; - keywords = "KEYWORD(S)"; - creator = "CREATOR"; - producer = "PRODUCER"; - creationDate = Calendar.getInstance(); - modifyDate = Calendar.getInstance(); - - // Writing info in Document Information dictionary - // TITLE - dico.setTitle(title); - // AUTHOR - dico.setAuthor(author); - // SUBJECT - dico.setSubject(subject); - // KEYWORDS - dico.setKeywords(keywords); - // CREATOR - dico.setCreator(creator); - // PRODUCER - dico.setProducer(producer); - // CREATION DATE - dico.setCreationDate(creationDate); - // MODIFY DATE - dico.setModificationDate(modifyDate); - - try { - ve = sync.validateMetadataSynchronization(doc, metadata); - // Test Detection of an Empty XMP (without any schemas) - for (ValidationError valid : ve) { - Assert.assertEquals(PreflightConstants.ERROR_METADATA_MISMATCH, valid - .getErrorCode()); - } - } catch (ValidationException e) { - throw new Exception(e.getMessage()); - } - } - - /** - * Check the detection of a XMP with empty common schemas - * - * @throws Exception - */ - @Test - public void testEmptyXMPSchemas() throws Exception { - title = "TITLE"; - author = "AUTHOR(S)"; - subject = "SUBJECTS"; - keywords = "KEYWORD(S)"; - creator = "CREATOR"; - producer = "PRODUCER"; - creationDate = Calendar.getInstance(); - modifyDate = Calendar.getInstance(); - - // building temporary XMP metadata (but empty) - metadata.createAndAddDublinCoreSchema(); - metadata.createAndAddAdobePDFSchema(); - metadata.createAndAddXMPBasicSchema(); - - // Writing info in Document Information dictionary - // TITLE - dico.setTitle(title); - // AUTHOR - dico.setAuthor(author); - // SUBJECT - dico.setSubject(subject); - // KEYWORDS - dico.setKeywords(keywords); - // CREATOR - dico.setCreator(creator); - // PRODUCER - dico.setProducer(producer); - // CREATION DATE - dico.setCreationDate(creationDate); - // MODIFY DATE - dico.setModificationDate(modifyDate); - - try { - ve = sync.validateMetadataSynchronization(doc, metadata); - // Test Detection of absent XMP values - Assert.assertEquals(8, ve.size()); - } catch (ValidationException e) { - throw new Exception(e.getMessage()); - } - - } - - /** - * Check detection of a null value in array (for Subject and author - * properties) - * - * @throws Exception - */ - @Test(expected = IllegalArgumentException.class) - public void testNullArrayValue() throws Exception { - // building temporary XMP metadata - - DublinCoreSchema dc = metadata.createAndAddDublinCoreSchema(); - - // AUTHOR - dico.setAuthor("dicoAuthor"); - dc.addCreator(null); - - // SUBJECT - dico.setSubject("dicoSubj"); - dc.addSubject(null); - - // Launching synchronization test - try { - ve = sync.validateMetadataSynchronization(doc, metadata); - // Test unsychronized value - Assert.assertEquals(2, ve.size()); - - } catch (ValidationException e) { - throw new Exception(e.getMessage()); - } - } - - /** - * in XMP, Subject and Author must be embedded in a single entry text array - * This function check the detection of multiple entries for these properties - * - * @throws Exception - */ - @Test - public void testBadSizeOfArrays() throws Exception { - // building temporary XMP metadata - - DublinCoreSchema dc = metadata.createAndAddDublinCoreSchema(); - AdobePDFSchema pdf = metadata.createAndAddAdobePDFSchema(); - XMPBasicSchema xmp = metadata.createAndAddXMPBasicSchema(); - - // Writing info in XMP and Document Information dictionary - // TITLE - dico.setTitle("dicoTitle"); - dc.setTitle("x-default", "XMPTitle"); - // AUTHOR - dico.setAuthor("dicoAuthor"); - dc.addCreator("XMPAuthor"); - dc.addCreator("2ndCreator"); - // SUBJECT - dico.setSubject("dicoSubj"); - dc.addSubject("XMPSubj"); - dc.addSubject("2ndSubj"); - // KEYWORDS - dico.setKeywords("DicoKeywords"); - pdf.setKeywords("XMPkeywords"); - // CREATOR - dico.setCreator("DicoCreator"); - xmp.setCreatorTool("XMPCreator"); - // PRODUCER - dico.setProducer("DicoProducer"); - pdf.setProducer("XMPProducer"); - // CREATION DATE - dico.setCreationDate(Calendar.getInstance()); - GregorianCalendar XMPCreate = new GregorianCalendar(2008, 11, 05); - xmp.setCreateDate(XMPCreate); - // MODIFY DATE - dico.setModificationDate(Calendar.getInstance()); - GregorianCalendar XMPModify = new GregorianCalendar(2009, 10, 15); - xmp.setModifyDate(XMPModify); - - // Launching synchronization test - try { - ve = sync.validateMetadataSynchronization(doc, metadata); - // Test unsychronized value - Assert.assertEquals(8, ve.size()); - } catch (ValidationException e) { - throw new Exception(e.getMessage()); - } - - } - - /** - * Check the detection of unsynchronized information between Document - * Information dictionary and XMP - * - * @throws Exception - */ - @Test - public void testAllInfoUnsynchronized() throws Exception { - // building temporary XMP metadata - - DublinCoreSchema dc = metadata.createAndAddDublinCoreSchema(); - AdobePDFSchema pdf = metadata.createAndAddAdobePDFSchema(); - XMPBasicSchema xmp = metadata.createAndAddXMPBasicSchema(); - - // Writing info in XMP and Document Information dictionary - // TITLE - dico.setTitle("dicoTitle"); - dc.setTitle("x-default", "XMPTitle"); - // AUTHOR - dico.setAuthor("dicoAuthor"); - dc.addCreator("XMPAuthor"); - // SUBJECT - dico.setSubject("dicoSubj"); - dc.addSubject("XMPSubj"); - // KEYWORDS - dico.setKeywords("DicoKeywords"); - pdf.setKeywords("XMPkeywords"); - // CREATOR - dico.setCreator("DicoCreator"); - xmp.setCreatorTool("XMPCreator"); - // PRODUCER - dico.setProducer("DicoProducer"); - pdf.setProducer("XMPProducer"); - // CREATION DATE - dico.setCreationDate(Calendar.getInstance()); - GregorianCalendar XMPCreate = new GregorianCalendar(2008, 11, 05); - xmp.setCreateDate(XMPCreate); - // MODIFY DATE - dico.setModificationDate(Calendar.getInstance()); - GregorianCalendar XMPModify = new GregorianCalendar(2009, 10, 15); - xmp.setModifyDate(XMPModify); - - // Launching synchronization test - try { - ve = sync.validateMetadataSynchronization(doc, metadata); - // Test unsychronized value - Assert.assertEquals(8, ve.size()); - } catch (ValidationException e) { - throw new Exception(e.getMessage()); - } - - } - - /** - * Check reaction when metadata are well-formed - * - * @throws Exception - */ - @Test - public void testAllInfoSynhcronized() throws Exception { - title = "TITLE"; - author = "AUTHOR(S)"; - subject = "SUBJECTS"; - keywords = "KEYWORD(S)"; - creator = "CREATOR"; - producer = "PRODUCER"; - creationDate = Calendar.getInstance(); - modifyDate = Calendar.getInstance(); - - // building temporary XMP metadata - DublinCoreSchema dc = metadata.createAndAddDublinCoreSchema(); - XMPBasicSchema xmp = metadata.createAndAddXMPBasicSchema(); - AdobePDFSchema pdf = metadata.createAndAddAdobePDFSchema(); - // Writing info in XMP and Document Information dictionary - // TITLE - dico.setTitle(title); - dc.setTitle("x-default", title); - // AUTHOR - dico.setAuthor(author); - dc.addCreator(author); - // SUBJECT - dico.setSubject(subject); - dc.addDescription("x-default", subject); - // KEYWORDS - dico.setKeywords(keywords); - pdf.setKeywords(keywords); - // CREATOR - dico.setCreator(creator); - xmp.setCreatorTool(creator); - // PRODUCER - dico.setProducer(producer); - pdf.setProducer(producer); - // CREATION DATE - dico.setCreationDate(creationDate); - xmp.setCreateDate(creationDate); - // MODIFY DATE - dico.setModificationDate(modifyDate); - xmp.setModifyDate(modifyDate); - - // Launching synchronization test - try { - ve = sync.validateMetadataSynchronization(doc, metadata); - // Checking all is synchronized - Assert.assertEquals(0, ve.size()); - } catch (ValidationException e) { - throw new Exception(e.getMessage()); - } - - } - - /** - * Check if FormatAccessException Generator is ok - * - * @throws Exception - */ - @Test - public void checkformatAccessException() throws Exception { - Throwable cause = new Throwable(); - Assert.assertSame(cause, sync.formatAccessException("test", "test", cause) - .getCause()); - } - - /** - * Check if SchemaAccessException Generator is ok - * - * @throws Exception - */ - @Test - public void checkSchemaAccessException() throws Exception { - Throwable cause = new Throwable(); - Assert.assertSame(cause, sync.SchemaAccessException("test", cause) - .getCause()); - } - - /** - * Check reaction when metadata are well-formed - * - * @throws Exception - */ - @Test - public void testBadPrefixSchemas() throws Exception { - title = "TITLE"; - author = "AUTHOR(S)"; - subject = "SUBJECTS"; - keywords = "KEYWORD(S)"; - creator = "CREATOR"; - producer = "PRODUCER"; - creationDate = Calendar.getInstance(); - modifyDate = Calendar.getInstance(); - - // building temporary XMP metadata - DublinCoreSchema dc = new DublinCoreSchema(metadata, "dctest"); - metadata.addSchema(dc); - XMPBasicSchema xmp = new XMPBasicSchema(metadata, "xmptest"); - metadata.addSchema(xmp); - AdobePDFSchema pdf = new AdobePDFSchema(metadata, "pdftest"); - metadata.addSchema(pdf); - - // Writing info in XMP and Document Information dictionary - // TITLE - dico.setTitle(title); - dc.setTitle("x-default", title); - // AUTHOR - dico.setAuthor(author); - dc.addCreator(author); - // SUBJECT - dico.setSubject(subject); - dc.addDescription("x-default", subject); - // KEYWORDS - dico.setKeywords(keywords); - pdf.setKeywords(keywords); - // CREATOR - dico.setCreator(creator); - xmp.setCreatorTool(creator); - // PRODUCER - dico.setProducer(producer); - pdf.setProducer(producer); - // CREATION DATE - dico.setCreationDate(creationDate); - xmp.setCreateDate(creationDate); - // MODIFY DATE - dico.setModificationDate(modifyDate); - xmp.setModifyDate(modifyDate); - - // Launching synchronization test - try { - ve = sync.validateMetadataSynchronization(doc, metadata); - for (ValidationError valid : ve) { - Assert.assertEquals(PreflightConstants.ERROR_METADATA_WRONG_NS_PREFIX, - valid.getErrorCode()); - } - } catch (ValidationException e) { - throw new Exception(e.getMessage()); - } - - } - - /** - * Check reaction when metadata are well-formed - * - * @throws Exception - */ - @Test - public void testdoublePrefixSchemas() throws Exception { - title = "TITLE"; - author = "AUTHOR(S)"; - subject = "SUBJECTS"; - keywords = "KEYWORD(S)"; - creator = "CREATOR"; - producer = "PRODUCER"; - creationDate = Calendar.getInstance(); - modifyDate = Calendar.getInstance(); - - // building temporary XMP metadata - DublinCoreSchema dc = metadata.createAndAddDublinCoreSchema(); - DublinCoreSchema dc2 = new DublinCoreSchema(metadata, "dctest"); - metadata.addSchema(dc2); - XMPBasicSchema xmp = metadata.createAndAddXMPBasicSchema(); - XMPBasicSchema xmp2 = new XMPBasicSchema(metadata, "xmptest"); - metadata.addSchema(xmp2); - AdobePDFSchema pdf = metadata.createAndAddAdobePDFSchema(); - AdobePDFSchema pdf2 = new AdobePDFSchema(metadata, "pdftest"); - metadata.addSchema(pdf2); - - // write some temp info in 'false' schemas - dc2.setCoverage("tmpcover"); - xmp2.setCreatorTool("tmpcreator"); - pdf2.setKeywords("tmpkeys"); - - // Writing info in XMP and Document Information dictionary - // TITLE - dico.setTitle(title); - dc.setTitle("x-default", title); - // AUTHOR - dico.setAuthor(author); - dc.addCreator(author); - // SUBJECT - dico.setSubject(subject); - dc.addDescription("x-default", subject); - // KEYWORDS - dico.setKeywords(keywords); - pdf.setKeywords(keywords); - // CREATOR - dico.setCreator(creator); - xmp.setCreatorTool(creator); - // PRODUCER - dico.setProducer(producer); - pdf.setProducer(producer); - // CREATION DATE - dico.setCreationDate(creationDate); - xmp.setCreateDate(creationDate); - // MODIFY DATE - dico.setModificationDate(modifyDate); - xmp.setModifyDate(modifyDate); - - // Launching synchronization test - try { - ve = sync.validateMetadataSynchronization(doc, metadata); - Assert.assertTrue(ve.isEmpty()); - } catch (ValidationException e) { - throw new Exception(e.getMessage()); - } - - } - - @After - public void checkErrors() throws Exception { - try { - doc.close(); - } catch (IOException e) { - throw new Exception("Error while closing PDF Document"); - } - /* - * Iterator<ValidationError> it=ve.iterator(); while(it.hasNext()){ - * ValidationError tmp=it.next(); System.out.println("Error:"+ - * tmp.getDetails()+"\n code: "+tmp.getErrorCode()); } + /** + * Check the detection of a PDF document without any information + * + * @throws Exception */ - } + @Test + public void TestDocumentWithoutInformation() throws Exception + { + try + { + ve = sync.validateMetadataSynchronization(doc, metadata); + // Test without any information + Assert.assertEquals(0, ve.size()); + } + catch (ValidationException e) + { + throw new Exception(e.getMessage()); + } + } + + /** + * Check the detection of a completely empty XMP document (without any schemas) + * + * @throws Exception + */ + @Test + public void testEmptyXMP() throws Exception + { + title = "TITLE"; + author = "AUTHOR(S)"; + subject = "SUBJECTS"; + keywords = "KEYWORD(S)"; + creator = "CREATOR"; + producer = "PRODUCER"; + creationDate = Calendar.getInstance(); + modifyDate = Calendar.getInstance(); + + // Writing info in Document Information dictionary + // TITLE + dico.setTitle(title); + // AUTHOR + dico.setAuthor(author); + // SUBJECT + dico.setSubject(subject); + // KEYWORDS + dico.setKeywords(keywords); + // CREATOR + dico.setCreator(creator); + // PRODUCER + dico.setProducer(producer); + // CREATION DATE + dico.setCreationDate(creationDate); + // MODIFY DATE + dico.setModificationDate(modifyDate); + + try + { + ve = sync.validateMetadataSynchronization(doc, metadata); + // Test Detection of an Empty XMP (without any schemas) + for (ValidationError valid : ve) + { + Assert.assertEquals(PreflightConstants.ERROR_METADATA_MISMATCH, valid.getErrorCode()); + } + } + catch (ValidationException e) + { + throw new Exception(e.getMessage()); + } + } + + /** + * Check the detection of a XMP with empty common schemas + * + * @throws Exception + */ + @Test + public void testEmptyXMPSchemas() throws Exception + { + title = "TITLE"; + author = "AUTHOR(S)"; + subject = "SUBJECTS"; + keywords = "KEYWORD(S)"; + creator = "CREATOR"; + producer = "PRODUCER"; + creationDate = Calendar.getInstance(); + modifyDate = Calendar.getInstance(); + + // building temporary XMP metadata (but empty) + metadata.createAndAddDublinCoreSchema(); + metadata.createAndAddAdobePDFSchema(); + metadata.createAndAddXMPBasicSchema(); + + // Writing info in Document Information dictionary + // TITLE + dico.setTitle(title); + // AUTHOR + dico.setAuthor(author); + // SUBJECT + dico.setSubject(subject); + // KEYWORDS + dico.setKeywords(keywords); + // CREATOR + dico.setCreator(creator); + // PRODUCER + dico.setProducer(producer); + // CREATION DATE + dico.setCreationDate(creationDate); + // MODIFY DATE + dico.setModificationDate(modifyDate); + + try + { + ve = sync.validateMetadataSynchronization(doc, metadata); + // Test Detection of absent XMP values + Assert.assertEquals(8, ve.size()); + } + catch (ValidationException e) + { + throw new Exception(e.getMessage()); + } + + } + + /** + * Check detection of a null value in array (for Subject and author properties) + * + * @throws Exception + */ + @Test(expected = IllegalArgumentException.class) + public void testNullArrayValue() throws Exception + { + // building temporary XMP metadata + + DublinCoreSchema dc = metadata.createAndAddDublinCoreSchema(); + + // AUTHOR + dico.setAuthor("dicoAuthor"); + dc.addCreator(null); + + // SUBJECT + dico.setSubject("dicoSubj"); + dc.addSubject(null); + + // Launching synchronization test + try + { + ve = sync.validateMetadataSynchronization(doc, metadata); + // Test unsychronized value + Assert.assertEquals(2, ve.size()); + + } + catch (ValidationException e) + { + throw new Exception(e.getMessage()); + } + } + + /** + * in XMP, Subject and Author must be embedded in a single entry text array This function check the detection of + * multiple entries for these properties + * + * @throws Exception + */ + @Test + public void testBadSizeOfArrays() throws Exception + { + // building temporary XMP metadata + + DublinCoreSchema dc = metadata.createAndAddDublinCoreSchema(); + AdobePDFSchema pdf = metadata.createAndAddAdobePDFSchema(); + XMPBasicSchema xmp = metadata.createAndAddXMPBasicSchema(); + + // Writing info in XMP and Document Information dictionary + // TITLE + dico.setTitle("dicoTitle"); + dc.setTitle("x-default", "XMPTitle"); + // AUTHOR + dico.setAuthor("dicoAuthor"); + dc.addCreator("XMPAuthor"); + dc.addCreator("2ndCreator"); + // SUBJECT + dico.setSubject("dicoSubj"); + dc.addSubject("XMPSubj"); + dc.addSubject("2ndSubj"); + // KEYWORDS + dico.setKeywords("DicoKeywords"); + pdf.setKeywords("XMPkeywords"); + // CREATOR + dico.setCreator("DicoCreator"); + xmp.setCreatorTool("XMPCreator"); + // PRODUCER + dico.setProducer("DicoProducer"); + pdf.setProducer("XMPProducer"); + // CREATION DATE + dico.setCreationDate(Calendar.getInstance()); + GregorianCalendar XMPCreate = new GregorianCalendar(2008, 11, 05); + xmp.setCreateDate(XMPCreate); + // MODIFY DATE + dico.setModificationDate(Calendar.getInstance()); + GregorianCalendar XMPModify = new GregorianCalendar(2009, 10, 15); + xmp.setModifyDate(XMPModify); + + // Launching synchronization test + try + { + ve = sync.validateMetadataSynchronization(doc, metadata); + // Test unsychronized value + Assert.assertEquals(8, ve.size()); + } + catch (ValidationException e) + { + throw new Exception(e.getMessage()); + } + + } + + /** + * Check the detection of unsynchronized information between Document Information dictionary and XMP + * + * @throws Exception + */ + @Test + public void testAllInfoUnsynchronized() throws Exception + { + // building temporary XMP metadata + + DublinCoreSchema dc = metadata.createAndAddDublinCoreSchema(); + AdobePDFSchema pdf = metadata.createAndAddAdobePDFSchema(); + XMPBasicSchema xmp = metadata.createAndAddXMPBasicSchema(); + + // Writing info in XMP and Document Information dictionary + // TITLE + dico.setTitle("dicoTitle"); + dc.setTitle("x-default", "XMPTitle"); + // AUTHOR + dico.setAuthor("dicoAuthor"); + dc.addCreator("XMPAuthor"); + // SUBJECT + dico.setSubject("dicoSubj"); + dc.addSubject("XMPSubj"); + // KEYWORDS + dico.setKeywords("DicoKeywords"); + pdf.setKeywords("XMPkeywords"); + // CREATOR + dico.setCreator("DicoCreator"); + xmp.setCreatorTool("XMPCreator"); + // PRODUCER + dico.setProducer("DicoProducer"); + pdf.setProducer("XMPProducer"); + // CREATION DATE + dico.setCreationDate(Calendar.getInstance()); + GregorianCalendar XMPCreate = new GregorianCalendar(2008, 11, 05); + xmp.setCreateDate(XMPCreate); + // MODIFY DATE + dico.setModificationDate(Calendar.getInstance()); + GregorianCalendar XMPModify = new GregorianCalendar(2009, 10, 15); + xmp.setModifyDate(XMPModify); + + // Launching synchronization test + try + { + ve = sync.validateMetadataSynchronization(doc, metadata); + // Test unsychronized value + Assert.assertEquals(8, ve.size()); + } + catch (ValidationException e) + { + throw new Exception(e.getMessage()); + } + + } + + /** + * Check reaction when metadata are well-formed + * + * @throws Exception + */ + @Test + public void testAllInfoSynhcronized() throws Exception + { + title = "TITLE"; + author = "AUTHOR(S)"; + subject = "SUBJECTS"; + keywords = "KEYWORD(S)"; + creator = "CREATOR"; + producer = "PRODUCER"; + creationDate = Calendar.getInstance(); + modifyDate = Calendar.getInstance(); + + // building temporary XMP metadata + DublinCoreSchema dc = metadata.createAndAddDublinCoreSchema(); + XMPBasicSchema xmp = metadata.createAndAddXMPBasicSchema(); + AdobePDFSchema pdf = metadata.createAndAddAdobePDFSchema(); + // Writing info in XMP and Document Information dictionary + // TITLE + dico.setTitle(title); + dc.setTitle("x-default", title); + // AUTHOR + dico.setAuthor(author); + dc.addCreator(author); + // SUBJECT + dico.setSubject(subject); + dc.addDescription("x-default", subject); + // KEYWORDS + dico.setKeywords(keywords); + pdf.setKeywords(keywords); + // CREATOR + dico.setCreator(creator); + xmp.setCreatorTool(creator); + // PRODUCER + dico.setProducer(producer); + pdf.setProducer(producer); + // CREATION DATE + dico.setCreationDate(creationDate); + xmp.setCreateDate(creationDate); + // MODIFY DATE + dico.setModificationDate(modifyDate); + xmp.setModifyDate(modifyDate); + + // Launching synchronization test + try + { + ve = sync.validateMetadataSynchronization(doc, metadata); + // Checking all is synchronized + Assert.assertEquals(0, ve.size()); + } + catch (ValidationException e) + { + throw new Exception(e.getMessage()); + } + + } + + /** + * Check if FormatAccessException Generator is ok + * + * @throws Exception + */ + @Test + public void checkformatAccessException() throws Exception + { + Throwable cause = new Throwable(); + Assert.assertSame(cause, sync.formatAccessException("test", "test", cause).getCause()); + } + + /** + * Check if SchemaAccessException Generator is ok + * + * @throws Exception + */ + @Test + public void checkSchemaAccessException() throws Exception + { + Throwable cause = new Throwable(); + Assert.assertSame(cause, sync.SchemaAccessException("test", cause).getCause()); + } + + /** + * Check reaction when metadata are well-formed + * + * @throws Exception + */ + @Test + public void testBadPrefixSchemas() throws Exception + { + title = "TITLE"; + author = "AUTHOR(S)"; + subject = "SUBJECTS"; + keywords = "KEYWORD(S)"; + creator = "CREATOR"; + producer = "PRODUCER"; + creationDate = Calendar.getInstance(); + modifyDate = Calendar.getInstance(); + + // building temporary XMP metadata + DublinCoreSchema dc = new DublinCoreSchema(metadata, "dctest"); + metadata.addSchema(dc); + XMPBasicSchema xmp = new XMPBasicSchema(metadata, "xmptest"); + metadata.addSchema(xmp); + AdobePDFSchema pdf = new AdobePDFSchema(metadata, "pdftest"); + metadata.addSchema(pdf); + + // Writing info in XMP and Document Information dictionary + // TITLE + dico.setTitle(title); + dc.setTitle("x-default", title); + // AUTHOR + dico.setAuthor(author); + dc.addCreator(author); + // SUBJECT + dico.setSubject(subject); + dc.addDescription("x-default", subject); + // KEYWORDS + dico.setKeywords(keywords); + pdf.setKeywords(keywords); + // CREATOR + dico.setCreator(creator); + xmp.setCreatorTool(creator); + // PRODUCER + dico.setProducer(producer); + pdf.setProducer(producer); + // CREATION DATE + dico.setCreationDate(creationDate); + xmp.setCreateDate(creationDate); + // MODIFY DATE + dico.setModificationDate(modifyDate); + xmp.setModifyDate(modifyDate); + + // Launching synchronization test + try + { + ve = sync.validateMetadataSynchronization(doc, metadata); + for (ValidationError valid : ve) + { + Assert.assertEquals(PreflightConstants.ERROR_METADATA_WRONG_NS_PREFIX, valid.getErrorCode()); + } + } + catch (ValidationException e) + { + throw new Exception(e.getMessage()); + } + + } + + /** + * Check reaction when metadata are well-formed + * + * @throws Exception + */ + @Test + public void testdoublePrefixSchemas() throws Exception + { + title = "TITLE"; + author = "AUTHOR(S)"; + subject = "SUBJECTS"; + keywords = "KEYWORD(S)"; + creator = "CREATOR"; + producer = "PRODUCER"; + creationDate = Calendar.getInstance(); + modifyDate = Calendar.getInstance(); + + // building temporary XMP metadata + DublinCoreSchema dc = metadata.createAndAddDublinCoreSchema(); + DublinCoreSchema dc2 = new DublinCoreSchema(metadata, "dctest"); + metadata.addSchema(dc2); + XMPBasicSchema xmp = metadata.createAndAddXMPBasicSchema(); + XMPBasicSchema xmp2 = new XMPBasicSchema(metadata, "xmptest"); + metadata.addSchema(xmp2); + AdobePDFSchema pdf = metadata.createAndAddAdobePDFSchema(); + AdobePDFSchema pdf2 = new AdobePDFSchema(metadata, "pdftest"); + metadata.addSchema(pdf2); + + // write some temp info in 'false' schemas + dc2.setCoverage("tmpcover"); + xmp2.setCreatorTool("tmpcreator"); + pdf2.setKeywords("tmpkeys"); + + // Writing info in XMP and Document Information dictionary + // TITLE + dico.setTitle(title); + dc.setTitle("x-default", title); + // AUTHOR + dico.setAuthor(author); + dc.addCreator(author); + // SUBJECT + dico.setSubject(subject); + dc.addDescription("x-default", subject); + // KEYWORDS + dico.setKeywords(keywords); + pdf.setKeywords(keywords); + // CREATOR + dico.setCreator(creator); + xmp.setCreatorTool(creator); + // PRODUCER + dico.setProducer(producer); + pdf.setProducer(producer); + // CREATION DATE + dico.setCreationDate(creationDate); + xmp.setCreateDate(creationDate); + // MODIFY DATE + dico.setModificationDate(modifyDate); + xmp.setModifyDate(modifyDate); + + // Launching synchronization test + try + { + ve = sync.validateMetadataSynchronization(doc, metadata); + Assert.assertTrue(ve.isEmpty()); + } + catch (ValidationException e) + { + throw new Exception(e.getMessage()); + } + + } + + @After + public void checkErrors() throws Exception + { + try + { + doc.close(); + } + catch (IOException e) + { + throw new Exception("Error while closing PDF Document"); + } + /* + * Iterator<ValidationError> it=ve.iterator(); while(it.hasNext()){ ValidationError tmp=it.next(); + * System.out.println("Error:"+ tmp.getDetails()+"\n code: "+tmp.getErrorCode()); } + */ + } }
Modified: pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/parser/TestPreflightConfiguration.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/parser/TestPreflightConfiguration.java?rev=1453416&r1=1453415&r2=1453416&view=diff ============================================================================== --- pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/parser/TestPreflightConfiguration.java (original) +++ pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/parser/TestPreflightConfiguration.java Wed Mar 6 16:46:35 2013 @@ -30,40 +30,48 @@ import org.apache.pdfbox.preflight.excep import org.apache.pdfbox.preflight.process.ValidationProcess; import org.junit.Test; -public class TestPreflightConfiguration { +public class TestPreflightConfiguration +{ - @Test(expected=MissingValidationProcessException.class) - public void testGetValidationProcess_MissingProcess() throws Exception { - PreflightConfiguration configuration = PreflightConfiguration.createPdfA1BConfiguration(); - configuration.getInstanceOfProcess("unknownProcess"); - } - - @Test - public void testGetValidationProcess_MissingProcess_NoError() throws Exception { - PreflightConfiguration configuration = PreflightConfiguration.createPdfA1BConfiguration(); - configuration.setErrorOnMissingProcess(false); - configuration.getInstanceOfProcess("unknownProcess"); - } - - @Test - public void testReplaceValidationProcess() throws Exception { - PreflightConfiguration configuration = PreflightConfiguration.createPdfA1BConfiguration(); - - String processName = "mock-process"; - configuration.replaceProcess(processName, MockProcess.class); - assertEquals(MockProcess.class, configuration.getInstanceOfProcess(processName).getClass()); - - configuration.replaceProcess(processName, MockProcess2.class); - assertEquals(MockProcess2.class, configuration.getInstanceOfProcess(processName).getClass()); - } - - public static class MockProcess implements ValidationProcess { - public void validate(PreflightContext ctx) throws ValidationException { - } - } - - public static class MockProcess2 extends MockProcess { - public void validate(PreflightContext ctx) throws ValidationException { - } - } + @Test(expected = MissingValidationProcessException.class) + public void testGetValidationProcess_MissingProcess() throws Exception + { + PreflightConfiguration configuration = PreflightConfiguration.createPdfA1BConfiguration(); + configuration.getInstanceOfProcess("unknownProcess"); + } + + @Test + public void testGetValidationProcess_MissingProcess_NoError() throws Exception + { + PreflightConfiguration configuration = PreflightConfiguration.createPdfA1BConfiguration(); + configuration.setErrorOnMissingProcess(false); + configuration.getInstanceOfProcess("unknownProcess"); + } + + @Test + public void testReplaceValidationProcess() throws Exception + { + PreflightConfiguration configuration = PreflightConfiguration.createPdfA1BConfiguration(); + + String processName = "mock-process"; + configuration.replaceProcess(processName, MockProcess.class); + assertEquals(MockProcess.class, configuration.getInstanceOfProcess(processName).getClass()); + + configuration.replaceProcess(processName, MockProcess2.class); + assertEquals(MockProcess2.class, configuration.getInstanceOfProcess(processName).getClass()); + } + + public static class MockProcess implements ValidationProcess + { + public void validate(PreflightContext ctx) throws ValidationException + { + } + } + + public static class MockProcess2 extends MockProcess + { + public void validate(PreflightContext ctx) throws ValidationException + { + } + } } Modified: pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/util/ByteArrayDataSource.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/util/ByteArrayDataSource.java?rev=1453416&r1=1453415&r2=1453416&view=diff ============================================================================== --- pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/util/ByteArrayDataSource.java (original) +++ pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/util/ByteArrayDataSource.java Wed Mar 6 16:46:35 2013 @@ -31,47 +31,55 @@ import javax.activation.DataSource; import org.apache.commons.io.IOUtils; -public class ByteArrayDataSource implements DataSource { - private ByteArrayOutputStream data; - private String type = null; - private String name = null; - - public ByteArrayDataSource(InputStream is) throws IOException { - data = new ByteArrayOutputStream(); - IOUtils.copyLarge(is, data); - IOUtils.closeQuietly(is); - } - - public String getContentType() { - return this.type; - } - - /** - * @param type - * the type to set - */ - public void setType(String type) { - this.type = type; - } - - /** - * @param name - * the name to set - */ - public void setName(String name) { - this.name = name; - } - - public InputStream getInputStream() throws IOException { - return new ByteArrayInputStream(data.toByteArray()); - } - - public String getName() { - return this.name; - } - - public OutputStream getOutputStream() throws IOException { - this.data = new ByteArrayOutputStream(); - return data; - } +public class ByteArrayDataSource implements DataSource +{ + private ByteArrayOutputStream data; + private String type = null; + private String name = null; + + public ByteArrayDataSource(InputStream is) throws IOException + { + data = new ByteArrayOutputStream(); + IOUtils.copyLarge(is, data); + IOUtils.closeQuietly(is); + } + + public String getContentType() + { + return this.type; + } + + /** + * @param type + * the type to set + */ + public void setType(String type) + { + this.type = type; + } + + /** + * @param name + * the name to set + */ + public void setName(String name) + { + this.name = name; + } + + public InputStream getInputStream() throws IOException + { + return new ByteArrayInputStream(data.toByteArray()); + } + + public String getName() + { + return this.name; + } + + public OutputStream getOutputStream() throws IOException + { + this.data = new ByteArrayOutputStream(); + return data; + } } Modified: pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/util/IsartorPdfProvider.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/util/IsartorPdfProvider.java?rev=1453416&r1=1453415&r2=1453416&view=diff ============================================================================== --- pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/util/IsartorPdfProvider.java (original) +++ pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/util/IsartorPdfProvider.java Wed Mar 6 16:46:35 2013 @@ -23,37 +23,39 @@ package org.apache.pdfbox.preflight.util import java.io.InputStream; -public class IsartorPdfProvider { -// public static File path; -// static { -// String ip = System.getProperty("isartor.path", null); -// if (ip != null) { -// path = new File(ip); -// if (!path.exists() || !path.isDirectory()) { -// path = null; -// } -// } -// } +public class IsartorPdfProvider +{ + // public static File path; + // static { + // String ip = System.getProperty("isartor.path", null); + // if (ip != null) { + // path = new File(ip); + // if (!path.exists() || !path.isDirectory()) { + // path = null; + // } + // } + // } - public static InputStream getIsartorDocument(String name) { - return IsartorPdfProvider.class.getResourceAsStream(name); -// -// if (path == null) { -// return null; -// } -// -// String[] ext = { "pdf" }; -// Iterator<?> iter = FileUtils.iterateFiles(path, ext, true); -// while (iter.hasNext()) { -// Object o = iter.next(); -// if (o instanceof File) { -// File isartorFile = (File) o; -// if (isartorFile.isFile() && name.equals(isartorFile.getName())) { -// return isartorFile; -// } -// } -// } -// return null; -// } - } + public static InputStream getIsartorDocument(String name) + { + return IsartorPdfProvider.class.getResourceAsStream(name); + // + // if (path == null) { + // return null; + // } + // + // String[] ext = { "pdf" }; + // Iterator<?> iter = FileUtils.iterateFiles(path, ext, true); + // while (iter.hasNext()) { + // Object o = iter.next(); + // if (o instanceof File) { + // File isartorFile = (File) o; + // if (isartorFile.isFile() && name.equals(isartorFile.getName())) { + // return isartorFile; + // } + // } + // } + // return null; + // } + } } Modified: pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/util/NOCatalogDocument.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/util/NOCatalogDocument.java?rev=1453416&r1=1453415&r2=1453416&view=diff ============================================================================== --- pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/util/NOCatalogDocument.java (original) +++ pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/util/NOCatalogDocument.java Wed Mar 6 16:46:35 2013 @@ -27,17 +27,21 @@ import org.apache.pdfbox.cos.COSDocument import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocumentCatalog; -public class NOCatalogDocument extends PDDocument { - public NOCatalogDocument() throws IOException { - super(); - } +public class NOCatalogDocument extends PDDocument +{ + public NOCatalogDocument() throws IOException + { + super(); + } - public NOCatalogDocument(COSDocument doc) { - super(doc); - } + public NOCatalogDocument(COSDocument doc) + { + super(doc); + } - @Override - public PDDocumentCatalog getDocumentCatalog() { - return null; - } + @Override + public PDDocumentCatalog getDocumentCatalog() + { + return null; + } } Modified: pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/utils/TestCOSUtils.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/utils/TestCOSUtils.java?rev=1453416&r1=1453415&r2=1453416&view=diff ============================================================================== --- pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/utils/TestCOSUtils.java (original) +++ pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/utils/TestCOSUtils.java Wed Mar 6 16:46:35 2013 @@ -43,168 +43,206 @@ import org.apache.pdfbox.persistence.uti import org.apache.pdfbox.preflight.utils.COSUtils; import org.junit.Test; -public class TestCOSUtils { +public class TestCOSUtils +{ - @Test - public void testIsInteger() { - try { - COSObject co = new COSObject(new COSInteger(10)); - co.setGenerationNumber(COSInteger.ZERO); - co.setObjectNumber(new COSInteger(10)); - - assertFalse(COSUtils.isInteger(co, new IOCOSDocument())); - - COSDocument doc = new COSDocument(); - addToXref(doc,new COSObjectKey(co), 1000); - COSUtils.isInteger(co, doc); - doc.close(); - } catch (IOException e) { - fail(e.getMessage()); - } - } - - @Test - public void testIsFloat() { - try { - COSObject co = new COSObject(new COSFloat(10.0f)); - co.setGenerationNumber(COSInteger.ZERO); - co.setObjectNumber(new COSInteger(10)); - - assertFalse(COSUtils.isFloat(co, new IOCOSDocument())); - - COSDocument doc = new COSDocument(); - addToXref(doc,new COSObjectKey(co), 1000); - COSUtils.isFloat(co, doc); - doc.close(); - } catch (IOException e) { - fail(e.getMessage()); - } - } - - @Test - public void testIsString() { - try { - COSObject co = new COSObject(new COSString("")); - co.setGenerationNumber(COSInteger.ZERO); - co.setObjectNumber(new COSInteger(10)); - - assertFalse(COSUtils.isString(co, new IOCOSDocument())); - - COSDocument doc = new COSDocument(); - addToXref(doc,new COSObjectKey(co), 1000); - COSUtils.isString(co, doc); - doc.close(); - } catch (IOException e) { - fail(e.getMessage()); - } - } - - @Test - public void testIsStream() { - try { - COSObject co = new COSObject(new COSStream(null)); - co.setGenerationNumber(COSInteger.ZERO); - co.setObjectNumber(new COSInteger(10)); - - assertFalse(COSUtils.isStream(co, new IOCOSDocument())); - - COSDocument doc = new COSDocument(); - addToXref(doc,new COSObjectKey(co), 1000); - COSUtils.isStream(co, doc); - doc.close(); - } catch (IOException e) { - fail(e.getMessage()); - } - } - - @Test - public void testIsDictionary() { - try { - COSObject co = new COSObject(new COSDictionary()); - co.setGenerationNumber(COSInteger.ZERO); - co.setObjectNumber(new COSInteger(10)); - - assertFalse(COSUtils.isDictionary(co, new IOCOSDocument())); - - COSDocument doc = new COSDocument(); - addToXref(doc,new COSObjectKey(co), 1000); - COSUtils.isDictionary(co, doc); - doc.close(); - } catch (IOException e) { - fail(e.getMessage()); - } - } - - @Test - public void testIsArray() { - try { - COSObject co = new COSObject(new COSArray()); - co.setGenerationNumber(COSInteger.ZERO); - co.setObjectNumber(new COSInteger(10)); - - assertFalse(COSUtils.isArray(co, new IOCOSDocument())); - - COSDocument doc = new COSDocument(); - addToXref(doc,new COSObjectKey(co), 1000); - COSUtils.isArray(co, doc); - doc.close(); - } catch (IOException e) { - fail(e.getMessage()); - } - } - - @Test - public void testCloseCOSDocumentNull() { - COSUtils.closeDocumentQuietly((COSDocument) null); - } - - @Test - public void testClosePDDocumentNull() { - COSUtils.closeDocumentQuietly((PDDocument) null); - } - - @Test - public void testCloseCOSDocumentIO() { - try { - COSUtils.closeDocumentQuietly(new IOCOSDocument()); - } catch (IOException e) { - fail(e.getMessage()); - } - } - - protected void addToXref( COSDocument doc, COSObjectKey key, long value) { - Map<COSObjectKey, Long> xrefTable = new HashMap<COSObjectKey, Long>(1); - xrefTable.put(key, value); - doc.addXRefTable(xrefTable); - } - - /** - * Class used to check the catch block in COSUtils methods - */ - private class IOCOSDocument extends COSDocument { - - IOCOSDocument() throws IOException { - super(); - } - - IOCOSDocument(File scratchDir) throws IOException { - super(scratchDir); - } - - IOCOSDocument(RandomAccess file) { - super(file); - } - - @Override - public void close() throws IOException { - super.close(); - throw new IOException("Exception for code coverage"); - } - - @Override - public COSObject getObjectFromPool(COSObjectKey key) throws IOException { - super.close(); - throw new IOException("Exception for code coverage"); + @Test + public void testIsInteger() + { + try + { + COSObject co = new COSObject(new COSInteger(10)); + co.setGenerationNumber(COSInteger.ZERO); + co.setObjectNumber(new COSInteger(10)); + + assertFalse(COSUtils.isInteger(co, new IOCOSDocument())); + + COSDocument doc = new COSDocument(); + addToXref(doc, new COSObjectKey(co), 1000); + COSUtils.isInteger(co, doc); + doc.close(); + } + catch (IOException e) + { + fail(e.getMessage()); + } + } + + @Test + public void testIsFloat() + { + try + { + COSObject co = new COSObject(new COSFloat(10.0f)); + co.setGenerationNumber(COSInteger.ZERO); + co.setObjectNumber(new COSInteger(10)); + + assertFalse(COSUtils.isFloat(co, new IOCOSDocument())); + + COSDocument doc = new COSDocument(); + addToXref(doc, new COSObjectKey(co), 1000); + COSUtils.isFloat(co, doc); + doc.close(); + } + catch (IOException e) + { + fail(e.getMessage()); + } + } + + @Test + public void testIsString() + { + try + { + COSObject co = new COSObject(new COSString("")); + co.setGenerationNumber(COSInteger.ZERO); + co.setObjectNumber(new COSInteger(10)); + + assertFalse(COSUtils.isString(co, new IOCOSDocument())); + + COSDocument doc = new COSDocument(); + addToXref(doc, new COSObjectKey(co), 1000); + COSUtils.isString(co, doc); + doc.close(); + } + catch (IOException e) + { + fail(e.getMessage()); + } + } + + @Test + public void testIsStream() + { + try + { + COSObject co = new COSObject(new COSStream(null)); + co.setGenerationNumber(COSInteger.ZERO); + co.setObjectNumber(new COSInteger(10)); + + assertFalse(COSUtils.isStream(co, new IOCOSDocument())); + + COSDocument doc = new COSDocument(); + addToXref(doc, new COSObjectKey(co), 1000); + COSUtils.isStream(co, doc); + doc.close(); + } + catch (IOException e) + { + fail(e.getMessage()); + } + } + + @Test + public void testIsDictionary() + { + try + { + COSObject co = new COSObject(new COSDictionary()); + co.setGenerationNumber(COSInteger.ZERO); + co.setObjectNumber(new COSInteger(10)); + + assertFalse(COSUtils.isDictionary(co, new IOCOSDocument())); + + COSDocument doc = new COSDocument(); + addToXref(doc, new COSObjectKey(co), 1000); + COSUtils.isDictionary(co, doc); + doc.close(); + } + catch (IOException e) + { + fail(e.getMessage()); + } + } + + @Test + public void testIsArray() + { + try + { + COSObject co = new COSObject(new COSArray()); + co.setGenerationNumber(COSInteger.ZERO); + co.setObjectNumber(new COSInteger(10)); + + assertFalse(COSUtils.isArray(co, new IOCOSDocument())); + + COSDocument doc = new COSDocument(); + addToXref(doc, new COSObjectKey(co), 1000); + COSUtils.isArray(co, doc); + doc.close(); + } + catch (IOException e) + { + fail(e.getMessage()); + } + } + + @Test + public void testCloseCOSDocumentNull() + { + COSUtils.closeDocumentQuietly((COSDocument) null); + } + + @Test + public void testClosePDDocumentNull() + { + COSUtils.closeDocumentQuietly((PDDocument) null); + } + + @Test + public void testCloseCOSDocumentIO() + { + try + { + COSUtils.closeDocumentQuietly(new IOCOSDocument()); + } + catch (IOException e) + { + fail(e.getMessage()); + } + } + + protected void addToXref(COSDocument doc, COSObjectKey key, long value) + { + Map<COSObjectKey, Long> xrefTable = new HashMap<COSObjectKey, Long>(1); + xrefTable.put(key, value); + doc.addXRefTable(xrefTable); + } + + /** + * Class used to check the catch block in COSUtils methods + */ + private class IOCOSDocument extends COSDocument + { + + IOCOSDocument() throws IOException + { + super(); + } + + IOCOSDocument(File scratchDir) throws IOException + { + super(scratchDir); + } + + IOCOSDocument(RandomAccess file) + { + super(file); + } + + @Override + public void close() throws IOException + { + super.close(); + throw new IOException("Exception for code coverage"); + } + + @Override + public COSObject getObjectFromPool(COSObjectKey key) throws IOException + { + super.close(); + throw new IOException("Exception for code coverage"); + } } - } }
