UsernamePasswordAuthenticatorBuilder.java

1
package fr.sii.ogham.email.builder.javamail;
2
3
import javax.mail.Authenticator;
4
5
import fr.sii.ogham.core.builder.Builder;
6
import fr.sii.ogham.core.builder.configuration.ConfigurationValueBuilder;
7
import fr.sii.ogham.core.builder.configuration.ConfigurationValueBuilderHelper;
8
import fr.sii.ogham.core.builder.configurer.Configurer;
9
import fr.sii.ogham.core.builder.context.BuildContext;
10
import fr.sii.ogham.core.fluent.AbstractParent;
11
import fr.sii.ogham.email.sender.impl.javamail.UpdatableUsernamePasswordAuthenticator;
12
import fr.sii.ogham.email.sender.impl.javamail.UsernamePasswordAuthenticator;
13
14
/**
15
 * Configures authentication mechanism based on username/password.
16
 * 
17
 * <p>
18
 * You can define direct values for username and password:
19
 * 
20
 * <pre>
21
 * .username("foo")
22
 * .password("bar")
23
 * </pre>
24
 * 
25
 * Or you can specify one or several property keys:
26
 * 
27
 * <pre>
28
 * .username("${ogham.email.javamail.authenticator.username}")
29
 * .password("${ogham.email.javamail.authenticator.password}")
30
 * </pre>
31
 * 
32
 * The evaluation of the properties will be evaluated when {@link #build()} is
33
 * called (by default).
34
 * 
35
 * <p>
36
 * If {@link #updatable(Boolean)} is set to true, it means that properties are
37
 * not evaluated when calling {@link #build()}. Instead, the property keys are
38
 * kept for later evaluation. The evaluation will then be done each time an
39
 * authentication to the mail server is started.
40
 * </p>
41
 * 
42
 * @author Aurélien Baudet
43
 *
44
 */
