From 85100ad3f7cfee87e497b9b78d4f9343615933b5 Mon Sep 17 00:00:00 2001 From: Andrei Solntsev Date: Fri, 17 Feb 2023 09:38:46 +0200 Subject: [PATCH] 1.8.1: added matcher `matchesText(regex)` --- CHANGELOG.txt | 3 ++ README.md | 5 +-- build.gradle | 2 +- src/main/java/com/codeborne/pdftest/PDF.java | 10 ++++++ .../codeborne/pdftest/assertj/PdfAssert.java | 14 ++++++++ .../pdftest/matchers/MatchesText.java | 34 +++++++++++++++++++ .../com/codeborne/pdftest/CreatePdfTest.java | 1 + .../com/codeborne/pdftest/MatchRegexTest.java | 17 ++++++++++ .../pdftest/assertj/ContainsTextTest.java | 10 ++---- .../pdftest/assertj/MatchRegexTest.java | 18 ++++++++++ 10 files changed, 104 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/codeborne/pdftest/matchers/MatchesText.java create mode 100644 src/test/java/com/codeborne/pdftest/MatchRegexTest.java create mode 100644 src/test/java/com/codeborne/pdftest/assertj/MatchRegexTest.java diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 761ee62..e062f6a 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,5 +1,8 @@ == Changelog +=== 1.8.1 (released 17.02.2023) +* Add matcher `matchesText(regex)` + === 1.8.0 (released 22.12.2022) * pdfbox from 2.0.24 to 2.0.27 * bump hamcrest from 1.3 to 2.2 diff --git a/README.md b/README.md index 6a80821..f9ea25a 100644 --- a/README.md +++ b/README.md @@ -46,14 +46,15 @@ If you use **Maven**, add the following dependency to pom.xml: com.codeborne pdf-test - 1.8.0 + 1.8.1 + test ``` If you use **Gradle**, add the following dependency to build.gradle: ```groovy - testCompile 'com.codeborne:pdf-test:1.8.0' + testImplementation 'com.codeborne:pdf-test:1.8.1' ``` ## How to contribute diff --git a/build.gradle b/build.gradle index fc2f621..8fcedd7 100644 --- a/build.gradle +++ b/build.gradle @@ -13,7 +13,7 @@ plugins { group = 'com.codeborne' archivesBaseName = 'pdf-test' -version = '1.8.0' +version = '1.8.1' defaultTasks 'test', 'install' diff --git a/src/main/java/com/codeborne/pdftest/PDF.java b/src/main/java/com/codeborne/pdftest/PDF.java index f1de72b..52e74c3 100644 --- a/src/main/java/com/codeborne/pdftest/PDF.java +++ b/src/main/java/com/codeborne/pdftest/PDF.java @@ -5,6 +5,7 @@ import com.codeborne.pdftest.matchers.ContainsTextCaseInsensitive; import com.codeborne.pdftest.matchers.DoesNotContainExactText; import com.codeborne.pdftest.matchers.DoesNotContainText; +import com.codeborne.pdftest.matchers.MatchesText; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature; import org.apache.pdfbox.text.PDFTextStripper; @@ -15,6 +16,7 @@ import java.net.URL; import java.nio.file.Paths; import java.util.Calendar; +import java.util.regex.Pattern; import static java.nio.file.Files.readAllBytes; @@ -142,4 +144,12 @@ public static Matcher doesNotContainExactText(String text) { public static Matcher containsTextCaseInsensitive(String text) { return new ContainsTextCaseInsensitive(text); } + + public static Matcher matchesText(String regex) { + return matchesText(Pattern.compile(regex)); + } + public static Matcher matchesText(Pattern regex) { + return new MatchesText(regex); + } + } diff --git a/src/main/java/com/codeborne/pdftest/assertj/PdfAssert.java b/src/main/java/com/codeborne/pdftest/assertj/PdfAssert.java index 726bbe9..7249aa1 100644 --- a/src/main/java/com/codeborne/pdftest/assertj/PdfAssert.java +++ b/src/main/java/com/codeborne/pdftest/assertj/PdfAssert.java @@ -6,8 +6,11 @@ import com.codeborne.pdftest.matchers.ContainsTextCaseInsensitive; import com.codeborne.pdftest.matchers.DoesNotContainExactText; import com.codeborne.pdftest.matchers.DoesNotContainText; +import com.codeborne.pdftest.matchers.MatchesText; import org.assertj.core.api.AbstractAssert; +import java.util.regex.Pattern; + import static org.hamcrest.MatcherAssert.assertThat; public class PdfAssert extends AbstractAssert { @@ -44,4 +47,15 @@ public PdfAssert containsTextCaseInsensitive(String substring) { assertThat(actual, new ContainsTextCaseInsensitive(substring)); return this; } + + public PdfAssert matchesText(String regex) { + return matchesText(Pattern.compile(regex)); + } + + public PdfAssert matchesText(Pattern regex) { + isNotNull(); + assertThat(actual, new MatchesText(regex)); + return this; + } + } diff --git a/src/main/java/com/codeborne/pdftest/matchers/MatchesText.java b/src/main/java/com/codeborne/pdftest/matchers/MatchesText.java new file mode 100644 index 0000000..33ee4c0 --- /dev/null +++ b/src/main/java/com/codeborne/pdftest/matchers/MatchesText.java @@ -0,0 +1,34 @@ +package com.codeborne.pdftest.matchers; + +import com.codeborne.pdftest.PDF; +import org.hamcrest.Description; + +import java.util.regex.Pattern; + +public class MatchesText extends PDFMatcher { + private final Pattern expectedText; + + public MatchesText(Pattern expectedText) { + this.expectedText = expectedText; + } + + @Override + protected boolean matchesSafely(PDF item) { + String reducedPdfText = reduceSpaces(item.text); + return expectedText.matcher(reducedPdfText).matches(); + } + + @Override + protected void describeMismatchSafely(PDF item, Description mismatchDescription) { + mismatchDescription.appendText("was \"").appendText(reduceSpaces(item.text)).appendText("\""); + } + + @Override + public void describeTo(Description description) { + description.appendText("a PDF matching "); + buildErrorMessage(description, expectedText.toString(), new String[0]); + } + + +} + diff --git a/src/test/java/com/codeborne/pdftest/CreatePdfTest.java b/src/test/java/com/codeborne/pdftest/CreatePdfTest.java index 377d7cb..48b2763 100644 --- a/src/test/java/com/codeborne/pdftest/CreatePdfTest.java +++ b/src/test/java/com/codeborne/pdftest/CreatePdfTest.java @@ -12,6 +12,7 @@ import java.nio.file.Paths; import static com.codeborne.pdftest.PDF.containsText; +import static com.codeborne.pdftest.PDF.matchesText; import static org.hamcrest.MatcherAssert.assertThat; public class CreatePdfTest { diff --git a/src/test/java/com/codeborne/pdftest/MatchRegexTest.java b/src/test/java/com/codeborne/pdftest/MatchRegexTest.java new file mode 100644 index 0000000..f9b5039 --- /dev/null +++ b/src/test/java/com/codeborne/pdftest/MatchRegexTest.java @@ -0,0 +1,17 @@ +package com.codeborne.pdftest; + +import org.junit.Test; + +import java.io.IOException; +import java.net.URL; + +import static com.codeborne.pdftest.PDF.matchesText; +import static org.hamcrest.MatcherAssert.assertThat; + +public class MatchRegexTest { + @Test + public void verifyTextWithRegex() throws IOException { + URL url = getClass().getClassLoader().getResource("50quickideas.pdf"); + assertThat(new PDF(url), matchesText(".*50 Quick Ideas.+50 Quick Ideas.*")); + } +} diff --git a/src/test/java/com/codeborne/pdftest/assertj/ContainsTextTest.java b/src/test/java/com/codeborne/pdftest/assertj/ContainsTextTest.java index 781204a..ec43e62 100644 --- a/src/test/java/com/codeborne/pdftest/assertj/ContainsTextTest.java +++ b/src/test/java/com/codeborne/pdftest/assertj/ContainsTextTest.java @@ -4,10 +4,8 @@ import org.junit.Before; import org.junit.Test; -import java.io.IOException; -import java.util.Objects; - import static com.codeborne.pdftest.assertj.Assertions.assertThat; +import static java.util.Objects.requireNonNull; import static org.assertj.core.api.Assertions.assertThatThrownBy; public class ContainsTextTest { @@ -17,10 +15,8 @@ public class ContainsTextTest { @Before public void setUp() throws Exception { - fiftyIdeasPdf = new PDF( - Objects.requireNonNull(getClass().getClassLoader().getResource("50quickideas.pdf")) - ); - minimalPdf = new PDF(Objects.requireNonNull(getClass().getClassLoader().getResource("minimal.pdf"))); + fiftyIdeasPdf = new PDF(requireNonNull(getClass().getClassLoader().getResource("50quickideas.pdf"))); + minimalPdf = new PDF(requireNonNull(getClass().getClassLoader().getResource("minimal.pdf"))); } @Test diff --git a/src/test/java/com/codeborne/pdftest/assertj/MatchRegexTest.java b/src/test/java/com/codeborne/pdftest/assertj/MatchRegexTest.java new file mode 100644 index 0000000..ffcdbc0 --- /dev/null +++ b/src/test/java/com/codeborne/pdftest/assertj/MatchRegexTest.java @@ -0,0 +1,18 @@ +package com.codeborne.pdftest.assertj; + +import com.codeborne.pdftest.PDF; +import org.junit.Test; + +import java.io.IOException; + +import static com.codeborne.pdftest.assertj.Assertions.assertThat; +import static java.util.Objects.requireNonNull; + +public class MatchRegexTest { + @Test + public void verifyTextWithRegex() throws IOException { + PDF pdf = new PDF(requireNonNull(getClass().getClassLoader().getResource("50quickideas.pdf"))); + assertThat(pdf).matchesText(".*50 Quick Ideas.+50 Quick Ideas.*"); + assertThat(pdf).matchesText("50 Quick Ideas.*Gojko Adzic.*2014-03-25.*©2013 - 2014.*"); + } +}