Document.java

1
/*-
2
 * #%L
3
 * io.earcam.utilitarian.site.search.offline
4
 * %%
5
 * Copyright (C) 2017 earcam
6
 * %%
7
 * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT)
8
 *
9
 * You <b>must</b> choose to accept, in full - any individual or combination of
10
 * the following licenses:
11
 * <ul>
12
 * 	<li><a href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause</a></li>
13
 * 	<li><a href="https://www.eclipse.org/legal/epl-v10.html">EPL-1.0</a></li>
14
 * 	<li><a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0</a></li>
15
 * 	<li><a href="https://opensource.org/licenses/MIT">MIT</a></li>
16
 * </ul>
17
 * #L%
18
 */
19
package io.earcam.utilitarian.site.search.offline;
20
21
import static java.util.stream.Collectors.joining;
22
23
import java.io.BufferedInputStream;
24
import java.io.FileInputStream;
25
import java.io.UncheckedIOException;
26
import java.net.URI;
27
import java.net.URLConnection;
28
import java.nio.file.Files;
29
import java.nio.file.Path;
30
import java.util.ArrayList;
31
import java.util.Arrays;
32
import java.util.HashMap;
33
import java.util.Iterator;
34
import java.util.List;
35
import java.util.Map;
36
import java.util.function.Function;
37
38
import io.earcam.unexceptional.Closing;
39
import io.earcam.unexceptional.Exceptional;
40
41
//make this XmlRootElement - get Maven to pump config, copy 'n' paste for "main runner"
42
public class Document {
43
	public static final String LOCAL_FILE = "file";
44
	public static final String REF_URL = "url";
45
46
	public static final String RAW_TEXT = "raw";
47
48
	public static final String TEXT = "text";
49
	public static final String TITLE = "title";
50
	public static final String DESCRIPTION = "description";
51
	public static final String CONTENT_TYPE = "contentType";
52
53
	private final Map<String, String> fields = new HashMap<>();
54
	private final List<String> tokens = new ArrayList<>();
55
	private final Path file;
56
	private final String refUrl;
57
58
59
	private Document(Path file, String refUrl)
60
	{
61
		this.file = file;
62
		this.refUrl = refUrl;
63
	}
64
65
66
	public Path file()
67
	{
68 1 1. file : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::file to ( if (x != null) null else throw new RuntimeException ) → KILLED
		return file;
69
	}
70
71
72
	public String raw()
73
	{
74 1 1. raw : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::raw to ( if (x != null) null else throw new RuntimeException ) → KILLED
		return fields.getOrDefault(RAW_TEXT, "");
75
	}
76
77
78
	public boolean hasRaw()
79
	{
80 1 1. hasRaw : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED
		return fields.containsKey(RAW_TEXT);
81
	}
82
83
84
	public List<String> tokens()
85
	{
86 1 1. tokens : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::tokens to ( if (x != null) null else throw new RuntimeException ) → KILLED
		return tokens;
87
	}
88
89
90
	public boolean hasTokens()
91
	{
92 2 1. hasTokens : negated conditional → KILLED
2. hasTokens : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED
		return !tokens.isEmpty();
93
	}
94
95
96
	public String refUrl()
97
	{
98 1 1. refUrl : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::refUrl to ( if (x != null) null else throw new RuntimeException ) → KILLED
		return refUrl;
99
	}
100
101
102
	@SuppressWarnings("squid:S1845")
103
	public String title()
104
	{
105 1 1. title : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::title to ( if (x != null) null else throw new RuntimeException ) → KILLED
		return fields.getOrDefault(TITLE, "");
106
	}
107
108
109
	public void field(String key, String value)
110
	{
111
		fields.put(key, value);
112
	}
113
114
115
	public String field(String key)
116
	{
117 1 1. field : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::field to ( if (x != null) null else throw new RuntimeException ) → KILLED
		return fields.getOrDefault(key, "");
118
	}
119
120
121
	public String contentType()
122
	{
123 1 1. contentType : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::contentType to ( if (x != null) null else throw new RuntimeException ) → KILLED
		return fields.getOrDefault(CONTENT_TYPE, "");
124
	}
125
126
127
	public static Document document(Path baseDir, URI baseUri, Path file)
128
	{
129
		String relativeUri = ensureTrailingSlash(baseUri, true);
130
		String refUrl = relativizeReferenceUri(baseDir, relativeUri, file);
131
132
		Document document = new Document(file, refUrl);
133 1 1. document : removed call to io/earcam/utilitarian/site/search/offline/Document::deduceContentType → KILLED
		deduceContentType(file, document);
134 1 1. document : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::document to ( if (x != null) null else throw new RuntimeException ) → KILLED
		return document;
135
	}
136
137
138
	private static void deduceContentType(Path file, Document document)
139
	{
140
		String contentType = null;
141
		Iterator<Function<Path, String>> detectors = Arrays.<Function<Path, String>> asList(
142
				Document::probeContentType,
143
				Document::guessContentTypeFromStream,
144
				Document::guessContentTypeFromName).iterator();
145
146
		try {
147 2 1. deduceContentType : negated conditional → KILLED
2. deduceContentType : negated conditional → KILLED
			while(contentType == null && detectors.hasNext()) {
148
				contentType = detectors.next().apply(file);
149
			}
150
		} catch(UncheckedIOException e) {
151 1 1. deduceContentType : removed call to io/earcam/unexceptional/Exceptional::swallow → SURVIVED
			Exceptional.swallow(e);
152
		}
153 1 1. deduceContentType : removed call to io/earcam/utilitarian/site/search/offline/Document::field → KILLED
		document.field(CONTENT_TYPE, contentType);
154
	}
155
156
157
	private static String probeContentType(Path file)
158
	{
159 1 1. probeContentType : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::probeContentType to ( if (x != null) null else throw new RuntimeException ) → KILLED
		return Exceptional.apply(Files::probeContentType, file);
160
	}
161
162
163
	private static String guessContentTypeFromStream(Path file)
164
	{
165
		BufferedInputStream input = new BufferedInputStream(Exceptional.apply(FileInputStream::new, file.toFile()));
166 1 1. guessContentTypeFromStream : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::guessContentTypeFromStream to ( if (x != null) null else throw new RuntimeException ) → SURVIVED
		return Closing.closeAfterApplying(input, URLConnection::guessContentTypeFromStream);
167
	}
168
169
170
	private static String guessContentTypeFromName(Path file)
171
	{
172 1 1. guessContentTypeFromName : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::guessContentTypeFromName to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
		return URLConnection.guessContentTypeFromName(file.getFileName().toString());
173
	}
174
175
176
	public static String relativizeReferenceUri(Path baseDir, String baseUri, Path file)
177
	{
178 1 1. relativizeReferenceUri : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::relativizeReferenceUri to ( if (x != null) null else throw new RuntimeException ) → KILLED
		return baseUri + baseDir.toUri().relativize(file.toUri()).toString();
179
	}
180
181
182
	private static String ensureTrailingSlash(URI uri, boolean uriPathOnly)
183
	{
184 1 1. ensureTrailingSlash : negated conditional → KILLED
		String yuri = uriPathOnly ? uri.getPath() : uri.toString();
185 5 1. ensureTrailingSlash : changed conditional boundary → SURVIVED
2. ensureTrailingSlash : Replaced integer subtraction with addition → KILLED
3. ensureTrailingSlash : negated conditional → KILLED
4. ensureTrailingSlash : negated conditional → KILLED
5. ensureTrailingSlash : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::ensureTrailingSlash to ( if (x != null) null else throw new RuntimeException ) → KILLED
		return (yuri.length() > 0 && yuri.charAt(yuri.length() - 1) == '/') ? yuri : (yuri + '/');
186
	}
187
188
189
	public Map<String, String> asMap()
190
	{
191
		HashMap<String, String> map = new HashMap<>(fields);
192
		map.put(REF_URL, refUrl);
193
		map.put(TEXT, tokens.stream().collect(joining(" ")));
194
195 1 1. asMap : mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::asMap to ( if (x != null) null else throw new RuntimeException ) → KILLED
		return map;
196
	}
197
}

