Skip to content

Commit 47f2ca4

Browse files
committed
added AssertJ matchers
1 parent 8d0ebb5 commit 47f2ca4

File tree

8 files changed

+205
-4
lines changed

8 files changed

+205
-4
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.6.1 (released 26.02.2021)
4+
* added AssertJ matchers
5+
36
=== 1.6.0 (released 21.01.2021)
47
* #4 add methods to search within given pages of PDF
58
* added method doesNotContainText() and doesNotContainExactText

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ PDF testing library
77

88
Be sure that your code generates correct PDF!
99

10-
## How to use
10+
## How to use (Hamcrest)
1111

1212
```java
1313
import com.codeborne.pdftest.PDF;
@@ -23,6 +23,21 @@ public class PDFContainsTextTest {
2323
}
2424
```
2525

26+
## How to use (AssertJ)
27+
28+
```java
29+
import com.codeborne.pdftest.PDF;
30+
import static com.codeborne.pdftest.assertj.Assertions.assertThat;
31+
32+
public class PDFContainsTextTest {
33+
@Test
34+
public void canAssertThatPdfContainsText() {
35+
PDF pdf = new PDF(new File("src/test/resources/50quickideas.pdf"));
36+
assertThat(pdf).containsExactText("50 Quick Ideas to Improve your User Stories");
37+
}
38+
}
39+
```
40+
2641

2742
## How to start
2843