45
public class UsernamePasswordAuthenticatorBuilder extends AbstractParent<JavaMailBuilder> implements Builder<Authenticator> {
46
	private final BuildContext buildContext;
47
	private final ConfigurationValueBuilderHelper<UsernamePasswordAuthenticatorBuilder, String> usernameValueBuilder;
48
	private final ConfigurationValueBuilderHelper<UsernamePasswordAuthenticatorBuilder, String> passwordValueBuilder;
49
	private final ConfigurationValueBuilderHelper<UsernamePasswordAuthenticatorBuilder, Boolean> updatableValueBuilder;
50
51
	/**
52
	 * Initializes the parent instance for fluent chaining (when method
53
	 * {@link #and()} is called).
54
	 * 
55
	 * @param parent
56
	 *            the parent builder
57
	 * @param buildContext
58
	 *            for registering instances and property evaluation
59
	 */
60
	public UsernamePasswordAuthenticatorBuilder(JavaMailBuilder parent, BuildContext buildContext) {
61
		super(parent);
62
		this.buildContext = buildContext;
63
		usernameValueBuilder = buildContext.newConfigurationValueBuilder(this, String.class);
64
		passwordValueBuilder = buildContext.newConfigurationValueBuilder(this, String.class);
65
		updatableValueBuilder = buildContext.newConfigurationValueBuilder(this, Boolean.class);
66
	}
67
68
	/**
69
	 * Set the username to use for the authentication.
70
	 * 
71
	 * <p>
72
	 * The value set using this method takes precedence over any property and
73
	 * default value configured using {@link #username()}.
74
	 * 
75
	 * <pre>
76
	 * .username("my-username")
77
	 * .username()
78
	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}")
79
	 *   .defaultValue("default-username")
80
	 * </pre>
81
	 * 
82
	 * <pre>
83
	 * .username("my-username")
84
	 * .username()
85
	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}")
86
	 *   .defaultValue("default-username")
87
	 * </pre>
88
	 * 
89
	 * In both cases, {@code username("my-username")} is used.
90
	 * 
91
	 * <p>
92
	 * If this method is called several times, only the last value is used.
93
	 * 
94
	 * <p>
95
	 * If {@code null} value is set, it is like not setting a value at all. The
96
	 * property/default value configuration is applied.
97
	 * 
98
	 * @param username
99
	 *            the username
100
	 * @return this instance for fluent chaining
101
	 */
102
	public UsernamePasswordAuthenticatorBuilder username(String username) {
103 1 1. username : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → NO_COVERAGE
		usernameValueBuilder.setValue(username);
104 1 1. username : replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::username → NO_COVERAGE
		return this;
105
	}
106
107
	/**
108
	 * Set the username to use for the authentication.
109
	 * 
110
	 * <p>
111
	 * This method is mainly used by {@link Configurer}s to register some
112
	 * property keys and/or a default value. The aim is to let developer be able
113
	 * to externalize its configuration (using system properties, configuration
114
	 * file or anything else). If the developer doesn't configure any value for
115
	 * the registered properties, the default value is used (if set).
116
	 * 
117
	 * <pre>
118
	 * .username()
119
	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}")
120
	 *   .defaultValue("default-username")
121
	 * </pre>
122
	 * 
123
	 * <p>
124
	 * Non-null value set using {@link #username(String)} takes precedence over
125
	 * property values and default value.
126
	 * 
127
	 * <pre>
128
	 * .username("my-username")
129
	 * .username()
130
	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}")
131
	 *   .defaultValue("default-username")
132
	 * </pre>
133
	 * 
134
	 * The value {@code "my-username"} is used regardless of the value of the
135
	 * properties and default value.
136
	 * 
137
	 * <p>
138
	 * See {@link ConfigurationValueBuilder} for more information.
139
	 * 
140
	 * 
141
	 * @return the builder to configure property keys/default value
142
	 */
143
	public ConfigurationValueBuilder<UsernamePasswordAuthenticatorBuilder, String> username() {
144 4 1. username : replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::username → KILLED
2. username : replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::username → KILLED
3. username : replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::username → KILLED
4. username : replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::username → KILLED
		return usernameValueBuilder;
145
	}
146
147
	/**
148
	 * Set the password to use for the authentication.
149
	 * 
150
	 * <p>
151
	 * The value set using this method takes precedence over any property and
152
	 * default value configured using {@link #password()}.
153
	 * 
154
	 * <pre>
155
	 * .password("my-password")
156
	 * .password()
157
	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}")
158
	 *   .defaultValue("default-password")
159
	 * </pre>
160
	 * 
161
	 * <pre>
162
	 * .password("my-password")
163
	 * .password()
164
	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}")
165
	 *   .defaultValue("default-password")
166
	 * </pre>
167
	 * 
168
	 * In both cases, {@code password("my-password")} is used.
169
	 * 
170
	 * <p>
171
	 * If this method is called several times, only the last value is used.
172
	 * 
173
	 * <p>
174
	 * If {@code null} value is set, it is like not setting a value at all. The
175
	 * property/default value configuration is applied.
176
	 * 
177
	 * @param password
178
	 *            the passowrd
179
	 * @return this instance for fluent chaining
180
	 */
181
	public UsernamePasswordAuthenticatorBuilder password(String password) {
182 1 1. password : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → NO_COVERAGE
		passwordValueBuilder.setValue(password);
183 1 1. password : replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::password → NO_COVERAGE
		return this;
184
	}
185
186
	/**
187
	 * Set the password to use for the authentication.
188
	 * 
189
	 * <p>
190
	 * This method is mainly used by {@link Configurer}s to register some
191
	 * property keys and/or a default value. The aim is to let developer be able
192
	 * to externalize its configuration (using system properties, configuration
193
	 * file or anything else). If the developer doesn't configure any value for
194
	 * the registered properties, the default value is used (if set).
195
	 * 
196
	 * <pre>
197
	 * .password()
198
	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}")
199
	 *   .defaultValue("default-password")
200
	 * </pre>
201
	 * 
202
	 * <p>
203
	 * Non-null value set using {@link #password(String)} takes precedence over
204
	 * property values and default value.
205
	 * 
206
	 * <pre>
207
	 * .password("my-password")
208
	 * .password()
209
	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}")
210
	 *   .defaultValue("default-password")
211
	 * </pre>
212
	 * 
213
	 * The value {@code "my-password"} is used regardless of the value of the
214
	 * properties and default value.
215
	 * 
216
	 * <p>
217
	 * See {@link ConfigurationValueBuilder} for more information.
218
	 * 
219
	 * 
220
	 * @return the builder to configure property keys/default value
221
	 */
222
	public ConfigurationValueBuilder<UsernamePasswordAuthenticatorBuilder, String> password() {
223 4 1. password : replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::password → KILLED
2. password : replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::password → KILLED
3. password : replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::password → KILLED
4. password : replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::password → KILLED
		return passwordValueBuilder;
224
	}
225
226
	/**
227
	 * If set to true, it means that properties are not evaluated when calling
228
	 * {@link #build()}. Instead, the property keys are kept for later
229
	 * evaluation. The evaluation will then be done each time an authentication
230
	 * to the mail server is started.
231
	 * 
232
	 * <p>
233
	 * The value set using this method takes precedence over any property and
234
	 * default value configured using {@link #updatable()}.
235
	 * 
236
	 * <pre>
237
	 * .updatable(true)
238
	 * .updatable()
239
	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}")
240
	 *   .defaultValue(false)
241
	 * </pre>
242
	 * 
243
	 * <pre>
244
	 * .updatable(true)
245
	 * .updatable()
246
	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}")
247
	 *   .defaultValue(false)
248
	 * </pre>
249
	 * 
250
	 * In both cases, {@code updatable(true)} is used.
251
	 * 
252
	 * <p>
253
	 * If this method is called several times, only the last value is used.
254
	 * 
255
	 * <p>
256
	 * If {@code null} value is set, it is like not setting a value at all. The
257
	 * property/default value configuration is applied.
258
	 * 
259
	 * @param updatable
260
	 *            true to evaluate expression when connecting to server
261
	 * @return this instance for fluent chaining
262
	 */
263
	public UsernamePasswordAuthenticatorBuilder updatable(Boolean updatable) {
264 1 1. updatable : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → NO_COVERAGE
		updatableValueBuilder.setValue(updatable);
265 1 1. updatable : replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::updatable → NO_COVERAGE
		return this;
266
	}
267
268
	/**
269
	 * If set to true, it means that properties are not evaluated when calling
270
	 * {@link #build()}. Instead, the property keys are kept for later
271
	 * evaluation. The evaluation will then be done each time an authentication
272
	 * to the mail server is started.
273
	 * 
274
	 * <p>
275
	 * This method is mainly used by {@link Configurer}s to register some
276
	 * property keys and/or a default value. The aim is to let developer be able
277
	 * to externalize its configuration (using system properties, configuration
278
	 * file or anything else). If the developer doesn't configure any value for
279
	 * the registered properties, the default value is used (if set).
280
	 * 
281
	 * <pre>
282
	 * .updatable()
283
	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}")
284
	 *   .defaultValue(false)
285
	 * </pre>
286
	 * 
287
	 * <p>
288
	 * Non-null value set using {@link #updatable(Boolean)} takes precedence
289
	 * over property values and default value.
290
	 * 
291
	 * <pre>
292
	 * .updatable(true)
293
	 * .updatable()
294
	 *   .properties("${custom.property.high-priority}", "${custom.property.low-priority}")
295
	 *   .defaultValue(false)
296
	 * </pre>
297
	 * 
298
	 * The value {@code true} is used regardless of the value of the properties
299
	 * and default value.
300
	 * 
301
	 * <p>
302
	 * See {@link ConfigurationValueBuilder} for more information.
303
	 * 
304
	 * 
305
	 * @return the builder to configure property keys/default value
306
	 */
307
	public ConfigurationValueBuilder<UsernamePasswordAuthenticatorBuilder, Boolean> updatable() {
308 1 1. updatable : replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::updatable → NO_COVERAGE
		return updatableValueBuilder;
309
	}
310
311
	@Override
312
	public Authenticator build() {
313
		boolean isUpdatable = updatableValueBuilder.getValue(false);
314 2 1. build : negated conditional → SURVIVED
2. build : negated conditional → TIMED_OUT
		if (isUpdatable) {
315 2 1. build : negated conditional → NO_COVERAGE
2. build : negated conditional → NO_COVERAGE
			if (usernameValueBuilder.hasValueOrProperties() && passwordValueBuilder.hasValueOrProperties()) {
316 1 1. build : replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::build → NO_COVERAGE
				return buildContext.register(new UpdatableUsernamePasswordAuthenticator(usernameValueBuilder, passwordValueBuilder));
317
			}
318
			return null;
319
		}
320
		String u = usernameValueBuilder.getValue();
321
		String p = passwordValueBuilder.getValue();
322 4 1. build : negated conditional → SURVIVED
2. build : negated conditional → NO_COVERAGE
3. build : negated conditional → KILLED
4. build : negated conditional → KILLED
		if (u != null && p != null) {
323 2 1. build : replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::build → NO_COVERAGE
2. build : replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::build → KILLED
			return buildContext.register(new UsernamePasswordAuthenticator(u, p));
324
		}
325
		return null;
326
	}
327
}

