MapAccessor.java

1
package pro.verron.officestamper.preset;
2
3
import org.springframework.expression.AccessException;
4
import org.springframework.expression.EvaluationContext;
5
import org.springframework.expression.PropertyAccessor;
6
import org.springframework.expression.TypedValue;
7
import org.springframework.lang.Nullable;
8
import org.springframework.util.Assert;
9
10
import java.util.Map;
11
12
/// MapAccessor is an implementation of the [PropertyAccessor] interface,
13
/// designed for accessing and manipulating properties specifically on Map objects.
14
/// It provides functionality to read and write entries in a Map based on the
15
/// property name provided.
16
public class MapAccessor
17
        implements PropertyAccessor {
18
19
    /// Constructs a new instance of `MapAccessor`.
20
    public MapAccessor(){
21
        // Explicit default constructor for Javadoc
22
    }
23
24
    @Override
25
    public Class<?>[] getSpecificTargetClasses() {
26 1 1. getSpecificTargetClasses : replaced return value with null for pro/verron/officestamper/preset/MapAccessor::getSpecificTargetClasses → SURVIVED
        return new Class<?>[]{Map.class};
27
    }
28
29
    @Override
30
    public boolean canRead(EvaluationContext context, @Nullable Object target, String name) {
31 3 1. canRead : replaced boolean return with true for pro/verron/officestamper/preset/MapAccessor::canRead → SURVIVED
2. canRead : negated conditional → KILLED
3. canRead : negated conditional → KILLED
        return (target instanceof Map<?, ?> map && map.containsKey(name));
32
    }
33
34
    @Override
35
    public TypedValue read(EvaluationContext context, @Nullable Object target, String name)
36
            throws AccessException {
37 1 1. read : removed call to org/springframework/util/Assert::state → SURVIVED
        Assert.state(target instanceof Map, "Target must be of type Map");
38
        Map<?, ?> map = (Map<?, ?>) target;
39
        Object value = map.get(name);
40 2 1. read : negated conditional → SURVIVED
2. read : negated conditional → KILLED
        if (value == null && !map.containsKey(name)) {
41
            throw new MapAccessException(name);
42
        }
43 1 1. read : replaced return value with null for pro/verron/officestamper/preset/MapAccessor::read → KILLED
        return new TypedValue(value);
44
    }
45
46
    @Override
47
    public boolean canWrite(EvaluationContext context, @Nullable Object target, String name) {
48 2 1. canWrite : replaced boolean return with true for pro/verron/officestamper/preset/MapAccessor::canWrite → NO_COVERAGE
2. canWrite : replaced boolean return with false for pro/verron/officestamper/preset/MapAccessor::canWrite → NO_COVERAGE
        return target instanceof Map;
49
    }
50
51
    @Override
52
    @SuppressWarnings("unchecked")
53
    public void write(EvaluationContext context, @Nullable Object target, String name, @Nullable Object newValue) {
54 1 1. write : removed call to org/springframework/util/Assert::state → NO_COVERAGE
        Assert.state(target instanceof Map, "Target must be of type Map");
55
        Map<Object, Object> map = (Map<Object, Object>) target;
56
        map.put(name, newValue);
57
    }
58
59
    /// Exception thrown from `read` in order to reset a cached
60
    /// PropertyAccessor, allowing other accessors to have a try.
61
    private static class MapAccessException
62
            extends AccessException {
63
64
        private final String key;
65
66
        public MapAccessException(String key) {
67
            super("");
68
            this.key = key;
69
        }
70
71
        @Override
72
        public String getMessage() {
73 1 1. getMessage : replaced return value with "" for pro/verron/officestamper/preset/MapAccessor$MapAccessException::getMessage → NO_COVERAGE
            return "Map does not contain a value for key '" + this.key + "'";
74
        }
75
    }
76
}

Mutations

26

1.1
Location : getSpecificTargetClasses
Killed by : none
replaced return value with null for pro/verron/officestamper/preset/MapAccessor::getSpecificTargetClasses → SURVIVED
Covering tests

31

1.1
Location : canRead
Killed by : pro.verron.officestamper.test.WhitespaceTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.WhitespaceTest]/[test-template:should_preserve_spaces(pro.verron.officestamper.test.ContextFactory, java.lang.String, java.lang.String)]/[test-template-invocation:#5]
negated conditional → KILLED

2.2
Location : canRead
Killed by : pro.verron.officestamper.test.WhitespaceTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.WhitespaceTest]/[test-template:should_preserve_spaces(pro.verron.officestamper.test.ContextFactory, java.lang.String, java.lang.String)]/[test-template-invocation:#5]
negated conditional → KILLED

3.3
Location : canRead
Killed by : none
replaced boolean return with true for pro/verron/officestamper/preset/MapAccessor::canRead → SURVIVED
Covering tests

37

1.1
Location : read
Killed by : none
removed call to org/springframework/util/Assert::state → SURVIVED
Covering tests

40

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

2.2
Location : read
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#21]
negated conditional → KILLED

43

1.1
Location : read
Killed by : pro.verron.officestamper.test.NullPointerResolutionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.NullPointerResolutionTest]/[test-template:nullPointerResolutionTest_testThrowingCase(pro.verron.officestamper.test.ContextFactory)]/[test-template-invocation:#2]
replaced return value with null for pro/verron/officestamper/preset/MapAccessor::read → KILLED

48

1.1
Location : canWrite
Killed by : none
replaced boolean return with true for pro/verron/officestamper/preset/MapAccessor::canWrite → NO_COVERAGE

2.2
Location : canWrite
Killed by : none
replaced boolean return with false for pro/verron/officestamper/preset/MapAccessor::canWrite → NO_COVERAGE

54

1.1
Location : write
Killed by : none
removed call to org/springframework/util/Assert::state → NO_COVERAGE

73

1.1
Location : getMessage
Killed by : none
replaced return value with "" for pro/verron/officestamper/preset/MapAccessor$MapAccessException::getMessage → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.21.0