diff --git a/build.gradle b/build.gradle index 4e03dfb0..849b2980 100644 --- a/build.gradle +++ b/build.gradle @@ -2,12 +2,12 @@ plugins { id 'checkstyle' id 'java' id 'maven-publish' - id "org.owasp.dependencycheck" version "6.2.2" + id "org.owasp.dependencycheck" version "6.3.2" id "org.sonarqube" version "3.3" } group 'com.docu-tools' -version = '1.2.4' +version = '1.2.6' sourceCompatibility = 17 targetCompatibility = 17 @@ -43,7 +43,7 @@ dependencies { testImplementation('org.junit.jupiter:junit-jupiter:5.8.1') testImplementation("org.hamcrest:hamcrest:2.2") - testImplementation("com.docu-tools:poipath:1.0.0") + testImplementation("com.docu-tools:poipath:1.1.1") } tasks.withType(Checkstyle) { @@ -56,8 +56,8 @@ tasks.withType(Checkstyle) { dependencyCheck { format = 'ALL' - junitFailOnCVSS = 7.0 - failBuildOnCVSS = 9.0 + junitFailOnCVSS = 6.0 + failBuildOnCVSS = 6.0 suppressionFile = 'config/dependency-check/suppression.xml' } diff --git a/src/test/java/com/docutools/jocument/impl/word/WordGeneratorTest.java b/src/test/java/com/docutools/jocument/impl/word/WordGeneratorTest.java index 7a58e32b..74715893 100644 --- a/src/test/java/com/docutools/jocument/impl/word/WordGeneratorTest.java +++ b/src/test/java/com/docutools/jocument/impl/word/WordGeneratorTest.java @@ -1,26 +1,29 @@ package com.docutools.jocument.impl.word; -import com.docutools.jocument.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +import com.docutools.jocument.CustomPlaceholderRegistry; +import com.docutools.jocument.Document; +import com.docutools.jocument.PlaceholderResolver; +import com.docutools.jocument.Template; +import com.docutools.jocument.TestUtils; import com.docutools.jocument.impl.CustomPlaceholderRegistryImpl; import com.docutools.jocument.impl.ReflectionResolver; import com.docutools.jocument.sample.model.SampleModelData; import com.docutools.jocument.sample.placeholders.QuotePlaceholder; -import com.docutools.poipath.PoiPath; -import org.apache.poi.xwpf.usermodel.XWPFDocument; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Tag; -import org.junit.jupiter.api.Test; - +import com.docutools.poipath.xwpf.XWPFDocumentWrapper; import java.io.IOException; import java.time.LocalDate; import java.time.Period; import java.time.format.DateTimeFormatter; import java.util.Locale; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; +import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; @DisplayName("Word Generator Tests") @Tag("automated") @@ -40,7 +43,7 @@ void cleanup() throws IOException { void shouldGenerateSimpleDocument() throws InterruptedException, IOException { // Arrange Template template = Template.fromClassPath("/templates/word/UserProfileTemplate.docx") - .orElseThrow(); + .orElseThrow(); PlaceholderResolver resolver = new ReflectionResolver(SampleModelData.PICARD_PERSON); // Act @@ -50,15 +53,15 @@ void shouldGenerateSimpleDocument() throws InterruptedException, IOException { // Assert assertThat(document.completed(), is(true)); xwpfDocument = TestUtils.getXWPFDocumentFromDocument(document); - var documentWrapper = PoiPath.xwpf(xwpfDocument); - assertThat(documentWrapper.paragraph(0).text(), equalTo("User Profile: Jean-Luc Picard")); - assertThat(documentWrapper.paragraph(2).text(), equalTo("Name: Jean-Luc")); - assertThat(documentWrapper.paragraph(3).text(), equalTo("Last Name: Picard")); - assertThat(documentWrapper.paragraph(4).text(), equalTo("Age: " - + Period.between(LocalDate.of(1948, 9, 23), LocalDate.now()).getYears() - + " (" - + DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.US).format(LocalDate.of(1948, 9, 23)) - + ")")); + var documentWrapper = new XWPFDocumentWrapper(xwpfDocument); + assertThat(documentWrapper.bodyElement(0).asParagraph().text(), equalTo("User Profile: Jean-Luc Picard")); + assertThat(documentWrapper.bodyElement(2).asParagraph().text(), equalTo("Name: Jean-Luc")); + assertThat(documentWrapper.bodyElement(3).asParagraph().text(), equalTo("Last Name: Picard")); + assertThat(documentWrapper.bodyElement(4).asParagraph().text(), equalTo("Age: " + + Period.between(LocalDate.of(1948, 9, 23), LocalDate.now()).getYears() + + " (" + + DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.US).format(LocalDate.of(1948, 9, 23)) + + ")")); } @Test @@ -66,7 +69,7 @@ void shouldGenerateSimpleDocument() throws InterruptedException, IOException { void shouldGenerateMultiLocaleDocument() throws InterruptedException, IOException { // Arrange Template template = Template.fromClassPath("/templates/word/UserProfileTemplateWithDifferentLocales.docx") - .orElseThrow(); + .orElseThrow(); PlaceholderResolver resolver = new ReflectionResolver(SampleModelData.PICARD_PERSON); // Act @@ -76,20 +79,20 @@ void shouldGenerateMultiLocaleDocument() throws InterruptedException, IOExceptio // Assert assertThat(document.completed(), is(true)); xwpfDocument = TestUtils.getXWPFDocumentFromDocument(document); - var documentWrapper = PoiPath.xwpf(xwpfDocument); - assertThat(documentWrapper.paragraph(0).text(), equalTo("User Profile: Jean-Luc Picard")); - assertThat(documentWrapper.paragraph(2).text(), equalTo("Name: Jean-Luc")); - assertThat(documentWrapper.paragraph(3).text(), equalTo("Last Name: Picard")); - assertThat(documentWrapper.paragraph(4).text(), equalTo("Age German: " - + Period.between(LocalDate.of(1948, 9, 23), LocalDate.now()).getYears() - + " (" - + DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.US).format(LocalDate.of(1948, 9, 23)) - + ")")); - assertThat(documentWrapper.paragraph(5).text(), equalTo("Age English: " - + Period.between(LocalDate.of(1948, 9, 23), LocalDate.now()).getYears() - + " (" - + DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.US).format(LocalDate.of(1948, 9, 23)) - + ")")); + var documentWrapper = new XWPFDocumentWrapper(xwpfDocument); + assertThat(documentWrapper.bodyElement(0).asParagraph().text(), equalTo("User Profile: Jean-Luc Picard")); + assertThat(documentWrapper.bodyElement(2).asParagraph().text(), equalTo("Name: Jean-Luc")); + assertThat(documentWrapper.bodyElement(3).asParagraph().text(), equalTo("Last Name: Picard")); + assertThat(documentWrapper.bodyElement(4).asParagraph().text(), equalTo("Age German: " + + Period.between(LocalDate.of(1948, 9, 23), LocalDate.now()).getYears() + + " (" + + DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.US).format(LocalDate.of(1948, 9, 23)) + + ")")); + assertThat(documentWrapper.bodyElement(5).asParagraph().text(), equalTo("Age English: " + + Period.between(LocalDate.of(1948, 9, 23), LocalDate.now()).getYears() + + " (" + + DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.US).format(LocalDate.of(1948, 9, 23)) + + ")")); } @Test @@ -97,7 +100,7 @@ void shouldGenerateMultiLocaleDocument() throws InterruptedException, IOExceptio void shouldReplacePlaceholdersInTables() throws InterruptedException, IOException { // Arrange Template template = Template.fromClassPath("/templates/word/UserProfileWithTableTemplate.docx") - .orElseThrow(); + .orElseThrow(); PlaceholderResolver resolver = new ReflectionResolver(SampleModelData.PICARD_PERSON); // Act @@ -107,8 +110,8 @@ void shouldReplacePlaceholdersInTables() throws InterruptedException, IOExceptio // Assert assertThat(document.completed(), is(true)); xwpfDocument = TestUtils.getXWPFDocumentFromDocument(document); - var documentWrapper = PoiPath.xwpf(xwpfDocument); - var table = documentWrapper.table(0); + var documentWrapper = new XWPFDocumentWrapper(xwpfDocument); + var table = documentWrapper.bodyElement(0).asTable(); var birthdate = DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.US).format(LocalDate.of(1948, 9, 23)); assertThat(table.row(0).cell(0).bodyElement(0).asParagraph().text(), equalTo("Name")); assertThat(table.row(0).cell(1).bodyElement(0).asParagraph().text(), equalTo("Value")); @@ -123,7 +126,7 @@ void shouldReplacePlaceholdersInTables() throws InterruptedException, IOExceptio void shouldResolveCollectionPlaceholders() throws InterruptedException, IOException { // Arrange Template template = Template.fromClassPath("/templates/word/CollectionsTemplate.docx") - .orElseThrow(); + .orElseThrow(); PlaceholderResolver resolver = new ReflectionResolver(SampleModelData.PICARD); // Act @@ -133,19 +136,19 @@ void shouldResolveCollectionPlaceholders() throws InterruptedException, IOExcept // Assert assertThat(document.completed(), is(true)); xwpfDocument = TestUtils.getXWPFDocumentFromDocument(document); - var documentWrapper = PoiPath.xwpf(xwpfDocument); - assertThat(documentWrapper.paragraph(0).text(), equalTo("Captain: Jean-Luc Picard")); - assertThat(documentWrapper.paragraph(2).text(), equalTo("First Officer")); - assertThat(documentWrapper.table(3).row(0).cell(0).bodyElement(0).asParagraph().text(), equalTo("Name")); - assertThat(documentWrapper.table(3).row(0).cell(1).bodyElement(0).asParagraph().text(), equalTo("Rank")); - assertThat(documentWrapper.table(3).row(0).cell(2).bodyElement(0).asParagraph().text(), equalTo("Uniform")); - assertThat(documentWrapper.table(3).row(1).cell(0).bodyElement(0).asParagraph().text(), equalTo("Riker")); - assertThat(documentWrapper.table(3).row(1).cell(1).bodyElement(0).asParagraph().text(), equalTo("3")); - assertThat(documentWrapper.table(3).row(1).cell(2).bodyElement(0).asParagraph().text(), equalTo("Red")); - assertThat(documentWrapper.paragraph(6).text(), equalTo("Services")); - assertThat(documentWrapper.paragraph(8).text(), equalTo("USS Enterprise")); - assertThat(documentWrapper.paragraph(9).text(), equalTo("US Defiant")); - assertThat(documentWrapper.paragraph(11).text(), equalTo("And that’s that.")); + var documentWrapper = new XWPFDocumentWrapper(xwpfDocument); + assertThat(documentWrapper.bodyElement(0).asParagraph().text(), equalTo("Captain: Jean-Luc Picard")); + assertThat(documentWrapper.bodyElement(2).asParagraph().text(), equalTo("First Officer")); + assertThat(documentWrapper.bodyElement(3).asTable().row(0).cell(0).bodyElement(0).asParagraph().text(), equalTo("Name")); + assertThat(documentWrapper.bodyElement(3).asTable().row(0).cell(1).bodyElement(0).asParagraph().text(), equalTo("Rank")); + assertThat(documentWrapper.bodyElement(3).asTable().row(0).cell(2).bodyElement(0).asParagraph().text(), equalTo("Uniform")); + assertThat(documentWrapper.bodyElement(3).asTable().row(1).cell(0).bodyElement(0).asParagraph().text(), equalTo("Riker")); + assertThat(documentWrapper.bodyElement(3).asTable().row(1).cell(1).bodyElement(0).asParagraph().text(), equalTo("3")); + assertThat(documentWrapper.bodyElement(3).asTable().row(1).cell(2).bodyElement(0).asParagraph().text(), equalTo("Red")); + assertThat(documentWrapper.bodyElement(6).asParagraph().text(), equalTo("Services")); + assertThat(documentWrapper.bodyElement(8).asParagraph().text(), equalTo("USS Enterprise")); + assertThat(documentWrapper.bodyElement(9).asParagraph().text(), equalTo("US Defiant")); + assertThat(documentWrapper.bodyElement(11).asParagraph().text(), equalTo("And that’s that.")); } @Test @@ -163,8 +166,8 @@ void shouldApplyCustomWordPlaceholder() throws InterruptedException, IOException // Assert assertThat(document.completed(), is(true)); xwpfDocument = TestUtils.getXWPFDocumentFromDocument(document); - var documentWrapper = PoiPath.xwpf(xwpfDocument); - assertThat(documentWrapper.paragraph(2).run(0).pictures().size(), equalTo(1)); + var documentWrapper = new XWPFDocumentWrapper(xwpfDocument); + assertThat(documentWrapper.bodyElement(2).asParagraph().run(0).pictures().size(), equalTo(1)); } @Test @@ -184,7 +187,8 @@ void shouldApplyForeignCustomWordPlaceholder() throws InterruptedException, IOEx // Assert assertThat(document.completed(), is(true)); xwpfDocument = TestUtils.getXWPFDocumentFromDocument(document); - var documentWrapper = PoiPath.xwpf(xwpfDocument); - assertThat(documentWrapper.paragraph(0).run(0).text(), equalTo("Live your life not celebrating victories, but overcoming defeats.")); + var documentWrapper = new XWPFDocumentWrapper(xwpfDocument); + assertThat(documentWrapper.bodyElement(0).asParagraph().run(0).text(), + equalTo("Live your life not celebrating victories, but overcoming defeats.")); } } \ No newline at end of file