FirstSupportingResolverAdapter.java

1
package fr.sii.ogham.template.freemarker.adapter;
2
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.List;
6
7
import fr.sii.ogham.core.resource.resolver.ResourceResolver;
8
import fr.sii.ogham.template.exception.NoResolverAdapterException;
9
import fr.sii.ogham.template.exception.ResolverAdapterException;
10
import fr.sii.ogham.template.freemarker.TemplateLoaderOptions;
11
import freemarker.cache.TemplateLoader;
12
13
/**
14
 * Decorator that will ask each resolver adapter if it is able to handle the
15
 * template resolver. If the resolver adapter supports it, then this
16
 * implementation asks the resolver adapter to provide the FreeMarker template
17
 * loader.
18
 * 
19
 * Only the first resolver adapter that can handle the template resolver is
20
 * used.
21
 * 
22
 * @author Cyril Dejonghe
23
 */
24
public class FirstSupportingResolverAdapter implements TemplateLoaderAdapter {
25
26
	/**
27
	 * The list of adapters used to convert the general resolvers into
28
	 * FreeMarker specific resolvers
29
	 */
30
	private List<TemplateLoaderAdapter> adapters;
31
32
	/**
33
	 * Initialize the decorator with none, one or several resolver adapter
34
	 * implementations. The registration order may be important.
35
	 * 
36
	 * @param adapters
37
	 *            the adapters to register
38
	 */
39
	public FirstSupportingResolverAdapter(TemplateLoaderAdapter... adapters) {
40
		this(new ArrayList<>(Arrays.asList(adapters)));
41
	}
42
43
	/**
44
	 * Initialize the decorator with the provided resolver adapter
45
	 * implementations. The registration order may be important.
46
	 * 
47
	 * @param adapters
48
	 *            the adapters to register
49
	 */
50
	public FirstSupportingResolverAdapter(List<TemplateLoaderAdapter> adapters) {
51
		super();
52
		this.adapters = adapters;
53
	}
54
55
	public FirstSupportingResolverAdapter() {
56
		this(new ArrayList<>());
57
	}
58
59
	@Override
60
	public boolean supports(ResourceResolver resolver) {
61
		for (TemplateLoaderAdapter adapter : adapters) {
62 1 1. supports : negated conditional → NO_COVERAGE
			if (adapter.supports(resolver)) {
63 1 1. supports : replaced boolean return with false for fr/sii/ogham/template/thymeleaf/common/adapter/FirstSupportingResolverAdapter::supports → NO_COVERAGE
				return true;
64
			}
65
		}
66 1 1. supports : replaced boolean return with true for fr/sii/ogham/template/thymeleaf/common/adapter/FirstSupportingResolverAdapter::supports → NO_COVERAGE
		return false;
67
	}
68
69
	@Override
70
	public TemplateLoader adapt(ResourceResolver resolver) throws ResolverAdapterException {
71
		for (TemplateLoaderAdapter adapter : adapters) {
72 1 1. adapt : negated conditional → RUN_ERROR
			if (adapter.supports(resolver)) {
73 1 1. adapt : replaced return value with null for fr/sii/ogham/template/thymeleaf/common/adapter/FirstSupportingResolverAdapter::adapt → RUN_ERROR
				return adapter.adapt(resolver);
74
			}
75
		}
76
		throw new NoResolverAdapterException("No resolver adapter found for the provided resolver: " + resolverName(resolver), resolver);
77
	}
78
79
	/**
80
	 * Register a new adapter. The adapter is added at the end.
81
	 * 
82
	 * @param adapter
83
	 *            the adapter to register
84
	 */
85
	public void addAdapter(TemplateLoaderAdapter adapter) {
86
		adapters.add(adapter);
87
	}
88
89
	public List<TemplateLoaderAdapter> getAdapters() {
90 1 1. getAdapters : replaced return value with Collections.emptyList for fr/sii/ogham/template/thymeleaf/common/adapter/FirstSupportingResolverAdapter::getAdapters → NO_COVERAGE
		return adapters;
91
	}
92
93
	@Override
94
	public void setOptions(TemplateLoaderOptions options) {
95
		for (TemplateLoaderAdapter adapter : adapters) {
96 1 1. setOptions : removed call to fr/sii/ogham/template/thymeleaf/common/adapter/TemplateResolverAdapter::setOptions → RUN_ERROR
			adapter.setOptions(options);
97
		}
98
	}
99
100
	private static String resolverName(ResourceResolver resolver) {
101
		return resolver==null ? "null" : resolver.getClass().getSimpleName();
102
	}
103
}

Mutations

62

1.1
Location : supports
Killed by :
negated conditional → NO_COVERAGE

63

1.1
Location : supports
Killed by :
replaced boolean return with false for fr/sii/ogham/template/thymeleaf/common/adapter/FirstSupportingResolverAdapter::supports → NO_COVERAGE

66

1.1
Location : supports
Killed by :
replaced boolean return with true for fr/sii/ogham/template/thymeleaf/common/adapter/FirstSupportingResolverAdapter::supports → NO_COVERAGE

72

1.1
Location : adapt
Killed by :
negated conditional → RUN_ERROR

73

1.1
Location : adapt
Killed by :
replaced return value with null for fr/sii/ogham/template/thymeleaf/common/adapter/FirstSupportingResolverAdapter::adapt → RUN_ERROR

90

1.1
Location : getAdapters
Killed by :
replaced return value with Collections.emptyList for fr/sii/ogham/template/thymeleaf/common/adapter/FirstSupportingResolverAdapter::getAdapters → NO_COVERAGE

96

1.1
Location : setOptions
Killed by :
removed call to fr/sii/ogham/template/thymeleaf/common/adapter/TemplateResolverAdapter::setOptions → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1