Mutations

103

1.1
Location : username
Killed by : none
removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → NO_COVERAGE

104

1.1
Location : username
Killed by : none
replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::username → NO_COVERAGE

144

1.1
Location : username
Killed by : oghamall.it.configuration.EmptyBuilderTest.emailSenderManuallyRegisteredButUnconfiguredTemplateParsersCantHandleMultiTemplateContent(oghamall.it.configuration.EmptyBuilderTest)
replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::username → KILLED

2.2
Location : username
Killed by : oghamspringbootv1autoconfigure.it.OghamSpringBoot1JavaMailAutoConfigurationTests.oghamAloneShouldUseOghamProperties(oghamspringbootv1autoconfigure.it.OghamSpringBoot1JavaMailAutoConfigurationTests)
replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::username → KILLED

3.3
Location : username
Killed by : oghamspringbootv2autoconfigure.it.OghamSpringBoot2JavaMailAutoConfigurationTests.oghamPropertiesWithSpringPropsShouldUseOghamPropertiesPrecedence(oghamspringbootv2autoconfigure.it.OghamSpringBoot2JavaMailAutoConfigurationTests)
replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::username → KILLED

4.4
Location : username
Killed by : oghamjavamail.it.UnreadableAttachmentTest.attachmentUnreadable(oghamjavamail.it.UnreadableAttachmentTest)
replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::username → KILLED

