ThymeleafV2TemplateDetector.java

1
package fr.sii.ogham.template.thymeleaf.v2;
2
3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.io.InputStreamReader;
7
import java.util.regex.Pattern;
8
9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
11
12
import fr.sii.ogham.core.exception.resource.ResourceResolutionException;
13
import fr.sii.ogham.core.exception.template.EngineDetectionException;
14
import fr.sii.ogham.core.resource.Resource;
15
import fr.sii.ogham.core.resource.path.ResourcePath;
16
import fr.sii.ogham.core.resource.resolver.ResourceResolver;
17
import fr.sii.ogham.core.template.context.Context;
18
import fr.sii.ogham.core.template.detector.TemplateEngineDetector;
19
20
/**
21
 * Detector that reads the content of the template. If the template contains the
22
 * Thymeleaf namespace (http://www.thymeleaf.org) then the detector returns
23
 * true. Otherwise it returns false.
24
 * 
25
 * @author Aurélien Baudet
26
 *
27
 */
28
public class ThymeleafV2TemplateDetector implements TemplateEngineDetector {
29
	private static final Logger LOG = LoggerFactory.getLogger(ThymeleafV2TemplateDetector.class);
30
31
	/**
32
	 * The pattern to search into the template
33
	 */
34
	private static final Pattern NAMESPACE_PATTERN = Pattern.compile("xmlns[^=]+=\\s*\"http://www.thymeleaf.org\"");
35
36
	/**
37
	 * The template resolver used to find the template
38
	 */
39
	private final ResourceResolver resolver;
40
41
	public ThymeleafV2TemplateDetector(ResourceResolver resolver) {
42
		super();
43
		this.resolver = resolver;
44
	}
45
46
	@Override
47
	public boolean canParse(ResourcePath template, Context ctx) throws EngineDetectionException {
48
		LOG.debug("Checking if Thymeleaf can handle the template {}", template);
49
		Resource resolvedTemplate = getTemplate(template);
50 3 1. canParse : negated conditional → NO_COVERAGE
2. canParse : negated conditional → TIMED_OUT
3. canParse : negated conditional → KILLED
		if (resolvedTemplate == null) {
51 2 1. canParse : replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → NO_COVERAGE
2. canParse : replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → KILLED
			return false;
52
		}
53
		try (BufferedReader br = new BufferedReader(new InputStreamReader(resolvedTemplate.getInputStream()))) {
54
			boolean containsThymeleafNamespace = containsThymeleafNamespace(br);
55 3 1. canParse : negated conditional → SURVIVED
2. canParse : negated conditional → NO_COVERAGE
3. canParse : negated conditional → TIMED_OUT
			if (containsThymeleafNamespace) {
56
				LOG.debug("The template {} contains the namespace http://www.thymeleaf.org. Thymeleaf can be used", template);
57
			} else {
58
				LOG.debug("The template {} doesn't contain the namespace http://www.thymeleaf.org. Thymeleaf can't be used", template);
59
			}
60 12 1. canParse : replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → NO_COVERAGE
2. canParse : replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → NO_COVERAGE
3. canParse : negated conditional → NO_COVERAGE
4. canParse : negated conditional → NO_COVERAGE
5. canParse : replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → TIMED_OUT
6. canParse : replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → TIMED_OUT
7. canParse : negated conditional → TIMED_OUT
8. canParse : negated conditional → TIMED_OUT
9. canParse : replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → KILLED
10. canParse : replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → KILLED
11. canParse : negated conditional → KILLED
12. canParse : negated conditional → KILLED
			return containsThymeleafNamespace || isEmptyTemplate(resolvedTemplate);
61
		} catch (IOException e) {
62
			throw new EngineDetectionException("Failed to detect because template can't be read by thymeleaf", e);
63
		}
64
	}
65
66
	private static boolean containsThymeleafNamespace(BufferedReader br) throws IOException {
67
		String line;
68
		do {
69
			line = br.readLine();
70 6 1. containsThymeleafNamespace : negated conditional → NO_COVERAGE
2. containsThymeleafNamespace : negated conditional → NO_COVERAGE
3. containsThymeleafNamespace : negated conditional → TIMED_OUT
4. containsThymeleafNamespace : negated conditional → TIMED_OUT
5. containsThymeleafNamespace : negated conditional → KILLED
6. containsThymeleafNamespace : negated conditional → KILLED
			if (line != null && NAMESPACE_PATTERN.matcher(line).find()) {
71 3 1. containsThymeleafNamespace : replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::containsThymeleafNamespace → NO_COVERAGE
2. containsThymeleafNamespace : replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::containsThymeleafNamespace → TIMED_OUT
3. containsThymeleafNamespace : replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::containsThymeleafNamespace → KILLED
				return true;
72
			}
73 2 1. containsThymeleafNamespace : negated conditional → NO_COVERAGE
2. containsThymeleafNamespace : negated conditional → TIMED_OUT
		} while (line != null);
74 3 1. containsThymeleafNamespace : replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::containsThymeleafNamespace → NO_COVERAGE
2. containsThymeleafNamespace : replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::containsThymeleafNamespace → TIMED_OUT
3. containsThymeleafNamespace : replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::containsThymeleafNamespace → KILLED
		return false;
75
	}
76
77
78
	private static boolean isEmptyTemplate(Resource template) throws IOException {
79
		try (InputStream stream = template.getInputStream()) {
80 9 1. isEmptyTemplate : replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::isEmptyTemplate → NO_COVERAGE
2. isEmptyTemplate : replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::isEmptyTemplate → NO_COVERAGE
3. isEmptyTemplate : negated conditional → NO_COVERAGE
4. isEmptyTemplate : replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::isEmptyTemplate → TIMED_OUT
5. isEmptyTemplate : replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::isEmptyTemplate → TIMED_OUT
6. isEmptyTemplate : negated conditional → TIMED_OUT
7. isEmptyTemplate : replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::isEmptyTemplate → KILLED
8. isEmptyTemplate : replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::isEmptyTemplate → KILLED
9. isEmptyTemplate : negated conditional → KILLED
			return stream.read() == -1;
81
		}
82
	}
83
84
	private Resource getTemplate(ResourcePath templateName) {
85
		try {
86 3 1. getTemplate : replaced return value with null for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::getTemplate → NO_COVERAGE
2. getTemplate : replaced return value with null for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::getTemplate → TIMED_OUT
3. getTemplate : replaced return value with null for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::getTemplate → KILLED
			return resolver.getResource(templateName);
87
		} catch (ResourceResolutionException e) {
88
			LOG.trace("Thymeleaf detector can't be applied because {} couldn't be resolved", templateName, e);
89
			return null;
90
		}
91
	}
92
93
}