Mutations

68

1.1
Location : file
Killed by : io.earcam.utilitarian.site.search.offline.RegexFilterTest.excludes()
mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::file to ( if (x != null) null else throw new RuntimeException ) → KILLED

74

1.1
Location : raw
Killed by : io.earcam.utilitarian.site.search.offline.PdfContentProcessorTest.willNotAttemptToProcessNonPdfContent()
mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::raw to ( if (x != null) null else throw new RuntimeException ) → KILLED

80

1.1
Location : hasRaw
Killed by : io.earcam.utilitarian.site.search.offline.XrefHtmlReferenceProcessorTest.processed()
replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED

86

1.1
Location : tokens
Killed by : io.earcam.utilitarian.site.search.offline.XrefHtmlReferenceProcessorTest.processed()
mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::tokens to ( if (x != null) null else throw new RuntimeException ) → KILLED

92

1.1
Location : hasTokens
Killed by : io.earcam.utilitarian.site.search.offline.XrefHtmlReferenceProcessorTest.skipped()
negated conditional → KILLED

2.2
Location : hasTokens
Killed by : io.earcam.utilitarian.site.search.offline.XrefHtmlReferenceProcessorTest.skipped()
replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED

98

1.1
Location : refUrl
Killed by : io.earcam.utilitarian.site.search.offline.DefaultIndexerTest.titleMapEnabledByDefault()
mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::refUrl to ( if (x != null) null else throw new RuntimeException ) → KILLED

