VelocityMailTemplate.java

1
/*
2
 * Copyright (c) P. Ottlinger
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *   https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package de.aikiit.mailversendala.template;
17
18
import de.aikiit.mailversendala.MailConfig;
19
import de.aikiit.mailversendala.csv.Mailing;
20
import org.apache.velocity.Template;
21
import org.apache.velocity.VelocityContext;
22
import org.apache.velocity.app.VelocityEngine;
23
import org.apache.velocity.app.event.EventCartridge;
24
import org.apache.velocity.app.event.implement.EscapeHtmlReference;
25
import org.apache.velocity.exception.VelocityException;
26
import org.apache.velocity.runtime.RuntimeConstants;
27
28
import java.io.IOException;
29
import java.io.StringWriter;
30
import java.nio.charset.StandardCharsets;
31
import java.util.Objects;
32
33
/**
34
 * Compiles external UTF-8 templates once per run and renders isolated recipient contexts.
35
 * HTML references are escaped; plain-text references are left unchanged.
36
 */
37
public class VelocityMailTemplate {
38
39
    private final Template html;
40
    private final Template plaintext;
41
42
    /**
43
     * Load both templates from the configured external directory before sending begins.
44
     * @param configuration directory and template filenames.
45
     * @throws IOException if a template is missing, unreadable or syntactically invalid.
46
     */
47
    public VelocityMailTemplate(MailConfig configuration) throws IOException {
48
        VelocityEngine engine = new VelocityEngine();
49 1 1. <init> : removed call to org/apache/velocity/app/VelocityEngine::setProperty → SURVIVED
        engine.setProperty(RuntimeConstants.RESOURCE_LOADERS, "file");
50 1 1. <init> : removed call to org/apache/velocity/app/VelocityEngine::setProperty → KILLED
        engine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, configuration.getTemplatePath());
51 1 1. <init> : removed call to org/apache/velocity/app/VelocityEngine::setProperty → SURVIVED
        engine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true);
52 1 1. <init> : removed call to org/apache/velocity/app/VelocityEngine::setProperty → KILLED
        engine.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, true);
53
        try {
54 1 1. <init> : removed call to org/apache/velocity/app/VelocityEngine::init → SURVIVED
            engine.init();
55
            html = engine.getTemplate(configuration.getHtmlTemplate(), StandardCharsets.UTF_8.name());
56
            plaintext = engine.getTemplate(configuration.getPlaintextTemplate(), StandardCharsets.UTF_8.name());
57
        } catch (VelocityException e) {
58
            throw new IOException("Cannot load mail templates from " + configuration.getTemplatePath(), e);
59
        }
60
    }
61
62
    /**
63
     * Render both alternatives for one recipient, without retaining recipient state.
64
     * @param recipient CSV recipient data.
65
     * @return rendered HTML and plain-text bodies.
66
     */
67
    public MailTemplate render(Mailing recipient) {
68
        VelocityContext htmlContext = context(recipient);
69
        EventCartridge events = new EventCartridge();
70
        events.addEventHandler(new EscapeHtmlReference());
71
        events.attachToContext(htmlContext);
72 1 1. render : replaced return value with null for de/aikiit/mailversendala/template/VelocityMailTemplate::render → KILLED
        return new RenderedMail(merge(html, htmlContext), merge(plaintext, context(recipient)));
73
    }
74
75
    private VelocityContext context(Mailing recipient) {
76
        VelocityContext context = new VelocityContext();
77
        String firstName = Objects.requireNonNullElse(recipient.firstname(), "");
78
        String lastName = Objects.requireNonNullElse(recipient.surname(), "");
79
        context.put("firstName", firstName);
80
        context.put("lastName", lastName);
81
        context.put("firstname", firstName);
82
        context.put("surname", lastName);
83
        context.put("email", Objects.requireNonNullElse(recipient.email(), ""));
84
        context.put("salutation", Objects.requireNonNullElse(recipient.salutation(), ""));
85 1 1. context : replaced return value with null for de/aikiit/mailversendala/template/VelocityMailTemplate::context → KILLED
        return context;
86
    }
