Skip to content

Commit

Permalink
Merge branch 'master' into feature/archive_action_results
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonOellerer committed Oct 4, 2021
2 parents ab82120 + f4fbd91 commit ef2f54e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 63 deletions.
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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'
}

Expand Down
120 changes: 62 additions & 58 deletions src/test/java/com/docutools/jocument/impl/word/WordGeneratorTest.java
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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
Expand All @@ -50,23 +53,23 @@ 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
@DisplayName("Generate a document from a template with multiple locales.")
void shouldGenerateMultiLocaleDocument() throws InterruptedException, IOException {
// Arrange
Template template = Template.fromClassPath("/templates/word/UserProfileTemplateWithDifferentLocales.docx")
.orElseThrow();
.orElseThrow();
PlaceholderResolver resolver = new ReflectionResolver(SampleModelData.PICARD_PERSON);

// Act
Expand All @@ -76,28 +79,28 @@ 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
@DisplayName("Replace placeholders in tables.")
void shouldReplacePlaceholdersInTables() throws InterruptedException, IOException {
// Arrange
Template template = Template.fromClassPath("/templates/word/UserProfileWithTableTemplate.docx")
.orElseThrow();
.orElseThrow();
PlaceholderResolver resolver = new ReflectionResolver(SampleModelData.PICARD_PERSON);

// Act
Expand All @@ -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"));
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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."));
}
}

0 comments on commit ef2f54e

Please sign in to comment.