105

1.1
Location : title
Killed by : io.earcam.utilitarian.site.search.offline.XrefHtmlReferenceProcessorTest.processed()
mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::title to ( if (x != null) null else throw new RuntimeException ) → KILLED

117

1.1
Location : field
Killed by : io.earcam.utilitarian.site.search.offline.PdfContentProcessorTest.processesContentAndMeta()
mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::field to ( if (x != null) null else throw new RuntimeException ) → KILLED

123

1.1
Location : contentType
Killed by : io.earcam.utilitarian.site.search.offline.XrefHtmlReferenceProcessorTest.processed()
mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::contentType to ( if (x != null) null else throw new RuntimeException ) → KILLED

133

1.1
Location : document
Killed by : io.earcam.utilitarian.site.search.offline.PdfContentProcessorTest.processesContentAndMeta()
removed call to io/earcam/utilitarian/site/search/offline/Document::deduceContentType → KILLED

134

1.1
Location : document
Killed by : io.earcam.utilitarian.site.search.offline.PdfContentProcessorTest.willNotAttemptToProcessNonPdfContent()
mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::document to ( if (x != null) null else throw new RuntimeException ) → KILLED

147

1.1
Location : deduceContentType
Killed by : io.earcam.utilitarian.site.search.offline.PdfContentProcessorTest.processesContentAndMeta()
negated conditional → KILLED

2.2
Location : deduceContentType
Killed by : io.earcam.utilitarian.site.search.offline.PdfContentProcessorTest.processesContentAndMeta()
negated conditional → KILLED

151

1.1
Location : deduceContentType
Killed by : none
removed call to io/earcam/unexceptional/Exceptional::swallow → SURVIVED

153

1.1
Location : deduceContentType
Killed by : io.earcam.utilitarian.site.search.offline.PdfContentProcessorTest.processesContentAndMeta()
removed call to io/earcam/utilitarian/site/search/offline/Document::field → KILLED

159

1.1
Location : probeContentType
Killed by : io.earcam.utilitarian.site.search.offline.RegexFilterTest.excludes()
mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::probeContentType to ( if (x != null) null else throw new RuntimeException ) → KILLED

166

1.1
Location : guessContentTypeFromStream
Killed by : none
mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::guessContentTypeFromStream to ( if (x != null) null else throw new RuntimeException ) → SURVIVED

172

1.1
Location : guessContentTypeFromName
Killed by : none
mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::guessContentTypeFromName to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

178

1.1
Location : relativizeReferenceUri
Killed by : io.earcam.utilitarian.site.search.offline.DefaultIndexerTest.titleMapEnabledByDefault()
mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::relativizeReferenceUri to ( if (x != null) null else throw new RuntimeException ) → KILLED

184

1.1
Location : ensureTrailingSlash
Killed by : io.earcam.utilitarian.site.search.offline.DefaultIndexerTest.titleMapEnabledByDefault()
negated conditional → KILLED

185

1.1
Location : ensureTrailingSlash
Killed by : none
changed conditional boundary → SURVIVED

2.2
Location : ensureTrailingSlash
Killed by : io.earcam.utilitarian.site.search.offline.PdfContentProcessorTest.willNotAttemptToProcessNonPdfContent()
Replaced integer subtraction with addition → KILLED

3.3
Location : ensureTrailingSlash
Killed by : io.earcam.utilitarian.site.search.offline.DefaultIndexerTest.titleMapEnabledByDefault()
negated conditional → KILLED

4.4
Location : ensureTrailingSlash
Killed by : io.earcam.utilitarian.site.search.offline.DefaultIndexerTest.titleMapEnabledByDefault()
negated conditional → KILLED

5.5
Location : ensureTrailingSlash
Killed by : io.earcam.utilitarian.site.search.offline.DefaultIndexerTest.titleMapEnabledByDefault()
mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::ensureTrailingSlash to ( if (x != null) null else throw new RuntimeException ) → KILLED

195

1.1
Location : asMap
Killed by : io.earcam.utilitarian.site.search.offline.DefaultIndexerTest.writesJsonToFileSystem()
mutated return of Object value for io/earcam/utilitarian/site/search/offline/Document::asMap to ( if (x != null) null else throw new RuntimeException ) → KILLED

Active mutators

Tests examined


Report generated by PIT 1.4.3