@@ -32,14 +47,14 @@ If you use **Maven**, add the following dependency to pom.xml:
3247
<dependency>
3348
<groupId>com.codeborne</groupId>
3449
<artifactId>pdf-test</artifactId>
35-
<version>1.5.2</version>
50+
<version>1.6.1</version>
3651
</dependency>
3752
```
3853

3954
If you use **Gradle**, add the following dependency to build.gradle:
4055

4156
```groovy
42-
testCompile 'com.codeborne:pdf-test:1.5.2'
57+
testCompile 'com.codeborne:pdf-test:1.6.1'
4358
```
4459

4560
## How to contribute

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ apply plugin: 'com.github.kt3k.coveralls'
1515

1616
group='com.codeborne'
1717
archivesBaseName = 'pdf-test'
18-
version='1.6.0'
18+
version='1.6.1'
1919

2020
[compileJava, compileTestJava]*.options.collect {options -> options.encoding = 'UTF-8'}
2121
[compileJava, compileTestJava]*.options.collect {options -> options.debug = true}
@@ -31,6 +31,7 @@ dependencies {
3131
implementation group: 'org.hamcrest', name: 'hamcrest-core', version: '1.3', transitive: false
3232
testImplementation group: 'junit', name: 'junit', version: '4.13.2', transitive: false
3333
testImplementation group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3', transitive: false
34+
implementation group: 'org.assertj', name: 'assertj-core', version: '3.19.0', transitive: false
3435
}
3536

3637
repositories{
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.codeborne.pdftest.assertj;
2+
3+
import com.codeborne.pdftest.PDF;
4+
5+
public class Assertions extends org.assertj.core.api.Assertions {
6+
public static PdfAssert assertThat(PDF actual) {
7+
return new PdfAssert(actual);
8+
}
9+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.codeborne.pdftest.assertj;
2+
3+
import com.codeborne.pdftest.PDF;
4+
import com.codeborne.pdftest.matchers.ContainsExactText;
5+
import com.codeborne.pdftest.matchers.ContainsText;
6+
import com.codeborne.pdftest.matchers.ContainsTextCaseInsensitive;
7+
import com.codeborne.pdftest.matchers.DoesNotContainExactText;
8+
import com.codeborne.pdftest.matchers.DoesNotContainText;
9+
import org.assertj.core.api.AbstractAssert;
10+
11+
import static org.hamcrest.MatcherAssert.assertThat;
12+
13+
public class PdfAssert extends AbstractAssert<PdfAssert, PDF> {
14+
public PdfAssert(PDF actual) {
15+
super(actual, PdfAssert.class);
16+
}
17+
18+
public PdfAssert containsText(String substring) {
19+
isNotNull();
20+
assertThat(actual, new ContainsText(substring));
21+
return this;
22+
}
23+
24+
public PdfAssert doesNotContainText(String substring) {
25+
isNotNull();
26+
assertThat(actual, new DoesNotContainText(substring));
27+
return this;
28+
}
29+
30+
public PdfAssert containsExactText(String substring) {
31+
isNotNull();
32+
assertThat(actual, new ContainsExactText(substring));
33+
return this;
34+
}
35+
36+
public PdfAssert doesNotContainExactText(String substring) {
37+
isNotNull();
38+
assertThat(actual, new DoesNotContainExactText(substring));
39+
return this;
40+
}
41+
42+
public PdfAssert containsTextCaseInsensitive(String substring) {
43+
isNotNull();
44+
assertThat(actual, new ContainsTextCaseInsensitive(substring));
45+
return this;
46+
}
47+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 org.assertj.core.api.Assertions.assertThatThrownBy;
10+
11+
public class ContainsExactTextTest {
12+
@Test
13+
public void canAssertThatPdfContainsText() throws IOException {
14+
PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
15+
assertThat(pdf).containsExactText("50 Quick Ideas to Improve your User Stories");
16+
}
17+
18+
@Test
19+
public void shouldBeCaseSensitive() throws IOException {
20+
PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
21+
assertThat(pdf).doesNotContainExactText("50 quick ideas");
22+
}
23+
24+
@Test
25+
public void shouldNotIgnoreWhitespaces() throws IOException {
26+
PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
27+
assertThat(pdf).containsExactText("Gojko Adzic");
28+
assertThat(pdf).doesNotContainExactText("Gojko Adzic");
29+
assertThat(pdf).doesNotContainExactText("\tGojko \t Adzic\n");
30+
assertThat(pdf).doesNotContainExactText("Gojko \u00a0 Adzic\r\n");
31+
}
32+
33+
@Test
34+
public void errorDescription() throws IOException {
35+
PDF pdf = new PDF(getClass().getClassLoader().getResource("minimal.pdf"));
36+
37+
assertThatThrownBy(() -> {
38+
assertThat(pdf).containsExactText("Goodbye word");
39+
})
40+
.isInstanceOf(AssertionError.class)
41+
.hasMessage("\nExpected: a PDF containing \"Goodbye word\"\n but: was \"Hello World\n\"");
42+
}
43+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 org.assertj.core.api.Assertions.assertThatThrownBy;
10+
11+
12+
public class ContainsTextCaseInsensitiveTest {
13+
@Test
14+
public void canAssertThatPdfContainsTextIgnoringCase() throws IOException {
15+
PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
16+
assertThat(pdf).containsTextCaseInsensitive("50 quick ideas to improve your user stories");
17+
assertThat(pdf).containsTextCaseInsensitive("50 quick IDEAS to improve YOUR user STORIES");
18+
}
19+
20+
@Test
21+
public void shouldIgnoreWhitespaces() throws IOException {
22+
PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
23+
assertThat(pdf).containsTextCaseInsensitive("\n\tgOJKO aDZIC \u00a0 ");
24+
}
25+
26+
@Test
27+
public void errorDescription() throws IOException {
28+
PDF pdf = new PDF(getClass().getClassLoader().getResource("minimal.pdf"));
29+
assertThatThrownBy(() -> {
30+
assertThat(pdf).containsTextCaseInsensitive("Goodbye word");
31+
})
32+
.isInstanceOf(AssertionError.class)
33+
.hasMessage("\nExpected: a PDF containing \"Goodbye word\"\n but: was \"Hello World\"");
34+
}
35+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 org.assertj.core.api.Assertions.assertThatThrownBy;
10+
11+
public class ContainsTextTest {
12+
@Test
13+
public void canAssertThatPdfContainsText() throws IOException {
14+
PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
15+
assertThat(pdf).containsText("50 Quick Ideas to Improve your User Stories");
16+
assertThat(pdf).containsText("Gojko Adzic");
17+
assertThat(pdf).containsText("©2013 - 2014 Gojko Adzic");
18+
assertThat(pdf).containsText("#50quickideas");
19+
assertThat(pdf).containsText("https://twitter.com/search?q=#50quickideas");
20+
}
21+
22+
@Test
23+
public void shouldBeCaseSensitive() throws IOException {
24+
PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
25+
assertThat(pdf).doesNotContainText("50 quick ideas");
26+
}
27+
28+
@Test
29+
public void shouldIgnoreWhitespaces() throws IOException {
30+
PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
31+
assertThat(pdf).containsText("Gojko Adzic");
32+
assertThat(pdf).containsText("\tGojko \t Adzic\t");
33+
assertThat(pdf).containsText("Gojko \n Adzic\t\n");
34+
assertThat(pdf).containsText("Gojko \n Adzic\n");
35+
assertThat(pdf).containsText("Gojko\r\n Adzic\r\n");
36+
assertThat(pdf).containsText("Gojko \u00a0 Adzic\r\n");
37+
}
38+
39+
@Test
40+
public void errorDescription() throws IOException {
41+
PDF pdf = new PDF(getClass().getClassLoader().getResource("minimal.pdf"));
42+
assertThatThrownBy(() -> {
43+
assertThat(pdf).containsText("Goodbye word");
44+
})
45+
.isInstanceOf(AssertionError.class)
46+
.hasMessage("\nExpected: a PDF containing \"Goodbye word\"\n but: was \"Hello World\"");
47+
}
48+
}

0 commit comments

Comments
 (0)