BeanContext.java

1
package fr.sii.ogham.core.template.context;
2
3
import java.util.Map;
4
5
import fr.sii.ogham.core.exception.template.BeanContextException;
6
import fr.sii.ogham.core.exception.template.ContextException;
7
import fr.sii.ogham.core.exception.util.BeanWrapperException;
8
import fr.sii.ogham.core.util.BeanUtils;
9
import fr.sii.ogham.core.util.EqualsBuilder;
10
import fr.sii.ogham.core.util.HashCodeBuilder;
11
import fr.sii.ogham.core.util.bean.MapBeanReadWrapper;
12
13
/**
14
 * Template context that provides variable values using a Java object. Each
15
 * property name of the object acts as a potential template variable name. Each
16
 * property value acts as a potential template variable value.
17
 * 
18
 * The variables can contain dot character (.) to indicate nested properties. A
19
 * nested property is a property on a child object. For example, if the provided
20
 * Java object looks like:
21
 * 
22
 * <pre>
23
 * public class NestedBean {
24
 * 	private SimpleBean nested;
25
 * 
26
 * 	public NestedBean(SimpleBean nested) {
27
 * 		super();
28
 * 		this.nested = nested;
29
 * 	}
30
 * 
31
 * 	public SimpleBean getNested() {
32
 * 		return nested;
33
 * 	}
34
 * 
35
 * 	public static class SimpleBean {
36
 * 		private String value;
37
 * 
38
 * 		public SimpleBean(String value) {
39
 * 			super();
40
 * 			this.value = value;
41
 * 		}
42
 * 
43
 * 		public String getValue() {
44
 * 			return value;
45
 * 		}
46
 * 
47
 * 	}
48
 * }
49
 * </pre>
50
 * 
51
 * Then the value of the nested object is accessible through the template
52
 * variable name "nested.value".
53
 * 
54
 * @author Aurélien Baudet
55
 * @see BeanUtils More information about bean conversion
56
 */
57
public class BeanContext implements Context {
58
	/**
59
	 * A Java object that is the source for the variable substitutions
60
	 */
61
	private Object bean;
62
63
	/**
64
	 * Register the bean to use for variable substitutions.
65
	 * 
66
	 * @param bean
67
	 *            the bean to use as source for the variable substitutions
68
	 */
69
	public BeanContext(Object bean) {
70
		super();
71
		this.bean = bean;
72
	}
73
74
	@Override
75
	public Map<String, Object> getVariables() throws ContextException {
76
		try {
77 6 1. getVariables : replaced return value with null for fr/sii/ogham/core/template/context/BeanContext::getVariables → NO_COVERAGE
2. getVariables : replaced return value with null for fr/sii/ogham/core/template/context/BeanContext::getVariables → TIMED_OUT
3. getVariables : replaced return value with null for fr/sii/ogham/core/template/context/BeanContext::getVariables → KILLED
4. getVariables : replaced return value with null for fr/sii/ogham/core/template/context/BeanContext::getVariables → KILLED
5. getVariables : replaced return value with null for fr/sii/ogham/core/template/context/BeanContext::getVariables → KILLED
6. getVariables : replaced return value with null for fr/sii/ogham/core/template/context/BeanContext::getVariables → KILLED
			return new MapBeanReadWrapper(bean);
78
		} catch (BeanWrapperException e) {
79
			throw new BeanContextException("Failed to generate context from bean", bean, e);
80
		}
81
	}
82
83
	@Override
84
	public String toString() {
85 3 1. toString : replaced return value with "" for fr/sii/ogham/core/template/context/BeanContext::toString → NO_COVERAGE
2. toString : replaced return value with "" for fr/sii/ogham/core/template/context/BeanContext::toString → SURVIVED
3. toString : replaced return value with "" for fr/sii/ogham/core/template/context/BeanContext::toString → TIMED_OUT
		return bean.toString();
86
	}
87
	
88
	@Override
89
	public int hashCode() {
90 1 1. hashCode : replaced int return with 0 for fr/sii/ogham/core/template/context/BeanContext::hashCode → NO_COVERAGE
		return new HashCodeBuilder().append(bean).hashCode();
91
	}
92
93
	@Override
94
	public boolean equals(Object obj) {
95 2 1. equals : replaced boolean return with false for fr/sii/ogham/core/template/context/BeanContext::equals → NO_COVERAGE
2. equals : replaced boolean return with true for fr/sii/ogham/core/template/context/BeanContext::equals → NO_COVERAGE
		return new EqualsBuilder(this, obj).appendFields("bean").isEqual();
96
	}
97
}

Mutations

77

1.1
Location : getVariables
Killed by : oghamfremarker.it.FreeMarkerParserTest.nested(oghamfremarker.it.FreeMarkerParserTest)
replaced return value with null for fr/sii/ogham/core/template/context/BeanContext::getVariables → KILLED

2.2
Location : getVariables
Killed by : none
replaced return value with null for fr/sii/ogham/core/template/context/BeanContext::getVariables → TIMED_OUT

3.3
Location : getVariables
Killed by : none
replaced return value with null for fr/sii/ogham/core/template/context/BeanContext::getVariables → NO_COVERAGE

4.4
Location : getVariables
Killed by : oghamall.it.email.EmailMultiTemplateTest.withFreemarkerOneVariantWithInvalidResourcePath(oghamall.it.email.EmailMultiTemplateTest)
replaced return value with null for fr/sii/ogham/core/template/context/BeanContext::getVariables → KILLED

5.5
Location : getVariables
Killed by : oghamthymeleafv2.it.resolver.StringResourceResolverTest.text(oghamthymeleafv2.it.resolver.StringResourceResolverTest)
replaced return value with null for fr/sii/ogham/core/template/context/BeanContext::getVariables → KILLED

6.6
Location : getVariables
Killed by : oghamthymeleafv3.it.resolver.StringResourceResolverTest.text(oghamthymeleafv3.it.resolver.StringResourceResolverTest)
replaced return value with null for fr/sii/ogham/core/template/context/BeanContext::getVariables → KILLED

85

1.1
Location : toString
Killed by : none
replaced return value with "" for fr/sii/ogham/core/template/context/BeanContext::toString → NO_COVERAGE

2.2
Location : toString
Killed by : none
replaced return value with "" for fr/sii/ogham/core/template/context/BeanContext::toString → TIMED_OUT

3.3
Location : toString
Killed by : none
replaced return value with "" for fr/sii/ogham/core/template/context/BeanContext::toString → SURVIVED

90

1.1
Location : hashCode
Killed by : none
replaced int return with 0 for fr/sii/ogham/core/template/context/BeanContext::hashCode → NO_COVERAGE

95

1.1
Location : equals
Killed by : none
replaced boolean return with false for fr/sii/ogham/core/template/context/BeanContext::equals → NO_COVERAGE

2.2
Location : equals
Killed by : none
replaced boolean return with true for fr/sii/ogham/core/template/context/BeanContext::equals → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT OGHAM