PebbleTextProcessor.java

1
package sh.okx.rankup.text.pebble;
2
3
import com.mitchellbosecke.pebble.PebbleEngine;
4
import com.mitchellbosecke.pebble.extension.AbstractExtension;
5
import com.mitchellbosecke.pebble.extension.Filter;
6
import com.mitchellbosecke.pebble.loader.StringLoader;
7
import java.io.IOException;
8
import java.io.StringWriter;
9
import java.lang.reflect.InvocationTargetException;
10
import java.text.DecimalFormat;
11
import java.util.HashMap;
12
import java.util.Map;
13
import java.util.logging.Logger;
14
import sh.okx.rankup.messages.pebble.InvalidRequirementException;
15
import sh.okx.rankup.placeholders.Placeholders;
16
import sh.okx.rankup.text.TextProcessor;
17
18
public class PebbleTextProcessor implements TextProcessor {
19
20
  private final Logger logger;
21
  private final Map<String, Object> context;
22
  private final Placeholders options;
23
24
  public PebbleTextProcessor(Logger logger, Map<String, Object> context, Placeholders options) {
25
    this.logger = logger;
26
    this.context = context;
27
    this.options = options;
28
  }
29
30
  @Override
31
  public String process(String string) {
32
    PebbleEngine engine = new PebbleEngine.Builder().autoEscaping(false).extension(
33
        new AbstractExtension() {
34
          @Override
35
          public Map<String, Filter> getFilters() {
36
            Map<String, Filter> filters = new HashMap<>();
37 1 1. getFilters : negated conditional → KILLED
            if (options != null) {
38
              DecimalFormat moneyFormat = options.getMoneyFormat();
39 1 1. getFilters : negated conditional → SURVIVED
              if (moneyFormat != null) {
40
                filters.put("money", new DecimalFormatFilter(moneyFormat));
41
                filters.put("shortmoney", new MoneyShortFilter(options));
42
              }
43
44
              DecimalFormat percentFormat = options.getPercentFormat();
45 1 1. getFilters : negated conditional → SURVIVED
              if (percentFormat != null) filters.put("percent", new DecimalFormatFilter(percentFormat));
46
47
              DecimalFormat simpleFormat = options.getSimpleFormat();
48 1 1. getFilters : negated conditional → KILLED
              if (simpleFormat != null) filters.put("simple", new DecimalFormatFilter(simpleFormat));
49
            }
50 1 1. getFilters : replaced return value with null for sh/okx/rankup/text/pebble/PebbleTextProcessor$1::getFilters → KILLED
            return filters;
51
          }
52
        })
53
        .loader(new StringLoader()).build();
54
    StringWriter writer = new StringWriter();
55
    try {
56
      try {
57 1 1. process : removed call to com/mitchellbosecke/pebble/template/PebbleTemplate::evaluate → KILLED
        engine.getTemplate(string).evaluate(writer, context);
58 1 1. process : replaced return value with "" for sh/okx/rankup/text/pebble/PebbleTextProcessor::process → KILLED
        return writer.toString();
59
      } catch (RuntimeException ex) {
60 1 1. process : negated conditional → NO_COVERAGE
        if (ex.getCause() instanceof InvocationTargetException) {
61 1 1. process : negated conditional → NO_COVERAGE
          if (ex.getCause().getCause() instanceof InvalidRequirementException) {
62
            InvalidRequirementException cause = (InvalidRequirementException) ex.getCause().getCause();
63
            logger.severe("Unknown requirement \"" + cause.getRequirement() + "\" on rank \"" + cause.getRank().getRank() + "\" in message:");
64
            for (String line : string.split("\n")) {
65
              logger.severe(line);
66
            }
67
            logger.severe("Change the message to not use that requirement, or add the requirement to the rank in the config.");
68
          } else {
69 1 1. process : removed call to java/lang/RuntimeException::printStackTrace → NO_COVERAGE
            ex.printStackTrace();
70
          }
71
        } else {
72 1 1. process : removed call to java/lang/RuntimeException::printStackTrace → NO_COVERAGE
          ex.printStackTrace();
73
        }
74 1 1. process : replaced return value with "" for sh/okx/rankup/text/pebble/PebbleTextProcessor::process → NO_COVERAGE
        return "Unable to parse message, please check console";
75
      }
76
    } catch (IOException e) {
77 1 1. process : removed call to java/io/IOException::printStackTrace → NO_COVERAGE
      e.printStackTrace();
78 1 1. process : replaced return value with "" for sh/okx/rankup/text/pebble/PebbleTextProcessor::process → NO_COVERAGE
      return string;
79
    }
80
  }
81
}

Mutations

37

1.1
Location : getFilters
Killed by : sh.okx.rankup.pebble.PebbleTest.[engine:junit-jupiter]/[class:sh.okx.rankup.pebble.PebbleTest]/[method:testIndex()]
negated conditional → KILLED

39

1.1
Location : getFilters
Killed by : none
negated conditional → SURVIVED

45

1.1
Location : getFilters
Killed by : none
negated conditional → SURVIVED

48

1.1
Location : getFilters
Killed by : sh.okx.rankup.pebble.PebbleTest.[engine:junit-jupiter]/[class:sh.okx.rankup.pebble.PebbleTest]/[method:testRequirementPresent()]
negated conditional → KILLED

50

1.1
Location : getFilters
Killed by : sh.okx.rankup.pebble.PebbleTest.[engine:junit-jupiter]/[class:sh.okx.rankup.pebble.PebbleTest]/[method:testRequirementPresent()]
replaced return value with null for sh/okx/rankup/text/pebble/PebbleTextProcessor$1::getFilters → KILLED

57

1.1
Location : process
Killed by : sh.okx.rankup.pebble.PebbleTest.[engine:junit-jupiter]/[class:sh.okx.rankup.pebble.PebbleTest]/[method:testIndex()]
removed call to com/mitchellbosecke/pebble/template/PebbleTemplate::evaluate → KILLED

58

1.1
Location : process
Killed by : sh.okx.rankup.pebble.PebbleTest.[engine:junit-jupiter]/[class:sh.okx.rankup.pebble.PebbleTest]/[method:testIndex()]
replaced return value with "" for sh/okx/rankup/text/pebble/PebbleTextProcessor::process → KILLED

60

1.1
Location : process
Killed by : none
negated conditional → NO_COVERAGE

61

1.1
Location : process
Killed by : none
negated conditional → NO_COVERAGE

69

1.1
Location : process
Killed by : none
removed call to java/lang/RuntimeException::printStackTrace → NO_COVERAGE

72

1.1
Location : process
Killed by : none
removed call to java/lang/RuntimeException::printStackTrace → NO_COVERAGE

74

1.1
Location : process
Killed by : none
replaced return value with "" for sh/okx/rankup/text/pebble/PebbleTextProcessor::process → NO_COVERAGE

77

1.1
Location : process
Killed by : none
removed call to java/io/IOException::printStackTrace → NO_COVERAGE

78

1.1
Location : process
Killed by : none
replaced return value with "" for sh/okx/rankup/text/pebble/PebbleTextProcessor::process → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.7.0