87
88
    private String merge(Template template, VelocityContext context) {
89
        StringWriter writer = new StringWriter();
90 1 1. merge : removed call to org/apache/velocity/Template::merge → KILLED
        template.merge(context, writer);
91 1 1. merge : replaced return value with "" for de/aikiit/mailversendala/template/VelocityMailTemplate::merge → KILLED
        return writer.toString();
92
    }
93
94
    private record RenderedMail(String html, String plaintext) implements MailTemplate {
95
        @Override
96
        public String getHtml() {
97 1 1. getHtml : replaced return value with "" for de/aikiit/mailversendala/template/VelocityMailTemplate$RenderedMail::getHtml → KILLED
            return html;
98
        }
99
100
        @Override
101
        public String getPlaintext() {
102 1 1. getPlaintext : replaced return value with "" for de/aikiit/mailversendala/template/VelocityMailTemplate$RenderedMail::getPlaintext → KILLED
            return plaintext;
103
        }
104
    }
105
}

Mutations

49

1.1
Location : <init>
Killed by : none
removed call to org/apache/velocity/app/VelocityEngine::setProperty → SURVIVED
Covering tests

50

1.1
Location : <init>
Killed by : de.aikiit.mailversendala.template.VelocityMailTemplateTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.template.VelocityMailTemplateTest]/[method:rejectsUnknownPlaceholdersRatherThanSendingUnexpandedReferences()]
removed call to org/apache/velocity/app/VelocityEngine::setProperty → KILLED

51

1.1
Location : <init>
Killed by : none
removed call to org/apache/velocity/app/VelocityEngine::setProperty → SURVIVED
Covering tests

52

1.1
Location : <init>
Killed by : de.aikiit.mailversendala.template.VelocityMailTemplateTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.template.VelocityMailTemplateTest]/[method:rejectsUnknownPlaceholdersRatherThanSendingUnexpandedReferences()]
removed call to org/apache/velocity/app/VelocityEngine::setProperty → KILLED

54

1.1
Location : <init>
Killed by : none
removed call to org/apache/velocity/app/VelocityEngine::init → SURVIVED
Covering tests

72

1.1
Location : render
Killed by : de.aikiit.mailversendala.template.VelocityMailTemplateTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.template.VelocityMailTemplateTest]/[method:escapesSalutationOnlyInHtml()]
replaced return value with null for de/aikiit/mailversendala/template/VelocityMailTemplate::render → KILLED

85

1.1
Location : context
Killed by : de.aikiit.mailversendala.template.VelocityMailTemplateTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.template.VelocityMailTemplateTest]/[method:rejectsUnknownPlaceholdersRatherThanSendingUnexpandedReferences()]
replaced return value with null for de/aikiit/mailversendala/template/VelocityMailTemplate::context → KILLED

90

1.1
Location : merge
Killed by : de.aikiit.mailversendala.template.VelocityMailTemplateTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.template.VelocityMailTemplateTest]/[method:rejectsUnknownPlaceholdersRatherThanSendingUnexpandedReferences()]
removed call to org/apache/velocity/Template::merge → KILLED

91

1.1
Location : merge
Killed by : de.aikiit.mailversendala.template.VelocityMailTemplateTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.template.VelocityMailTemplateTest]/[method:escapesSalutationOnlyInHtml()]
replaced return value with "" for de/aikiit/mailversendala/template/VelocityMailTemplate::merge → KILLED

97

1.1
Location : getHtml
Killed by : de.aikiit.mailversendala.template.VelocityMailTemplateTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.template.VelocityMailTemplateTest]/[method:escapesSalutationOnlyInHtml()]
replaced return value with "" for de/aikiit/mailversendala/template/VelocityMailTemplate$RenderedMail::getHtml → KILLED

102

1.1
Location : getPlaintext
Killed by : de.aikiit.mailversendala.template.VelocityMailTemplateTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.template.VelocityMailTemplateTest]/[method:escapesSalutationOnlyInHtml()]
replaced return value with "" for de/aikiit/mailversendala/template/VelocityMailTemplate$RenderedMail::getPlaintext → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.2 support