diff --git a/engine/src/main/java/pro/verron/officestamper/api/OfficeStamperConfiguration.java b/engine/src/main/java/pro/verron/officestamper/api/OfficeStamperConfiguration.java index 576820b0..13b7e1c8 100644 --- a/engine/src/main/java/pro/verron/officestamper/api/OfficeStamperConfiguration.java +++ b/engine/src/main/java/pro/verron/officestamper/api/OfficeStamperConfiguration.java @@ -3,74 +3,53 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.expression.spel.SpelParserConfiguration; -import pro.verron.officestamper.preset.DefaultingResolver; -import pro.verron.officestamper.preset.PassingResolver; -import pro.verron.officestamper.preset.ThrowingResolver; import java.util.List; import java.util.Map; import java.util.function.Function; public interface OfficeStamperConfiguration { - Logger logger = LoggerFactory.getLogger(OfficeStamperConfiguration.class); - default ExceptionResolver getExceptionResolver(boolean tracing) { - if (isFailOnUnresolvedExpression()) - return new ThrowingResolver(tracing); - if (replaceWithDefaultOnError()) - return new DefaultingResolver(replacementDefault(), tracing); - return new PassingResolver(tracing); - - } - /** * Checks if the failOnUnresolvedExpression flag is set to true or false. * * @return true if failOnUnresolvedExpression is set to true, false otherwise. */ + @Deprecated(since = "2.5", forRemoval = true) boolean isFailOnUnresolvedExpression(); - default boolean replaceWithDefaultOnError() { - return isLeaveEmptyOnExpressionError() || isReplaceUnresolvedExpressions(); - } - - default String replacementDefault() { - return isLeaveEmptyOnExpressionError() - ? "" - : getUnresolvedExpressionsDefaultValue(); - } + /** + * Sets the failOnUnresolvedExpression flag to determine whether unresolved expressions should + * cause an exception to be thrown. + * + * @param failOnUnresolvedExpression flag indicating whether to fail on unresolved expressions + * + * @return the updated OfficeStamperConfiguration object + */ + @Deprecated(since = "2.5", forRemoval = true) + OfficeStamperConfiguration setFailOnUnresolvedExpression(boolean failOnUnresolvedExpression); /** * Determines whether to leave empty on expression error. * * @return true if expression errors are left empty, false otherwise */ - boolean isLeaveEmptyOnExpressionError(); + @Deprecated(since = "2.5", forRemoval = true) boolean isLeaveEmptyOnExpressionError(); /** * Determines whether unresolved expressions in the OfficeStamper configuration should be replaced. * * @return true if unresolved expressions should be replaced, false otherwise. */ - boolean isReplaceUnresolvedExpressions(); + @Deprecated(since = "2.5", forRemoval = true) boolean isReplaceUnresolvedExpressions(); /** * Retrieves the default value for unresolved expressions. * * @return the default value for unresolved expressions */ - String getUnresolvedExpressionsDefaultValue(); - - /** - * Sets the failOnUnresolvedExpression flag to determine whether unresolved expressions should - * cause an exception to be thrown. - * - * @param failOnUnresolvedExpression flag indicating whether to fail on unresolved expressions - * - * @return the updated OfficeStamperConfiguration object - */ - OfficeStamperConfiguration setFailOnUnresolvedExpression(boolean failOnUnresolvedExpression); + @Deprecated(since = "2.5", forRemoval = true) String getUnresolvedExpressionsDefaultValue(); /** * Sets the default value for unresolved expressions in the OfficeStamperConfiguration object. @@ -79,6 +58,7 @@ default String replacementDefault() { * * @return the updated OfficeStamperConfiguration object */ + @Deprecated(since = "2.5", forRemoval = true) OfficeStamperConfiguration unresolvedExpressionsDefaultValue(String unresolvedExpressionsDefaultValue); /** @@ -88,6 +68,7 @@ default String replacementDefault() { * * @return the updated OfficeStamperConfiguration object */ + @Deprecated(since = "2.5", forRemoval = true) OfficeStamperConfiguration replaceUnresolvedExpressions( boolean replaceUnresolvedExpressions ); @@ -99,9 +80,8 @@ OfficeStamperConfiguration replaceUnresolvedExpressions( * * @return the updated OfficeStamperConfiguration object */ - OfficeStamperConfiguration leaveEmptyOnExpressionError( - boolean leaveEmpty - ); + @Deprecated(since = "2.5", forRemoval = true) + OfficeStamperConfiguration leaveEmptyOnExpressionError(boolean leaveEmpty); /** * Exposes an interface to the expression language. @@ -126,8 +106,7 @@ OfficeStamperConfiguration exposeInterfaceToExpressionLanguage( * @return the updated OfficeStamperConfiguration object */ OfficeStamperConfiguration addCommentProcessor( - Class interfaceClass, - Function commentProcessorFactory + Class interfaceClass, Function commentProcessorFactory ); /** @@ -152,9 +131,7 @@ OfficeStamperConfiguration addCommentProcessor( * * @return the updated OfficeStamperConfiguration object */ - OfficeStamperConfiguration setLineBreakPlaceholder( - String lineBreakPlaceholder - ); + OfficeStamperConfiguration setLineBreakPlaceholder(String lineBreakPlaceholder); /** * Retrieves the EvaluationContextConfigurer for configuring the Spring Expression Language (SPEL) EvaluationContext @@ -230,9 +207,7 @@ OfficeStamperConfiguration setSpelParserConfiguration( * * @return the updated OfficeStamperConfiguration instance */ - OfficeStamperConfiguration setResolvers( - List resolvers - ); + OfficeStamperConfiguration setResolvers(List resolvers); /** * Adds an ObjectResolver to the OfficeStamperConfiguration. @@ -241,8 +216,9 @@ OfficeStamperConfiguration setResolvers( * * @return The updated OfficeStamperConfiguration. */ - OfficeStamperConfiguration addResolver( - ObjectResolver resolver - ); + OfficeStamperConfiguration addResolver(ObjectResolver resolver); + + ExceptionResolver getExceptionResolver(); + OfficeStamperConfiguration setExceptionResolver(ExceptionResolver exceptionResolver); } diff --git a/engine/src/main/java/pro/verron/officestamper/core/DocxStamper.java b/engine/src/main/java/pro/verron/officestamper/core/DocxStamper.java index ebdf707f..f1d239d5 100644 --- a/engine/src/main/java/pro/verron/officestamper/core/DocxStamper.java +++ b/engine/src/main/java/pro/verron/officestamper/core/DocxStamper.java @@ -52,7 +52,7 @@ public DocxStamper(OfficeStamperConfiguration configuration) { configuration.getCommentProcessors(), configuration.getPreprocessors(), configuration.getSpelParserConfiguration(), - configuration.getExceptionResolver(logger.isTraceEnabled())); + configuration.getExceptionResolver()); } private DocxStamper( diff --git a/engine/src/main/java/pro/verron/officestamper/core/DocxStamperConfiguration.java b/engine/src/main/java/pro/verron/officestamper/core/DocxStamperConfiguration.java index 48d0bacf..58010c2b 100644 --- a/engine/src/main/java/pro/verron/officestamper/core/DocxStamperConfiguration.java +++ b/engine/src/main/java/pro/verron/officestamper/core/DocxStamperConfiguration.java @@ -1,12 +1,12 @@ package pro.verron.officestamper.core; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.expression.EvaluationContext; import org.springframework.expression.spel.SpelParserConfiguration; import org.springframework.lang.NonNull; import pro.verron.officestamper.api.*; -import pro.verron.officestamper.preset.CommentProcessorFactory; -import pro.verron.officestamper.preset.EvaluationContextConfigurers; -import pro.verron.officestamper.preset.Resolvers; +import pro.verron.officestamper.preset.*; import java.util.ArrayList; import java.util.HashMap; @@ -26,7 +26,7 @@ */ public class DocxStamperConfiguration implements OfficeStamperConfiguration { - + private static final Logger logger = LoggerFactory.getLogger(DocxStamperConfiguration.class); private final Map, Function> commentProcessors = new HashMap<>(); private final List resolvers = new ArrayList<>(); @@ -39,6 +39,7 @@ public class DocxStamperConfiguration private boolean replaceUnresolvedExpressions = false; private String unresolvedExpressionsDefaultValue = null; private SpelParserConfiguration spelParserConfiguration = new SpelParserConfiguration(); + private ExceptionResolver exceptionResolver = computeExceptionResolver(logger.isTraceEnabled()); /** * Creates a new configuration with default values. @@ -61,12 +62,30 @@ public DocxStamperConfiguration() { Resolvers.fallback())); } + /** + * Resets all the comment processors in the configuration. This method clears the + * map of comment processors, effectively removing all registered comment processors. + * Comment processors are used to process comments within the document. + */ + public void resetCommentProcessors() { + this.commentProcessors.clear(); + } + + /** + * Resets all the resolvers in the DocxStamperConfiguration object. + * This method clears the list of resolvers, effectively removing all registered resolvers. + * Resolvers are used to resolve objects during the stamping process. + */ + public void resetResolvers() { + this.resolvers.clear(); + } + /** *

isFailOnUnresolvedExpression.

* * @return a boolean */ - @Override + @Deprecated(since = "2.5", forRemoval = true) @Override public boolean isFailOnUnresolvedExpression() { return failOnUnresolvedExpression; } @@ -80,12 +99,56 @@ public boolean isFailOnUnresolvedExpression() { * * @return a {@link DocxStamperConfiguration} object */ - @Override + @Deprecated(since = "2.5", forRemoval = true) @Override public DocxStamperConfiguration setFailOnUnresolvedExpression(boolean failOnUnresolvedExpression) { this.failOnUnresolvedExpression = failOnUnresolvedExpression; + this.exceptionResolver = computeExceptionResolver(logger.isTraceEnabled()); return this; } + private ExceptionResolver computeExceptionResolver(boolean tracing) { + if (failOnUnresolvedExpression) return new ThrowingResolver(tracing); + if (replaceWithDefaultOnError()) return new DefaultingResolver(replacementDefault(), tracing); + return new PassingResolver(tracing); + } + + private boolean replaceWithDefaultOnError() { + return isLeaveEmptyOnExpressionError() || isReplaceUnresolvedExpressions(); + } + + private String replacementDefault() { + return isLeaveEmptyOnExpressionError() + ? "" + : getUnresolvedExpressionsDefaultValue(); + } + + /** + *

isLeaveEmptyOnExpressionError.

+ * + * @return a boolean + */ + @Override public boolean isLeaveEmptyOnExpressionError() { + return leaveEmptyOnExpressionError; + } + + /** + *

isReplaceUnresolvedExpressions.

+ * + * @return a boolean + */ + @Override public boolean isReplaceUnresolvedExpressions() { + return replaceUnresolvedExpressions; + } + + /** + *

Getter for the field unresolvedExpressionsDefaultValue.

+ * + * @return a {@link String} object + */ + @Override public String getUnresolvedExpressionsDefaultValue() { + return unresolvedExpressionsDefaultValue; + } + /** * Indicates the default value to use for expressions that doesn't resolve. * @@ -95,9 +158,10 @@ public DocxStamperConfiguration setFailOnUnresolvedExpression(boolean failOnUnre * * @see DocxStamperConfiguration#replaceUnresolvedExpressions */ - @Override + @Deprecated(since = "2.5", forRemoval = true) @Override public DocxStamperConfiguration unresolvedExpressionsDefaultValue(String unresolvedExpressionsDefaultValue) { this.unresolvedExpressionsDefaultValue = unresolvedExpressionsDefaultValue; + this.exceptionResolver = computeExceptionResolver(logger.isTraceEnabled()); return this; } @@ -109,9 +173,10 @@ public DocxStamperConfiguration unresolvedExpressionsDefaultValue(String unresol * * @return a {@link DocxStamperConfiguration} object */ - @Override + @Deprecated(since = "2.5", forRemoval = true) @Override public DocxStamperConfiguration replaceUnresolvedExpressions(boolean replaceUnresolvedExpressions) { this.replaceUnresolvedExpressions = replaceUnresolvedExpressions; + this.exceptionResolver = computeExceptionResolver(logger.isTraceEnabled()); return this; } @@ -124,9 +189,10 @@ public DocxStamperConfiguration replaceUnresolvedExpressions(boolean replaceUnre * * @return a {@link DocxStamperConfiguration} object */ - @Override + @Deprecated(since = "2.5", forRemoval = true) @Override public DocxStamperConfiguration leaveEmptyOnExpressionError(boolean leaveEmpty) { this.leaveEmptyOnExpressionError = leaveEmpty; + this.exceptionResolver = computeExceptionResolver(logger.isTraceEnabled()); return this; } @@ -139,8 +205,7 @@ public DocxStamperConfiguration leaveEmptyOnExpressionError(boolean leaveEmpty) * * @return a {@link DocxStamperConfiguration} object */ - @Override - public DocxStamperConfiguration exposeInterfaceToExpressionLanguage( + @Override public DocxStamperConfiguration exposeInterfaceToExpressionLanguage( Class interfaceClass, Object implementation ) { this.expressionFunctions.put(interfaceClass, implementation); @@ -156,10 +221,8 @@ public DocxStamperConfiguration exposeInterfaceToExpressionLanguage( * * @return a {@link DocxStamperConfiguration} object */ - @Override - public DocxStamperConfiguration addCommentProcessor( - Class interfaceClass, - Function commentProcessorFactory + @Override public DocxStamperConfiguration addCommentProcessor( + Class interfaceClass, Function commentProcessorFactory ) { this.commentProcessors.put(interfaceClass, commentProcessorFactory); return this; @@ -170,48 +233,16 @@ public DocxStamperConfiguration addCommentProcessor( * * @param preprocessor the preprocessor to add. */ - @Override - public void addPreprocessor(PreProcessor preprocessor) { + @Override public void addPreprocessor(PreProcessor preprocessor) { preprocessors.add(preprocessor); } - /** - *

isReplaceUnresolvedExpressions.

- * - * @return a boolean - */ - @Override - public boolean isReplaceUnresolvedExpressions() { - return replaceUnresolvedExpressions; - } - - /** - *

isLeaveEmptyOnExpressionError.

- * - * @return a boolean - */ - @Override - public boolean isLeaveEmptyOnExpressionError() { - return leaveEmptyOnExpressionError; - } - - /** - *

Getter for the field unresolvedExpressionsDefaultValue.

- * - * @return a {@link String} object - */ - @Override - public String getUnresolvedExpressionsDefaultValue() { - return unresolvedExpressionsDefaultValue; - } - /** *

Getter for the field lineBreakPlaceholder.

* * @return a {@link String} object */ - @Override - public String getLineBreakPlaceholder() { + @Override public String getLineBreakPlaceholder() { return lineBreakPlaceholder; } @@ -224,8 +255,7 @@ public String getLineBreakPlaceholder() { * * @return the configuration object for chaining. */ - @Override - public DocxStamperConfiguration setLineBreakPlaceholder(@NonNull String lineBreakPlaceholder) { + @Override public DocxStamperConfiguration setLineBreakPlaceholder(@NonNull String lineBreakPlaceholder) { this.lineBreakPlaceholder = lineBreakPlaceholder; return this; } @@ -235,8 +265,7 @@ public DocxStamperConfiguration setLineBreakPlaceholder(@NonNull String lineBrea * * @return a {@link EvaluationContextConfigurer} object */ - @Override - public EvaluationContextConfigurer getEvaluationContextConfigurer() { + @Override public EvaluationContextConfigurer getEvaluationContextConfigurer() { return evaluationContextConfigurer; } @@ -249,8 +278,7 @@ public EvaluationContextConfigurer getEvaluationContextConfigurer() { * * @return a {@link DocxStamperConfiguration} object */ - @Override - public DocxStamperConfiguration setEvaluationContextConfigurer( + @Override public DocxStamperConfiguration setEvaluationContextConfigurer( EvaluationContextConfigurer evaluationContextConfigurer ) { this.evaluationContextConfigurer = evaluationContextConfigurer; @@ -262,8 +290,7 @@ public DocxStamperConfiguration setEvaluationContextConfigurer( * * @return a {@link SpelParserConfiguration} object */ - @Override - public SpelParserConfiguration getSpelParserConfiguration() { + @Override public SpelParserConfiguration getSpelParserConfiguration() { return spelParserConfiguration; } @@ -277,8 +304,7 @@ public SpelParserConfiguration getSpelParserConfiguration() { * * @return a {@link DocxStamperConfiguration} object */ - @Override - public DocxStamperConfiguration setSpelParserConfiguration( + @Override public DocxStamperConfiguration setSpelParserConfiguration( SpelParserConfiguration spelParserConfiguration ) { this.spelParserConfiguration = spelParserConfiguration; @@ -290,8 +316,7 @@ public DocxStamperConfiguration setSpelParserConfiguration( * * @return a {@link Map} object */ - @Override - public Map, Object> getExpressionFunctions() { + @Override public Map, Object> getExpressionFunctions() { return expressionFunctions; } @@ -300,8 +325,7 @@ public Map, Object> getExpressionFunctions() { * * @return a {@link Map} object */ - @Override - public Map, Function> getCommentProcessors() { + @Override public Map, Function> getCommentProcessors() { return commentProcessors; } @@ -310,8 +334,7 @@ public Map, Function> g * * @return a {@link List} object */ - @Override - public List getPreprocessors() { + @Override public List getPreprocessors() { return preprocessors; } @@ -320,8 +343,7 @@ public List getPreprocessors() { * * @return The list of object resolvers. */ - @Override - public List getResolvers() { + @Override public List getResolvers() { return resolvers; } @@ -336,8 +358,7 @@ public List getResolvers() { * * @return The updated DocxStamperConfiguration instance. */ - @Override - public DocxStamperConfiguration setResolvers( + @Override public DocxStamperConfiguration setResolvers( List resolvers ) { this.resolvers.clear(); @@ -354,27 +375,19 @@ public DocxStamperConfiguration setResolvers( * @return The modified `DocxStamperConfiguration` object, with the resolver added to the beginning of the * resolver list. */ - @Override - public DocxStamperConfiguration addResolver(ObjectResolver resolver) { + @Override public DocxStamperConfiguration addResolver(ObjectResolver resolver) { resolvers.add(0, resolver); return this; } - /** - * Resets all the comment processors in the configuration. This method clears the - * map of comment processors, effectively removing all registered comment processors. - * Comment processors are used to process comments within the document. - */ - public void resetCommentProcessors() { - this.commentProcessors.clear(); + @Override public ExceptionResolver getExceptionResolver() { + return exceptionResolver; } - /** - * Resets all the resolvers in the DocxStamperConfiguration object. - * This method clears the list of resolvers, effectively removing all registered resolvers. - * Resolvers are used to resolve objects during the stamping process. - */ - public void resetResolvers() { - this.resolvers.clear(); + @Override public DocxStamperConfiguration setExceptionResolver(ExceptionResolver exceptionResolver) { + this.exceptionResolver = exceptionResolver; + return this; } + + } diff --git a/engine/src/main/java/pro/verron/officestamper/preset/DefaultingResolver.java b/engine/src/main/java/pro/verron/officestamper/preset/DefaultingResolver.java index f9ff4d61..7ee29dfe 100644 --- a/engine/src/main/java/pro/verron/officestamper/preset/DefaultingResolver.java +++ b/engine/src/main/java/pro/verron/officestamper/preset/DefaultingResolver.java @@ -16,12 +16,23 @@ public class DefaultingResolver private final boolean tracing; private final Logger logger = LoggerFactory.getLogger(DefaultingResolver.class); + public DefaultingResolver() { + this(""); + } + + public DefaultingResolver(String defaultValue) { + this(defaultValue, false); + } public DefaultingResolver(String defaultValue, boolean tracing) { this.defaultValue = defaultValue; this.tracing = tracing; } + public DefaultingResolver(boolean tracing) { + this("", tracing); + } + @Override public String resolve(Placeholder placeholder, String message, Exception cause) { if (tracing) logger.warn(message, cause); else logger.warn(message); diff --git a/engine/src/main/java/pro/verron/officestamper/preset/PassingResolver.java b/engine/src/main/java/pro/verron/officestamper/preset/PassingResolver.java index 02942b4b..c24aec8b 100644 --- a/engine/src/main/java/pro/verron/officestamper/preset/PassingResolver.java +++ b/engine/src/main/java/pro/verron/officestamper/preset/PassingResolver.java @@ -15,6 +15,10 @@ public class PassingResolver private final boolean tracing; + public PassingResolver() { + this(false); + } + public PassingResolver(boolean tracing) {this.tracing = tracing;} @Override public String resolve(Placeholder placeholder, String message, Exception cause) { diff --git a/engine/src/main/java/pro/verron/officestamper/preset/ThrowingResolver.java b/engine/src/main/java/pro/verron/officestamper/preset/ThrowingResolver.java index 0d32effa..8a4edbf3 100644 --- a/engine/src/main/java/pro/verron/officestamper/preset/ThrowingResolver.java +++ b/engine/src/main/java/pro/verron/officestamper/preset/ThrowingResolver.java @@ -14,6 +14,10 @@ public class ThrowingResolver implements ExceptionResolver { private final boolean tracing; + public ThrowingResolver() { + this(false); + } + public ThrowingResolver(boolean tracing) {this.tracing = tracing;} @Override public String resolve(Placeholder placeholder, String message, Exception cause) { diff --git a/engine/src/test/java/pro/verron/officestamper/test/DefaultTests.java b/engine/src/test/java/pro/verron/officestamper/test/DefaultTests.java index 1e424e45..1ac5addf 100644 --- a/engine/src/test/java/pro/verron/officestamper/test/DefaultTests.java +++ b/engine/src/test/java/pro/verron/officestamper/test/DefaultTests.java @@ -8,9 +8,7 @@ import org.springframework.context.expression.MapAccessor; import org.springframework.expression.spel.SpelParserConfiguration; import pro.verron.officestamper.api.OfficeStamperConfiguration; -import pro.verron.officestamper.preset.EvaluationContextConfigurers; -import pro.verron.officestamper.preset.OfficeStamperConfigurations; -import pro.verron.officestamper.preset.Resolvers; +import pro.verron.officestamper.preset.*; import java.io.IOException; import java.io.InputStream; @@ -1147,8 +1145,9 @@ private static Arguments expressionReplacementInGlobalParagraphsTest() In this paragraph, the variable name should be resolved to the value Homer Simpson. In this paragraph, the variable foo should not be resolved: ${foo}. """; - OfficeStamperConfiguration config = OfficeStamperConfigurations.standard() - .setFailOnUnresolvedExpression(false); + OfficeStamperConfiguration config = OfficeStamperConfigurations + .standard() + .setExceptionResolver(new PassingResolver()); return arguments("expressionReplacementInGlobalParagraphsTest", config, context, template, expected); } @@ -1180,8 +1179,9 @@ private static Arguments expressionReplacementInTablesTest() { |=== """; - OfficeStamperConfiguration config = OfficeStamperConfigurations.standard() - .setFailOnUnresolvedExpression(false); + var config = OfficeStamperConfigurations + .standard() + .setExceptionResolver(new PassingResolver()); return arguments("expressionReplacementInTablesTest", config, context, template, expected); } @@ -1246,8 +1246,9 @@ private static Arguments expressionReplacementWithCommentTest() { . """; - var config = OfficeStamperConfigurations.standard() - .setFailOnUnresolvedExpression(false); + var config = OfficeStamperConfigurations + .standard() + .setExceptionResolver(new PassingResolver()); return arguments("expressionReplacementWithCommentsTest", config, context, template, expected); } @@ -1293,9 +1294,9 @@ private static Arguments leaveEmptyOnExpressionErrorTest() { Leave me empty . ❬❘u=single❭ """; - var config = OfficeStamperConfigurations.standard() - .setFailOnUnresolvedExpression(false) - .leaveEmptyOnExpressionError(true); + var config = OfficeStamperConfigurations + .standard() + .setExceptionResolver(new DefaultingResolver()); return arguments("leaveEmptyOnExpressionErrorTest", config, context, template, expected); } @@ -1342,11 +1343,9 @@ private static Arguments mapAccessorAndReflectivePropertyAccessorTest_shouldReso """; var config = OfficeStamperConfigurations.standard() - .setFailOnUnresolvedExpression(false) .setLineBreakPlaceholder("\n") .addResolver(Resolvers.nullToDefault("N/C")) - .replaceUnresolvedExpressions(true) - .unresolvedExpressionsDefaultValue("N/C") + .setExceptionResolver(new DefaultingResolver("N/C")) .setEvaluationContextConfigurer(ctx -> ctx.addPropertyAccessor(new MapAccessor())); return arguments("mapAccessorAndReflectivePropertyAccessorTest_shouldResolveMapAndPropertyPlaceholders", @@ -1374,8 +1373,9 @@ private static Arguments nullPointerResolutionTest_testWithDefaultSpel() { """; - var config = OfficeStamperConfigurations.standard() - .setFailOnUnresolvedExpression(false); + var config = OfficeStamperConfigurations + .standard() + .setExceptionResolver(new PassingResolver()); return arguments("nullPointerResolutionTest_testWithDefaultSpel", config, context, template, expected); } diff --git a/engine/src/test/java/pro/verron/officestamper/test/FailOnUnresolvedPlaceholderTest.java b/engine/src/test/java/pro/verron/officestamper/test/FailOnUnresolvedPlaceholderTest.java index c7e667ba..3a50d82d 100644 --- a/engine/src/test/java/pro/verron/officestamper/test/FailOnUnresolvedPlaceholderTest.java +++ b/engine/src/test/java/pro/verron/officestamper/test/FailOnUnresolvedPlaceholderTest.java @@ -2,6 +2,8 @@ import org.junit.jupiter.api.Test; import pro.verron.officestamper.api.OfficeStamperException; +import pro.verron.officestamper.preset.PassingResolver; +import pro.verron.officestamper.preset.ThrowingResolver; import java.io.IOException; import java.io.InputStream; @@ -24,7 +26,7 @@ void fails() var context = new Name("Homer"); try (var template = getResource("FailOnUnresolvedExpressionTest.docx")) { var config = standard() - .setFailOnUnresolvedExpression(true); + .setExceptionResolver(new ThrowingResolver()); var stamper = new TestDocxStamper<>(config); assertThrows(OfficeStamperException.class, () -> stamper.stampAndLoad(template, context)); @@ -40,7 +42,7 @@ void doesNotFail() "FailOnUnresolvedExpressionTest.docx")) ) { var config = standard() - .setFailOnUnresolvedExpression(false); + .setExceptionResolver(new PassingResolver()); var stamper = new TestDocxStamper<>(config); assertDoesNotThrow(() -> stamper.stampAndLoad(template, context)); } diff --git a/engine/src/test/java/pro/verron/officestamper/test/NullPointerResolutionTest.java b/engine/src/test/java/pro/verron/officestamper/test/NullPointerResolutionTest.java index 0e7fe797..f8285fa9 100644 --- a/engine/src/test/java/pro/verron/officestamper/test/NullPointerResolutionTest.java +++ b/engine/src/test/java/pro/verron/officestamper/test/NullPointerResolutionTest.java @@ -17,9 +17,15 @@ class NullPointerResolutionTest { @Test void nullPointerResolutionTest_testThrowingCase() throws IOException { - var subContext = new SubContext("Fullish2", - List.of("Fullish3", "Fullish4", "Fullish5")); - var context = new NullishContext("Fullish1", subContext, null, null); + var context = new NullishContext( + "Fullish1", + new SubContext( + "Fullish2", + List.of("Fullish3", "Fullish4", "Fullish5") + ), + null, + null + ); try (var template = getResource("NullPointerResolution.docx")) { var configuration = OfficeStamperConfigurations.standard(); var stamper = new TestDocxStamper(configuration); diff --git a/engine/src/test/java/pro/verron/officestamper/test/PlaceholderReplacementInHeaderAndFooterTest.java b/engine/src/test/java/pro/verron/officestamper/test/PlaceholderReplacementInHeaderAndFooterTest.java index eef39eb8..f530b6dd 100644 --- a/engine/src/test/java/pro/verron/officestamper/test/PlaceholderReplacementInHeaderAndFooterTest.java +++ b/engine/src/test/java/pro/verron/officestamper/test/PlaceholderReplacementInHeaderAndFooterTest.java @@ -2,6 +2,8 @@ import org.junit.jupiter.api.Test; import pro.verron.officestamper.preset.Image; +import pro.verron.officestamper.preset.OfficeStamperConfigurations; +import pro.verron.officestamper.preset.PassingResolver; import java.nio.file.Path; @@ -20,9 +22,9 @@ class PlaceholderReplacementInHeaderAndFooterTest { void expressionReplacementInHeaderAndFooterTest() { var context = new Name("Homer Simpson", getImage(Path.of("butterfly.png"))); var template = getResource("ExpressionReplacementInHeaderAndFooterTest.docx"); - var configuration = standard() - .setFailOnUnresolvedExpression(false); - var stamper = new TestDocxStamper(configuration); + var config = standard() + .setExceptionResolver(new PassingResolver()); + var stamper = new TestDocxStamper(config); var actual = stamper.stampAndLoadAndExtract(template, context); assertEquals(""" [header, name="/word/header2.xml"] diff --git a/engine/src/test/java/pro/verron/officestamper/test/PlaceholderReplacementInTextBoxesTest.java b/engine/src/test/java/pro/verron/officestamper/test/PlaceholderReplacementInTextBoxesTest.java index f467fd75..5632b130 100644 --- a/engine/src/test/java/pro/verron/officestamper/test/PlaceholderReplacementInTextBoxesTest.java +++ b/engine/src/test/java/pro/verron/officestamper/test/PlaceholderReplacementInTextBoxesTest.java @@ -1,6 +1,8 @@ package pro.verron.officestamper.test; import org.junit.jupiter.api.Test; +import pro.verron.officestamper.preset.OfficeStamperConfigurations; +import pro.verron.officestamper.preset.PassingResolver; import static org.junit.jupiter.api.Assertions.assertEquals; import static pro.verron.officestamper.preset.OfficeStamperConfigurations.standard; @@ -16,9 +18,9 @@ class PlaceholderReplacementInTextBoxesTest { void expressionReplacementInTextBoxesTest() { var context = new Name("Bart Simpson"); var template = getResource("ExpressionReplacementInTextBoxesTest.docx"); - var configuration = standard() - .setFailOnUnresolvedExpression(false); - var stamper = new TestDocxStamper(configuration); + var config = standard() + .setExceptionResolver(new PassingResolver()); + var stamper = new TestDocxStamper(config); var actual = stamper.stampAndLoadAndExtract(template, context); String expected = """ Expression Replacement in TextBoxes diff --git a/engine/src/test/java/pro/verron/officestamper/test/ResolutionTest.java b/engine/src/test/java/pro/verron/officestamper/test/ResolutionTest.java index f89205c3..75bf4da2 100644 --- a/engine/src/test/java/pro/verron/officestamper/test/ResolutionTest.java +++ b/engine/src/test/java/pro/verron/officestamper/test/ResolutionTest.java @@ -4,9 +4,13 @@ import org.junit.jupiter.api.function.ThrowingSupplier; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; +import pro.verron.officestamper.api.ExceptionResolver; import pro.verron.officestamper.api.OfficeStamperException; import pro.verron.officestamper.api.StringResolver; +import pro.verron.officestamper.preset.DefaultingResolver; import pro.verron.officestamper.preset.OfficeStamperConfigurations; +import pro.verron.officestamper.preset.PassingResolver; +import pro.verron.officestamper.preset.ThrowingResolver; import java.nio.file.Path; @@ -50,10 +54,14 @@ void testStaticResolution( var resource = getResource(Path.of(template)); var configuration = OfficeStamperConfigurations.standard(); - configuration.setFailOnUnresolvedExpression(shouldFail); - configuration.leaveEmptyOnExpressionError(emptyOnError); - configuration.replaceUnresolvedExpressions(shouldReplace); - configuration.unresolvedExpressionsDefaultValue(replacementValue); + configuration.setExceptionResolver( + computeExceptionResolver( + shouldFail, + emptyOnError, + shouldReplace, + replacementValue + ) + ); var stamper = new TestDocxStamper<>(configuration); if (shouldFail) { @@ -66,6 +74,18 @@ void testStaticResolution( } } + private ExceptionResolver computeExceptionResolver( + boolean shouldFail, + boolean emptyOnError, + boolean shouldReplace, + String replacementValue + ) { + if (shouldFail) return new ThrowingResolver(); + if (emptyOnError) return new DefaultingResolver(""); + if (shouldReplace) return new DefaultingResolver(replacementValue); + return new PassingResolver(); + } + /** * This method is a unit test for the `testCustomResolution` method. It uses parameterized testing with a CSV source * to test various scenarios. @@ -135,12 +155,16 @@ void testCustomResolution( var resource = getResource(Path.of(template)); var configuration = OfficeStamperConfigurations.raw(); - configuration.setFailOnUnresolvedExpression(shouldFail); - configuration.leaveEmptyOnExpressionError(emptyOnError); - configuration.replaceUnresolvedExpressions(shouldReplace); + configuration.setExceptionResolver( + computeExceptionResolver( + shouldFail, + emptyOnError, + shouldReplace, + unresolvedReplacementValue + ) + ); if (withResolver) configuration.addResolver(new CustomResolver()); CustomContext customContext = new CustomContext(withValue ? new CustomValue() : null); - configuration.unresolvedExpressionsDefaultValue(unresolvedReplacementValue); var stamper = new TestDocxStamper<>(configuration); if (expectedFail) {