ExceptionResolvers.java

1
package pro.verron.officestamper.preset;
2
3
import org.slf4j.Logger;
4
import org.slf4j.LoggerFactory;
5
import pro.verron.officestamper.api.ExceptionResolver;
6
import pro.verron.officestamper.api.OfficeStamperException;
7
import pro.verron.officestamper.api.Placeholder;
8
9
10
/**
11
 * The ExceptionResolvers class provides a set of static factory methods to create different types of ExceptionResolver
12
 * implementations.
13
 * These resolvers are designed to handle exceptions that occur during the processing of placeholders in text
14
 * documents.
15
 * This class is a utility class and cannot be instantiated.
16
 */
17
public class ExceptionResolvers {
18
19
    private static final Logger logger = LoggerFactory.getLogger(ExceptionResolvers.class);
20
21
    static {
22
        if (!logger.isTraceEnabled())
23
            logger.info("Set TRACE log level, to add stacktrace info to resolution exceptions");
24
    }
25
26
    private ExceptionResolvers() {
27
        throw new OfficeStamperException("Utility class");
28
    }
29
30
    /**
31
     * The passing resolver will handle exceptions by returning the placeholder expression.
32
     * It logs the exception message, and the stack trace if tracing is enabled.
33
     */
34
    public static ExceptionResolver passing() {
35
        return new PassingResolver(logger.isTraceEnabled());
36
    }
37
38
    /**
39
     * The defaulting resolver class will handle exceptions by returning an empty string.
40
     * It logs the exception message, and the stack trace if tracing is enabled.
41
     */
42
    public static ExceptionResolver defaulting() {
43
        return new DefaultingResolver("", logger.isTraceEnabled());
44
    }
45
46
    /**
47
     * The defaulting resolver class will handle exceptions by returning a default value.
48
     * It logs the exception message, and the stack trace if tracing is enabled.
49
     */
50
    public static ExceptionResolver defaulting(String value) {
51
        return new DefaultingResolver(value, logger.isTraceEnabled());
52
    }
53
54
    /**
55
     * The throwing resolver will handle exceptions by immediately throwing an OfficeStamperException.
56
     * It is used to propagate errors encountered during the processing of placeholders in text documents.
57
     */
58
    public static ExceptionResolver throwing() {
59
        return new ThrowingResolver(logger.isTraceEnabled());
60
    }
61
62
    @Deprecated(since="2.5", forRemoval = true)
63
    public static ExceptionResolver legacyBehavior(
64
            boolean shouldFail,
65
            boolean emptyOnError,
66
            boolean shouldReplace,
67
            String replacementValue
68
    ) {
69
        if (shouldFail) return new ThrowingResolver(logger.isTraceEnabled());
70
        if (emptyOnError) return new DefaultingResolver("", logger.isTraceEnabled());
71
        if (shouldReplace) return new DefaultingResolver(replacementValue, logger.isTraceEnabled());
72
        return new PassingResolver(logger.isTraceEnabled());
73
    }
74
75
    private record DefaultingResolver(String value, boolean tracing)
76
            implements ExceptionResolver {
77
78
        private static final Logger logger = LoggerFactory.getLogger(DefaultingResolver.class);
79
80
        @Override public String resolve(Placeholder placeholder, String message, Exception cause) {
81
            if (tracing) logger.warn(message, cause);
82
            else logger.warn(message);
83 1 1. resolve : replaced return value with "" for pro/verron/officestamper/preset/ExceptionResolvers$DefaultingResolver::resolve → KILLED
            return value;
84
        }
85
    }
86
87
    private record PassingResolver(boolean tracing)
88
            implements ExceptionResolver {
89
90
        @Override public String resolve(Placeholder placeholder, String message, Exception cause) {
91
            if (tracing) logger.warn(message, cause);
92
            else logger.warn(message);
93 1 1. resolve : replaced return value with "" for pro/verron/officestamper/preset/ExceptionResolvers$PassingResolver::resolve → KILLED
            return placeholder.expression();
94
        }
95
    }
96
97
    private record ThrowingResolver(boolean tracing)
98
            implements ExceptionResolver {
99
100
        @Override public String resolve(Placeholder placeholder, String message, Exception cause) {
101 1 1. resolve : negated conditional → SURVIVED
            if (tracing) throw new OfficeStamperException(message, cause);
102
            else throw new OfficeStamperException(message);
103
        }
104
    }
105
}

Mutations

83

1.1
Location : resolve
Killed by : pro.verron.officestamper.test.ResolutionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ResolutionTest]/[test-template:testCustomResolution(java.lang.String, boolean, boolean, boolean, boolean, boolean, java.lang.String, boolean, java.lang.String)]/[test-template-invocation:#6]
replaced return value with "" for pro/verron/officestamper/preset/ExceptionResolvers$DefaultingResolver::resolve → KILLED

93

1.1
Location : resolve
Killed by : pro.verron.officestamper.test.ResolutionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ResolutionTest]/[test-template:testCustomResolution(java.lang.String, boolean, boolean, boolean, boolean, boolean, java.lang.String, boolean, java.lang.String)]/[test-template-invocation:#3]
replaced return value with "" for pro/verron/officestamper/preset/ExceptionResolvers$PassingResolver::resolve → KILLED

101

1.1
Location : resolve
Killed by : none
negated conditional → SURVIVED
Covering tests

Active mutators

Tests examined


Report generated by PIT 1.17.1