| 1 | package fr.sii.ogham.core.builder.env; | |
| 2 | ||
| 3 | import static fr.sii.ogham.core.CoreConstants.DEFAULT_CLASSPATH_PROPERTY_PRIORITY; | |
| 4 | import static fr.sii.ogham.core.CoreConstants.DEFAULT_FILE_PROPERTY_PRIORITY; | |
| 5 | import static fr.sii.ogham.core.CoreConstants.DEFAULT_MANUAL_PROPERTY_PRIORITY; | |
| 6 | import static fr.sii.ogham.core.CoreConstants.DEFAULT_SYSTEM_PROPERTY_PRIORITY; | |
| 7 | ||
| 8 | import java.util.ArrayList; | |
| 9 | import java.util.Collections; | |
| 10 | import java.util.Comparator; | |
| 11 | import java.util.List; | |
| 12 | import java.util.Properties; | |
| 13 | ||
| 14 | import fr.sii.ogham.core.builder.env.props.AbstractProps; | |
| 15 | import fr.sii.ogham.core.builder.env.props.Props; | |
| 16 | import fr.sii.ogham.core.builder.env.props.PropsBuilder; | |
| 17 | import fr.sii.ogham.core.builder.env.props.PropsPath; | |
| 18 | import fr.sii.ogham.core.convert.Converter; | |
| 19 | import fr.sii.ogham.core.convert.DefaultConverter; | |
| 20 | import fr.sii.ogham.core.env.FirstExistingPropertiesResolver; | |
| 21 | import fr.sii.ogham.core.env.JavaPropertiesResolver; | |
| 22 | import fr.sii.ogham.core.env.PropertyResolver; | |
| 23 | import fr.sii.ogham.core.fluent.AbstractParent; | |
| 24 | import fr.sii.ogham.core.util.BuilderUtils; | |
| 25 | ||
| 26 | /** | |
| 27 | * Builds the {@link PropertyResolver}: | |
| 28 | * <ul> | |
| 29 | * <li>If a custom resolver is defined, use it directly</li> | |
| 30 | * <li>If no custom resolver, use the {@link JavaPropertiesResolver} with merged | |
| 31 | * {@link Properties} and converter instance provided by | |
| 32 | * {@link ConverterBuilder#build()}</li> | |
| 33 | * </ul> | |
| 34 | * | |
| 35 | * @author Aurélien Baudet | |
| 36 | * | |
| 37 | * @param <P> | |
| 38 | * the type of the parent builder (when calling {@link #and()} | |
| 39 | * method) | |
| 40 | */ | |
| 41 | public class SimpleEnvironmentBuilder<P> extends AbstractParent<P> implements EnvironmentBuilder<P> { | |
| 42 | private PropertyResolver resolver; | |
| 43 | private ConverterBuilder<EnvironmentBuilder<P>> converterBuilder; | |
| 44 | private List<AbstractProps> props; | |
| 45 | private int currentIndex; | |
| 46 | ||
| 47 | /** | |
| 48 | * Initializes the builder with the provided parent (parent is used when | |
| 49 | * calling {@link #and()} method). | |
| 50 | * | |
| 51 | * @param parent | |
| 52 | * the parent instance | |
| 53 | */ | |
| 54 | public SimpleEnvironmentBuilder(P parent) { | |
| 55 | super(parent); | |
| 56 | props = new ArrayList<>(); | |
| 57 | currentIndex = 0; | |
| 58 | } | |
| 59 | ||
| 60 | @Override | |
| 61 | public EnvironmentBuilder<P> override() { | |
| 62 |
1
1. override : removed call to java/util/List::clear → NO_COVERAGE |
props.clear(); |
| 63 |
1
1. override : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder::override → NO_COVERAGE |
return this; |
| 64 | } | |
| 65 | ||
| 66 | @Override | |
| 67 | public EnvironmentBuilder<P> properties(String path) { | |
| 68 | int priority = DEFAULT_CLASSPATH_PROPERTY_PRIORITY; | |
| 69 |
1
1. properties : negated conditional → RUN_ERROR |
if(path.startsWith("file:")) { |
| 70 | priority = DEFAULT_FILE_PROPERTY_PRIORITY; | |
| 71 | } | |
| 72 |
1
1. properties : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder::properties → RUN_ERROR |
return properties(path, priority); |
| 73 | } | |
| 74 | ||
| 75 | @Override | |
| 76 | public EnvironmentBuilder<P> properties(String path, int priority) { | |
| 77 | props.add(new PropsPath(path, priority, currentIndex)); | |
| 78 |
1
1. properties : Replaced integer addition with subtraction → RUN_ERROR |
currentIndex++; |
| 79 |
1
1. properties : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder::properties → RUN_ERROR |
return this; |
| 80 | } | |
| 81 | ||
| 82 | @Override | |
| 83 | public SimpleEnvironmentBuilder<P> properties(Properties properties) { | |
| 84 |
1
1. properties : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder::properties → NO_COVERAGE |
return properties(properties, DEFAULT_MANUAL_PROPERTY_PRIORITY); |
| 85 | } | |
| 86 | ||
| 87 | @Override | |
| 88 | public SimpleEnvironmentBuilder<P> properties(Properties properties, int priority) { | |
| 89 | props.add(new Props(properties, priority, currentIndex)); | |
| 90 |
1
1. properties : Replaced integer addition with subtraction → NO_COVERAGE |
currentIndex++; |
| 91 |
1
1. properties : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder::properties → NO_COVERAGE |
return this; |
| 92 | } | |
| 93 | ||
| 94 | @Override | |
| 95 | public SimpleEnvironmentBuilder<P> systemProperties() { | |
| 96 |
1
1. systemProperties : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder::systemProperties → RUN_ERROR |
return systemProperties(DEFAULT_SYSTEM_PROPERTY_PRIORITY); |
| 97 | } | |
| 98 | ||
| 99 | @Override | |
| 100 | public SimpleEnvironmentBuilder<P> systemProperties(int priority) { | |
| 101 | props.add(new Props(BuilderUtils.getDefaultProperties(), priority, currentIndex)); | |
| 102 |
1
1. systemProperties : Replaced integer addition with subtraction → RUN_ERROR |
currentIndex++; |
| 103 |
1
1. systemProperties : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder::systemProperties → RUN_ERROR |
return this; |
| 104 | } | |
| 105 | ||
| 106 | @Override | |
| 107 | public ConverterBuilder<EnvironmentBuilder<P>> converter() { | |
| 108 |
1
1. converter : negated conditional → RUN_ERROR |
if (converterBuilder == null) { |
| 109 | converterBuilder = new SimpleConverterBuilder<>(this); | |
| 110 | } | |
| 111 |
1
1. converter : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder::converter → RUN_ERROR |
return converterBuilder; |
| 112 | } | |
| 113 | ||
| 114 | @Override | |
| 115 | public SimpleEnvironmentBuilder<P> resolver(PropertyResolver resolver) { | |
| 116 | this.resolver = resolver; | |
| 117 |
1
1. resolver : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder::resolver → NO_COVERAGE |
return this; |
| 118 | } | |
| 119 | ||
| 120 | @Override | |
| 121 | public PropertiesBuilder<EnvironmentBuilder<P>> properties() { | |
| 122 |
1
1. properties : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder::properties → RUN_ERROR |
return properties(DEFAULT_MANUAL_PROPERTY_PRIORITY); |
| 123 | } | |
| 124 | ||
| 125 | @Override | |
| 126 | public PropertiesBuilder<EnvironmentBuilder<P>> properties(int priority) { | |
| 127 | PropertiesBuilder<EnvironmentBuilder<P>> propsBuilder = new SimplePropertiesBuilder<>(this); | |
| 128 | props.add(new PropsBuilder(propsBuilder, priority, currentIndex)); | |
| 129 |
1
1. properties : Replaced integer addition with subtraction → RUN_ERROR |
currentIndex++; |
| 130 |
1
1. properties : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder::properties → RUN_ERROR |
return propsBuilder; |
| 131 | } | |
| 132 | ||
| 133 | /** | |
| 134 | * Build the {@link PropertyResolver}: | |
| 135 | * <ul> | |
| 136 | * <li>If a custom resolver is defined, use it directly</li> | |
| 137 | * <li>If no custom resolver, use the {@link JavaPropertiesResolver} with | |
| 138 | * merged {@link Properties} and converter instance provided by | |
| 139 | * {@link ConverterBuilder#build()}</li> | |
| 140 | * </ul> | |
| 141 | */ | |
| 142 | @Override | |
| 143 | public PropertyResolver build() { | |
| 144 |
1
1. build : negated conditional → RUN_ERROR |
if (resolver != null) { |
| 145 |
1
1. build : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder::build → NO_COVERAGE |
return resolver; |
| 146 | } | |
| 147 | Converter converter = buildConverter(); | |
| 148 | List<PropertyResolver> delegates = buildProperties(converter); | |
| 149 |
1
1. build : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder::build → RUN_ERROR |
return new FirstExistingPropertiesResolver(delegates); |
| 150 | } | |
| 151 | ||
| 152 | private Converter buildConverter() { | |
| 153 |
1
1. buildConverter : negated conditional → RUN_ERROR |
if (converterBuilder != null) { |
| 154 |
1
1. buildConverter : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder::buildConverter → RUN_ERROR |
return converterBuilder.build(); |
| 155 | } | |
| 156 |
1
1. buildConverter : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder::buildConverter → RUN_ERROR |
return new DefaultConverter(); |
| 157 | } | |
| 158 | ||
| 159 | private List<PropertyResolver> buildProperties(Converter converter) { | |
| 160 | // sort by priority and then by registration order | |
| 161 | // highest priority comes first | |
| 162 | List<AbstractProps> orderedProps = new ArrayList<>(props); | |
| 163 |
1
1. buildProperties : removed call to java/util/Collections::sort → RUN_ERROR |
Collections.sort(orderedProps, new PriorityComparator()); |
| 164 | // build the resolvers separately | |
| 165 | List<PropertyResolver> builtProperties = new ArrayList<>(); | |
| 166 | for (AbstractProps prop : orderedProps) { | |
| 167 | builtProperties.add(new JavaPropertiesResolver(prop.getProps(), converter)); | |
| 168 | } | |
| 169 |
1
1. buildProperties : replaced return value with Collections.emptyList for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder::buildProperties → RUN_ERROR |
return builtProperties; |
| 170 | } | |
| 171 | ||
| 172 | private static class PriorityComparator implements Comparator<AbstractProps> { | |
| 173 | ||
| 174 | @Override | |
| 175 | public int compare(AbstractProps o1, AbstractProps o2) { | |
| 176 |
1
1. compare : negated conditional → RUN_ERROR |
if (o1.getPriority() == o2.getPriority()) { |
| 177 |
3
1. compare : changed conditional boundary → RUN_ERROR 2. compare : replaced int return with 0 for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder$PriorityComparator::compare → RUN_ERROR 3. compare : negated conditional → RUN_ERROR |
return o1.getIndex() <= o2.getIndex() ? -1 : 1; |
| 178 | } | |
| 179 |
3
1. compare : changed conditional boundary → RUN_ERROR 2. compare : negated conditional → RUN_ERROR 3. compare : replaced int return with 0 for fr/sii/ogham/core/builder/env/SimpleEnvironmentBuilder$PriorityComparator::compare → RUN_ERROR |
return o1.getPriority() <= o2.getPriority() ? 1 : -1; |
| 180 | } | |
| 181 | ||
| 182 | } | |
| 183 | } | |
Mutations | ||
| 62 |
1.1 |
|
| 63 |
1.1 |
|
| 69 |
1.1 |
|
| 72 |
1.1 |
|
| 78 |
1.1 |
|
| 79 |
1.1 |
|
| 84 |
1.1 |
|
| 90 |
1.1 |
|
| 91 |
1.1 |
|
| 96 |
1.1 |
|
| 102 |
1.1 |
|
| 103 |
1.1 |
|
| 108 |
1.1 |
|
| 111 |
1.1 |
|
| 117 |
1.1 |
|
| 122 |
1.1 |
|
| 129 |
1.1 |
|
| 130 |
1.1 |
|
| 144 |
1.1 |
|
| 145 |
1.1 |
|
| 149 |
1.1 |
|
| 153 |
1.1 |
|
| 154 |
1.1 |
|
| 156 |
1.1 |
|
| 163 |
1.1 |
|
| 169 |
1.1 |
|
| 176 |
1.1 |
|
| 177 |
1.1 2.2 3.3 |
|
| 179 |
1.1 2.2 3.3 |