Skip to content

Commit 85100ad

Browse files
committed
1.8.1: added matcher matchesText(regex)
1 parent fdd34bc commit 85100ad

File tree

10 files changed

+104
-10
lines changed

10 files changed

+104
-10
lines changed

CHANGELOG.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
== Changelog
22

3+
=== 1.8.1 (released 17.02.2023)
4+
* Add matcher `matchesText(regex)`
5+
36
=== 1.8.0 (released 22.12.2022)
47
* pdfbox from 2.0.24 to 2.0.27
58
* bump hamcrest from 1.3 to 2.2

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ If you use **Maven**, add the following dependency to pom.xml:
4646
<dependency>
4747
<groupId>com.codeborne</groupId>
4848
<artifactId>pdf-test</artifactId>
49-
<version>1.8.0</version>
49+
<version>1.8.1</version>
50+
<scope>test</scope>
5051
</dependency>
5152
```
5253

5354
If you use **Gradle**, add the following dependency to build.gradle:
5455

5556
```groovy
56-
testCompile 'com.codeborne:pdf-test:1.8.0'
57+
testImplementation 'com.codeborne:pdf-test:1.8.1'
5758
```
5859

5960
## How to contribute

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ plugins {
1313

1414
group = 'com.codeborne'
1515
archivesBaseName = 'pdf-test'
16-
version = '1.8.0'
16+
version = '1.8.1'
1717

1818
defaultTasks 'test', 'install'
1919

src/main/java/com/codeborne/pdftest/PDF.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.codeborne.pdftest.matchers.ContainsTextCaseInsensitive;
66
import com.codeborne.pdftest.matchers.DoesNotContainExactText;
77
import com.codeborne.pdftest.matchers.DoesNotContainText;
8+
import com.codeborne.pdftest.matchers.MatchesText;
89
import org.apache.pdfbox.pdmodel.PDDocument;
910
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
1011
import org.apache.pdfbox.text.PDFTextStripper;
@@ -15,6 +16,7 @@
1516
import java.net.URL;
1617
import java.nio.file.Paths;
1718
import java.util.Calendar;
19+
import java.util.regex.Pattern;
1820

1921
import static java.nio.file.Files.readAllBytes;
2022

@@ -142,4 +144,12 @@ public static Matcher<PDF> doesNotContainExactText(String text) {
142144
public static Matcher<PDF> containsTextCaseInsensitive(String text) {
143145
return new ContainsTextCaseInsensitive(text);
144146
}
147+
148+
public static Matcher<PDF> matchesText(String regex) {
149+
return matchesText(Pattern.compile(regex));
150+
}
151+
public static Matcher<PDF> matchesText(Pattern regex) {
152+
return new MatchesText(regex);
153+
}
154+
145155
}

src/main/java/com/codeborne/pdftest/assertj/PdfAssert.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import com.codeborne.pdftest.matchers.ContainsTextCaseInsensitive;
77
import com.codeborne.pdftest.matchers.DoesNotContainExactText;
88
import com.codeborne.pdftest.matchers.DoesNotContainText;
9+
import com.codeborne.pdftest.matchers.MatchesText;
910
import org.assertj.core.api.AbstractAssert;
1011

12+
import java.util.regex.Pattern;
13+
1114
import static org.hamcrest.MatcherAssert.assertThat;
1215

1316
public class PdfAssert extends AbstractAssert<PdfAssert, PDF> {
@@ -44,4 +47,15 @@ public PdfAssert containsTextCaseInsensitive(String substring) {
4447
assertThat(actual, new ContainsTextCaseInsensitive(substring));
4548
return this;
4649
}
50+
51+
public PdfAssert matchesText(String regex) {
52+
return matchesText(Pattern.compile(regex));
53+
}
54+
55+
public PdfAssert matchesText(Pattern regex) {
56+
isNotNull();
57+
assertThat(actual, new MatchesText(regex));
58+
return this;
59+
}
60+
4761
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.codeborne.pdftest.matchers;
2+
3+
import com.codeborne.pdftest.PDF;
4+
import org.hamcrest.Description;
5+
6+
import java.util.regex.Pattern;
7+
8+
public class MatchesText extends PDFMatcher {
9+
private final Pattern expectedText;
10+
11+
public MatchesText(Pattern expectedText) {
12+
this.expectedText = expectedText;
13+
}
14+
15+
@Override
16+
protected boolean matchesSafely(PDF item) {
17+
String reducedPdfText = reduceSpaces(item.text);
18+
return expectedText.matcher(reducedPdfText).matches();
19+
}
20+
21+
@Override
22+
protected void describeMismatchSafely(PDF item, Description mismatchDescription) {
23+
mismatchDescription.appendText("was \"").appendText(reduceSpaces(item.text)).appendText("\"");
24+
}
25+
26+
@Override
27+
public void describeTo(Description description) {
28+
description.appendText("a PDF matching ");
29+
buildErrorMessage(description, expectedText.toString(), new String[0]);
30+
}
31+
32+
33+
}
34+

src/test/java/com/codeborne/pdftest/CreatePdfTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.nio.file.Paths;
1313

1414
import static com.codeborne.pdftest.PDF.containsText;
15+
import static com.codeborne.pdftest.PDF.matchesText;
1516
import static org.hamcrest.MatcherAssert.assertThat;
1617

1718
public class CreatePdfTest {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codeborne.pdftest;
2+
3+
import org.junit.Test;
4+
5+
import java.io.IOException;
6+
import java.net.URL;
7+
8+
import static com.codeborne.pdftest.PDF.matchesText;
9+
import static org.hamcrest.MatcherAssert.assertThat;
10+
11+
public class MatchRegexTest {
12+
@Test
13+
public void verifyTextWithRegex() throws IOException {
14+
URL url = getClass().getClassLoader().getResource("50quickideas.pdf");
15+
assertThat(new PDF(url), matchesText(".*50 Quick Ideas.+50 Quick Ideas.*"));
16+
}
17+
}

src/test/java/com/codeborne/pdftest/assertj/ContainsTextTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
import org.junit.Before;
55
import org.junit.Test;
66

7-
import java.io.IOException;
8-
import java.util.Objects;
9-
107
import static com.codeborne.pdftest.assertj.Assertions.assertThat;
8+
import static java.util.Objects.requireNonNull;
119
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1210

1311
public class ContainsTextTest {
@@ -17,10 +15,8 @@ public class ContainsTextTest {
1715

1816
@Before
1917
public void setUp() throws Exception {
20-
fiftyIdeasPdf = new PDF(
21-
Objects.requireNonNull(getClass().getClassLoader().getResource("50quickideas.pdf"))
22-
);
23-
minimalPdf = new PDF(Objects.requireNonNull(getClass().getClassLoader().getResource("minimal.pdf")));
18+
fiftyIdeasPdf = new PDF(requireNonNull(getClass().getClassLoader().getResource("50quickideas.pdf")));
19+
minimalPdf = new PDF(requireNonNull(getClass().getClassLoader().getResource("minimal.pdf")));
2420
}
2521

2622
@Test
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.codeborne.pdftest.assertj;
2+
3+
import com.codeborne.pdftest.PDF;
4+
import org.junit.Test;
5+
6+
import java.io.IOException;
7+
8+
import static com.codeborne.pdftest.assertj.Assertions.assertThat;
9+
import static java.util.Objects.requireNonNull;
10+
11+
public class MatchRegexTest {
12+
@Test
13+
public void verifyTextWithRegex() throws IOException {
14+
PDF pdf = new PDF(requireNonNull(getClass().getClassLoader().getResource("50quickideas.pdf")));
15+
assertThat(pdf).matchesText(".*50 Quick Ideas.+50 Quick Ideas.*");
16+
assertThat(pdf).matchesText("50 Quick Ideas.*Gojko Adzic.*2014-03-25.*©2013 - 2014.*");
17+
}
18+
}

0 commit comments

Comments
 (0)