TextPrefixSubjectProvider.java

1
package fr.sii.ogham.core.subject.provider;
2
3
import fr.sii.ogham.core.message.Message;
4
import fr.sii.ogham.core.message.content.Content;
5
import fr.sii.ogham.core.message.content.MayHaveStringContent;
6
import fr.sii.ogham.core.message.content.StringContent;
7
import fr.sii.ogham.core.message.content.UpdatableStringContent;
8
9
/**
10
 * Provider that analyzes the content of the message. If the first line contains
11
 * the provided prefix (<code>"Subject:"</code> by default), then the subject is
12
 * the first line without the prefix. The extracted subject is trimmed. If the
13
 * extracted subject is empty then the final subject is empty string. If the
14
 * first line doesn't contain the prefix, then the subject is null.
15
 * 
16
 * @author Aurélien Baudet
17
 *
18
 */
19
public class TextPrefixSubjectProvider implements SubjectProvider {
20
	private static final String NEW_LINE = "\n";
21
	private static final String DEFAULT_PREFIX = "Subject:";
22
23
	/**
24
	 * The prefix
25
	 */
26
	private String prefix;
27
28
	public TextPrefixSubjectProvider() {
29
		this(DEFAULT_PREFIX);
30
	}
31
32
	public TextPrefixSubjectProvider(String prefix) {
33
		super();
34
		this.prefix = prefix;
35
	}
36
37
	@Override
38
	public String provide(Message message) {
39
		Content msgContent = message.getContent();
40 2 1. provide : negated conditional → RUN_ERROR
2. provide : negated conditional → RUN_ERROR
		if(msgContent instanceof MayHaveStringContent && ((MayHaveStringContent) msgContent).canProvideString()) {
41
			String content = ((MayHaveStringContent) msgContent).asString();
42
			int idx = content.indexOf(NEW_LINE);
43 3 1. provide : changed conditional boundary → RUN_ERROR
2. provide : negated conditional → RUN_ERROR
3. provide : negated conditional → RUN_ERROR
			if (idx > 0 && content.startsWith(prefix)) {
44
				// remove the subject from the content and update the content
45 1 1. provide : Replaced integer addition with subtraction → RUN_ERROR
				String bodyContent = content.substring(idx+1);
46 1 1. provide : negated conditional → RUN_ERROR
				if(msgContent instanceof UpdatableStringContent) {
47 1 1. provide : removed call to fr/sii/ogham/core/message/content/UpdatableStringContent::setStringContent → RUN_ERROR
					((UpdatableStringContent) msgContent).setStringContent(bodyContent);
48
				} else {
49 1 1. provide : removed call to fr/sii/ogham/core/message/Message::setContent → NO_COVERAGE
					message.setContent(new StringContent(bodyContent));
50
				}
51
				// returns the subject
52 1 1. provide : replaced return value with "" for fr/sii/ogham/core/subject/provider/TextPrefixSubjectProvider::provide → RUN_ERROR
				return content.substring(prefix.length(), idx).trim();
53
			}
54
		}
55 1 1. provide : replaced return value with "" for fr/sii/ogham/core/subject/provider/TextPrefixSubjectProvider::provide → RUN_ERROR
		return null;
56
	}
57
58
}

Mutations

40

1.1
Location : provide
Killed by :
negated conditional → RUN_ERROR

2.2
Location : provide
Killed by :
negated conditional → RUN_ERROR

43

1.1
Location : provide
Killed by :
changed conditional boundary → RUN_ERROR

2.2
Location : provide
Killed by :
negated conditional → RUN_ERROR

3.3
Location : provide
Killed by :
negated conditional → RUN_ERROR

45

1.1
Location : provide
Killed by :
Replaced integer addition with subtraction → RUN_ERROR

46

1.1
Location : provide
Killed by :
negated conditional → RUN_ERROR

47

1.1
Location : provide
Killed by :
removed call to fr/sii/ogham/core/message/content/UpdatableStringContent::setStringContent → RUN_ERROR

49

1.1
Location : provide
Killed by :
removed call to fr/sii/ogham/core/message/Message::setContent → NO_COVERAGE

52

1.1
Location : provide
Killed by :
replaced return value with "" for fr/sii/ogham/core/subject/provider/TextPrefixSubjectProvider::provide → RUN_ERROR

55

1.1
Location : provide
Killed by :
replaced return value with "" for fr/sii/ogham/core/subject/provider/TextPrefixSubjectProvider::provide → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1