SimpleResourceEngineDetector.java

1
package fr.sii.ogham.core.template.detector;
2
3
import org.slf4j.Logger;
4
import org.slf4j.LoggerFactory;
5
6
import fr.sii.ogham.core.exception.resource.ResourceResolutionException;
7
import fr.sii.ogham.core.exception.template.EngineDetectionException;
8
import fr.sii.ogham.core.resource.Resource;
9
import fr.sii.ogham.core.resource.SimpleResource;
10
import fr.sii.ogham.core.resource.path.ResourcePath;
11
import fr.sii.ogham.core.resource.resolver.ResourceResolver;
12
import fr.sii.ogham.core.template.context.Context;
13
14
/**
15
 * A template engine detector that indicates it can parse the template if the
16
 * resource represented by the template name corresponds to a
17
 * {@link SimpleResource}.
18
 * 
19
 * This is useful when the template name is not a path but directly the template
20
 * content as string.
21
 * 
22
 * @author Aurélien Baudet
23
 *
24
 */
25
public class SimpleResourceEngineDetector implements TemplateEngineDetector {
26
	private static final Logger LOG = LoggerFactory.getLogger(SimpleResourceEngineDetector.class);
27
28
	private final ResourceResolver resolver;
29
	private final TemplateEngineDetector delegate;
30
31
	/**
32
	 * The resolver is used to check if the resolved template from provided name
33
	 * is a {@link SimpleResource}. The delegate is used to make the real
34
	 * detection if it is a {@link SimpleResource}.
35
	 * 
36
	 * @param resolver
37
	 *            the resolver to check if resolved template from provided name
38
	 *            is a {@link SimpleResource} or not
39
	 * @param delegate
40
	 *            the delegate to call if it is a {@link SimpleResource}
41
	 */
42
	public SimpleResourceEngineDetector(ResourceResolver resolver, TemplateEngineDetector delegate) {
43
		super();
44
		this.resolver = resolver;
45
		this.delegate = delegate;
46
	}
47
48
	@Override
49
	public boolean canParse(ResourcePath template, Context ctx) throws EngineDetectionException {
50
		Resource resource = getResource(template);
51
		// no resource matches requested template
52
		// => can't parse (we need a SimpleResource)
53 2 1. canParse : negated conditional → NO_COVERAGE
2. canParse : negated conditional → KILLED
		if (resource == null) {
54 2 1. canParse : replaced boolean return with true for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::canParse → NO_COVERAGE
2. canParse : replaced boolean return with true for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::canParse → KILLED
			return false;
55
		}
56
57
		// resource found but not a SimpleResource
58
		// => can't parse
59 2 1. canParse : negated conditional → NO_COVERAGE
2. canParse : negated conditional → KILLED
		if (!(resource instanceof SimpleResource)) {
60 1 1. canParse : replaced boolean return with true for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::canParse → NO_COVERAGE
			return false;
61
		}
62
63
		// it is a SimpleResource
64
		// => may be able to parse, it depends on delegate
65 4 1. canParse : replaced boolean return with false for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::canParse → NO_COVERAGE
2. canParse : replaced boolean return with true for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::canParse → SURVIVED
3. canParse : replaced boolean return with true for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::canParse → NO_COVERAGE
4. canParse : replaced boolean return with false for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::canParse → KILLED
		return delegate.canParse(template, ctx);
66
	}
67
68
	private Resource getResource(ResourcePath template) {
69
		try {
70 2 1. getResource : replaced return value with null for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::getResource → NO_COVERAGE
2. getResource : replaced return value with null for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::getResource → KILLED
			return resolver.getResource(template);
71
		} catch (ResourceResolutionException e) {
72
			LOG.trace("resource resolution couldn't resolve template {} while trying detect template engine", template, e);
73
			return null;
74
		}
75
	}
76
77
}

Mutations

53

1.1
Location : canParse
Killed by : oghamall.it.email.EmailTemplateStringTest.htmlTemplateStringWithFreemarker(oghamall.it.email.EmailTemplateStringTest)
negated conditional → KILLED

2.2
Location : canParse
Killed by : none
negated conditional → NO_COVERAGE

54

1.1
Location : canParse
Killed by : oghamall.it.template.TemplateErrorTest.singleTemplateNotFound(oghamall.it.template.TemplateErrorTest)
replaced boolean return with true for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::canParse → KILLED

2.2
Location : canParse
Killed by : none
replaced boolean return with true for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::canParse → NO_COVERAGE

59

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

2.2
Location : canParse
Killed by : oghamall.it.email.EmailTemplateStringTest.htmlTemplateStringWithFreemarker(oghamall.it.email.EmailTemplateStringTest)
negated conditional → KILLED

60

1.1
Location : canParse
Killed by : none
replaced boolean return with true for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::canParse → NO_COVERAGE

65

1.1
Location : canParse
Killed by : oghamall.it.email.EmailTemplateStringTest.htmlTemplateStringWithFreemarker(oghamall.it.email.EmailTemplateStringTest)
replaced boolean return with false for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::canParse → KILLED

2.2
Location : canParse
Killed by : none
replaced boolean return with false for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::canParse → NO_COVERAGE

3.3
Location : canParse
Killed by : none
replaced boolean return with true for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::canParse → SURVIVED

4.4
Location : canParse
Killed by : none
replaced boolean return with true for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::canParse → NO_COVERAGE

70

1.1
Location : getResource
Killed by : none
replaced return value with null for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::getResource → NO_COVERAGE

2.2
Location : getResource
Killed by : oghamall.it.email.EmailTemplateStringTest.htmlTemplateStringWithFreemarker(oghamall.it.email.EmailTemplateStringTest)
replaced return value with null for fr/sii/ogham/core/template/detector/SimpleResourceEngineDetector::getResource → KILLED

Active mutators

Tests examined


Report generated by PIT OGHAM