Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

test: Unit test to run through extended pdf test data.

  • Loading branch information...
commit 7244ea9d77d9a2d71234ed30277ca95e8a2db5eb 1 parent 1f85c72
@kjw kjw authored
View
1  .classpath
@@ -11,5 +11,6 @@
<classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/>
<classpathentry kind="lib" path="lib/commons-logging-adapters-1.1.1.jar"/>
<classpathentry kind="lib" path="lib/commons-logging-api-1.1.1.jar"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
View
1  .gitignore
@@ -1,3 +1,4 @@
bin
+output
*.swp
*~
View
3  .gitmodules
@@ -0,0 +1,3 @@
+[submodule "test-data/extended"]
+ path = test-data/extended
+ url = kward@labs.crossref.org:/home/kward/pdftestdata
View
23 README.rst
@@ -0,0 +1,23 @@
+Testing
+=====================================================
+
+Extended test data (in the form of lots of PDFs)
+can be inserted as a git submodule. Use these
+instructions to get the test data. First check out
+the pdftestadata repo from labs.crossref.org:
+
+$ git clone <your_user_name>@labs.crossref.org:
+ /home/kward/pdftestdata
+
+Then initialise
+git submodules within your pdfmark repo:
+
+$ git submodule init
+
+Then alter your ~/.git/config file - point the
+"test-data/extended" submodule to your local
+copy of the pdftestdata repo. Finally, update the
+submodules within your pdfmark repo:
+
+$ git submodule update
+
View
BIN  lib/junit-4.8.jar
Binary file not shown
View
6 script/run-test
@@ -0,0 +1,6 @@
+PROJ_HOME=/Users/karl/Dropbox/Code/pdfmark
+LIB_DIR=$PROJ_HOME/lib
+BIN_DIR=$PROJ_HOME/bin
+LIB_LIST=$LIB_DIR/commons-logging-1.1.1.jar:$LIB_DIR/commons-logging-adapters-1.1.1.jar:$LIB_DIR/commons-logging-api-1.1.1.jar:$LIB_DIR/httpclient-4.0.jar:$LIB_DIR/httpcore-4.0.1.jar:$LIB_DIR/httpcore-nio-4.0.1.jar:$LIB_DIR/httpmime-4.0.jar:$LIB_DIR/iText-2.1.7.jar:$LIB_DIR/jargs.jar:$LIB_DIR/junit-4.8.jar
+
+java -classpath $LIB_LIST:$BIN_DIR org.crossref.pdfmark.test.Main $@
View
1  src/org/crossref/pdfmark/XmpUtils.java
@@ -149,6 +149,7 @@ private static XmpArray parseRdfList(Element list) {
for (String checkAgainst : noWriteList) {
if (schema.getXmlns().equals(checkAgainst)) {
found = true;
+ break;
}
}
if (!found) {
View
11 src/org/crossref/pdfmark/test/Main.java
@@ -0,0 +1,11 @@
+package org.crossref.pdfmark.test;
+import org.junit.runner.JUnitCore;
+
+
+public class Main {
+
+ public static void main(String[] args) {
+ JUnitCore.main("org.crossref.pdfmark.test.MarkBuilderTest");
+ }
+
+}
View
79 src/org/crossref/pdfmark/test/MarkBuilderTest.java
@@ -0,0 +1,79 @@
+package org.crossref.pdfmark.test;
+
+import java.io.File;
+
+import junit.framework.TestCase;
+
+import org.crossref.pdfmark.MarkBuilder;
+import org.crossref.pdfmark.MetadataGrabber;
+import org.crossref.pdfmark.XmpUtils;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.lowagie.text.pdf.PdfStamper;
+import static org.junit.Assert.*;
+
+/**
+ * Runs through PDFs, adding metadata for their DOIs. DOIs are located from
+ * a text file called "doi.txt", in the PDF info directory.
+ */
+public class MarkBuilderTest {
+
+ private static final String EXTENDED_TEST_DIR = "test-data/extended";
+
+ private static final String OUTPUT_DIR = "output/extended";
+
+ @Before
+ public void makeOutputDir() {
+ File outputDir = new File(OUTPUT_DIR);
+ if (!outputDir.exists()) {
+ outputDir.mkdirs();
+ }
+ }
+
+ @Before
+ public void checkForExtendedTestData() {
+ File exTestDir = new File(EXTENDED_TEST_DIR);
+ assertTrue("Extended test data not available. Grab the git submodule.",
+ exTestDir.exists());
+ }
+
+ @Test
+ public void addMetadataToPdfs() throws Exception {
+ File exTestDir = new File(EXTENDED_TEST_DIR);
+
+ for (String pdfDir : exTestDir.list()) {
+ if (pdfDir.startsWith(".")) {
+ continue; // skip .git and anything else
+ }
+
+ PdfInfoDirectory pdfInfo = new PdfInfoDirectory(EXTENDED_TEST_DIR
+ + File.separator + pdfDir, OUTPUT_DIR);
+ pdfInfo.create();
+
+ String doi = pdfInfo.getDoi();
+
+ if (!doi.isEmpty()) {
+ PdfStamper stamper = pdfInfo.getStamper();
+ byte[] existingXmp = pdfInfo.getExistingXmp();
+
+ MetadataGrabber grabber = new MetadataGrabber();
+ MarkBuilder builder = new MarkBuilder() {
+ @Override
+ public void onFailure(String doi, int code, String msg) {
+ fail("Could not get DOI " + doi + ": " + code + " " + msg);
+ }
+ };
+
+ grabber.grabOne(doi, builder);
+ grabber.waitForEmpty();
+
+ byte[] merged = XmpUtils.mergeXmp(existingXmp, builder.getXmpData());
+ stamper.setXmpMetadata(merged);
+ }
+
+ pdfInfo.dispose();
+ }
+ }
+
+}
View
28 src/org/crossref/pdfmark/test/StamperTest.java → src/org/crossref/pdfmark/test/PdfInfoDirectory.java
@@ -1,20 +1,18 @@
package org.crossref.pdfmark.test;
+import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
-import org.junit.After;
-import org.junit.Before;
-
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
-public class StamperTest {
+public class PdfInfoDirectory {
- private String operatingPath;
+private String operatingPath;
private String outputPath;
@@ -22,7 +20,8 @@
private byte[] existingXmp;
- public StamperTest(String newOperatingPath, String newOutputPath) throws IOException {
+ public PdfInfoDirectory(String newOperatingPath, String newOutputPath)
+ throws IOException {
operatingPath = newOperatingPath;
outputPath = newOutputPath;
@@ -43,8 +42,18 @@ protected PdfStamper getStamper() {
return existingXmp;
}
- @Before
- protected void createStamper() throws IOException, DocumentException {
+ protected String getDoi() throws IOException {
+ File doiFile = new File(getOperatingPath() + File.separator + "doi.txt");
+ String doi = "";
+ if (doiFile.exists()) {
+ DataInputStream dIn = new DataInputStream(new FileInputStream(doiFile));
+ doi = dIn.readLine();
+ dIn.close();
+ }
+ return doi;
+ }
+
+ protected void create() throws IOException, DocumentException {
/* Find a PDF file in the operating directory. */
String pdfFileName = null;
File operatingDir = new File(operatingPath);
@@ -68,8 +77,7 @@ protected void createStamper() throws IOException, DocumentException {
stamper = new PdfStamper(reader, new FileOutputStream(pdfOutPath));
}
- @After
- protected void disposeStamper() throws IOException, DocumentException {
+ protected void dispose() throws IOException, DocumentException {
/* Close the stamper. */
stamper.close();
}
View
60 src/org/crossref/pdfmark/test/StockPdfTest.java
@@ -1,60 +0,0 @@
-package org.crossref.pdfmark.test;
-
-import java.io.BufferedInputStream;
-import java.io.ByteArrayInputStream;
-import java.io.DataInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-
-import org.crossref.pdfmark.MarkBuilder;
-import org.crossref.pdfmark.MetadataGrabber;
-import org.crossref.pdfmark.XmpUtils;
-import org.junit.Before;
-import org.junit.Test;
-
-import com.lowagie.text.pdf.PdfStamper;
-import static org.junit.Assert.*;
-
-/**
- * Runs through a PDF, adding metadata for a DOI. The DOI is located from
- * a text file called "doi.txt", in the operating directory (concept from
- * StamperTest).
- */
-public class StockPdfTest extends StamperTest {
-
- private String doi;
-
- public StockPdfTest(String operatingPath, String outputPath) throws IOException {
- super(operatingPath, outputPath);
- }
-
- @Before
- protected void locateDoi() throws IOException {
- File doiFile = new File(getOperatingPath() + "doi.txt");
- DataInputStream dIn = new DataInputStream(new FileInputStream(doiFile));
- doi = dIn.readLine();
- dIn.close();
- }
-
- @Test
- protected void addMetadataToPdf() throws Exception {
- PdfStamper stamper = getStamper();
- byte[] existingXmp = getExistingXmp();
-
- MetadataGrabber grabber = new MetadataGrabber();
- MarkBuilder builder = new MarkBuilder() {
- @Override
- public void onFailure(String doi, int code, String msg) {
- fail("Could not get DOI " + doi + ": " + code + " " + msg);
- }
- };
-
- grabber.grabOne(doi, builder);
- grabber.waitForEmpty();
-
- byte[] merged = XmpUtils.mergeXmp(existingXmp, builder.getXmpData());
- stamper.setXmpMetadata(merged);
- }
-
-}
1  test-data/extended
@@ -0,0 +1 @@
+Subproject commit 957a37386aa1d45671c47c156942c346ddfbf1aa
Please sign in to comment.
Something went wrong with that request. Please try again.