Skip to content
Commits on Source (3)
libsejda-java (3.2.84-1) unstable; urgency=medium
* New upstream version 3.2.84.
-- Markus Koschany <apo@debian.org> Tue, 10 Sep 2019 13:46:12 +0200
libsejda-java (3.2.75-1) unstable; urgency=medium
* New upstream version 3.2.75.
......
......@@ -6,7 +6,7 @@
<artifactId>sejda-parent</artifactId>
<packaging>pom</packaging>
<name>sejda</name>
<version>3.2.75</version>
<version>3.2.84</version>
<description>An extendible and configurable PDF manipulation layer library. A ready to use java library to perform PDF documents manipulation without having to deal with the low level API. Sejda offers many "ready to go" manipulations implemented using the SAMBox but it can be extended to use other implementations.</description>
<organization>
......@@ -32,7 +32,7 @@
<connection>scm:git:git@github.com:torakiki/sejda.git</connection>
<developerConnection>scm:git:git@github.com:torakiki/sejda.git</developerConnection>
<url>scm:git:git@github.com:torakiki/sejda.git</url>
<tag>v3.2.75</tag>
<tag>v3.2.84</tag>
</scm>
<developers>
......@@ -338,7 +338,7 @@
<jdepend.version>2.9.1</jdepend.version>
<hibernate-validator.version>4.2.0.Final</hibernate-validator.version>
<hamcrest.version>1.3</hamcrest.version>
<sambox.version>1.1.53</sambox.version>
<sambox.version>1.1.55</sambox.version>
<bouncycastle.version>1.61</bouncycastle.version>
<twelvemonkeys.version>3.4.1</twelvemonkeys.version>
<apache.poi.version>3.15</apache.poi.version>
......
......@@ -10,7 +10,7 @@
<parent>
<groupId>org.sejda</groupId>
<artifactId>sejda-parent</artifactId>
<version>3.2.75</version>
<version>3.2.84</version>
<relativePath>../pom.xml</relativePath>
</parent>
......
......@@ -16,10 +16,17 @@
*/
package org.sejda.cli.transformer;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.sejda.cli.model.CombineReorderTaskCliArguments;
import org.sejda.conversion.MultiplePdfMergeInputAdapter;
import org.sejda.model.exception.SejdaRuntimeException;
import org.sejda.model.input.FileIndexAndPage;
import org.sejda.model.input.PdfFileSource;
import org.sejda.model.input.PdfMergeInput;
import org.sejda.model.parameter.CombineReorderParameters;
import org.sejda.model.rotation.Rotation;
......@@ -30,9 +37,14 @@ public class CombineReorderCliArgumentsTransformer extends BaseCliArgumentsTrans
public CombineReorderParameters toTaskParameters(CombineReorderTaskCliArguments taskCliArguments) {
CombineReorderParameters parameters = new CombineReorderParameters();
populateAbstractParameters(parameters, taskCliArguments);
populateSourceParameters(parameters, taskCliArguments);
populateOutputTaskParameters(parameters, taskCliArguments);
MultiplePdfMergeInputAdapter mergeInputsAdapter = extractPdfMergeInputs(taskCliArguments);
for (PdfMergeInput eachMergeInput : mergeInputsAdapter.getPdfMergeInputs()) {
parameters.addInput(eachMergeInput);
}
taskCliArguments.getPages().stream()
.map(in -> parseIndexAndPage(in))
.filter(item -> item.isPresent())
......@@ -42,6 +54,18 @@ public class CombineReorderCliArgumentsTransformer extends BaseCliArgumentsTrans
return parameters;
}
private MultiplePdfMergeInputAdapter extractPdfMergeInputs(CombineReorderTaskCliArguments taskCliArguments) {
List<PdfFileSource> inputFiles = taskCliArguments.getFiles().stream().flatMap(a -> a.getPdfFileSources().stream())
.collect(Collectors.toList());
if (inputFiles.isEmpty()) {
throw new SejdaRuntimeException("No input files specified");
}
MultiplePdfMergeInputAdapter mergeInputsAdapter = new MultiplePdfMergeInputAdapter(inputFiles, Collections.emptyList());
return mergeInputsAdapter;
}
private Optional<FileIndexAndPage> parseIndexAndPage(String in) {
String[] parts = in.split(":");
try {
......
......@@ -45,7 +45,7 @@ import org.sejda.model.input.PdfMergeInput;
import org.sejda.model.input.PdfSource;
import org.sejda.model.output.*;
import org.sejda.model.parameter.AlternateMixMultipleInputParameters;
import org.sejda.model.parameter.MergeParameters;
import org.sejda.model.parameter.BaseMergeParameters;
import org.sejda.model.parameter.base.MultiplePdfSourceTaskParameters;
import org.sejda.model.parameter.base.SinglePdfSourceTaskParameters;
import org.sejda.model.parameter.base.TaskParameters;
......@@ -113,10 +113,8 @@ public abstract class AbstractTestSuite {
}
protected void assertHasFileSource(TaskParameters parameters, File file, String password) {
if (parameters instanceof AlternateMixMultipleInputParameters) {
assertHasFileSource((AlternateMixMultipleInputParameters) parameters, file, password);
} else if (parameters instanceof MergeParameters) {
assertHasFileSource((MergeParameters) parameters, file, password);
if (parameters instanceof BaseMergeParameters) {
assertHasFileSource((BaseMergeParameters) parameters, file, password);
} else if (parameters instanceof SinglePdfSourceTaskParameters) {
assertHasFileSource((SinglePdfSourceTaskParameters) parameters, file, password);
} else if (parameters instanceof MultiplePdfSourceTaskParameters) {
......@@ -132,7 +130,7 @@ public abstract class AbstractTestSuite {
matchesPdfFileSource(file, password, parameters.getSource()));
}
protected void assertHasFileSource(MergeParameters parameters, File file, String password) {
protected void assertHasFileSource(BaseMergeParameters<?> parameters, File file, String password) {
List<PdfSource<?>> sourcesList = new ArrayList<PdfSource<?>>();
for (PdfMergeInput eachInput : parameters.getPdfInputList()) {
sourcesList.add(eachInput.getSource());
......
......@@ -27,6 +27,7 @@ import org.junit.Test;
import org.sejda.cli.command.StandardTestableTask;
import org.sejda.model.parameter.RotateParameters;
import org.sejda.model.pdf.page.PageRange;
import org.sejda.model.pdf.page.PredefinedSetOfPages;
import org.sejda.model.rotation.Rotation;
/**
......@@ -67,19 +68,19 @@ public class RotateTaskTest extends AbstractTaskTest {
@Test
public void predefinedPages_ALL_PAGES() {
RotateParameters parameters = defaultCommandLine().with("-m", "all").invokeSejdaConsole();
assertContainsAll(Arrays.asList(1, 2, 3, 4, 5), parameters.getPages(5));
assertEquals(PredefinedSetOfPages.ALL_PAGES, parameters.getPredefinedSetOfPages());
}
@Test
public void predefinedPages_ODD_PAGES() {
RotateParameters parameters = defaultCommandLine().with("-m", "odd").invokeSejdaConsole();
assertContainsAll(Arrays.asList(1, 3, 5), parameters.getPages(5));
assertEquals(PredefinedSetOfPages.ODD_PAGES, parameters.getPredefinedSetOfPages());
}
@Test
public void predefinedPages_EVEN_PAGES() {
RotateParameters parameters = defaultCommandLine().with("-m", "even").invokeSejdaConsole();
assertContainsAll(Arrays.asList(2, 4), parameters.getPages(5));
assertEquals(PredefinedSetOfPages.EVEN_PAGES, parameters.getPredefinedSetOfPages());
}
@Test
......@@ -87,7 +88,7 @@ public class RotateTaskTest extends AbstractTaskTest {
RotateParameters parameters = defaultCommandLine().with("-s", "3,5,8-10,2,2,9-9,30-")
.invokeSejdaConsole();
assertContainsAll(parameters.getPageSelection(), Arrays.asList(new PageRange(3, 3), new PageRange(5, 5),
assertContainsAll(parameters.getPageSelection().keySet(), Arrays.asList(new PageRange(3, 3), new PageRange(5, 5),
new PageRange(8, 10), new PageRange(2, 2), new PageRange(9, 9), new PageRange(30)));
}
......
......@@ -10,7 +10,7 @@
<parent>
<groupId>org.sejda</groupId>
<artifactId>sejda-parent</artifactId>
<version>3.2.75</version>
<version>3.2.84</version>
<relativePath>../pom.xml</relativePath>
</parent>
......
......@@ -5,7 +5,7 @@
<parent>
<groupId>org.sejda</groupId>
<artifactId>sejda-parent</artifactId>
<version>3.2.75</version>
<version>3.2.84</version>
<relativePath>../pom.xml</relativePath>
</parent>
......
......@@ -87,7 +87,9 @@ public final class IOUtils {
}
private static Path tmpFile(Path location) throws IOException {
return Files.createTempFile(location, "." + BUFFER_NAME, null);
// don't add leading dot on Windows
String prefix = (IS_OS_WINDOWS ? "" : ".") + BUFFER_NAME;
return Files.createTempFile(location, prefix, null);
}
/**
......
......@@ -88,66 +88,70 @@ public abstract class BaseTaskTest<T extends TaskParameters> implements Testable
IOUtils.closeQuietly(testContext);
}
public PdfStreamSource shortInput() {
public static PdfStreamSource shortInput() {
return PdfStreamSource.newInstanceNoPassword(
getClass().getClassLoader().getResourceAsStream("pdf/short-test-file.pdf"), "short-test-file.pdf");
BaseTaskTest.class.getClassLoader().getResourceAsStream("pdf/short-test-file.pdf"), "short-test-file.pdf");
}
public PdfStreamSource regularInput() {
public static PdfStreamSource regularInput() {
return PdfStreamSource.newInstanceNoPassword(
getClass().getClassLoader().getResourceAsStream("pdf/test-pdf.pdf"), "test-file.pdf");
BaseTaskTest.class.getClassLoader().getResourceAsStream("pdf/test-pdf.pdf"), "test-file.pdf");
}
public PdfStreamSource mediumInput() {
public static PdfStreamSource mediumInput() {
return PdfStreamSource.newInstanceNoPassword(
getClass().getClassLoader().getResourceAsStream("pdf/medium_test.pdf"), "medium-test-file.pdf");
BaseTaskTest.class.getClassLoader().getResourceAsStream("pdf/medium_test.pdf"), "medium-test-file.pdf");
}
public PdfStreamSource largeInput() {
public static PdfStreamSource largeInput() {
return PdfStreamSource.newInstanceNoPassword(
getClass().getClassLoader().getResourceAsStream("pdf/large_test.pdf"), "large-test-file.pdf");
BaseTaskTest.class.getClassLoader().getResourceAsStream("pdf/large_test.pdf"), "large-test-file.pdf");
}
public PdfStreamSource largeOutlineInput() {
public static PdfStreamSource largeOutlineInput() {
return PdfStreamSource.newInstanceNoPassword(
getClass().getClassLoader().getResourceAsStream("pdf/large_outline.pdf"),
BaseTaskTest.class.getClassLoader().getResourceAsStream("pdf/large_outline.pdf"),
"large-outline-test-file.pdf");
}
public PdfStreamSource encryptedInput() {
public static PdfStreamSource encryptedInput() {
return PdfStreamSource.newInstanceWithPassword(
getClass().getClassLoader().getResourceAsStream("pdf/encrypted_AES128_user_pwd.pdf"),
BaseTaskTest.class.getClassLoader().getResourceAsStream("pdf/encrypted_AES128_user_pwd.pdf"),
"encrypted-test-file.pdf", "test");
}
public PdfStreamSource formInput() {
public static PdfStreamSource formInput() {
return PdfStreamSource.newInstanceNoPassword(
getClass().getClassLoader().getResourceAsStream("pdf/forms/two_pages_form.pdf"), "test-form.pdf");
BaseTaskTest.class.getClassLoader().getResourceAsStream("pdf/forms/two_pages_form.pdf"), "test-form.pdf");
}
public PdfStreamSource stronglyEncryptedInput() {
public static PdfStreamSource stronglyEncryptedInput() {
return PdfStreamSource.newInstanceWithPassword(
getClass().getClassLoader().getResourceAsStream("pdf/encrypted_AES256_user_pwd.pdf"),
BaseTaskTest.class.getClassLoader().getResourceAsStream("pdf/encrypted_AES256_user_pwd.pdf"),
"strongly-encrypted-test-file.pdf", "test");
}
public PdfStreamSource customInput(String path) {
return PdfStreamSource.newInstanceNoPassword(getClass().getClassLoader().getResourceAsStream(path),
public static PdfStreamSource customInput(String path) {
return PdfStreamSource.newInstanceNoPassword(BaseTaskTest.class.getClassLoader().getResourceAsStream(path),
randomAlphanumeric(16) + ".pdf");
}
public PdfFileSource customInputAsFileSource(String path) {
public static PdfFileSource customInputAsFileSource(String path) {
String filename = new File(path).getName();
InputStream in = getClass().getClassLoader().getResourceAsStream(path);
return customInputAsFileSource(path, filename);
}
public static PdfFileSource customInputAsFileSource(String path, String filename) {
InputStream in = BaseTaskTest.class.getClassLoader().getResourceAsStream(path);
return PdfFileSource.newInstanceNoPassword(streamToTmpFile(in, filename));
}
public PdfStreamSource customInput(String path, String name) {
public static PdfStreamSource customInput(String path, String name) {
requireNotBlank(name, "Name cannot be blank");
return PdfStreamSource.newInstanceNoPassword(getClass().getClassLoader().getResourceAsStream(path), name);
return PdfStreamSource.newInstanceNoPassword(BaseTaskTest.class.getClassLoader().getResourceAsStream(path), name);
}
public PdfFileSource customInput(PDDocument doc, String name) {
public static PdfFileSource customInput(PDDocument doc, String name) {
try {
File tmp = org.sejda.core.support.io.IOUtils.createTemporaryBufferWithName(name);
doc.writeTo(tmp);
......@@ -157,28 +161,32 @@ public abstract class BaseTaskTest<T extends TaskParameters> implements Testable
}
}
public PdfStreamSource customEncryptedInput(String path, String password) {
return PdfStreamSource.newInstanceWithPassword(getClass().getClassLoader().getResourceAsStream(path),
public static PdfStreamSource customEncryptedInput(String path, String password) {
return PdfStreamSource.newInstanceWithPassword(BaseTaskTest.class.getClassLoader().getResourceAsStream(path),
randomAlphanumeric(16) + ".pdf", password);
}
public StreamSource customNonPdfInput(String path) {
public static StreamSource customNonPdfInput(String path) {
String extension = FilenameUtils.getExtension(path);
String filename = randomAlphanumeric(16) + "." + extension;
return customNonPdfInput(path, filename);
}
public StreamSource customNonPdfInput(String path, String filename) {
return StreamSource.newInstance(getClass().getClassLoader().getResourceAsStream(path), filename);
public static StreamSource customNonPdfInput(String path, String filename) {
return StreamSource.newInstance(BaseTaskTest.class.getClassLoader().getResourceAsStream(path), filename);
}
public FileSource customNonPdfInputAsFileSource(String path) {
public static FileSource customNonPdfInputAsFileSource(String path) {
String filename = new File(path).getName();
InputStream in = getClass().getClassLoader().getResourceAsStream(path);
return customNonPdfInputAsFileSource(path, filename);
}
public static FileSource customNonPdfInputAsFileSource(String path, String filename) {
InputStream in = BaseTaskTest.class.getClassLoader().getResourceAsStream(path);
return FileSource.newInstance(streamToTmpFile(in, filename));
}
private File streamToTmpFile(InputStream in, String filename) {
public static File streamToTmpFile(InputStream in, String filename) {
try {
File tmp = org.sejda.core.support.io.IOUtils.createTemporaryBufferWithName(filename);
OutputStream out = new BufferedOutputStream(new FileOutputStream(tmp));
......@@ -192,30 +200,30 @@ public abstract class BaseTaskTest<T extends TaskParameters> implements Testable
}
public void assertPageText(PDPage page, String text) {
public static void assertPageText(PDPage page, String text) {
org.sejda.core.service.TestUtils.assertPageText(page, text);
}
public void assertPageTextExactLines(PDPage page, String text) {
public static void assertPageTextExactLines(PDPage page, String text) {
org.sejda.core.service.TestUtils.assertPageTextExactLines(page, text);
}
public void assertPageTextContains(PDPage page, String text) {
public static void assertPageTextContains(PDPage page, String text) {
org.sejda.core.service.TestUtils.assertPageTextContains(page, text);
}
public void assertMediaBox(PDPage page, float width, float height) {
public static void assertMediaBox(PDPage page, float width, float height) {
assertEquals(page.getMediaBox().getWidth(), width, 0.01);
assertEquals(page.getMediaBox().getHeight(), height, 0.01);
}
public <T> List<T> getAnnotationsOf(PDPage page, Class<T> clazz) {
public static <T> List<T> getAnnotationsOf(PDPage page, Class<T> clazz) {
return org.sejda.core.service.TestUtils.getAnnotationsOf(page, clazz);
}
// returns 1-based page numbers
public List<Integer> getPagesContainingImages(PDDocument doc) {
public static List<Integer> getPagesContainingImages(PDDocument doc) {
List<Integer> result = new ArrayList<>();
for(int i = 0; i < doc.getNumberOfPages(); i++) {
PDPage page = doc.getPage(i);
......
......@@ -23,11 +23,15 @@ import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
import org.sejda.model.input.PdfSource;
import org.sejda.model.input.ImageMergeInput;
import org.sejda.model.input.MergeInput;
import org.sejda.model.input.PdfMergeInput;
import org.sejda.model.outline.OutlinePolicy;
import org.sejda.model.output.ExistingOutputPolicy;
import org.sejda.model.parameter.CombineReorderParameters;
......@@ -43,18 +47,24 @@ public abstract class CombineReorderTaskTest extends BaseTaskTest<CombineReorder
private CombineReorderParameters parameters;
private void setUpParameters(List<PdfSource<?>> sources) {
private void setUpParameters() {
setUpParameters(Collections.emptyList());
}
private void setUpParameters(List<MergeInput> inputs) {
parameters = new CombineReorderParameters();
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE);
parameters.setCompress(true);
parameters.setVersion(PdfVersion.VERSION_1_6);
parameters.addSources(sources);
inputs.forEach(source -> {
parameters.addInput(source);
});
}
private List<PdfSource<?>> basicInputs() {
List<PdfSource<?>> input = new ArrayList<PdfSource<?>>();
input.add(customInput("pdf/multipage-test-a.pdf"));
input.add(customInput("pdf/multipage-test-b.pdf"));
private List<MergeInput> basicInputs() {
List<MergeInput> input = new ArrayList<>();
input.add(new PdfMergeInput(customInput("pdf/multipage-test-a.pdf")));
input.add(new PdfMergeInput(customInput("pdf/multipage-test-b.pdf")));
return input;
}
......@@ -145,9 +155,9 @@ public abstract class CombineReorderTaskTest extends BaseTaskTest<CombineReorder
@Test
public void keepsOutline() throws IOException {
List<PdfSource<?>> inputs = new ArrayList<PdfSource<?>>();
inputs.add(customInput("pdf/large_outline.pdf"));
inputs.add(customInput("pdf/test_outline.pdf"));
List<MergeInput> inputs = new ArrayList<>();
inputs.add(new PdfMergeInput(customInput("pdf/large_outline.pdf")));
inputs.add(new PdfMergeInput(customInput("pdf/test_outline.pdf")));
setUpParameters(inputs);
parameters.setOutlinePolicy(OutlinePolicy.RETAIN);
......@@ -180,9 +190,9 @@ public abstract class CombineReorderTaskTest extends BaseTaskTest<CombineReorder
@Test
public void discardsOutline() throws IOException {
List<PdfSource<?>> inputs = new ArrayList<PdfSource<?>>();
inputs.add(customInput("pdf/large_outline.pdf"));
inputs.add(customInput("pdf/test_outline.pdf"));
List<MergeInput> inputs = new ArrayList<>();
inputs.add(new PdfMergeInput(customInput("pdf/large_outline.pdf")));
inputs.add(new PdfMergeInput(customInput("pdf/test_outline.pdf")));
setUpParameters(inputs);
parameters.setOutlinePolicy(OutlinePolicy.DISCARD);
......@@ -203,6 +213,55 @@ public abstract class CombineReorderTaskTest extends BaseTaskTest<CombineReorder
testContext.assertHasOutline(false);
}
@Test
public void mergeImagesAndPdfs() throws IOException {
setUpParameters();
parameters.addInput(new ImageMergeInput(customNonPdfInput("image/draft.png")));
parameters.addInput(new PdfMergeInput(customInput("pdf/test-pdf.pdf")));
parameters.addPage(1, 3);
// image here
parameters.addPage(0, 1);
parameters.addPage(1, 2);
parameters.addPage(1, 1);
testContext.pdfOutputTo(parameters);
execute(parameters);
testContext.assertTaskCompleted();
testContext.assertPages(4);
testContext.forEachPdfOutput(d -> {
assertEquals(Arrays.asList(2), getPagesContainingImages(d));
});
}
@Test
public void mergeImagesAndPdfsWithSpecificPageSize() throws IOException {
setUpParameters();
ImageMergeInput image = new ImageMergeInput(customNonPdfInput("image/draft.png"));
image.setShouldPageSizeMatchImageSize(true);
parameters.addInput(image);
parameters.addInput(new PdfMergeInput(customInput("pdf/test-pdf.pdf")));
// image here
parameters.addPage(0, 1);
parameters.addPage(1, 3);
testContext.pdfOutputTo(parameters);
execute(parameters);
testContext.assertTaskCompleted();
testContext.assertPages(2);
testContext.forEachPdfOutput(d -> {
assertEquals(d.getPage(0).getMediaBox().getWidth(), 248, 0.0);
});
}
void assertPageHasText(PDDocument doc, int page, String expected) throws IOException {
PDFTextStripper textStripper = new PDFTextStripper();
textStripper.setStartPage(page);
......
......@@ -25,13 +25,15 @@ import static org.junit.Assert.assertThat;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.io.IOException;
import java.util.Arrays;
import org.junit.Ignore;
import org.junit.Test;
import org.sejda.model.output.ExistingOutputPolicy;
import org.sejda.model.parameter.PageOrientation;
import org.sejda.model.parameter.PageSize;
import org.sejda.model.PageOrientation;
import org.sejda.model.PageSize;
import org.sejda.model.parameter.image.JpegToPdfParameters;
import org.sejda.model.rotation.Rotation;
import org.sejda.sambox.pdmodel.PDDocument;
import org.sejda.sambox.pdmodel.PDPage;
import org.sejda.sambox.pdmodel.common.PDRectangle;
......@@ -222,5 +224,43 @@ public abstract class JpegToPdfTaskTest extends BaseTaskTest<JpegToPdfParameters
});
}
@Test
public void withoutRotations() throws Exception {
JpegToPdfParameters parameters = new JpegToPdfParameters();
parameters.addSource(customNonPdfInputAsFileSource("image/draft.png"));
parameters.addSource(customNonPdfInputAsFileSource("image/no_exif.JPG"));
testContext.pdfOutputTo(parameters);
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE);
execute(parameters);
testContext.assertTaskCompleted();
testContext.forEachPdfOutput(d -> {
assertEquals(d.getPage(0).getRotation(), 0);
assertEquals(d.getPage(1).getRotation(), 0);
});
}
@Test
public void withRotations() throws Exception {
JpegToPdfParameters parameters = new JpegToPdfParameters();
parameters.addSource(customNonPdfInputAsFileSource("image/draft.png"));
parameters.addSource(customNonPdfInputAsFileSource("image/no_exif.JPG"));
parameters.setRotations(Arrays.asList(Rotation.DEGREES_90, Rotation.DEGREES_180));
testContext.pdfOutputTo(parameters);
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE);
execute(parameters);
testContext.assertTaskCompleted();
testContext.forEachPdfOutput(d -> {
assertEquals(d.getPage(0).getRotation(), 90);
assertEquals(d.getPage(1).getRotation(), 180);
});
}
protected abstract void assertImageAtLocation(PDDocument Doc, PDPage page, Point2D position, int width, int height);
}
......@@ -27,7 +27,7 @@ import org.junit.Ignore;
import org.junit.Test;
import org.sejda.model.input.PdfSource;
import org.sejda.model.output.ExistingOutputPolicy;
import org.sejda.model.parameter.PageSize;
import org.sejda.model.PageSize;
import org.sejda.model.parameter.ResizePagesParameters;
import org.sejda.model.pdf.page.PageRange;
import org.sejda.model.scale.Margins;
......
......@@ -20,7 +20,9 @@
*/
package org.sejda.core.service;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.io.IOException;
......@@ -32,6 +34,7 @@ import org.sejda.model.pdf.PdfVersion;
import org.sejda.model.pdf.page.PageRange;
import org.sejda.model.pdf.page.PredefinedSetOfPages;
import org.sejda.model.rotation.Rotation;
import org.sejda.sambox.pdmodel.PDDocument;
/**
* Abstract test unit for the rotate task
......@@ -146,4 +149,59 @@ public abstract class RotateTaskTest extends BaseTaskTest<RotateParameters> {
});
}
@Test
public void testPredefinedSetOfPages() throws IOException {
parameters = new RotateParameters(Rotation.DEGREES_180, PredefinedSetOfPages.EVEN_PAGES);
parameters.addSource(regularInput());
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE);
testContext.directoryOutputTo(parameters);
assertThat(parameters.getRotation(0), is(Rotation.DEGREES_180));
assertThat(parameters.getRotation(1), is(Rotation.DEGREES_0));
execute(parameters);
testContext.assertTaskCompleted();
testContext.forEachPdfOutput(d -> {
assertPageRotation(d, 0, 0);
assertPageRotation(d, 1, 180);
assertPageRotation(d, 2, 0);
assertPageRotation(d, 3, 180);
});
}
@Test
public void testDifferentRotationsPerSource() throws IOException {
parameters = new RotateParameters();
parameters.addSource(shortInput());
parameters.addSource(mediumInput());
parameters.addPageRangePerSource(0, PageRange.one(1), Rotation.DEGREES_90);
parameters.addPageRangePerSource(1, PageRange.one(1), Rotation.DEGREES_270);
// affects both sources
parameters.addPageRange(PageRange.one(2), Rotation.DEGREES_180);
parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE);
testContext.directoryOutputTo(parameters);
execute(parameters);
testContext.assertTaskCompleted();
testContext.forPdfOutput("short-test-file.pdf", d -> {
assertPageRotation(d, 0, 90);
assertPageRotation(d, 1, 180);
});
testContext.forPdfOutput("medium-test-file.pdf", d -> {
assertPageRotation(d, 0, 270);
assertPageRotation(d, 1, 180);
});
}
private void assertPageRotation(PDDocument doc, int pageIndex, int expectedDegrees) {
assertEquals(expectedDegrees, doc.getPage(pageIndex).getRotation());
}
}
......@@ -24,6 +24,7 @@ import static org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
......@@ -277,7 +278,7 @@ public class TaskTestContext implements Closeable {
}
/**
* asserts that a multiple output task has generated the given number of output files
* asserts that a multiple output task has generated the given number of output files, hidden files don't count
*
* @return
* @throws IOException
......@@ -287,10 +288,8 @@ public class TaskTestContext implements Closeable {
return assertEmptyMultipleOutput();
}
requireMultipleOutputs();
String[] files = fileOutput.list();
assertEquals("An unexpected number of output files has been created: " + StringUtils.join(files, ","), size,
files.length);
assertEquals("Some output file is hidden", size, Files.list(fileOutput.toPath()).filter(p -> {
assertEquals("An unexpected number of output files has been created: ", size,
Files.list(fileOutput.toPath()).filter(p -> {
if (IS_OS_WINDOWS) {
try {
return !(Boolean) Files.getAttribute(p, "dos:hidden");
......@@ -307,6 +306,24 @@ public class TaskTestContext implements Closeable {
return this;
}
/**
* asserts that a multiple output task has generated the given number of output files, including hidden files (in case the task is expected to have some hidden leftover)
*
* @param size
* @return
*/
public TaskTestContext assertOutputSizeIncludingHidden(int size) {
if (size == 0) {
return assertEmptyMultipleOutput();
}
requireMultipleOutputs();
String[] files = fileOutput.list();
assertEquals("An unexpected number of output files has been created: " + StringUtils.join(files, ","), size,
files.length);
return this;
}
/**
* asserts that a multiple output task has generated no output
*
......@@ -524,7 +541,7 @@ public class TaskTestContext implements Closeable {
}
public void assertTaskWarning(String message) {
assertThat(taskWarnings, hasItem(message));
assertThat(taskWarnings, hasItem(containsString(message)));
}
public void assertNoTaskWarnings() {
......
......@@ -10,7 +10,7 @@
<parent>
<groupId>org.sejda</groupId>
<artifactId>sejda-parent</artifactId>
<version>3.2.75</version>
<version>3.2.84</version>
<relativePath>../pom.xml</relativePath>
</parent>
......
......@@ -5,7 +5,7 @@
<parent>
<groupId>org.sejda</groupId>
<artifactId>sejda-parent</artifactId>
<version>3.2.75</version>
<version>3.2.84</version>
<relativePath>../pom.xml</relativePath>
</parent>
......
......@@ -10,7 +10,7 @@
<parent>
<groupId>org.sejda</groupId>
<artifactId>sejda-parent</artifactId>
<version>3.2.75</version>
<version>3.2.84</version>
<relativePath>../pom.xml</relativePath>
</parent>
......
......@@ -6,7 +6,7 @@
<parent>
<groupId>org.sejda</groupId>
<artifactId>sejda-parent</artifactId>
<version>3.2.75</version>
<version>3.2.84</version>
<relativePath>../pom.xml</relativePath>
</parent>
......