Skip to content

Commit

Permalink
1.8.1: added matcher matchesText(regex)
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Feb 17, 2023
1 parent fdd34bc commit 85100ad
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ If you use **Maven**, add the following dependency to pom.xml:
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>pdf-test</artifactId>
<version>1.8.0</version>
<version>1.8.1</version>
<scope>test</scope>
</dependency>
```

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
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {

group = 'com.codeborne'
archivesBaseName = 'pdf-test'
version = '1.8.0'
version = '1.8.1'

defaultTasks 'test', 'install'

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/codeborne/pdftest/PDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -142,4 +144,12 @@ public static Matcher<PDF> doesNotContainExactText(String text) {
public static Matcher<PDF> containsTextCaseInsensitive(String text) {
return new ContainsTextCaseInsensitive(text);
}

public static Matcher<PDF> matchesText(String regex) {
return matchesText(Pattern.compile(regex));
}
public static Matcher<PDF> matchesText(Pattern regex) {
return new MatchesText(regex);
}

}
14 changes: 14 additions & 0 deletions src/main/java/com/codeborne/pdftest/assertj/PdfAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<PdfAssert, PDF> {
Expand Down Expand Up @@ -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;
}

}
34 changes: 34 additions & 0 deletions src/main/java/com/codeborne/pdftest/matchers/MatchesText.java
Original file line number Diff line number Diff line change
@@ -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]);
}


}

1 change: 1 addition & 0 deletions src/test/java/com/codeborne/pdftest/CreatePdfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/com/codeborne/pdftest/MatchRegexTest.java
Original file line number Diff line number Diff line change
@@ -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.*"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/codeborne/pdftest/assertj/MatchRegexTest.java
Original file line number Diff line number Diff line change
@@ -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.*");
}
}

0 comments on commit 85100ad

Please sign in to comment.