| 1 | package fr.sii.ogham.spring.template.freemarker; | |
| 2 | ||
| 3 | import java.util.Iterator; | |
| 4 | import java.util.NoSuchElementException; | |
| 5 | ||
| 6 | import org.springframework.context.ApplicationContext; | |
| 7 | ||
| 8 | import freemarker.ext.beans.BeanModel; | |
| 9 | import freemarker.ext.beans.BeansWrapper; | |
| 10 | import freemarker.ext.beans.IteratorModel; | |
| 11 | import freemarker.ext.beans.StringModel; | |
| 12 | import freemarker.template.TemplateCollectionModel; | |
| 13 | import freemarker.template.TemplateHashModelEx2; | |
| 14 | import freemarker.template.TemplateModel; | |
| 15 | import freemarker.template.TemplateModelException; | |
| 16 | ||
| 17 | /** | |
| 18 | * Specific model to be able to access Spring beans from template using | |
| 19 | * {@code @beanName.method(args)} syntax. | |
| 20 | * | |
| 21 | * @author Aurélien Baudet | |
| 22 | * | |
| 23 | */ | |
| 24 | public class SpringBeansTemplateHashModelEx implements TemplateHashModelEx2 { | |
| 25 | private final ApplicationContext applicationContext; | |
| 26 | private final BeansWrapper beansWrapper; | |
| 27 | ||
| 28 | public SpringBeansTemplateHashModelEx(ApplicationContext applicationContext, BeansWrapper beansWrapper) { | |
| 29 | super(); | |
| 30 | this.applicationContext = applicationContext; | |
| 31 | this.beansWrapper = beansWrapper; | |
| 32 | } | |
| 33 | ||
| 34 | @Override | |
| 35 | public int size() throws TemplateModelException { | |
| 36 |
1
1. size : replaced int return with 0 for fr/sii/ogham/spring/template/freemarker/SpringBeansTemplateHashModelEx::size → NO_COVERAGE |
return applicationContext.getBeanDefinitionCount(); |
| 37 | } | |
| 38 | ||
| 39 | @Override | |
| 40 | public TemplateCollectionModel keys() throws TemplateModelException { | |
| 41 |
1
1. keys : replaced return value with null for fr/sii/ogham/spring/template/freemarker/SpringBeansTemplateHashModelEx::keys → RUN_ERROR |
return new IteratorModel(new Iterator<String>() { |
| 42 | private int currentIdx = 0; | |
| 43 | ||
| 44 | @Override | |
| 45 | public boolean hasNext() { | |
| 46 |
3
1. hasNext : changed conditional boundary → RUN_ERROR 2. hasNext : replaced boolean return with true for fr/sii/ogham/spring/template/freemarker/SpringBeansTemplateHashModelEx$1::hasNext → RUN_ERROR 3. hasNext : negated conditional → RUN_ERROR |
return currentIdx < applicationContext.getBeanDefinitionCount(); |
| 47 | } | |
| 48 | ||
| 49 | @Override | |
| 50 | public String next() { | |
| 51 |
1
1. next : negated conditional → RUN_ERROR |
if (!hasNext()) { |
| 52 | throw new NoSuchElementException(); | |
| 53 | } | |
| 54 |
2
1. next : Replaced integer addition with subtraction → RUN_ERROR 2. next : replaced return value with "" for fr/sii/ogham/spring/template/freemarker/SpringBeansTemplateHashModelEx$1::next → RUN_ERROR |
return "@" + applicationContext.getBeanDefinitionNames()[currentIdx++]; |
| 55 | } | |
| 56 | ||
| 57 | }, beansWrapper); | |
| 58 | } | |
| 59 | ||
| 60 | @Override | |
| 61 | public TemplateCollectionModel values() throws TemplateModelException { | |
| 62 |
1
1. values : replaced return value with null for fr/sii/ogham/spring/template/freemarker/SpringBeansTemplateHashModelEx::values → RUN_ERROR |
return new IteratorModel(new Iterator<Object>() { |
| 63 | private int currentIdx = 0; | |
| 64 | ||
| 65 | @Override | |
| 66 | public boolean hasNext() { | |
| 67 |
3
1. hasNext : negated conditional → RUN_ERROR 2. hasNext : changed conditional boundary → RUN_ERROR 3. hasNext : replaced boolean return with true for fr/sii/ogham/spring/template/freemarker/SpringBeansTemplateHashModelEx$2::hasNext → RUN_ERROR |
return currentIdx < applicationContext.getBeanDefinitionCount(); |
| 68 | } | |
| 69 | ||
| 70 | @Override | |
| 71 | public Object next() { | |
| 72 |
1
1. next : negated conditional → RUN_ERROR |
if (!hasNext()) { |
| 73 | throw new NoSuchElementException(); | |
| 74 | } | |
| 75 |
1
1. next : Replaced integer addition with subtraction → RUN_ERROR |
String name = applicationContext.getBeanDefinitionNames()[currentIdx++]; |
| 76 |
1
1. next : replaced return value with null for fr/sii/ogham/spring/template/freemarker/SpringBeansTemplateHashModelEx$2::next → RUN_ERROR |
return new LazySpringBeanAccessModel(applicationContext, beansWrapper, name); |
| 77 | } | |
| 78 | ||
| 79 | }, beansWrapper); | |
| 80 | } | |
| 81 | ||
| 82 | @Override | |
| 83 | public TemplateModel get(String key) throws TemplateModelException { | |
| 84 |
2
1. get : replaced return value with null for fr/sii/ogham/spring/template/freemarker/SpringBeansTemplateHashModelEx::get → NO_COVERAGE 2. get : negated conditional → NO_COVERAGE |
return new BeanModel(applicationContext.getBean(key.startsWith("@") ? key.substring(1) : key), beansWrapper); |
| 85 | } | |
| 86 | ||
| 87 | @Override | |
| 88 | public boolean isEmpty() throws TemplateModelException { | |
| 89 |
3
1. isEmpty : negated conditional → NO_COVERAGE 2. isEmpty : changed conditional boundary → NO_COVERAGE 3. isEmpty : replaced boolean return with true for fr/sii/ogham/spring/template/freemarker/SpringBeansTemplateHashModelEx::isEmpty → NO_COVERAGE |
return applicationContext.getBeanDefinitionCount() <= 0; |
| 90 | } | |
| 91 | ||
| 92 | @Override | |
| 93 | public KeyValuePairIterator keyValuePairIterator() throws TemplateModelException { | |
| 94 |
1
1. keyValuePairIterator : replaced return value with null for fr/sii/ogham/spring/template/freemarker/SpringBeansTemplateHashModelEx::keyValuePairIterator → NO_COVERAGE |
return new KeyValuePairIterator() { |
| 95 | private int currentIdx = 0; | |
| 96 | ||
| 97 | @Override | |
| 98 | public KeyValuePair next() throws TemplateModelException { | |
| 99 |
1
1. next : negated conditional → NO_COVERAGE |
if (!hasNext()) { |
| 100 | throw new NoSuchElementException(); | |
| 101 | } | |
| 102 | KeyValuePair pair = new KeyValuePair() { | |
| 103 | ||
| 104 | @Override | |
| 105 | public TemplateModel getValue() throws TemplateModelException { | |
| 106 | String name = applicationContext.getBeanDefinitionNames()[currentIdx]; | |
| 107 |
1
1. getValue : replaced return value with null for fr/sii/ogham/spring/template/freemarker/SpringBeansTemplateHashModelEx$3$1::getValue → NO_COVERAGE |
return new LazySpringBeanAccessModel(applicationContext, beansWrapper, name); |
| 108 | } | |
| 109 | ||
| 110 | @Override | |
| 111 | public TemplateModel getKey() throws TemplateModelException { | |
| 112 |
1
1. getKey : replaced return value with null for fr/sii/ogham/spring/template/freemarker/SpringBeansTemplateHashModelEx$3$1::getKey → NO_COVERAGE |
return new StringModel(applicationContext.getBeanDefinitionNames()[currentIdx], beansWrapper); |
| 113 | } | |
| 114 | }; | |
| 115 |
1
1. next : Replaced integer addition with subtraction → NO_COVERAGE |
currentIdx++; |
| 116 |
1
1. next : replaced return value with null for fr/sii/ogham/spring/template/freemarker/SpringBeansTemplateHashModelEx$3::next → NO_COVERAGE |
return pair; |
| 117 | } | |
| 118 | ||
| 119 | @Override | |
| 120 | public boolean hasNext() throws TemplateModelException { | |
| 121 |
3
1. hasNext : replaced boolean return with true for fr/sii/ogham/spring/template/freemarker/SpringBeansTemplateHashModelEx$3::hasNext → NO_COVERAGE 2. hasNext : changed conditional boundary → NO_COVERAGE 3. hasNext : negated conditional → NO_COVERAGE |
return currentIdx < applicationContext.getBeanDefinitionCount(); |
| 122 | } | |
| 123 | }; | |
| 124 | } | |
| 125 | ||
| 126 | } | |
Mutations | ||
| 36 |
1.1 |
|
| 41 |
1.1 |
|
| 46 |
1.1 2.2 3.3 |
|
| 51 |
1.1 |
|
| 54 |
1.1 2.2 |
|
| 62 |
1.1 |
|
| 67 |
1.1 2.2 3.3 |
|
| 72 |
1.1 |
|
| 75 |
1.1 |
|
| 76 |
1.1 |
|
| 84 |
1.1 2.2 |
|
| 89 |
1.1 2.2 3.3 |
|
| 94 |
1.1 |
|
| 99 |
1.1 |
|
| 107 |
1.1 |
|
| 112 |
1.1 |
|
| 115 |
1.1 |
|
| 116 |
1.1 |
|
| 121 |
1.1 2.2 3.3 |