Mutations

50

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

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

3.3
Location : canParse
Killed by : oghamthymeleafv2.it.ThymeleafDetectorTest.notFound(oghamthymeleafv2.it.ThymeleafDetectorTest)
negated conditional → KILLED

51

1.1
Location : canParse
Killed by : oghamthymeleafv2.it.ThymeleafDetectorTest.notFound(oghamthymeleafv2.it.ThymeleafDetectorTest)
replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → KILLED

2.2
Location : canParse
Killed by : none
replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → NO_COVERAGE

55

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

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

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

60

1.1
Location : canParse
Killed by : none
replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → NO_COVERAGE

2.2
Location : canParse
Killed by : oghamthymeleafv2.it.ThymeleafDetectorTest.foundButEmpty(oghamthymeleafv2.it.ThymeleafDetectorTest)
replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → KILLED

3.3
Location : canParse
Killed by : none
replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → TIMED_OUT

4.4
Location : canParse
Killed by : none
replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → TIMED_OUT

5.5
Location : canParse
Killed by : none
replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → NO_COVERAGE

6.6
Location : canParse
Killed by : oghamthymeleafv2.it.ThymeleafDetectorTest.foundButNotThymeleaf(oghamthymeleafv2.it.ThymeleafDetectorTest)
replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → KILLED

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

8.8
Location : canParse
Killed by : none
negated conditional → TIMED_OUT

9.9
Location : canParse
Killed by : oghamthymeleafv2.it.ThymeleafDetectorTest.foundButNotThymeleaf(oghamthymeleafv2.it.ThymeleafDetectorTest)
negated conditional → KILLED

