package org.apache.pdfbox.pdmodel;
import java.io.IOException;
import java.util.List;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.interactive.annotation.AnnotationsFilter;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationRubberStamp;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationSquare;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* This unit test validates the correct working behavior of PDPage annotations
* filtering
*
* @author Maxime Veron
*
*/
public class TestPDPageAnnotationsFiltering
{
// test mock page for annotations filtering
private PDPage page;
@Before
public void initMock()
{
COSDictionary mockedPageWithAnnotations = new COSDictionary();
COSArray annotsDictionnary = new COSArray();
annotsDictionnary.add(new PDAnnotationRubberStamp().getCOSObject());
annotsDictionnary.add(new PDAnnotationSquare().getCOSObject());
annotsDictionnary.add(new PDAnnotationLink().getCOSObject());
mockedPageWithAnnotations.setItem(COSName.ANNOTS, annotsDictionnary);
page = new PDPage(mockedPageWithAnnotations);
}
@Test
public void validateNoFiltering() throws IOException
{
List annotations = page.getAnnotations();
Assert.assertEquals(3, annotations.size());
Assert.assertTrue(annotations.get(0) instanceof PDAnnotationRubberStamp);
Assert.assertTrue(annotations.get(1) instanceof PDAnnotationSquare);
Assert.assertTrue(annotations.get(2) instanceof PDAnnotationLink);
}
@Test
public void validateAllFiltered() throws IOException
{
List annotations = page.getAnnotations(new AnnotationsFilter()
{
@Override
public boolean accept(PDAnnotation annotation)
{
return false;
}
});
Assert.assertEquals(0, annotations.size());
}
@Test
public void validateSelectedFew() throws IOException
{
List annotations = page.getAnnotations(new AnnotationsFilter()
{
@Override
public boolean accept(PDAnnotation annotation)
{
return (annotation instanceof PDAnnotationLink || annotation instanceof PDAnnotationSquare);
}
});
Assert.assertEquals(2, annotations.size());
Assert.assertTrue(annotations.get(0) instanceof PDAnnotationSquare);
Assert.assertTrue(annotations.get(1) instanceof PDAnnotationLink);
}
}