Skip to content

Commit

Permalink
add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Oct 30, 2016
1 parent a392ea1 commit d3bffa0
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 87 deletions.
57 changes: 0 additions & 57 deletions src/main/java/com/codeborne/pdftest/IO.java

This file was deleted.

46 changes: 29 additions & 17 deletions src/main/java/com/codeborne/pdftest/PDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
import java.io.*;
import java.net.URI;
import java.net.URL;
import java.nio.file.Paths;
import java.util.Calendar;

import static com.codeborne.pdftest.IO.readBytes;
import static com.codeborne.pdftest.IO.readFile;
import static com.codeborne.pdftest.IO.toURL;
import static java.nio.file.Files.readAllBytes;

public class PDF {
public final byte[] content;
Expand All @@ -38,8 +37,7 @@ private PDF(String name, byte[] content) {
this.content = content;

try (InputStream inputStream = new ByteArrayInputStream(content)) {
PDDocument pdf = PDDocument.load(inputStream);
try {
try (PDDocument pdf = PDDocument.load(inputStream)) {
this.text = new PDFTextStripper().getText(pdf);
this.numberOfPages = pdf.getNumberOfPages();
this.author = pdf.getDocumentInformation().getAuthor();
Expand All @@ -56,36 +54,50 @@ private PDF(String name, byte[] content) {
this.signerName = signature == null ? null : signature.getName();
this.signatureTime = signature == null ? null : signature.getSignDate();
}
finally {
try {pdf.close();}
catch (IOException ignore) {}
}
}
catch (Exception e) {
throw new IllegalArgumentException("Invalid PDF " + name, e);
throw new IllegalArgumentException("Invalid PDF file: " + name, e);
}
}

public PDF(File pdfFile) {
this(pdfFile.getAbsolutePath(), readFile(pdfFile));
public PDF(File pdfFile) throws IOException {
this(pdfFile.getAbsolutePath(), readAllBytes(Paths.get(pdfFile.getAbsolutePath())));
}

public PDF(URL url) {
public PDF(URL url) throws IOException {
this(url.toString(), readBytes(url));
}

public PDF(URI uri) {
this(toURL(uri));
public PDF(URI uri) throws IOException {
this(uri.toURL());
}

public PDF(byte[] content) {
this("", content);
}

public PDF(InputStream inputStream) {
public PDF(InputStream inputStream) throws IOException {
this(readBytes(inputStream));
}


private static byte[] readBytes(URL url) throws IOException {
try (InputStream inputStream = url.openStream()) {
return readBytes(inputStream);
}
}

private static byte[] readBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream(2048);
byte[] buffer = new byte[2048];

int nRead;
while ((nRead = inputStream.read(buffer, 0, buffer.length)) != -1) {
result.write(buffer, 0, nRead);
}

return result.toByteArray();
}

public static Matcher<PDF> containsText(String text) {
return new ContainsText(text);
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/codeborne/pdftest/CreatePdfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

public class CreatePdfTest {
@Test
public void fromFile() throws URISyntaxException, IOException {
public void fromFile() throws IOException {
File file = new File("src/test/resources/50quickideas.pdf");
assertThat(new PDF(file), containsText("50 Quick Ideas"));
}

@Test
public void fromUrl() throws URISyntaxException, IOException {
public void fromUrl() throws IOException {
URL url = getClass().getClassLoader().getResource("50quickideas.pdf");
assertThat(new PDF(url), containsText("50 Quick Ideas"));
}
Expand All @@ -34,7 +34,7 @@ public void fromUri() throws URISyntaxException, IOException {
}

@Test
public void fromInputStream() throws URISyntaxException, IOException {
public void fromInputStream() throws IOException {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("50quickideas.pdf");
assertThat(new PDF(inputStream), containsText("50 Quick Ideas"));
}
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/com/codeborne/pdftest/InvalidPdfTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.codeborne.pdftest;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.NoSuchFileException;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

public class InvalidPdfTest {
@Test(expected = NoSuchFileException.class)
public void throws_IOException_ifFailedToReadPdfFile() throws IOException {
new PDF(new File("src/test/resources/invalid-file.pdf"));
}

@Test
public void throws_IllegalArgumentException_inCaseOfInvalidPdfFile() throws IOException {
try {
new PDF(new File("build.gradle"));
fail("expected IllegalArgumentException");
}
catch (IllegalArgumentException expected) {
assertThat(expected.getMessage(), is("Invalid PDF file: " + System.getProperty("user.dir") + "/build.gradle"));
assertThat(expected.getCause(), is(notNullValue()));
}
}
}
3 changes: 1 addition & 2 deletions src/test/java/com/codeborne/pdftest/PDFInformationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.junit.Test;

import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
Expand All @@ -17,7 +16,7 @@

public class PDFInformationTest {
@Test
public void canGetInformationFromPdf() throws URISyntaxException, IOException, ParseException {
public void canGetInformationFromPdf() throws IOException, ParseException {
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Tallinn"));

PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,46 @@
import com.codeborne.pdftest.PDF;
import org.junit.Test;

import java.io.IOException;

import static com.codeborne.pdftest.PDF.containsExactText;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

public class ContainsExactTextTest {
@Test
public void canAssertThatPdfContainsText() {
public void canAssertThatPdfContainsText() throws IOException {
PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
assertThat(pdf, containsExactText("50 Quick Ideas to Improve your User Stories"));
}

@Test
public void shouldBeCaseSensitive() {
public void shouldBeCaseSensitive() throws IOException {
PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
assertThat(pdf, not(containsExactText("50 quick ideas")));
}

@Test
public void shouldNotIgnoreWhitespaces() {
public void shouldNotIgnoreWhitespaces() throws IOException {
PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
assertThat(pdf, containsExactText("Gojko Adzic"));
assertThat(pdf, not(containsExactText("Gojko Adzic")));
assertThat(pdf, not(containsExactText("\tGojko \t Adzic\n")));
assertThat(pdf, not(containsExactText("Gojko \u00a0 Adzic\r\n")));
}

@Test
public void errorDescription() throws IOException {
PDF pdf = new PDF(getClass().getClassLoader().getResource("minimal.pdf"));
try {
assertThat(pdf, containsExactText("Goodbye word"));
fail("expected AssertionError");
}
catch (AssertionError expected) {
assertThat(expected.getMessage(),
is("\nExpected: a PDF containing \"Goodbye word\"\n but: was \"Hello World\n\""));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,37 @@
import com.codeborne.pdftest.PDF;
import org.junit.Test;

import java.io.IOException;

import static com.codeborne.pdftest.PDF.containsTextCaseInsensitive;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

public class ContainsTextCaseInsensitiveTest {
@Test
public void canAssertThatPdfContainsTextIgnoringCase() {
public void canAssertThatPdfContainsTextIgnoringCase() throws IOException {
PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
assertThat(pdf, containsTextCaseInsensitive("50 quick ideas to improve your user stories"));
assertThat(pdf, containsTextCaseInsensitive("50 quick IDEAS to improve YOUR user STORIES"));
}

@Test
public void shouldIgnoreWhitespaces() {
public void shouldIgnoreWhitespaces() throws IOException {
PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
assertThat(pdf, containsTextCaseInsensitive("\n\tgOJKO aDZIC \u00a0 "));
}

@Test
public void errorDescription() throws IOException {
PDF pdf = new PDF(getClass().getClassLoader().getResource("minimal.pdf"));
try {
assertThat(pdf, containsTextCaseInsensitive("Goodbye word"));
fail("expected AssertionError");
}
catch (AssertionError expected) {
assertThat(expected.getMessage(),
is("\nExpected: a PDF containing \"Goodbye word\"\n but: was \"Hello World\""));
}
}
}
23 changes: 20 additions & 3 deletions src/test/java/com/codeborne/pdftest/matchers/ContainsTextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import com.codeborne.pdftest.PDF;
import org.junit.Test;

import java.io.IOException;

import static com.codeborne.pdftest.PDF.containsText;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

public class ContainsTextTest {
@Test
public void canAssertThatPdfContainsText() {
public void canAssertThatPdfContainsText() throws IOException {
PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
assertThat(pdf, containsText("50 Quick Ideas to Improve your User Stories"));
assertThat(pdf, containsText("Gojko Adzic"));
Expand All @@ -19,13 +23,13 @@ public void canAssertThatPdfContainsText() {
}

@Test
public void shouldBeCaseSensitive() {
public void shouldBeCaseSensitive() throws IOException {
PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
assertThat(pdf, not(containsText("50 quick ideas")));
}

@Test
public void shouldIgnoreWhitespaces() {
public void shouldIgnoreWhitespaces() throws IOException {
PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
assertThat(pdf, containsText("Gojko Adzic"));
assertThat(pdf, containsText("\tGojko \t Adzic\t"));
Expand All @@ -34,4 +38,17 @@ public void shouldIgnoreWhitespaces() {
assertThat(pdf, containsText("Gojko\r\n Adzic\r\n"));
assertThat(pdf, containsText("Gojko \u00a0 Adzic\r\n"));
}

@Test
public void errorDescription() throws IOException {
PDF pdf = new PDF(getClass().getClassLoader().getResource("minimal.pdf"));
try {
assertThat(pdf, containsText("Goodbye word"));
fail("expected AssertionError");
}
catch (AssertionError expected) {
assertThat(expected.getMessage(),
is("\nExpected: a PDF containing \"Goodbye word\"\n but: was \"Hello World\""));
}
}
}
Binary file added src/test/resources/minimal.pdf
Binary file not shown.

0 comments on commit d3bffa0

Please sign in to comment.