10.10
Location : canParse
Killed by : none
negated conditional → TIMED_OUT

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

12.12
Location : canParse
Killed by : oghamthymeleafv2.it.ThymeleafDetectorTest.foundButEmpty(oghamthymeleafv2.it.ThymeleafDetectorTest)
negated conditional → KILLED

70

1.1
Location : containsThymeleafNamespace
Killed by : none
negated conditional → TIMED_OUT

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

3.3
Location : containsThymeleafNamespace
Killed by : oghamthymeleafv2.it.ThymeleafDetectorTest.foundButEmpty(oghamthymeleafv2.it.ThymeleafDetectorTest)
negated conditional → KILLED

4.4
Location : containsThymeleafNamespace
Killed by : oghamthymeleafv2.it.ThymeleafDetectorTest.foundButNotThymeleaf(oghamthymeleafv2.it.ThymeleafDetectorTest)
negated conditional → KILLED

5.5
Location : containsThymeleafNamespace
Killed by : none
negated conditional → NO_COVERAGE

6.6
Location : containsThymeleafNamespace
Killed by : none
negated conditional → TIMED_OUT

71

1.1
Location : containsThymeleafNamespace
Killed by : oghamthymeleafv2.it.ThymeleafDetectorTest.found(oghamthymeleafv2.it.ThymeleafDetectorTest)
replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::containsThymeleafNamespace → KILLED

2.2
Location : containsThymeleafNamespace
Killed by : none
replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::containsThymeleafNamespace → TIMED_OUT

3.3
Location : containsThymeleafNamespace
Killed by : none
replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::containsThymeleafNamespace → NO_COVERAGE

73

1.1
Location : containsThymeleafNamespace
Killed by : none
negated conditional → TIMED_OUT

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

74

1.1
Location : containsThymeleafNamespace
Killed by : oghamthymeleafv2.it.ThymeleafDetectorTest.foundButNotThymeleaf(oghamthymeleafv2.it.ThymeleafDetectorTest)
replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::containsThymeleafNamespace → KILLED

2.2
Location : containsThymeleafNamespace
Killed by : none
replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::containsThymeleafNamespace → TIMED_OUT

3.3
Location : containsThymeleafNamespace
Killed by : none
replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::containsThymeleafNamespace → NO_COVERAGE

80

1.1
Location : isEmptyTemplate
Killed by : oghamthymeleafv2.it.ThymeleafDetectorTest.foundButEmpty(oghamthymeleafv2.it.ThymeleafDetectorTest)
replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::isEmptyTemplate → KILLED

2.2
Location : isEmptyTemplate
Killed by : none
replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::isEmptyTemplate → NO_COVERAGE

3.3
Location : isEmptyTemplate
Killed by : none
replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::isEmptyTemplate → TIMED_OUT

4.4
Location : isEmptyTemplate
Killed by : none
replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::isEmptyTemplate → TIMED_OUT

5.5
Location : isEmptyTemplate
Killed by : none
replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::isEmptyTemplate → NO_COVERAGE

6.6
Location : isEmptyTemplate
Killed by : oghamthymeleafv2.it.ThymeleafDetectorTest.foundButNotThymeleaf(oghamthymeleafv2.it.ThymeleafDetectorTest)
replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::isEmptyTemplate → KILLED

7.7
Location : isEmptyTemplate
Killed by : oghamthymeleafv2.it.ThymeleafDetectorTest.foundButEmpty(oghamthymeleafv2.it.ThymeleafDetectorTest)
negated conditional → KILLED

8.8
Location : isEmptyTemplate
Killed by : none
negated conditional → NO_COVERAGE

9.9
Location : isEmptyTemplate
Killed by : none
negated conditional → TIMED_OUT

86

1.1
Location : getTemplate
Killed by : oghamthymeleafv2.it.ThymeleafDetectorTest.foundButEmpty(oghamthymeleafv2.it.ThymeleafDetectorTest)
replaced return value with null for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::getTemplate → KILLED

2.2
Location : getTemplate
Killed by : none
replaced return value with null for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::getTemplate → TIMED_OUT

3.3
Location : getTemplate
Killed by : none
replaced return value with null for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::getTemplate → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT OGHAM