Skip to content

Commit

Permalink
Refactor and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonOellerer committed Jan 28, 2022
1 parent 7a39e3e commit 706516e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
/**
* Takes a {@link Object} of any type and resolves placeholder names with reflective access to its type.
*
* @author codecitizen
* @see PlaceholderResolver
* @since 2020-02-19
* @author AntonOellerer
* @see ReflectionResolver
* @since 2021-12-21
*/
public class FutureReflectionResolver extends ReflectionResolver {
private static final Logger logger = LogManager.getLogger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ private static void setup() {
var path = Path.of(pathString);
var file = path.toFile();
if (file.exists() && file.canRead()) {
try {
try (var reader = new BufferedReader(new FileReader(file))) {
logger.info("Parsing mappings");
var reader = new BufferedReader(new FileReader(file));
placeholderMappings = reader.lines()
.filter(line -> !line.isEmpty())
.filter(line -> !line.startsWith("//"))
Expand All @@ -50,7 +49,7 @@ private static void setup() {
logger.error("Mappings file {} can not be read", pathString);
}
} else {
logger.info("No mapper found");
logger.info("Path to mapping file is null");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.docutools.jocument.TestUtils;
import com.docutools.jocument.impl.CustomPlaceholderRegistryImpl;
import com.docutools.jocument.impl.FutureReflectionResolver;
import com.docutools.jocument.impl.PlaceholderMapperImpl;
import com.docutools.jocument.impl.ReflectionResolver;
import com.docutools.jocument.sample.model.SampleModelData;
import com.docutools.jocument.sample.placeholders.QuotePlaceholder;
Expand Down Expand Up @@ -193,7 +194,6 @@ void shouldApplyForeignCustomWordPlaceholder() throws InterruptedException, IOEx
equalTo("Live your life not celebrating victories, but overcoming defeats."));
}


@Test
@DisplayName("Resolve legacy placeholder")
void shouldResolveLegacy() throws IOException, InterruptedException {
Expand Down Expand Up @@ -224,6 +224,40 @@ void shouldResolveLegacy() throws IOException, InterruptedException {
assertThat(documentWrapper.bodyElement(11).asParagraph().text(), equalTo("And that’s that."));
}

@Test
@DisplayName("Do not fail when placeholder mapping can not be opened.")
void shouldNotFailOnUnavailableMappingFile() throws InterruptedException {
// Arrange
PlaceholderMapperImpl.configure("/does/not/exist");
Template template = Template.fromClassPath("/templates/word/UserProfileTemplate.docx")
.orElseThrow();
PlaceholderResolver resolver = new ReflectionResolver(SampleModelData.PICARD_PERSON);

// Act
Document document = template.startGeneration(resolver);
document.blockUntilCompletion(60000L); // 1 minute

// Assert
assertThat(document.completed(), is(true));
}

@Test
@DisplayName("Do not fail when placeholder mapping can not be opened.")
void shouldNotFailOnNullMappingFile() throws InterruptedException {
// Arrange
PlaceholderMapperImpl.configure(null);
Template template = Template.fromClassPath("/templates/word/UserProfileTemplate.docx")
.orElseThrow();
PlaceholderResolver resolver = new ReflectionResolver(SampleModelData.PICARD_PERSON);

// Act
Document document = template.startGeneration(resolver);
document.blockUntilCompletion(60000L); // 1 minute

// Assert
assertThat(document.completed(), is(true));
}

@Test
@DisplayName("Resolve future placeholder")
void shouldResolveFuture() throws IOException, InterruptedException {
Expand Down
5 changes: 5 additions & 0 deletions src/test/resources/mappings.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//Captain Data
captain-name:name

// Officer Data
officer-name:name
officer-rank:rank
officer-uniform:uniform

// Ship Data
service-ship-name:shipName

0 comments on commit 706516e

Please sign in to comment.