182

1.1
Location : password
Killed by : none
removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → NO_COVERAGE

183

1.1
Location : password
Killed by : none
replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::password → NO_COVERAGE

223

1.1
Location : password
Killed by : oghamall.it.configuration.EmptyBuilderTest.emailSenderManuallyRegisteredButUnconfiguredTemplateParsersCantHandleMultiTemplateContent(oghamall.it.configuration.EmptyBuilderTest)
replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::password → KILLED

2.2
Location : password
Killed by : oghamspringbootv2autoconfigure.it.OghamSpringBoot2JavaMailAutoConfigurationTests.oghamPropertiesWithSpringPropsShouldUseOghamPropertiesPrecedence(oghamspringbootv2autoconfigure.it.OghamSpringBoot2JavaMailAutoConfigurationTests)
replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::password → KILLED

3.3
Location : password
Killed by : oghamjavamail.it.UnreadableAttachmentTest.attachmentUnreadable(oghamjavamail.it.UnreadableAttachmentTest)
replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::password → KILLED

4.4
Location : password
Killed by : oghamspringbootv1autoconfigure.it.OghamSpringBoot1JavaMailAutoConfigurationTests.oghamAloneShouldUseOghamProperties(oghamspringbootv1autoconfigure.it.OghamSpringBoot1JavaMailAutoConfigurationTests)
replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::password → KILLED

264

1.1
Location : updatable
Killed by : none
removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → NO_COVERAGE

265

1.1
Location : updatable
Killed by : none
replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::updatable → NO_COVERAGE

308

1.1
Location : updatable
Killed by : none
replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::updatable → NO_COVERAGE

314

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

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

315

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

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

316

1.1
Location : build
Killed by : none
replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::build → NO_COVERAGE

322

1.1
Location : build
Killed by : oghamall.it.email.EmailSMTPAuthenticationTest.authenticated(oghamall.it.email.EmailSMTPAuthenticationTest)
negated conditional → KILLED

2.2
Location : build
Killed by : none
negated conditional → SURVIVED

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

4.4
Location : build
Killed by : oghamall.it.email.EmailSMTPAuthenticationTest.authenticated(oghamall.it.email.EmailSMTPAuthenticationTest)
negated conditional → KILLED

323

1.1
Location : build
Killed by : oghamall.it.email.EmailSMTPAuthenticationTest.authenticated(oghamall.it.email.EmailSMTPAuthenticationTest)
replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::build → KILLED

2.2
Location : build
Killed by : none
replaced return value with null for fr/sii/ogham/email/builder/javamail/UsernamePasswordAuthenticatorBuilder::build → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT OGHAM